USE [master]
GO
/****** Object:  Database [db_edits]    Script Date: 11/10/2017 15:12:19 ******/
CREATE DATABASE [db_edits] ON  PRIMARY 
( NAME = N'db_edits', FILENAME = N'D:\data-mdf\db_edits.mdf' , SIZE = 867328KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'db_edits_log', FILENAME = N'D:\data-ldf\db_edits_log.ldf' , SIZE = 757248KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
ALTER DATABASE [db_edits] SET COMPATIBILITY_LEVEL = 100
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [db_edits].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [db_edits] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [db_edits] SET ANSI_NULLS OFF
GO
ALTER DATABASE [db_edits] SET ANSI_PADDING OFF
GO
ALTER DATABASE [db_edits] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [db_edits] SET ARITHABORT OFF
GO
ALTER DATABASE [db_edits] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [db_edits] SET AUTO_CREATE_STATISTICS ON
GO
ALTER DATABASE [db_edits] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [db_edits] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [db_edits] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [db_edits] SET CURSOR_DEFAULT  GLOBAL
GO
ALTER DATABASE [db_edits] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [db_edits] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [db_edits] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [db_edits] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [db_edits] SET  DISABLE_BROKER
GO
ALTER DATABASE [db_edits] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [db_edits] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [db_edits] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [db_edits] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [db_edits] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [db_edits] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [db_edits] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [db_edits] SET  READ_WRITE
GO
ALTER DATABASE [db_edits] SET RECOVERY FULL
GO
ALTER DATABASE [db_edits] SET  MULTI_USER
GO
ALTER DATABASE [db_edits] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [db_edits] SET DB_CHAINING OFF
GO
EXEC sys.sp_db_vardecimal_storage_format N'db_edits', N'ON'
GO
USE [db_edits]
GO
/****** Object:  User [useredits]    Script Date: 11/10/2017 15:12:19 ******/
CREATE USER [useredits] FOR LOGIN [useredits] WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object:  User [edits_app]    Script Date: 11/10/2017 15:12:19 ******/
CREATE USER [edits_app] FOR LOGIN [edits_app] WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object:  User [admin]    Script Date: 11/10/2017 15:12:19 ******/
CREATE USER [admin] WITHOUT LOGIN WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object:  Table [dbo].[t_transaction_history_process]    Script Date: 11/10/2017 15:12:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[t_transaction_history_process](
	[transaction_history_process_id] [bigint] IDENTITY(1,1) NOT NULL,
	[transaction_id] [bigint] NOT NULL,
	[process_date] [datetime] NOT NULL,
	[user_id] [varchar](8) NOT NULL,
	[workflow_template_action_id] [bigint] NULL,
	[action_description] [varchar](200) NULL,
	[reason_id] [int] NULL,
	[remarks] [varchar](255) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [tinyint] NOT NULL,
 CONSTRAINT [PK__t_transa__ED3D41C657B2EEB2] PRIMARY KEY CLUSTERED 
(
	[transaction_history_process_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  UserDefinedFunction [dbo].[get_latest_transaction_process]    Script Date: 11/10/2017 15:12:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE FUNCTION [dbo].[get_latest_transaction_process]
(	
	@pi_transaction_id BIGINT
)
RETURNS TABLE 
AS
RETURN 
(
	SELECT TOP 1
		transaction_history_process_id, 
		transaction_id, 
		process_date, 
		user_id, 
		workflow_template_action_id, 
		reason_id, 
		remarks, 
		created_by, 
		created_date, 
		modified_by, 
		modified_date, 
		is_delete
	FROM t_transaction_history_process
	WHERE transaction_id = @pi_transaction_id
	ORDER BY created_date DESC
)
GO
/****** Object:  UserDefinedFunction [dbo].[get_history_transaction]    Script Date: 11/10/2017 15:12:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[get_history_transaction]
(	
	@pi_transaction_id BIGINT
)
RETURNS TABLE 
AS
RETURN 
(
	
	SELECT 
		transaction_history_process_id, 
		transaction_id, 
		CAST (process_date AS DATE) AS process_date, 
		user_id, 
		workflow_template_action_id, 
		action_description, 
		reason_id, 
		remarks, 
		created_by, 
		created_date, 
		modified_by, 
		modified_date, 
		is_delete
	FROM 
		t_transaction_history_process
	WHERE transaction_id = @pi_transaction_id
	AND is_delete = 0
)
GO
/****** Object:  UserDefinedFunction [dbo].[func_get_latest_transaction_action]    Script Date: 11/10/2017 15:12:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[func_get_latest_transaction_action] 
(
	@pi_transaction_id BIGINT
)
RETURNS BIGINT
AS
BEGIN
	-- Declare the return variable here
	DECLARE @pi_action_id BIGINT;

	SELECT TOP 1 
	@pi_action_id = workflow_template_action_id
	FROM t_transaction_history_process
	WHERE transaction_id = @pi_transaction_id
	ORDER BY created_date DESC

	-- Return the result of the function
	RETURN @pi_action_id;

END
GO
/****** Object:  Table [dbo].[t_transaction_document]    Script Date: 11/10/2017 15:12:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[t_transaction_document](
	[transaction_document_id] [bigint] IDENTITY(1,1) NOT NULL,
	[transaction_id] [bigint] NOT NULL,
	[document_type_id] [int] NOT NULL,
	[token] [varchar](30) NULL,
	[receive_doc_check] [bit] NOT NULL,
	[hashcode] [varchar](50) NULL,
	[document_index] [varchar](100) NULL,
	[document_name] [varchar](100) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__t_transa__AC0635CA3A228BCB] PRIMARY KEY CLUSTERED 
(
	[transaction_document_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[t_transaction_document_file]    Script Date: 11/10/2017 15:12:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[t_transaction_document_file](
	[transaction_document_file_id] [bigint] IDENTITY(1,1) NOT NULL,
	[transaction_document_id] [bigint] NOT NULL,
	[document_name] [varchar](200) NULL,
	[document_url] [varchar](100) NOT NULL,
	[hashcode_ori] [varchar](50) NOT NULL,
	[hashcode] [varchar](50) NOT NULL,
	[upload_by] [varchar](8) NOT NULL,
	[upload_date] [datetime] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__t_transa__0AB7A9E43FDB6521] PRIMARY KEY CLUSTERED 
(
	[transaction_document_file_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  View [dbo].[v_m_transaction_document_files]    Script Date: 11/10/2017 15:12:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_m_transaction_document_files]
AS
  SELECT
    ttd.transaction_document_id,  -- id master
    ttdf.transaction_document_file_id,  -- id detail
    ttd.transaction_id,
    ttd.document_type_id,
    ttd.token AS token,
    ttd.hashcode AS final_hashcode,
    ttd.document_index AS final_document_index,
    ttd.document_name AS final_document_name,
    ttdf.document_name,
    ttdf.document_url,
    ttdf.hashcode_ori,
    ttdf.hashcode,
    ttdf.upload_by,
    ttdf.upload_date,
    CONVERT(varchar, ttdf.upload_date, 103) +' '+ CONVERT(varchar(15),CAST(ttdf.upload_date AS TIME),108) AS upload_date_str
  FROM
	  dbo.t_transaction_document AS ttd INNER JOIN
	  dbo.t_transaction_document_file AS ttdf ON ttd.transaction_document_id = ttdf.transaction_document_id
  WHERE
    ttd.is_delete = '0'
    AND ttdf.is_delete = '0'
GO
/****** Object:  Table [dbo].[t_transaction]    Script Date: 11/10/2017 15:12:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[t_transaction](
	[transaction_id] [bigint] IDENTITY(1,1) NOT NULL,
	[transaction_type_id] [int] NOT NULL,
	[register_number] [varchar](9) NULL,
	[transaction_date] [datetime2](7) NULL,
	[source_app_id] [int] NULL,
	[policy_id_reference] [varchar](12) NOT NULL,
	[policy_holder_reference] [varchar](100) NOT NULL,
	[product_name_reference] [varchar](100) NOT NULL,
	[currency_id_reference] [varchar](3) NOT NULL,
	[is_policy_holder] [tinyint] NOT NULL,
	[customer] [varchar](100) NOT NULL,
	[address] [varchar](255) NOT NULL,
	[mobile_phone] [varchar](15) NOT NULL,
	[email_address] [varchar](30) NULL,
	[signature_url] [varchar](300) NULL,
	[signature_hashcode] [varchar](50) NULL,
	[photo_url] [varchar](300) NULL,
	[photo_hashcode] [varchar](50) NULL,
	[reference_number] [varchar](50) NULL,
	[currency_id] [int] NULL,
	[currency_code] [varchar](3) NULL,
	[transaction_value] [decimal](8, 2) NULL,
	[is_notify_document_completion] [tinyint] NULL,
	[notification_date] [datetime] NULL,
	[workflow_template_stage_id] [bigint] NULL,
	[user_id] [varchar](8) NOT NULL,
	[branch_id] [int] NOT NULL,
	[status_id] [int] NOT NULL,
	[eform_workflow_id] [int] NULL,
	[source_transaction_id] [smallint] NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [tinyint] NOT NULL,
 CONSTRAINT [PK__t_transa__85C600AF2BD46C74] PRIMARY KEY CLUSTERED 
(
	[transaction_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_t_transaction_policy_id] ON [dbo].[t_transaction] 
(
	[policy_id_reference] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_t_transaction_reg_number] ON [dbo].[t_transaction] 
(
	[register_number] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_t_transaction_type_id] ON [dbo].[t_transaction] 
(
	[transaction_type_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  StoredProcedure [dbo].[AddTransaction]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[AddTransaction] (
	@pi_transaction_type_id INT,
	@pi_policy_id_reference VARCHAR(12),
	@pi_policy_holder_reference VARCHAR(100),
	@pi_product_name_reference VARCHAR(100),
	@pi_currency_id_reference VARCHAR(3),
	@pi_currency_code VARCHAR(3),
	@pi_is_policy_holder TINYINT = 0,
	@pi_customer VARCHAR(100),
	@pi_address VARCHAR(255),
	@pi_mobile_phone VARCHAR(15),
	@pi_email_address VARCHAR(30) = NULL,
	@pi_user_id VARCHAR(8),
	@pi_branch_id INT,
	@pi_status_id INT,
	@pi_source_transaction_id SMALLINT,
	@pi_transaction_id BIGINT OUTPUT
)
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	INSERT INTO t_transaction
	(
		transaction_type_id,
		policy_id_reference,
		policy_holder_reference,
		product_name_reference,
		currency_id_reference,
		currency_code,
		is_policy_holder,
		customer,
		address,
		mobile_phone,
		email_address,
		user_id,
		branch_id,
		status_id,
		created_by,
		created_date,
		modified_by,
		modified_date,
		source_transaction_id
	)
	VALUES (
		@pi_transaction_type_id,
		@pi_policy_id_reference,
		@pi_policy_holder_reference,
		@pi_product_name_reference,
		@pi_currency_id_reference,
		@pi_currency_code,
		COALESCE(@pi_is_policy_holder,0),
		@pi_customer,
		@pi_address,
		@pi_mobile_phone,
		@pi_email_address,
		@pi_user_id,
		@pi_branch_id,
		@pi_status_id,
		@pi_user_id,
		GETDATE(),
		@pi_user_id,
		GETDATE(),
		@pi_source_transaction_id
	);

	SET @pi_transaction_id = SCOPE_IDENTITY();

	RETURN
    
END
GO
/****** Object:  StoredProcedure [dbo].[AddTransactionWrapper]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[AddTransactionWrapper] (
	@pi_transaction_type_id INT,
	@pi_policy_id_reference VARCHAR(12),
	@pi_policy_holder_reference VARCHAR(100),
	@pi_product_name_reference VARCHAR(100),
	@pi_currency_id_reference VARCHAR(3),
	@pi_currency_code VARCHAR(3),
	@pi_is_policy_holder TINYINT = 0,
	@pi_customer VARCHAR(100),
	@pi_address VARCHAR(255),
	@pi_mobile_phone VARCHAR(15),
	@pi_email_address VARCHAR(30) = NULL,
	@pi_user_id VARCHAR(8),
	@pi_branch_id INT,
	@pi_status_id INT,
	@pi_source_transaction_id SMALLINT
)
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	DECLARE @transid BIGINT;
	DECLARE @transreg VARCHAR(9);
	DECLARE @is_ph TINYINT;

	EXEC AddTransaction 
		@pi_transaction_type_id,
		@pi_policy_id_reference,
		@pi_policy_holder_reference,
		@pi_product_name_reference,
		@pi_currency_id_reference,
		@pi_currency_code,
		@pi_is_policy_holder,
		@pi_customer,
		@pi_address,
		@pi_mobile_phone,
		@pi_email_address,
		@pi_user_id,
		@pi_branch_id,
		@pi_status_id,
		@pi_source_transaction_id,
		@transid OUTPUT;

	SET @transreg = (
		SELECT register_number
		FROM t_transaction
		WHERE transaction_id = @transid
	);

	SELECT @transid AS trans_id, @transreg AS trans_reg;
    
END
GO
/****** Object:  Table [dbo].[m_user_organization]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_user_organization](
	[user_organization_id] [bigint] IDENTITY(1,1) NOT NULL,
	[user_id_organization] [varchar](8) NOT NULL,
	[effective_date] [date] NOT NULL,
	[organization_id] [int] NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_user_o__D949BB7C10615C29] PRIMARY KEY CLUSTERED 
(
	[user_organization_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_branch]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_branch](
	[branch_id] [int] IDENTITY(1,1) NOT NULL,
	[branch_code] [varchar](8) NOT NULL,
	[description] [varchar](150) NOT NULL,
	[address] [varchar](255) NULL,
	[city_id] [int] NOT NULL,
	[province_id] [int] NOT NULL,
	[postal_code] [varchar](10) NULL,
	[phone_number_1] [varchar](30) NULL,
	[phone_number_2] [varchar](30) NULL,
	[user_pic] [varchar](8) NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK_m_branch] PRIMARY KEY CLUSTERED 
(
	[branch_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_m_branch] ON [dbo].[m_branch] 
(
	[description] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_m_branch_1] ON [dbo].[m_branch] 
(
	[branch_code] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_m_branch_2] ON [dbo].[m_branch] 
(
	[city_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_m_branch_3] ON [dbo].[m_branch] 
(
	[province_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[m_organization]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_organization](
	[organization_id] [int] IDENTITY(1,1) NOT NULL,
	[organization_code] [varchar](8) NULL,
	[description] [varchar](60) NOT NULL,
	[level_type_id] [int] NOT NULL,
	[user_id_head] [varchar](8) NULL,
	[organization_id_parent] [int] NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_organi__C0B2F4327C5A637C] PRIMARY KEY CLUSTERED 
(
	[organization_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_user]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_user](
	[user_id] [int] IDENTITY(1,1) NOT NULL,
	[username] [varchar](8) NOT NULL,
	[user_fullname] [varchar](50) NOT NULL,
	[branch_id] [int] NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [tinyint] NOT NULL,
 CONSTRAINT [PK__m_user__B9BE370F09B45E9A] PRIMARY KEY CLUSTERED 
(
	[user_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [IX_m_user_unq] UNIQUE NONCLUSTERED 
(
	[username] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_m_user_user_fullname] ON [dbo].[m_user] 
(
	[user_fullname] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_m_user_username_idx] ON [dbo].[m_user] 
(
	[username] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  View [dbo].[v_user]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_user] AS
SELECT 
	m.user_id,
	m.username,
	m.user_fullname,
	m.branch_id,
	m.status_id,
	m.created_by,
	m.created_date,
	m.modified_by,
	m.modified_date,
	b.branch_code,
	b.description,
	org.organization_id,
	org.description AS organization
FROM m_user m
LEFT JOIN m_branch b ON (m.branch_id = b.branch_id)
LEFT JOIN (
	SELECT 
		g.status_id, 
		g.user_id_organization, 
		g.organization_id, 
		m.description
	FROM m_user_organization g
	JOIN m_organization m ON m.organization_id = g.organization_id
	WHERE g.status_id = 5 and g.is_delete = 0
) AS org ON (m.username = org.user_id_organization)
where
	m.is_delete = 0
GO
/****** Object:  Table [dbo].[m_transaction_task_status]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_transaction_task_status](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[description] [varchar](160) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NULL,
	[modified_date] [datetime] NULL,
	[is_delete] [tinyint] NOT NULL,
 CONSTRAINT [PK_m_transaction_task_status] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[t_transaction_task]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[t_transaction_task](
	[transaction_task_id] [bigint] IDENTITY(1,1) NOT NULL,
	[transaction_id] [bigint] NOT NULL,
	[process_sequence] [int] NOT NULL,
	[user_id] [varchar](8) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NULL,
	[modified_date] [datetime] NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__t_transa__BD4B9E624B4D17CD] PRIMARY KEY CLUSTERED 
(
	[transaction_task_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_transaction_type]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_transaction_type](
	[transaction_type_id] [int] IDENTITY(1,1) NOT NULL,
	[transaction_type_code] [char](7) NULL,
	[transaction_category_id] [int] NOT NULL,
	[description] [varchar](150) NOT NULL,
	[remarks] [varchar](150) NULL,
	[footnote_receipt_form] [varchar](400) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_transa__439ABFC12838E5BA] PRIMARY KEY CLUSTERED 
(
	[transaction_type_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_workflow_template_stage]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_workflow_template_stage](
	[workflow_template_stage_id] [bigint] IDENTITY(1,1) NOT NULL,
	[workflow_template_id] [int] NOT NULL,
	[description] [varchar](255) NOT NULL,
	[is_stage_approval] [char](1) NOT NULL,
	[approv_matrix_id] [int] NULL,
	[pic_is_requester] [char](1) NOT NULL,
	[group_pic_id] [int] NULL,
	[user_workflow_id] [int] NULL,
	[can_edit_information] [char](1) NOT NULL,
	[can_upload_document] [char](1) NOT NULL,
	[can_delete_document] [char](1) NOT NULL,
	[seq_number] [numeric](18, 0) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_workfl__DA8575D21D864D1D] PRIMARY KEY CLUSTERED 
(
	[workflow_template_stage_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_transaction_status]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_transaction_status](
	[transaction_status_id] [int] IDENTITY(1,1) NOT NULL,
	[description] [varchar](50) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [varchar](1) NOT NULL,
 CONSTRAINT [PK_m_transaction_status] PRIMARY KEY CLUSTERED 
(
	[transaction_status_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_eform_document]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_eform_document](
	[eform_document_id] [int] IDENTITY(1,1) NOT NULL,
	[eform_id] [int] NOT NULL,
	[document_id] [int] NOT NULL,
	[minimum_files] [numeric](18, 0) NOT NULL,
	[maximum_files] [numeric](18, 0) NOT NULL,
	[seq_number] [numeric](18, 0) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_eform___BF27DE2600EA0E6F] PRIMARY KEY CLUSTERED 
(
	[eform_document_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_eform]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_eform](
	[eform_id] [int] IDENTITY(1,1) NOT NULL,
	[eform_code] [varchar](8) NULL,
	[transaction_type_id] [int] NOT NULL,
	[is_need_surat_kuasa] [char](1) NOT NULL,
	[remarks] [varchar](255) NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_eform__E893FF5030CE2BBB] PRIMARY KEY CLUSTERED 
(
	[eform_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_document]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_document](
	[document_id] [int] IDENTITY(1,1) NOT NULL,
	[document_code] [varchar](8) NULL,
	[description] [varchar](200) NOT NULL,
	[remarks] [varchar](255) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_docume__9666E8AC2CFD9AD7] PRIMARY KEY CLUSTERED 
(
	[document_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  UserDefinedFunction [dbo].[func_is_document_complete]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- by: Pribadi Pamungkas. 2017-10-04
CREATE FUNCTION [dbo].[func_is_document_complete]
(
	@pi_transaction_id BIGINT
)
RETURNS BIT
AS
BEGIN
	DECLARE 
	  @v_min_valid BIT,
	  @v_max_valid BIT,
	  @v_hasil BIT

  SELECT
    @v_min_valid = CASE
                      WHEN med.minimum_files > 0 THEN CASE  -- jika minimum dibatasi
                                                        WHEN COALESCE(df.jumlah_file,0) >= med.minimum_files THEN 1 -- jika yg diupload lebih besar atau sama dengan minimum, maka benar
                                                        ELSE 0                                                      -- jika tidak, maka salah
                                                      END
                      ELSE 1  -- jika minimum tidak dibatasi, maka benar
                    END,
    @v_max_valid = CASE
                      WHEN med.maximum_files > 0 THEN CASE  -- jika maksimum dibatasi
                                                        WHEN COALESCE(df.jumlah_file,0) <= med.maximum_files THEN 1 -- jika yg diupload lebih sedikit atau sama dengan maksimum, maka benar
                                                        ELSE 0                                                      -- jika tidak, maka salah
                                                      END
                      ELSE 1  -- jika maksimum tidak dibatasi, maka benar
                    END
  FROM
    dbo.t_transaction AS t
    INNER JOIN 
    dbo.m_eform AS me ON t.transaction_type_id = me.transaction_type_id
    INNER JOIN 
    dbo.m_eform_document AS med ON me.eform_id = med.eform_id
    INNER JOIN 
    dbo.m_document AS md ON med.document_id = md.document_id
    INNER JOIN
    (
      SELECT
	      ttd.transaction_id,
	      ttd.document_type_id,
	      COUNT(*) AS jumlah_file
      FROM
	      t_transaction_document AS ttd INNER JOIN
	      t_transaction_document_file AS ttdf ON ttd.transaction_document_id = ttdf.transaction_document_id
      WHERE
        ttd.is_delete = '0'
        AND ttdf.is_delete = '0'
      GROUP BY
	      ttd.transaction_id,
	      ttd.document_type_id
    ) AS df ON t.transaction_id = df.transaction_id
               AND md.document_id = df.document_type_id
  WHERE
    (t.is_delete = 0)
    AND (me.is_delete = '0')
    AND (med.is_delete = '0')
    AND (md.is_delete = '0')
    AND t.transaction_id = @pi_transaction_id
  IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			SET @v_hasil = 0
	ELSE
	  BEGIN
      IF @v_min_valid=1 AND @v_max_valid=1
        SET @v_hasil = 1
      ELSE
        SET @v_hasil = 0
    END

	RETURN @v_hasil

END
GO
/****** Object:  View [dbo].[v_get_transaction_all]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_get_transaction_all] AS

SELECT
  t.transaction_id,
  t.transaction_type_id,
  t.register_number,
  t.transaction_date,
  t.source_app_id,
  t.policy_id_reference,
  t.policy_holder_reference,
  t.product_name_reference,
  t.currency_id_reference,
  t.is_policy_holder,
  t.customer,
  t.address,
  t.mobile_phone,
  t.email_address,
  t.signature_url,
  t.photo_url,
  t.reference_number,
  t.currency_id,
  t.currency_code,
  t.transaction_value,
  t.is_notify_document_completion,
  t.notification_date,
  t.workflow_template_stage_id,
  t.user_id,
  t.branch_id,
  t.status_id,
  t.created_by,
  t.created_date,
  t.modified_by,
  t.modified_date,
  t.is_delete,
  y.description AS type_desc,
  st.description AS stage_desc,
  b.description AS branch_desc,
  u.username AS user_username,
  u.user_fullname AS user_userfullname,
  ts.description AS status_desc,
  dbo.func_is_document_complete(t.transaction_id) AS document_valid,
  dbo.func_get_latest_transaction_action(t.transaction_id) AS last_action_id,
  st.can_edit_information,
  st.can_upload_document,
  st.can_delete_document,
  t.source_transaction_id
FROM
  dbo.t_transaction AS t
  INNER JOIN 
  dbo.m_transaction_type AS y ON y.transaction_type_id = t.transaction_type_id
  INNER JOIN 
  dbo.m_workflow_template_stage AS st ON st.workflow_template_stage_id = t.workflow_template_stage_id
  LEFT JOIN 
  dbo.m_branch AS b ON b.branch_id = t.branch_id
  INNER JOIN 
  dbo.m_user AS u ON u.username = t.user_id
  INNER JOIN 
  dbo.m_transaction_status AS ts ON t.status_id = ts.transaction_status_id
GO
/****** Object:  View [dbo].[v_t_transaction_task]    Script Date: 11/10/2017 15:12:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_t_transaction_task] AS

SELECT 

	tt.transaction_task_id, 
	tt.transaction_id, 
	tt.process_sequence, 
	tt.user_id,
	u.user_fullname, 
	vt.register_number,
	vt.policy_id_reference,
	vt.policy_holder_reference,
	vt.branch_desc,
	vt.type_desc,
	vt.stage_desc,
	tt.status_id, 
	ts.description AS status_description,
	tt.is_delete
	
FROM
	t_transaction_task AS tt
	INNER JOIN 
	v_get_transaction_all AS vt ON vt.transaction_id = tt.transaction_id
	INNER JOIN
	m_transaction_task_status AS ts ON ts.id = tt.status_id
	INNER JOIN
	v_user AS u on u.username = tt.user_id

WHERE
	tt.is_delete = 0
	AND ts.is_delete = 0
GO
/****** Object:  StoredProcedure [dbo].[proc_update_os_transaction]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		KHU
-- Create date: 2017-09-09
-- Description:	SP Process From Layar Document Standing Transaction
-- =============================================
CREATE PROCEDURE [dbo].[proc_update_os_transaction] 
	
	@pi_transaction_task_id BIGINT,
	--@pi_transaction_id BIGINT,
	@pi_user_id VARCHAR(8),
	@pi_remarks VARCHAR(255),

	@po_message VARCHAR(400) OUTPUT

AS
BEGIN

	SET NOCOUNT ON;
	
	DECLARE @v_result BIT = 0

	BEGIN TRY
	BEGIN TRANSACTION proses

		DECLARE
			@v_transaction_task_id BIGINT,
			@v_transaction_id BIGINT,
			@v_process_sequence INT,
			@v_user_id VARCHAR(8),
			@status_id INT
	
		SELECT
			@v_transaction_task_id = transaction_task_id,
			@v_transaction_id = transaction_id,
			@v_process_sequence = process_sequence, 
			@v_user_id = user_id, 
			@status_id = status_id
		FROM
			dbo.v_t_transaction_task
		WHERE
			transaction_task_id = @pi_transaction_task_id

		IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			BEGIN
				SET @po_message = 'Transaction not found'
				GOTO ERROR_OUT
			END


		-- Transaction Task Status
		-- 1 Taken
		-- 2 Available
		-- 3 Not Available
		IF @status_id = 1
			BEGIN
				UPDATE
					t_transaction_task
				SET
					status_id = 2,    -- 2 : Available
					modified_by = @pi_user_id,
					modified_date = GETDATE()
				WHERE
					transaction_id = @v_transaction_id AND
					process_sequence = @v_process_sequence AND
					is_delete = 0
			
				IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
					BEGIN
						SET @po_message = 'Failed Update Task Status'
						GOTO ERROR_OUT
					END


				INSERT INTO dbo.t_transaction_history_process
					(transaction_id,
					process_date,
					[user_id],
					workflow_template_action_id,
					action_description,
					reason_id,
					remarks,
					created_by,
					created_date,
					modified_by,
					modified_date,
					is_delete)
				VALUES
					(@v_transaction_id, -- transaction_id - bigint
					GETDATE(), -- process_date - datetime
					@pi_user_id, -- user_id - int
					NULL, -- workflow_template_action_id - bigint
					'Release Task',
					NULL, -- reason_id - int
					@pi_remarks, -- remarks - varchar(255)
					@pi_user_id, -- created_by - varchar(8)
					GETDATE(), -- created_date - datetime
					@pi_user_id, -- modified_by - varchar(8)
					GETDATE(), -- modified_date - datetime
					'0'  -- is_delete - char(1)
					)
				IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
					BEGIN
						SET @po_message = 'Failed insert transaction history process'
						GOTO ERROR_OUT
					END

			END
		
		ELSE IF @status_id = 2

			BEGIN

				UPDATE
					t_transaction_task
				SET
					status_id = 3,    -- 3 : Not Available
					modified_by = @pi_user_id,
					modified_date = GETDATE()
				WHERE
					transaction_id = @v_transaction_id AND
					process_sequence = @v_process_sequence AND
					[user_id] <> @pi_user_id AND
					is_delete = 0
			
				IF (@@ERROR <> 0)
					BEGIN
						SET @po_message = 'Failed Update Task Status Not Available'
						GOTO ERROR_OUT
					END


				UPDATE
					t_transaction_task
				SET
					status_id = 1,    -- 1 : Taken
					modified_by = @pi_user_id,
					modified_date = GETDATE()
				WHERE
					--transaction_id = @pi_transaction_id AND
					[user_id] = @pi_user_id AND
					transaction_task_id = @pi_transaction_task_id AND
					is_delete = 0
			
				IF (@@ERROR <> 0)
					BEGIN
						SET @po_message = 'Failed Update Task Status Taken'
						GOTO ERROR_OUT
					END


				INSERT INTO dbo.t_transaction_history_process
					(transaction_id,
					process_date,
					[user_id],
					workflow_template_action_id,
					action_description,
					reason_id,
					remarks,
					created_by,
					created_date,
					modified_by,
					modified_date,
					is_delete)
				VALUES
					(@v_transaction_id, -- transaction_id - bigint
					GETDATE(), -- process_date - datetime
					@pi_user_id, -- user_id - int
					NULL, -- workflow_template_action_id - bigint
					'Take Task',
					NULL, -- reason_id - int
					NULL, -- remarks - varchar(255)
					@pi_user_id, -- created_by - varchar(8)
					GETDATE(), -- created_date - datetime
					@pi_user_id, -- modified_by - varchar(8)
					GETDATE(), -- modified_date - datetime
					'0'  -- is_delete - char(1)
					)
				IF  (@@ERROR <> 0)
					BEGIN
						SET @po_message = 'Failed insert transaction history process Not Available'
						GOTO ERROR_OUT
					END

			END

		ELSE IF @status_id = 3
			BEGIN
				
				DECLARE
					@v_user_fullname VARCHAR(50)

				SELECT 
					@v_user_fullname = user_fullname
				FROM
					dbo.v_t_transaction_task
				WHERE 
					transaction_id = @v_transaction_id AND
					process_sequence = @v_process_sequence AND
					status_id = 1 -- 1 : Taken

				SELECT
					@v_transaction_task_id = transaction_task_id,
					@v_process_sequence = process_sequence, 
					@v_user_id = user_id, 
					@status_id = status_id
				FROM
					dbo.t_transaction_task
				WHERE
					[user_id] = @v_user_id AND
					transaction_task_id = @pi_transaction_task_id AND
					status_id = 3 AND -- 3 : Not Available
					is_delete = 0
				IF (@@ROWCOUNT = 1)
					BEGIN
						SET @po_message = 'Failed, Transaction has been taken by ' + @v_user_fullname
						GOTO ERROR_OUT
					END
			END


		COMMIT TRANSACTION proses
		SET @po_message = 'Sukses'  -- variable output 
			SET @v_result = 1
			SELECT @v_result AS [status], @po_message AS [message]  -- dual output stored procedure, variable output dan table output
			GOTO CLEAN_OUT

	END TRY
	BEGIN CATCH	-- tangkap error message selain yg sudah dideskripsikan
		SET @po_message = LEFT(ERROR_MESSAGE(),400)	-- berhubung variable diatas diset 400, maka potong error message
		GOTO ERROR_OUT
	END CATCH

	ERROR_OUT: 
		ROLLBACK TRANSACTION proses
		SELECT @v_result AS [status], @po_message AS [message]  -- dual output stored procedure, variable output dan table output
		--RETURN 1

	CLEAN_OUT:
		--RETURN 0

END
GO
/****** Object:  StoredProcedure [dbo].[proc_generate_token]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- by: Pribadi Pamungkas. 2017-09-13
CREATE PROCEDURE [dbo].[proc_generate_token]
	@pi_tipe VARCHAR(1),
	@po_token VARCHAR(10) OUTPUT
AS
BEGIN

	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

  SELECT
    @po_token = RIGHT(CAST(YEAR(GETDATE()) AS VARCHAR(4)),2) +
                @pi_tipe +
                CHAR(
                    ASCII('A') + 
                    (
                      ABS(
                            CHECKSUM(
                                      NEWID()
                                    )
                         ) % 25
                    )
                  ) + 
                CHAR(
                    ASCII('A') + 
                    (
                      ABS(
                            CHECKSUM(
                                      NEWID()
                                    )
                         ) % 25
                    )
                  ) + 
                CHAR(
                    ASCII('A') + 
                    (
                      ABS(
                            CHECKSUM(
                                      NEWID()
                                    )
                         ) % 25
                    )
                  ) + 
                RIGHT(
                  '0000' +
                  CAST(
                        (
                          ABS(
                              CHECKSUM(
                                        NEWID()
                                      )
                             ) % 10000
                        ) AS VARCHAR(4)
                      ),4)

END
GO
/****** Object:  StoredProcedure [dbo].[proc_document_received]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- by: Pribadi Pamungkas. 2017-10-17
-- SP untuk set/unset document received

CREATE PROCEDURE [dbo].[proc_document_received]
    @pi_transaction_id BIGINT,
    @pi_document_type_id INT,
    @pi_receive_doc_check BIT,
    @pi_user_id VARCHAR(8),
    @po_message VARCHAR(200) OUTPUT
AS
BEGIN

	SET NOCOUNT ON; -- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
	
	DECLARE @v_result BIT = 0
	
	BEGIN TRAN

    IF EXISTS(
			 SELECT
				*
			 FROM
				dbo.t_transaction_document
			 WHERE
				transaction_id = @pi_transaction_id
				AND document_type_id = @pi_document_type_id
    )
	   BEGIN  -- jika sudah ada, maka update t_transaction_document

		  UPDATE
			 t_transaction_document
		  SET
			 receive_doc_check = COALESCE(@pi_receive_doc_check,0),
			 modified_date = GETDATE(),
			 modified_by = @pi_user_id
		  WHERE
			 transaction_id = @pi_transaction_id
			 AND document_type_id = @pi_document_type_id
		  IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			 BEGIN
				SET @po_message = 'Failed update t_transaction_document'
				GOTO ERROR_OUT
			 END
	   END
    ELSE
	   BEGIN  -- jika belum ada, maka insert t_transaction_document
		  -- start: generate token
		  DECLARE	
			 @v_retval_token INT,
			 @po_token VARCHAR(10),
			 @v_tipe_token VARCHAR(1) = 'D'

		  EXEC @v_retval_token = [dbo].[proc_generate_token]
							 @pi_tipe = @v_tipe_token,
							 @po_token = @po_token OUTPUT
		  IF (@@ERROR <> 0) OR (@v_retval_token <> 0)
			 BEGIN
				SET @po_message = 'Error generating token.'
				GOTO ERROR_OUT
			 END

		  WHILE(
				EXISTS(
					   SELECT
						  token
					   FROM
						  t_transaction_document
					   WHERE
						  token = @po_token
				)
		  )
			 BEGIN
				EXEC @v_retval_token = [dbo].[proc_generate_token]
								    @pi_tipe = @v_tipe_token,
								    @po_token = @po_token OUTPUT
				IF (@@ERROR <> 0) OR (@v_retval_token <> 0)
				    BEGIN
					   SET @po_message = 'Error generating token.'
					   GOTO ERROR_OUT
				    END
			 END
		  -- end: generate token
    	
		  INSERT INTO dbo.t_transaction_document
    				(transaction_id,
    				document_type_id,
    				token,
    				receive_doc_check,
    				hashcode,
    				document_index,
    				document_name,
    				created_by,
    				created_date,
    				modified_by,
    				modified_date,
    				is_delete)
		  VALUES
    				(@pi_transaction_id, -- transaction_id - bigint
    				@pi_document_type_id, -- document_type_id - int
    				@po_token, -- token - varchar(30)
    				COALESCE(@pi_receive_doc_check,0), -- receive_doc_check - bit
    				NULL, -- hashcode - varchar(30)        -- ini diisi nanti kalau filenya sudah disatukan dalam PDF
    				NULL, -- document_index - varchar(100) -- URL di DMS (?)
    				NULL, -- document_name - varchar(100)  -- ini diisi nama file yang akan disimpan di DMS ([Nomor Register]_[Jenis Transaksi]_[Jenis Dokumen])
    				@pi_user_id, -- created_by - varchar(8)
    				GETDATE(), -- created_date - datetime
    				@pi_user_id, -- modified_by - varchar(8)
    				GETDATE(), -- modified_date - datetime
    				'0'  -- is_delete - char(1)
    				)
		  IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			 BEGIN
				SET @po_message = 'Failed insert t_transaction_document'
				GOTO ERROR_OUT
			 END

	   END

	
	COMMIT TRAN
	SET @po_message = 'Sukses'  -- variable output 
	SET @v_result = 1
	SELECT @v_result AS [status], @po_message AS [message]  -- dual output stored procedure, variable output dan table output
	GOTO CLEAN_OUT

	
	ERROR_OUT: 
		ROLLBACK TRAN
		SELECT @v_result AS [status], @po_message AS [message]  -- dual output stored procedure, variable output dan table output
		--RETURN 1


	CLEAN_OUT:
		--RETURN 0


END
GO
/****** Object:  Table [dbo].[s_user_session]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[s_user_session](
	[session_id] [varchar](50) NOT NULL,
	[signature] [varchar](100) NOT NULL,
	[username] [varchar](3) NOT NULL,
	[first_login] [datetime] NULL,
	[last_activity] [datetime] NULL,
	[session_expire] [datetime] NULL,
	[ip_address] [varchar](30) NULL,
	[user_agent] [varchar](300) NULL,
 CONSTRAINT [PK_s_user_session] PRIMARY KEY CLUSTERED 
(
	[session_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_sig_s_user_session] ON [dbo].[s_user_session] 
(
	[signature] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_sig_uid_s_user_session] ON [dbo].[s_user_session] 
(
	[signature] ASC,
	[username] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_uid_s_user_session] ON [dbo].[s_user_session] 
(
	[username] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  StoredProcedure [dbo].[proc_insert_document]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- by: Pribadi Pamungkas. 2017-09-13
-- SP untuk insert dokumen

CREATE PROCEDURE [dbo].[proc_insert_document]
  @pi_transaction_id BIGINT,
  @pi_document_type_id INT,
  @pi_document_name VARCHAR(200), -- nama file asli yang diupload
  @pi_document_url VARCHAR(100),  -- nama file yg ada di server (nama file setelah diupload)
  @pi_hashcode_ori VARCHAR(30),   -- hashcode sebelum diresize (hashcode file asli)
  @pi_hashcode VARCHAR(30),       -- hashcode setelah file gambar diresize
  @pi_upload_date DATETIME,
  --@pi_user_id VARCHAR(8),
  @pi_session_id VARCHAR(50),
	@po_message VARCHAR(200) OUTPUT
AS
BEGIN

	SET NOCOUNT ON; -- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
	
	BEGIN TRAN
	
	DECLARE
	  @v_transaction_document_id BIGINT,
	  @v_user_id VARCHAR(3)
	
	SELECT
		@v_user_id = username
	FROM
		dbo.s_user_session
	WHERE
	  session_id = @pi_session_id
	
  IF EXISTS(
              SELECT
              	*
              FROM
              	dbo.t_transaction_document
              WHERE
                transaction_id = @pi_transaction_id
                AND document_type_id = @pi_document_type_id
           )
    BEGIN  -- jika sudah ada, maka update t_transaction_document
      SELECT 
        @v_transaction_document_id = transaction_document_id
      FROM
        t_transaction_document
      WHERE
        transaction_id = @pi_transaction_id
        AND document_type_id = @pi_document_type_id
      IF (COALESCE(@v_transaction_document_id,'')='')
				BEGIN
					SET @po_message = 'Empty transaction_document_id'
					ROLLBACK TRAN
					RETURN 1
				END

      UPDATE
        t_transaction_document
      SET
        modified_date = GETDATE(),
        modified_by = @v_user_id
      WHERE
        transaction_id = @pi_transaction_id
        AND document_type_id = @pi_document_type_id
      IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
				BEGIN
					SET @po_message = 'Failed update t_transaction_document'
					ROLLBACK TRAN
					RETURN 1
				END
    END
  ELSE
    BEGIN  -- jika belum ada, maka insert t_transaction_document
    	-- start: generate token
      DECLARE	
        @v_retval_token INT,
		    @po_token VARCHAR(10),
		    @v_tipe_token VARCHAR(1) = 'D'

      EXEC	@v_retval_token = [dbo].[proc_generate_token]
		      @pi_tipe = @v_tipe_token,
		      @po_token = @po_token OUTPUT
      IF (@@ERROR <> 0) OR (@v_retval_token <> 0)
	      BEGIN
		      SET @po_message = 'Error generating token.'
		      ROLLBACK TRAN
		      RETURN 1
	      END

      WHILE(
              EXISTS(
                      SELECT
  	                    token
                      FROM
  	                    t_transaction_document
                      WHERE
                        token = @po_token
                    )
           )
        BEGIN
          EXEC	@v_retval_token = [dbo].[proc_generate_token]
		          @pi_tipe = @v_tipe_token,
		          @po_token = @po_token OUTPUT
          IF (@@ERROR <> 0) OR (@v_retval_token <> 0)
	          BEGIN
		          SET @po_message = 'Error generating token.'
		          ROLLBACK TRAN
		          RETURN 1
	          END
        END
    	-- end: generate token
    	
    	INSERT INTO dbo.t_transaction_document
    	        (transaction_id,
    	         document_type_id,
    	         token,
    	         hashcode,
    	         document_index,
    	         document_name,
    	         created_by,
    	         created_date,
    	         modified_by,
    	         modified_date,
    	         is_delete)
    	VALUES
    	        (@pi_transaction_id, -- transaction_id - bigint
    	         @pi_document_type_id, -- document_type_id - int
    	         @po_token, -- token - varchar(30)
    	         NULL, -- hashcode - varchar(30)        -- ini diisi nanti kalau filenya sudah disatukan dalam PDF
    	         NULL, -- document_index - varchar(100) -- URL di DMS (?)
    	         NULL, -- document_name - varchar(100)  -- ini diisi nama file yang akan disimpan di DMS ([Nomor Register]_[Jenis Transaksi]_[Jenis Dokumen])
    	         @v_user_id, -- created_by - varchar(8)
    	         GETDATE(), -- created_date - datetime
    	         @v_user_id, -- modified_by - varchar(8)
    	         GETDATE(), -- modified_date - datetime
    	         '0'  -- is_delete - char(1)
    	         )
    	IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
				BEGIN
					SET @po_message = 'Failed insert t_transaction_document'
					ROLLBACK TRAN
					RETURN 1
				END
    	
    	SELECT @v_transaction_document_id = SCOPE_IDENTITY()
    	IF (COALESCE(@v_transaction_document_id,'')='')
				BEGIN
					SET @po_message = 'Empty identity output from t_transaction_document'
					ROLLBACK TRAN
					RETURN 1
				END
        
    END

  	INSERT INTO dbo.t_transaction_document_file
  	        (transaction_document_id,
  	         document_name,
  	         document_url,
  	         hashcode_ori,
  	         hashcode,
  	         upload_by,
  	         upload_date,
  	         created_by,
  	         created_date,
  	         modified_by,
  	         modified_date,
  	         is_delete)
  	VALUES
  	        (@v_transaction_document_id, -- transaction_document_id - bigint
  	         @pi_document_name, -- document_name - varchar(200)
  	         @pi_document_url, -- document_url - varchar(100)
  	         @pi_hashcode_ori, -- hashcode_ori - varchar(30)
  	         @pi_hashcode, -- hashcode - varchar(30)
  	         @v_user_id, -- upload_by - varchar(8)
  	         COALESCE(@pi_upload_date,GETDATE()), -- upload_date - datetime
  	         @v_user_id, -- created_by - varchar(8)
  	         GETDATE(), -- created_date - datetime
  	         @v_user_id, -- modified_by - varchar(8)
  	         GETDATE(), -- modified_date - datetime
  	         '0'  -- is_delete - char(1)
  	         )
  	IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			BEGIN
				SET @po_message = 'Failed insert t_transaction_document_file'
				ROLLBACK TRAN
				RETURN 1
			END

  
	COMMIT TRAN
	
	SET @po_message = 'Sukses'
	SELECT @po_message  -- dual output stored procedure, variable output dan table output
	
	RETURN 0

END
GO
/****** Object:  Table [dbo].[m_user_workflow]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_user_workflow](
	[user_workflow_id] [int] IDENTITY(1,1) NOT NULL,
	[user_workflow_code] [varchar](1) NOT NULL,
	[user_workflow_name] [varchar](1) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[user_workflow_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_source_transaction]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_source_transaction](
	[source_transaction_id] [smallint] IDENTITY(1,1) NOT NULL,
	[description] [varchar](255) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [bit] NOT NULL,
 CONSTRAINT [PK_m_source_transaction] PRIMARY KEY CLUSTERED 
(
	[source_transaction_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_source_of_funds]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_source_of_funds](
	[source_of_funds_id] [int] IDENTITY(1,1) NOT NULL,
	[description] [varchar](160) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NULL,
	[modified_date] [datetime] NULL,
	[is_delete] [tinyint] NOT NULL,
 CONSTRAINT [PK_m_source_of_funds] PRIMARY KEY CLUSTERED 
(
	[source_of_funds_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[t_transaction_task_log]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[t_transaction_task_log](
	[id] [bigint] IDENTITY(1,1) NOT NULL,
	[transaction_task_id] [bigint] NOT NULL,
	[transaction_id] [bigint] NOT NULL,
	[process_sequence] [int] NOT NULL,
	[user_id] [varchar](8) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK_t_transaction_task_log] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[x_dev_sit]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[x_dev_sit](
	[No] [tinyint] IDENTITY(1,1) NOT NULL,
	[Module] [varchar](50) NULL,
	[Date_Raised] [date] NULL,
	[Issue_Descriptions] [varchar](max) NULL,
	[Raised_By] [varchar](3) NULL,
	[PIC] [varchar](3) NULL,
	[Priority] [varchar](1) NULL,
	[Type] [varchar](20) NULL,
	[Status] [varchar](20) NULL,
	[Resolution] [varchar](255) NULL,
	[Target_Date] [date] NULL,
	[Closed_Date] [date] NULL,
	[Comment] [varchar](255) NULL,
	[created_by] [varchar](8) NULL,
	[created_date] [datetime] NULL,
	[modified_by] [varchar](8) NULL,
	[modified_date] [datetime] NULL,
 CONSTRAINT [PK_x_dev_sit] PRIMARY KEY CLUSTERED 
(
	[No] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[t_transaction_log]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[t_transaction_log](
	[t_transaction_log_id] [bigint] IDENTITY(1,1) NOT NULL,
	[transaction_id] [bigint] NOT NULL,
	[transaction_type_id] [int] NOT NULL,
	[register_number] [varchar](9) NULL,
	[transaction_date] [datetime] NOT NULL,
	[source_app_id] [int] NULL,
	[policy_id_reference] [varchar](12) NOT NULL,
	[policy_holder_reference] [varchar](100) NOT NULL,
	[product_name_reference] [varchar](100) NOT NULL,
	[currency_id_reference] [varchar](20) NOT NULL,
	[is_policy_holder] [tinyint] NOT NULL,
	[customer] [varchar](100) NOT NULL,
	[address] [varchar](255) NOT NULL,
	[mobile_phone] [varchar](15) NOT NULL,
	[email_address] [varchar](30) NULL,
	[signature_url] [varchar](300) NULL,
	[signature_hashcode] [varchar](50) NULL,
	[photo_url] [varchar](300) NULL,
	[photo_hashcode] [varchar](50) NULL,
	[reference_number] [varchar](50) NULL,
	[currency_id] [int] NULL,
	[currency_code] [varchar](3) NULL,
	[transaction_value] [decimal](8, 2) NULL,
	[is_notify_document_completion] [tinyint] NULL,
	[notification_date] [datetime] NULL,
	[workflow_template_stage_id] [bigint] NULL,
	[user_id] [varchar](8) NOT NULL,
	[branch_id] [int] NOT NULL,
	[status_id] [int] NOT NULL,
	[eform_workflow_id] [int] NULL,
	[source_transaction_id] [smallint] NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[is_delete] [tinyint] NOT NULL,
 CONSTRAINT [PK_t_transaction_log] PRIMARY KEY CLUSTERED 
(
	[t_transaction_log_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_t_transaction_log] ON [dbo].[t_transaction_log] 
(
	[transaction_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_t_transaction_log_1] ON [dbo].[t_transaction_log] 
(
	[register_number] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[user_menu_access]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[user_menu_access](
	[id] [bigint] IDENTITY(1,1) NOT NULL,
	[user_id] [varchar](3) NOT NULL,
	[menu_id] [int] NOT NULL,
	[group_id] [int] NULL,
	[is_exclude] [tinyint] NOT NULL,
	[created_by] [varchar](3) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](3) NULL,
	[modified_date] [datetime] NULL,
	[is_delete] [tinyint] NOT NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[s_log]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[s_log](
	[log_id] [bigint] IDENTITY(1,1) NOT NULL,
	[date] [datetime] NOT NULL,
	[request_parameter] [nvarchar](max) NULL,
	[response] [nvarchar](max) NULL,
 CONSTRAINT [PK_s_log] PRIMARY KEY CLUSTERED 
(
	[log_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[t_transaction_form_log]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[t_transaction_form_log](
	[id] [bigint] IDENTITY(1,1) NOT NULL,
	[transaction_form_id] [bigint] NOT NULL,
	[transaction_id] [bigint] NOT NULL,
	[register_number] [varchar](9) NOT NULL,
	[transaction_sub_type] [int] NULL,
	[currency] [varchar](10) NULL,
	[money_loans] [varchar](50) NULL,
	[term] [varchar](50) NULL,
	[installment_date] [date] NULL,
	[identity_date] [datetime] NULL,
	[amount] [varchar](30) NULL,
	[submission_date] [datetime] NULL,
	[source_of_fund_id] [int] NULL,
	[gross_income_id] [int] NULL,
	[gross_income_year_id] [int] NULL,
	[is_settlement] [tinyint] NULL,
	[settlement_name] [varchar](50) NULL,
	[policy_no] [varchar](50) NULL,
	[is_transfer_bank] [tinyint] NULL,
	[in_currency] [varchar](50) NULL,
	[beneficiary_name] [varchar](100) NULL,
	[bank_name] [varchar](100) NULL,
	[branch] [varchar](100) NULL,
	[account_no] [varchar](50) NULL,
	[transfer_amount] [varchar](50) NULL,
	[remarks] [varchar](255) NULL,
	[reinstatement_type] [int] NULL,
	[m_alteration_type_id] [int] NULL,
	[event_date] [date] NULL,
	[settlement_policy_no] [varchar](160) NULL,
	[is_cancel_from_inception] [tinyint] NULL,
	[is_surrender] [tinyint] NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[is_delete] [tinyint] NOT NULL,
 CONSTRAINT [PK_t_transaction_form_log] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_t_transaction_form_log] ON [dbo].[t_transaction_form_log] 
(
	[transaction_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_t_transaction_form_log_1] ON [dbo].[t_transaction_form_log] 
(
	[register_number] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[m_alteration_type]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_alteration_type](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[description] [varchar](160) NOT NULL,
	[is_technical] [tinyint] NOT NULL,
	[created_by] [varchar](8) NULL,
	[created_date] [datetime] NULL,
	[modified_by] [varchar](8) NULL,
	[modified_date] [datetime] NULL,
	[is_delete] [tinyint] NOT NULL,
 CONSTRAINT [PK_m_alteration_type] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_m_alteration_type_desc] ON [dbo].[m_alteration_type] 
(
	[description] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_m_alteration_type_desc_tech] ON [dbo].[m_alteration_type] 
(
	[description] ASC,
	[is_technical] ASC,
	[is_delete] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_m_alteration_type_is_tech] ON [dbo].[m_alteration_type] 
(
	[is_technical] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[log_t_transaction_task]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_t_transaction_task](
	[transaction_task_id] [bigint] NULL,
	[transaction_id] [bigint] NOT NULL,
	[process_id_transaction_task] [bigint] NOT NULL,
	[user_id] [int] NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_t_transaction_history_workflow]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_t_transaction_history_workflow](
	[transaction_history_workflow_id] [bigint] NULL,
	[transaction_id] [bigint] NOT NULL,
	[workflow_template_stage_id] [bigint] NULL,
	[last_user] [varchar](8) NOT NULL,
	[last_submit_date] [varchar](50) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_t_transaction_history_process]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_t_transaction_history_process](
	[transaction_history_process_id] [bigint] NULL,
	[transaction_id] [bigint] NOT NULL,
	[process_date] [datetime] NOT NULL,
	[user_id] [varchar](8) NOT NULL,
	[workflow_template_action_id] [bigint] NULL,
	[action_description] [varchar](200) NULL,
	[reason_id] [int] NULL,
	[remarks] [varchar](255) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_t_transaction_form]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_t_transaction_form](
	[transaction_form_id] [bigint] NULL,
	[transaction_id] [bigint] NULL,
	[transaction_sub_type] [int] NULL,
	[currency] [varchar](10) NULL,
	[money_loans] [varchar](50) NULL,
	[term] [varchar](50) NULL,
	[identity_date] [datetime] NULL,
	[amount] [varchar](30) NULL,
	[submission_date] [datetime] NULL,
	[source_of_fund_id] [int] NULL,
	[gross_income_id] [int] NULL,
	[gross_income_year_id] [int] NULL,
	[is_settlement] [char](1) NULL,
	[settlement_name] [varchar](50) NULL,
	[policy_no] [varchar](50) NULL,
	[is_transfer_bank] [char](1) NULL,
	[in_currency] [varchar](50) NULL,
	[beneficiary_name] [varchar](100) NULL,
	[bank_name] [varchar](100) NULL,
	[branch] [varchar](100) NULL,
	[account_no] [varchar](50) NULL,
	[transfer_amount] [varchar](50) NULL,
	[remarks] [varchar](255) NULL,
	[created_by] [varchar](8) NULL,
	[created_date] [datetime] NULL,
	[modified_by] [varchar](8) NULL,
	[modified_date] [datetime] NULL,
	[is_delete] [char](1) NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_t_transaction_document_file_history]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_t_transaction_document_file_history](
	[transaction_document_file_history_id] [bigint] NULL,
	[transaction_document_file_id] [bigint] NOT NULL,
	[workflow_template_stage_id] [bigint] NOT NULL,
	[process_date] [datetime] NOT NULL,
	[user_id] [int] NOT NULL,
	[token] [varchar](30) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_t_transaction_document_file]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_t_transaction_document_file](
	[transaction_document_file_id] [bigint] NULL,
	[transaction_document_id] [bigint] NOT NULL,
	[document_name] [varchar](100) NOT NULL,
	[document_url] [varchar](100) NOT NULL,
	[hashcode_ori] [varchar](30) NOT NULL,
	[hashcode] [varchar](30) NOT NULL,
	[upload_by] [varchar](8) NOT NULL,
	[upload_date] [datetime] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_t_transaction_document]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_t_transaction_document](
	[transaction_document_id] [bigint] NULL,
	[transaction_id] [bigint] NOT NULL,
	[document_type_id] [int] NOT NULL,
	[token] [varchar](30) NULL,
	[hashcode] [varchar](30) NULL,
	[document_index] [varchar](100) NULL,
	[document_name] [varchar](100) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_t_transaction]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_t_transaction](
	[transaction_id] [bigint] NULL,
	[transaction_type_id] [int] NOT NULL,
	[register_number] [varchar](9) NULL,
	[transaction_date] [datetime] NULL,
	[source_app_id] [int] NULL,
	[policy_id_reference] [varchar](12) NOT NULL,
	[policy_holder_reference] [varchar](100) NOT NULL,
	[product_name_reference] [varchar](100) NOT NULL,
	[currency_id_reference] [varchar](20) NOT NULL,
	[is_policy_holder] [char](1) NOT NULL,
	[customer] [varchar](100) NOT NULL,
	[address] [varchar](255) NOT NULL,
	[mobile_phone] [varchar](15) NOT NULL,
	[email_address] [varchar](30) NULL,
	[signature_url] [varchar](300) NULL,
	[photo_url] [varchar](300) NULL,
	[reference_number] [varchar](50) NULL,
	[currency_id] [int] NULL,
	[transaction_value] [decimal](8, 2) NULL,
	[is_notify_document_completion] [char](1) NULL,
	[notification_date] [datetime] NULL,
	[workflow_template_stage_id] [bigint] NULL,
	[user_id] [varchar](8) NOT NULL,
	[branch_id] [int] NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_s_m_log_type]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_s_m_log_type](
	[log_type_id] [tinyint] NOT NULL,
	[icon] [varchar](20) NULL,
	[description] [varchar](20) NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_s_error_code]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_s_error_code](
	[error_code] [smallint] NOT NULL,
	[error_description] [varchar](100) NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_s_action_type]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_s_action_type](
	[id_action_type] [tinyint] NOT NULL,
	[description] [varchar](40) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_s_action_list]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_s_action_list](
	[action_id] [int] NULL,
	[action_name] [varchar](60) NULL,
	[action_type] [tinyint] NOT NULL,
	[condition_field] [varchar](500) NULL,
	[result_object] [varchar](60) NULL,
	[result_field] [varchar](1000) NULL,
	[default_order] [varchar](20) NULL,
	[max_result_row] [smallint] NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_workflow_template_stage]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_workflow_template_stage](
	[workflow_template_stage_id] [bigint] NOT NULL,
	[workflow_template_id] [int] NOT NULL,
	[description] [varchar](255) NOT NULL,
	[is_stage_approval] [char](1) NOT NULL,
	[approv_matrix_id] [int] NULL,
	[pic_is_requester] [char](1) NOT NULL,
	[group_pic_id] [int] NULL,
	[user_workflow_id] [int] NULL,
	[can_edit_information] [char](1) NOT NULL,
	[can_upload_document] [char](1) NOT NULL,
	[can_delete_document] [char](1) NOT NULL,
	[seq_number] [numeric](18, 0) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_workflow_template_action]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_workflow_template_action](
	[workflow_template_action_id] [bigint] NULL,
	[workflow_template_stage_id] [bigint] NULL,
	[description] [varchar](255) NOT NULL,
	[is_final_action] [char](1) NOT NULL,
	[workflow_template_id] [int] NULL,
	[destination_stage_id] [int] NULL,
	[is_send_notif_to_requester] [char](1) NOT NULL,
	[is_send_notif_to_next_pic] [char](1) NOT NULL,
	[is_notif_document_completion] [char](1) NOT NULL,
	[is_check_mandatory_document] [char](1) NOT NULL,
	[is_remarks_mandatory] [char](1) NULL,
	[is_fill_reference_number] [char](1) NOT NULL,
	[is_check_transaction_status_core] [char](1) NULL,
	[code_transaction_status] [varchar](50) NULL,
	[is_need_reason] [char](1) NOT NULL,
	[reason_category_id] [int] NULL,
	[transaction_status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_workflow_template]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_workflow_template](
	[workflow_template_id] [int] NULL,
	[workflow_template_code] [varchar](8) NOT NULL,
	[description] [varchar](225) NOT NULL,
	[remarks] [varchar](225) NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_user_organization]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_user_organization](
	[user_organization_id] [bigint] NULL,
	[user_id_organization] [varchar](8) NOT NULL,
	[effective_date] [date] NOT NULL,
	[organization_id] [int] NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_user_menu]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_user_menu](
	[user_menu_id] [int] NULL,
	[user_menu] [varchar](8) NOT NULL,
	[is_group_menu] [char](1) NOT NULL,
	[group_menu_id] [int] NOT NULL,
	[is_include] [char](1) NOT NULL,
	[is_exclude] [char](1) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_user]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_user](
	[user_id] [int] NULL,
	[username] [varchar](8) NOT NULL,
	[user_fullname] [varchar](50) NOT NULL,
	[branch_id] [int] NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_transaction_type]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_transaction_type](
	[transaction_type_id] [int] NULL,
	[transaction_type_code] [char](7) NULL,
	[transaction_category_id] [int] NOT NULL,
	[description] [varchar](150) NOT NULL,
	[remarks] [varchar](150) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_transaction_category]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_transaction_category](
	[transaction_category_id] [int] NULL,
	[transaction_category_code] [char](6) NOT NULL,
	[description] [varchar](150) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_status]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_status](
	[status_id] [int] NULL,
	[status_code] [char](6) NOT NULL,
	[description] [varchar](50) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_source_app]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_source_app](
	[source_app_id] [int] NULL,
	[source_app_code] [char](6) NOT NULL,
	[source_app_name] [varchar](50) NOT NULL,
	[source_main_url] [varchar](100) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_reason_category]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_reason_category](
	[reason_category_id] [int] NULL,
	[reason_category_code] [varchar](10) NULL,
	[description] [varchar](100) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_reason]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_reason](
	[reason_id] [int] NULL,
	[reason_category_id] [int] NOT NULL,
	[description] [varchar](225) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_province]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_province](
	[province_id] [int] NULL,
	[province_code] [char](6) NULL,
	[province_name] [varchar](30) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_pic]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_pic](
	[pic_id] [int] NULL,
	[pic_code] [varchar](8) NOT NULL,
	[description] [varchar](50) NOT NULL,
	[remarks] [varchar](255) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_organization]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_organization](
	[organization_id] [int] NULL,
	[organization_code] [varchar](8) NULL,
	[description] [varchar](60) NOT NULL,
	[level_type_id] [int] NOT NULL,
	[user_id_head] [varchar](8) NULL,
	[organization_id_parent] [int] NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_menu_category]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_menu_category](
	[menu_category_id] [int] NULL,
	[description] [varchar](160) NULL,
	[created_by] [varchar](3) NULL,
	[created_date] [datetime] NULL,
	[modified_by] [varchar](3) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [tinyint] NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_menu]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_menu](
	[menu_id] [int] NULL,
	[menu_category_id] [int] NOT NULL,
	[url] [varchar](400) NULL,
	[description] [varchar](255) NOT NULL,
	[level] [int] NOT NULL,
	[menu_id_parent] [int] NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NULL,
	[modified_date] [datetime] NULL,
	[is_delete] [char](1) NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_level_type]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_level_type](
	[level_type_id] [int] NULL,
	[level_type_code] [varchar](8) NOT NULL,
	[description] [varchar](60) NOT NULL,
	[head_description] [varchar](60) NOT NULL,
	[seq_number] [numeric](18, 0) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_group_pic_detail]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_group_pic_detail](
	[group_pic_detail_id] [bigint] NULL,
	[group_pic_id] [int] NOT NULL,
	[source_app_id] [int] NULL,
	[branch_id] [varchar](5) NOT NULL,
	[transaction_type_id] [varchar](5) NOT NULL,
	[user_id_group_pic_detail] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_group_pic]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_group_pic](
	[group_pic_id] [int] NULL,
	[group_pic_code] [varchar](12) NOT NULL,
	[description] [varchar](225) NOT NULL,
	[remarks] [varchar](225) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [varchar](1) NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_group_menu_mapping]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_group_menu_mapping](
	[group_menu_mapping_id] [int] NULL,
	[group_menu_id] [int] NOT NULL,
	[menu_id] [int] NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_group_menu]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_group_menu](
	[group_menu_id] [int] NULL,
	[group_menu_code] [varchar](6) NULL,
	[description] [varchar](255) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NULL,
	[modified_date] [datetime] NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_eform_workflow]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_eform_workflow](
	[eform_workflow_id] [int] NULL,
	[eform_id] [int] NOT NULL,
	[workflow_template_id] [int] NOT NULL,
	[effective_date] [date] NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_eform_setting_detail]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_eform_setting_detail](
	[eform_setting_detail_id] [int] NULL,
	[eform_setting_id] [bigint] NOT NULL,
	[component_type_id] [bigint] NOT NULL,
	[label] [varchar](50) NULL,
	[row] [numeric](18, 0) NULL,
	[column_order] [numeric](18, 0) NULL,
	[width] [numeric](18, 0) NULL,
	[height] [numeric](18, 0) NULL,
	[data_type_id] [int] NULL,
	[source_data] [varchar](50) NULL,
	[saved_value] [varchar](50) NULL,
	[view_value] [varchar](50) NULL,
	[data_lenght] [varchar](50) NULL,
	[is_mandatory_field] [char](1) NOT NULL,
	[is_used_for_approval] [char](1) NOT NULL,
	[is_sub_question] [char](1) NOT NULL,
	[component_id_parent] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_eform_setting]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_eform_setting](
	[eform_setting_id] [bigint] NULL,
	[eform_id] [int] NOT NULL,
	[description] [varchar](50) NOT NULL,
	[effective_date] [date] NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_eform_document]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_eform_document](
	[eform_document_id] [int] NULL,
	[eform_id] [int] NOT NULL,
	[document_id] [int] NOT NULL,
	[minimum_files] [numeric](18, 0) NOT NULL,
	[maximum_files] [numeric](18, 0) NOT NULL,
	[seq_number] [numeric](18, 0) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_eform]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_eform](
	[eform_id] [int] NULL,
	[eform_code] [varchar](8) NULL,
	[transaction_type_id] [int] NOT NULL,
	[is_need_surat_kuasa] [char](1) NOT NULL,
	[remarks] [varchar](255) NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[transaction_form_template_id] [smallint] NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_document_type]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_document_type](
	[document_type_id] [int] NULL,
	[document_type_code] [varchar](50) NOT NULL,
	[description] [varchar](255) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [varchar](1) NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_document]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_document](
	[document_id] [int] NULL,
	[document_code] [varchar](8) NULL,
	[description] [varchar](200) NOT NULL,
	[remarks] [varchar](255) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_data_type]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_data_type](
	[data_type_id] [int] NULL,
	[data_type_code] [varchar](50) NOT NULL,
	[description] [varchar](255) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [varchar](1) NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_currency]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_currency](
	[currency_id] [int] NULL,
	[currency_code] [varchar](50) NOT NULL,
	[description] [varchar](255) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_component_type]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_component_type](
	[component_type_id] [bigint] NULL,
	[component_type_code] [varchar](8) NOT NULL,
	[description] [varchar](50) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_city]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_city](
	[city_id] [int] NULL,
	[city_code] [char](6) NULL,
	[province_id] [int] NOT NULL,
	[city_name] [varchar](160) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_branch]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_branch](
	[branch_id] [int] NULL,
	[branch_code] [varchar](8) NOT NULL,
	[description] [varchar](150) NOT NULL,
	[address] [varchar](255) NULL,
	[city_id] [int] NOT NULL,
	[province_id] [int] NOT NULL,
	[postal_code] [varchar](10) NULL,
	[phone_number_1] [varchar](30) NULL,
	[phone_number_2] [varchar](30) NULL,
	[user_pic] [varchar](8) NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_approv_matrix_detail]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_approv_matrix_detail](
	[approv_matrix_detail_id] [bigint] NULL,
	[approv_matrix_id] [int] NOT NULL,
	[maximum_value] [decimal](12, 2) NOT NULL,
	[level_type_id] [int] NOT NULL,
	[currency_id] [int] NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_approv_matrix]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_approv_matrix](
	[approv_matrix_id] [int] NULL,
	[approv_matrix_code] [char](8) NULL,
	[description] [varchar](225) NOT NULL,
	[remarks] [varchar](225) NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_m_transaction_form_template]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_m_transaction_form_template](
	[transaction_form_template_id] [smallint] NULL,
	[description] [varchar](150) NOT NULL,
	[filename] [varchar](200) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
	[AuditDataState] [varchar](10) NULL,
	[AuditDMLAction] [varchar](10) NULL,
	[AuditUser] [sysname] NULL,
	[AuditDateTime] [datetime] NULL,
	[UpdateColumns] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[log_DDLEvents]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[log_DDLEvents](
	[EventDate] [datetime] NOT NULL,
	[EventType] [nvarchar](64) NULL,
	[EventDDL] [nvarchar](max) NULL,
	[EventXML] [xml] NULL,
	[DatabaseName] [nvarchar](255) NULL,
	[SchemaName] [nvarchar](255) NULL,
	[ObjectName] [nvarchar](255) NULL,
	[HostName] [varchar](64) NULL,
	[IPAddress] [varchar](32) NULL,
	[ProgramName] [nvarchar](255) NULL,
	[LoginName] [nvarchar](255) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  StoredProcedure [dbo].[GenerateTriggersDML]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROC [dbo].[GenerateTriggersDML]    
 @Schemaname Sysname = 'dbo'    
,@Tablename  Sysname    
,@GenerateScriptOnly    bit = 1   
,@ForceDropAuditTable   bit = 0   
,@IgnoreExistingColumnMismatch   bit = 0   
,@DontAuditforUsers NVARCHAR(4000) =  '' 
,@DontAuditforColumns NVARCHAR(4000) =  '' 
AS    
    
SET NOCOUNT ON    
 /* NOTE FOR DEVELOPER:   
Parameters    
@Schemaname						- SchemaName to which the table belongs to. Default value 'dbo'.    
@Tablename						- TableName for which the procs needs to be generated.    
@GenerateScriptOnly				- When passed 1 , this will generate the scripts alone..    
								  When passed 0 , this will create the audit tables and triggers in the current database.    
								  Default value is 1    
@ForceDropAuditTable			- When passed 1 , will drop the audit table and recreate  
								  When passed 0 , will generate the alter scripts  
								  Default value is 0  
@IgnoreExistingColumnMismatch	- When passed 1 , will not stop with the error on the mismatch of existing column and will create the trigger.  
                                  When passed 0 , will stop with the error on the mismatch of existing column.  
                                  Default value is 0  
@DontAuditforUsers				- Pass the UserName as comma seperated for whom the audit is not required. 
								  Default value is '' which will do audit for all the users. 
 
@DontAuditforColumns - Pass the ColumnNames as comma seperated for which the audit is not required. 
      Default value is '' which will do audit for all the users. 
*/
   
DECLARE @SQL VARCHAR(MAX)    
DECLARE @SQLTrigger VARCHAR(MAX)    
DECLARE @ErrMsg VARCHAR(MAX)     
 
DECLARE @AuditTableName SYSNAME    
DECLARE @QuotedSchemaName SYSNAME 
DECLARE @QuotedTableName SYSNAME 
DECLARE @QuotedAuditTableName SYSNAME 
DECLARE @InsertTriggerName SYSNAME 
DECLARE @UpdateTriggerName SYSNAME 
DECLARE @DeleteTriggerName SYSNAME 
DECLARE @QuotedInsertTriggerName SYSNAME 
DECLARE @QuotedUpdateTriggerName SYSNAME 
DECLARE @QuotedDeleteTriggerName SYSNAME 
DECLARE @DontAuditforUsersTmp NVARCHAR(4000) 
 
SELECT @AuditTableName =  'log_'+@Tablename    
SELECT @QuotedSchemaName = QUOTENAME(@Schemaname) 
SELECT @QuotedTableName = QUOTENAME(@Tablename) 
SELECT @QuotedAuditTableName = QUOTENAME(@AuditTableName) 
SELECT @InsertTriggerName = @Tablename + '_Insert'   
SELECT @UpdateTriggerName = @Tablename + '_Update'   
SELECT @DeleteTriggerName = @Tablename + '_Delete'   
SELECT @QuotedInsertTriggerName = QUOTENAME(@InsertTriggerName) 
SELECT @QuotedUpdateTriggerName = QUOTENAME(@UpdateTriggerName) 
SELECT @QuotedDeleteTriggerName = QUOTENAME(@DeleteTriggerName) 
 
IF LTRIM(RTRIM(@DontAuditforUsers)) <> '' 
BEGIN 
 
    IF RIGHT(@DontAuditforUsers,1) = ',' 
    BEGIN 
        SELECT @DontAuditforUsersTmp = LEFT(@DontAuditforUsers,LEN(@DontAuditforUsers) -1) 
    END 
    ELSE 
    BEGIN 
        SELECT @DontAuditforUsersTmp = @DontAuditforUsers 
    END 
     
    SELECT @DontAuditforUsersTmp = REPLACE(@DontAuditforUsersTmp,',',''',''') 
 
END 
 
 
SELECT @DontAuditforColumns =',' + UPPER(@DontAuditforColumns) + ',' 
 
IF NOT EXISTS (SELECT 1     
             FROM sys.objects     
            WHERE Name= @TableName    
              AND Schema_id=Schema_id(@Schemaname)    
              AND Type = 'U')  
BEGIN 
    SELECT @ErrMsg = @QuotedSchemaName + '.' + @QuotedTableName + ' Table Not Found '  
    RAISERROR(@ErrMsg ,16,1) 
    RETURN 
END 
 
    
----------------------------------------------------------------------------------------------------------------------    
-- Audit Create OR Alter table     
----------------------------------------------------------------------------------------------------------------------    
   
DECLARE @ColList VARCHAR(MAX)   
DECLARE @InsertColList VARCHAR(MAX)   
DECLARE @UpdateCheck VARCHAR(MAX)   
  
DECLARE @NewAddedCols TABLE  
(  
 ColumnName SYSNAME  
,DataType   SYSNAME  
,CharLength INT  
,Collation  SYSNAME NULL  
,ChangeType VARCHAR(20) NULL  
,MainTableColumnName SYSNAME  NULL  
,MainTableDataType   SYSNAME  NULL  
,MainTableCharLength INT  NULL  
,MainTableCollation SYSNAME  NULL  
,AuditTableColumnName SYSNAME  NULL  
,AuditTableDataType   SYSNAME  NULL  
,AuditTableCharLength INT  NULL  
,AuditTableCollation SYSNAME  NULL  
)  
  
   
SELECT @ColList = ''   
SELECT @UpdateCheck = ' '   
SELECT @SQL = ''  
SELECT @InsertColList = '' 
 
  
SELECT @ColList = @ColList +   CASE SC.is_identity   
                                           WHEN 1 THEN 'CONVERT(' + ST.name + ',' + QUOTENAME(SC.name) + ') as ' + QUOTENAME(SC.name)   
                                           ELSE QUOTENAME(SC.name)   
                                           END + ','   
                , @InsertColList = @InsertColList + QUOTENAME(SC.name) + ',' 
                ,  @UpdateCheck = @UpdateCheck +  
                                  CASE  
                                  WHEN CHARINDEX(',' + UPPER(SC.NAME) + ',',@DontAuditforColumns) = 0 THEN 'CASE WHEN UPDATE(' + QUOTENAME(SC.name) + ') THEN ''' + QUOTENAME(SC.name) + '-'' ELSE '''' END + ' +  CHAR(10)   
                                  ELSE '' 
                                  END 
              FROM SYS.COLUMNS SC   
              JOIN SYS.OBJECTS SO   
                ON SC.object_id = SO.object_id      
              JOIN SYS.schemas SCH   
                ON SCH.schema_id = SO.schema_id   
              JOIN SYS.types ST   
                ON ST.user_type_id = SC.user_type_id   
               AND ST.system_type_id = SC.system_type_id    
             WHERE SCH.Name = @Schemaname    
               AND SO.name  = @Tablename    
               AND UPPER(ST.name)  <> UPPER('timestamp')   
   
            SELECT @ColList = SUBSTRING(@ColList,1,LEN(@ColList)-1)   
            SELECT @UpdateCheck = SUBSTRING(@UpdateCheck,1,LEN(@UpdateCheck)-3)   
            SELECT @InsertColList = SUBSTRING(@InsertColList,1,LEN(@InsertColList)-1)   
  
            SELECT @InsertColList = @InsertColList + ',AuditDataState,AuditDMLAction,AuditUser,AuditDateTime,UpdateColumns'  
  
IF EXISTS (SELECT 1     
             FROM sys.objects     
            WHERE Name= @AuditTableName    
              AND Schema_id=Schema_id(@Schemaname)    
              AND Type = 'U')  AND @ForceDropAuditTable = 0  
 BEGIN  
  
            ----------------------------------------------------------------------------------------------------------------------    
            -- Get the comparision metadata for Main and Audit Tables  
            ----------------------------------------------------------------------------------------------------------------------    
  
            INSERT INTO @NewAddedCols  
            (ColumnName,DataType,CharLength,Collation,ChangeType,MainTableColumnName  
            ,MainTableDataType,MainTableCharLength,MainTableCollation,AuditTableColumnName,AuditTableDataType,AuditTableCharLength,AuditTableCollation)  
            SELECT ISNULL(MainTable.ColumnName,AuditTable.ColumnName)  
                  ,ISNULL(MainTable.DataType,AuditTable.DataType)  
                  ,ISNULL(MainTable.CharLength,AuditTable.CharLength)  
                  ,ISNULL(MainTable.Collation,AuditTable.Collation)  
                  ,CASE   
                   WHEN MainTable.ColumnName IS NULL THEN 'Deleted'  
                   WHEN AuditTable.ColumnName IS NULL THEN 'Added'  
                   ELSE NULL  
                   END   
                  ,MainTable.ColumnName  
                  ,MainTable.DataType  
                  ,MainTable.CharLength  
                  ,MainTable.Collation  
                  ,AuditTable.ColumnName  
                  ,AuditTable.DataType  
                  ,AuditTable.CharLength  
                  ,AuditTable.Collation  
              FROM  
      
            (  
            SELECT SC.Name As ColumnName,ST.Name as DataType,SC.is_identity as isIdentity,SC.Max_length as CharLength,SC.Collation_Name as Collation  
              FROM SYS.COLUMNS SC   
              JOIN SYS.OBJECTS SO   
                ON SC.object_id = SO.object_id      
              JOIN SYS.schemas SCH   
                ON SCH.schema_id = SO.schema_id   
              JOIN SYS.types ST   
                ON ST.user_type_id = SC.user_type_id   
               AND ST.system_type_id = SC.system_type_id    
             WHERE SCH.Name = @Schemaname    
               AND SO.name  = @Tablename    
               AND UPPER(ST.name)  <> UPPER('timestamp')   
            ) MainTable  
            FULL OUTER JOIN  
            (  
            SELECT SC.Name As ColumnName,ST.Name as DataType,SC.is_identity as isIdentity,SC.Max_length as CharLength,SC.Collation_Name as Collation  
              FROM SYS.COLUMNS SC   
              JOIN SYS.OBJECTS SO   
                ON SC.object_id = SO.object_id      
              JOIN SYS.schemas SCH   
                ON SCH.schema_id = SO.schema_id   
              JOIN SYS.types ST   
                ON ST.user_type_id = SC.user_type_id   
               AND ST.system_type_id = SC.system_type_id    
             WHERE SCH.Name = @Schemaname    
               AND SO.name  = @AuditTableName    
               AND UPPER(ST.name)  <> UPPER('timestamp')   
               AND SC.Name NOT IN ('AuditDataState','AuditDMLAction','AuditUser','AuditDateTime','UpdateColumns')  
            ) AuditTable  
            ON MainTable.ColumnName = AuditTable.ColumnName  
           
         ----------------------------------------------------------------------------------------------------------------------    
        -- Find data type changes between table  
        ----------------------------------------------------------------------------------------------------------------------    
  
            IF EXISTS ( SELECT * FROM @NewAddedCols NC  
                         WHERE NC.MainTableColumnName = NC.AuditTableColumnName  
                           AND (  
                               NC.MainTableDataType   <> NC.AuditTableDataType  
                               OR  
                               NC.MainTableCharLength  > NC.AuditTableCharLength  
                               OR  
                               NC.MainTableCollation  <> NC.AuditTableCollation  
                               )  
                )  
            BEGIN  
                SELECT CONVERT(VARCHAR(50),  
                           CASE  
                           WHEN NC.MainTableDataType   <> NC.AuditTableDataType   THEN 'DataType Mismatch'  
                           WHEN NC.MainTableCharLength  > NC.AuditTableCharLength THEN 'Length in maintable is greater than Audit Table'  
                           WHEN NC.MainTableCollation  <> NC.AuditTableCollation  THEN 'Collation Difference'  
                           END) AS Mismatch  
                      ,NC.MainTableColumnName  
                      ,NC.MainTableDataType  
                      ,NC.MainTableCharLength  
                      ,NC.MainTableCollation  
                      ,NC.AuditTableColumnName  
                      ,NC.AuditTableDataType  
                      ,NC.AuditTableCharLength  
                      ,NC.AuditTableCollation  
                  FROM @NewAddedCols NC  
                 WHERE NC.MainTableColumnName = NC.AuditTableColumnName  
                   AND (  
                       NC.MainTableDataType   <> NC.AuditTableDataType  
                       OR  
                       NC.MainTableCharLength  > NC.AuditTableCharLength  
                       OR  
                       NC.MainTableCollation  <> NC.AuditTableCollation  
                       )  
  
                RAISERROR('There are differences in Datatype or Lesser Length or Collation difference between the Main table and Audit Table. Please refer the output',16,1)  
                IF @IgnoreExistingColumnMismatch = 0  
                BEGIN  
                    RETURN  
                END  
            END  
  
        ----------------------------------------------------------------------------------------------------------------------    
        -- Find the new and deleted columns   
        ----------------------------------------------------------------------------------------------------------------------    
  
          IF EXISTS(SELECT * FROM @NewAddedCols WHERE ChangeType IS NOT NULL)  
          BEGIN  
  
                SELECT @SQL = @SQL +   'ALTER TABLE ' + @QuotedSchemaName + '.' + @QuotedAuditTableName   
                                   + CASE  
                                     WHEN NC.ChangeType ='Added' THEN ' ADD '  + QUOTENAME(NC.ColumnName) + ' ' + NC.DataType + ' '   
                                     +  CASE   
                                        WHEN NC.DataType IN ('char','varchar','nchar','nvarchar') AND NC.CharLength = -1 THEN '(max) COLLATE ' + NC.Collation + ' NULL '  
                                        WHEN NC.DataType IN ('char','varchar') THEN '(' + CONVERT(VARCHAR(5),NC.CharLength) + ') COLLATE ' + NC.Collation + ' NULL '  
                                        WHEN NC.DataType IN ('nchar','nvarchar') THEN '(' + CONVERT(VARCHAR(5),NC.CharLength/2) + ') COLLATE ' + NC.Collation + ' NULL '  
                                        ELSE ''  
                                        END  
                                     WHEN NC.ChangeType ='Deleted' THEN ' DROP COLUMN '  + QUOTENAME(NC.ColumnName)   
                                     END + CHAR(10)  
                                       
                  FROM @NewAddedCols NC  
                  WHERE NC.ChangeType IS NOT NULL  
          END  
  
  
 END  
 ELSE  
 BEGIN  
   
            SELECT @SQL = '  IF EXISTS (SELECT 1     
                                          FROM sys.objects     
                                         WHERE Name=''' + @AuditTableName + '''    
                                           AND Schema_id=Schema_id(''' + @Schemaname + ''')    
                                           AND Type = ''U'')    
                            DROP TABLE ' + @QuotedSchemaName + '.' + @QuotedAuditTableName + ' 
   
                    SELECT ' + @ColList + '    
                        ,AuditDataState=CONVERT(VARCHAR(10),'''')     
                        ,AuditDMLAction=CONVERT(VARCHAR(10),'''')      
                        ,AuditUser =CONVERT(SYSNAME,'''')    
                        ,AuditDateTime=CONVERT(DATETIME,''01-JAN-1900'')    
                        ,UpdateColumns = CONVERT(VARCHAR(MAX),'''')   
                        Into ' + @QuotedSchemaName + '.' + @QuotedAuditTableName + '    
                    FROM ' + @QuotedSchemaName + '.' + @QuotedTableName +'    
                    WHERE 1=2 '    
END  
 
 
  
IF @GenerateScriptOnly = 1    
BEGIN    
    PRINT REPLICATE ('-',200)    
    PRINT '--Create \ Alter Script Audit table for ' + @QuotedSchemaName + '.' + @QuotedTableName    
    PRINT REPLICATE ('-',200)    
    PRINT @SQL    
    IF LTRIM(RTRIM(@SQL)) <> ''  
    BEGIN  
        PRINT 'GO'   
    END  
    ELSE  
    BEGIN  
        PRINT '-- No changes in table structure'  
    END   
END    
ELSE    
BEGIN    
    IF RTRIM(LTRIM(@SQL)) = ''  
    BEGIN  
        PRINT 'No Table Changes Found'   
    END  
    ELSE  
    BEGIN  
        PRINT 'Creating \ Altered Audit table for ' + @QuotedSchemaName + '.' + @QuotedTableName    
        EXEC(@SQL)    
        PRINT 'Audit table ' + @QuotedSchemaName + '.' + @QuotedAuditTableName + ' Created \ Altered succesfully'    
    END  
END    
    
    
----------------------------------------------------------------------------------------------------------------------    
-- Create Insert Trigger    
----------------------------------------------------------------------------------------------------------------------    
    
    
SELECT @SQL = '    
IF EXISTS (SELECT 1     
             FROM sys.objects     
            WHERE Name=''' + @Tablename + '_Insert' + '''    
              AND Schema_id=Schema_id(''' + @Schemaname + ''')    
              AND Type = ''TR'')    
DROP TRIGGER ' + @QuotedSchemaName + '.' + @QuotedInsertTriggerName     
SELECT @SQLTrigger = '    
CREATE TRIGGER '  + @QuotedSchemaName + '.' + @QuotedInsertTriggerName + ' 
ON '  + @QuotedSchemaName + '.' + @QuotedTableName + '    
FOR INSERT    
AS    
'  
 
 
IF LTRIM(RTRIM(@DontAuditforUsersTmp)) <> ''  
BEGIN 
 
SELECT @SQLTrigger = @SQLTrigger + CHAR(10) + ' IF SUSER_NAME() NOT IN (''' + @DontAuditforUsersTmp + ''')' 
SELECT @SQLTrigger = @SQLTrigger + CHAR(10) + ' BEGIN' 
 
END 
 
 
 
  
SELECT @SQLTrigger =  @SQLTrigger  + CHAR(10) + ' INSERT INTO ' + @QuotedSchemaName + '.' + @QuotedAuditTableName + CHAR(10) +   
 '(' + @InsertColList + ')' + CHAR(10) +   
 'SELECT ' + @ColList + ',''New'',''Insert'',SUSER_SNAME(),getdate(),''''  FROM INSERTED '    
 
 IF LTRIM(RTRIM(@DontAuditforUsersTmp)) <> ''  
BEGIN 
 
    SELECT @SQLTrigger = @SQLTrigger + CHAR(10) + ' END' 
 
END 
 
    
IF @GenerateScriptOnly = 1    
BEGIN    
    PRINT REPLICATE ('-',200)    
    PRINT '--Create Script Insert Trigger for ' +  @QuotedSchemaName + '.' + @QuotedTablename    
    PRINT REPLICATE ('-',200)    
    PRINT @SQL    
    PRINT 'GO'    
    PRINT @SQLTrigger    
    PRINT 'GO'    
END    
ELSE    
BEGIN    
    PRINT 'Creating Insert Trigger ' + @QuotedInsertTriggerName + '  for ' + @QuotedSchemaName + '.' + @QuotedTablename    
    EXEC(@SQL)    
    EXEC(@SQLTrigger)    
    PRINT 'Trigger ' + @QuotedSchemaName + '.' + @QuotedInsertTriggerName  + ' Created succesfully'    
END    
    
    
----------------------------------------------------------------------------------------------------------------------    
-- Create Delete Trigger    
----------------------------------------------------------------------------------------------------------------------    
    
    
SELECT @SQL = '    
    
IF EXISTS (SELECT 1     
             FROM sys.objects     
            WHERE Name=''' + @Tablename + '_Delete' + '''    
              AND Schema_id=Schema_id(''' + @Schemaname + ''')    
              AND Type = ''TR'')    
DROP TRIGGER ' + @QuotedSchemaName + '.' + + @QuotedDeleteTriggerName + '    
'    
    
SELECT @SQLTrigger =     
'    
CREATE TRIGGER '  + @QuotedSchemaName + '.'  + @QuotedDeleteTriggerName + '    
ON '+ @QuotedSchemaName + '.' + @QuotedTableName + '    
FOR DELETE    
AS   ' 
 
IF LTRIM(RTRIM(@DontAuditforUsersTmp)) <> ''  
BEGIN 
 
SELECT @SQLTrigger = @SQLTrigger + CHAR(10) + ' IF SUSER_NAME() NOT IN (''' + @DontAuditforUsersTmp + ''')' 
SELECT @SQLTrigger = @SQLTrigger + CHAR(10) + ' BEGIN' 
 
END 
 
 
  
SELECT @SQLTrigger = @SQLTrigger + CHAR(10) + '  INSERT INTO ' + @QuotedSchemaName + '.' + @QuotedAuditTableName + CHAR(10) +   
 '(' + @InsertColList + ')' + CHAR(10) +   
 'SELECT ' + @ColList + ',''Old'',''Delete'',SUSER_SNAME(),getdate(),''''  FROM DELETED'    
 
IF LTRIM(RTRIM(@DontAuditforUsersTmp)) <> ''  
BEGIN 
 
    SELECT @SQLTrigger = @SQLTrigger + CHAR(10) + ' END' 
 
END 
 
    
IF @GenerateScriptOnly = 1    
BEGIN    
    PRINT REPLICATE ('-',200)    
    PRINT '--Create Script Delete Trigger for ' + @QuotedSchemaName + '.' + @QuotedTableName    
    PRINT REPLICATE ('-',200)    
    PRINT @SQL    
    PRINT 'GO'    
    PRINT @SQLTrigger    
    PRINT 'GO'    
END    
ELSE    
BEGIN    
    PRINT 'Creating Delete Trigger ' + @QuotedDeleteTriggerName + '  for ' + @QuotedSchemaName + '.' + @QuotedTableName    
    EXEC(@SQL)    
    EXEC(@SQLTrigger)    
    PRINT 'Trigger ' + @QuotedSchemaName + '.' + @QuotedDeleteTriggerName + ' Created succesfully'    
END    
    
----------------------------------------------------------------------------------------------------------------------    
-- Create Update Trigger    
----------------------------------------------------------------------------------------------------------------------    
    
    
SELECT @SQL = '    
    
IF EXISTS (SELECT 1     
             FROM sys.objects     
            WHERE Name=''' + @Tablename + '_Update' + '''    
              AND Schema_id=Schema_id(''' + @Schemaname + ''')    
              AND Type = ''TR'')    
DROP TRIGGER ' + @QuotedSchemaName + '.' + @QuotedUpdateTriggerName + '    
'    
    
SELECT @SQLTrigger =    
'    
CREATE TRIGGER ' + @QuotedSchemaName + '.'  + @QuotedUpdateTriggerName + '      
ON '+ @QuotedSchemaName + '.' + @QuotedTableName + '    
FOR UPDATE    
AS ' 
 
IF LTRIM(RTRIM(@DontAuditforUsersTmp)) <> ''  
BEGIN 
 
SELECT @SQLTrigger = @SQLTrigger + CHAR(10) + ' IF SUSER_NAME() NOT IN (''' + @DontAuditforUsersTmp + ''')' 
SELECT @SQLTrigger = @SQLTrigger + CHAR(10) + ' BEGIN' 
 
END 
 
 
  
SELECT @SQLTrigger = @SQLTrigger + CHAR(10) + '   
 
    DECLARE @UpdatedCols varchar(max) 
 
   SELECT @UpdatedCols = ' + @UpdateCheck + ' 
    
   IF LTRIM(RTRIM(@UpdatedCols)) <> '''' 
   BEGIN 
          INSERT INTO ' + @QuotedSchemaName + '.' + @QuotedAuditTableName + CHAR(10) +   
         '(' + @InsertColList + ')' + CHAR(10) +   
         'SELECT ' + @ColList +',''New'',''Update'',SUSER_SNAME(),getdate(),@UpdatedCols  FROM INSERTED     
    
          INSERT INTO ' + @QuotedSchemaName + '.' + @QuotedAuditTableName + CHAR(10) +   
         '(' + @InsertColList + ')' + CHAR(10) +   
         'SELECT ' + @ColList +',''Old'',''Update'',SUSER_SNAME(),getdate(),@UpdatedCols  FROM DELETED  
   END'    
 
IF LTRIM(RTRIM(@DontAuditforUsersTmp)) <> ''  
BEGIN 
 
    SELECT @SQLTrigger = @SQLTrigger + CHAR(10) + ' END' 
 
END 
 
 
    
IF @GenerateScriptOnly = 1    
BEGIN    
    PRINT REPLICATE ('-',200)    
    PRINT '--Create Script Update Trigger for ' + @QuotedSchemaName + '.' + @QuotedTableName    
    PRINT REPLICATE ('-',200)    
    PRINT @SQL    
    PRINT 'GO'    
    PRINT @SQLTrigger    
    PRINT 'GO'    
END    
ELSE    
BEGIN    
    PRINT 'Creating Delete Trigger ' + @QuotedUpdateTriggerName + '  for ' + @QuotedSchemaName + '.' + @QuotedTableName    
    EXEC(@SQL)    
    EXEC(@SQLTrigger)    
    PRINT 'Trigger ' + @QuotedSchemaName + '.' + @QuotedUpdateTriggerName + '  Created succesfully'    
END    
    
SET NOCOUNT OFF
GO
/****** Object:  Table [dbo].[m_gross_income_year]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_gross_income_year](
	[gross_income_year_id] [int] IDENTITY(1,1) NOT NULL,
	[description] [varchar](160) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NULL,
	[modified_date] [datetime] NULL,
	[is_delete] [tinyint] NOT NULL,
 CONSTRAINT [PK_m_gross_income_year] PRIMARY KEY CLUSTERED 
(
	[gross_income_year_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_gross_income]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_gross_income](
	[gross_income_id] [int] IDENTITY(1,1) NOT NULL,
	[description] [varchar](160) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NULL,
	[modified_date] [datetime] NULL,
	[is_delete] [tinyint] NOT NULL,
 CONSTRAINT [PK_m_gross_income] PRIMARY KEY CLUSTERED 
(
	[gross_income_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_user_group_menu_mapping]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_user_group_menu_mapping](
	[id] [bigint] IDENTITY(1,1) NOT NULL,
	[user_id] [varchar](3) NOT NULL,
	[group_menu_id] [int] NOT NULL,
	[created_by] [varchar](3) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](3) NULL,
	[modified_date] [datetime] NULL,
	[is_delete] [tinyint] NOT NULL,
 CONSTRAINT [PK_m_user_group_menu_mapping] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_m_user_group_menu_mapping] ON [dbo].[m_user_group_menu_mapping] 
(
	[user_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_m_user_group_menu_mapping_1] ON [dbo].[m_user_group_menu_mapping] 
(
	[group_menu_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_m_user_group_menu_mapping_2] ON [dbo].[m_user_group_menu_mapping] 
(
	[user_id] ASC,
	[group_menu_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[m_reinstatement_type]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_reinstatement_type](
	[m_reinstatement_type_id] [int] IDENTITY(1,1) NOT NULL,
	[description] [varchar](160) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NULL,
	[modified_date] [datetime] NULL,
	[is_delete] [tinyint] NOT NULL,
 CONSTRAINT [PK_m_reinstatement_type] PRIMARY KEY CLUSTERED 
(
	[m_reinstatement_type_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_transaction_form_template]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_transaction_form_template](
	[transaction_form_template_id] [smallint] IDENTITY(1,1) NOT NULL,
	[description] [varchar](150) NOT NULL,
	[filename] [varchar](200) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK_m_transaction_form_template] PRIMARY KEY CLUSTERED 
(
	[transaction_form_template_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_transaction_category]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_transaction_category](
	[transaction_category_id] [int] IDENTITY(1,1) NOT NULL,
	[transaction_category_code] [char](6) NOT NULL,
	[description] [varchar](150) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[transaction_category_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_status]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_status](
	[status_id] [int] IDENTITY(1,1) NOT NULL,
	[status_code] [char](6) NOT NULL,
	[description] [varchar](50) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[status_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_province]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_province](
	[province_id] [int] IDENTITY(1,1) NOT NULL,
	[province_code] [char](6) NULL,
	[province_name] [varchar](30) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_provin__08DCB60F5ECA0095] PRIMARY KEY CLUSTERED 
(
	[province_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [IX_m_province_code_unq] UNIQUE NONCLUSTERED 
(
	[province_code] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_m_province] ON [dbo].[m_province] 
(
	[province_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_m_province_name_idx] ON [dbo].[m_province] 
(
	[province_name] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[m_group_pic]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_group_pic](
	[group_pic_id] [int] IDENTITY(1,1) NOT NULL,
	[group_pic_code] [varchar](12) NOT NULL,
	[description] [varchar](225) NOT NULL,
	[remarks] [varchar](225) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_group___0A8798C76A06A917] PRIMARY KEY CLUSTERED 
(
	[group_pic_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_menu_category]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_menu_category](
	[menu_category_id] [int] IDENTITY(1,1) NOT NULL,
	[description] [varchar](160) NULL,
	[created_by] [varchar](3) NULL,
	[created_date] [datetime] NULL,
	[modified_by] [varchar](3) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [tinyint] NULL,
 CONSTRAINT [PK_m_menu_category] PRIMARY KEY CLUSTERED 
(
	[menu_category_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  StoredProcedure [dbo].[CheckSession]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[CheckSession] (
	@usercode VARCHAR(3),
	@sessid VARCHAR(50),
	@sig VARCHAR(100),
	@ipaddress VARCHAR(30),
	@useragent VARCHAR(300),
	@expirenum INT
)
AS
BEGIN

	SET NOCOUNT ON;

	--DECLARE @userid INT;
	DECLARE @sessexist INT;



			SET @sessexist = (
				SELECT COUNT(session_id) 
				FROM s_user_session
				WHERE username = @usercode
				AND session_id = @sessid
				AND session_expire > GETDATE()
			);

			IF @sessexist > 0
	
				UPDATE s_user_session
				SET last_activity = GETDATE(),
				session_expire = DATEADD(HOUR, @expirenum, GETDATE())
				WHERE session_id = @sessid
				AND username = @usercode

			ELSE

				INSERT INTO s_user_session
				(
					session_id,
					signature,
					username,
					first_login,
					last_activity,
					session_expire,
					ip_address,
					user_agent
				)
				VALUES (
					@sessid,
					@sig,
					@usercode,
					GETDATE(),
					GETDATE(),
					DATEADD(HOUR, @expirenum, GETDATE()),
					@ipaddress,
					@useragent
				);

END
GO
/****** Object:  Table [dbo].[m_component_type]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_component_type](
	[component_type_id] [bigint] IDENTITY(1,1) NOT NULL,
	[component_type_code] [varchar](8) NOT NULL,
	[description] [varchar](50) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[component_type_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[t_transaction_form]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[t_transaction_form](
	[transaction_form_id] [bigint] IDENTITY(1,1) NOT NULL,
	[transaction_id] [bigint] NOT NULL,
	[register_number] [varchar](9) NOT NULL,
	[transaction_sub_type] [int] NULL,
	[currency] [varchar](10) NULL,
	[money_loans] [varchar](50) NULL,
	[term] [varchar](50) NULL,
	[installment_date] [date] NULL,
	[identity_date] [datetime] NULL,
	[amount] [varchar](30) NULL,
	[submission_date] [datetime] NULL,
	[source_of_fund_id] [int] NULL,
	[gross_income_id] [int] NULL,
	[gross_income_year_id] [int] NULL,
	[is_settlement] [tinyint] NULL,
	[settlement_name] [varchar](50) NULL,
	[policy_no] [varchar](50) NULL,
	[is_transfer_bank] [tinyint] NULL,
	[in_currency] [varchar](50) NULL,
	[beneficiary_name] [varchar](100) NULL,
	[bank_name] [varchar](100) NULL,
	[branch] [varchar](100) NULL,
	[account_no] [varchar](50) NULL,
	[transfer_amount] [varchar](50) NULL,
	[amount_of_drawn] [varchar](50) NULL,
	[amount_of_benefits] [varchar](50) NULL,
	[remarks] [varchar](255) NULL,
	[reinstatement_type] [int] NULL,
	[m_alteration_type_id] [int] NULL,
	[event_date] [date] NULL,
	[settlement_policy_no] [varchar](160) NULL,
	[is_cancel_from_inception] [tinyint] NULL,
	[is_surrender] [tinyint] NULL,
	[is_benefit_type] [tinyint] NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [tinyint] NOT NULL,
 CONSTRAINT [PK__t_transa__F57D76D86E2C3FB6] PRIMARY KEY CLUSTERED 
(
	[transaction_form_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_t_transaction_form_trans_id] ON [dbo].[t_transaction_form] 
(
	[transaction_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_t_transaction_form_transaction_id] ON [dbo].[t_transaction_form] 
(
	[transaction_id] ASC,
	[is_delete] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[s_error_code]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[s_error_code](
	[error_code] [smallint] NOT NULL,
	[error_description] [varchar](100) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_s_error_code] ON [dbo].[s_error_code] 
(
	[error_code] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_s_error_code_desc] ON [dbo].[s_error_code] 
(
	[error_code] ASC,
	[error_description] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[s_action_type]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[s_action_type](
	[id_action_type] [tinyint] NOT NULL,
	[description] [varchar](40) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK_s_action_type] PRIMARY KEY CLUSTERED 
(
	[id_action_type] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_s_action_type_desc] ON [dbo].[s_action_type] 
(
	[id_action_type] ASC,
	[description] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[s_action_list]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[s_action_list](
	[action_id] [int] IDENTITY(1,1) NOT NULL,
	[action_name] [varchar](60) NULL,
	[action_type] [int] NOT NULL,
	[condition_field] [varchar](max) NULL,
	[result_object] [varchar](100) NULL,
	[result_field] [varchar](max) NULL,
	[default_order] [varchar](30) NULL,
	[max_result_row] [smallint] NULL,
	[datasource] [varchar](30) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK_s_action_list] PRIMARY KEY CLUSTERED 
(
	[action_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [IX_s_action_list_act_name] UNIQUE NONCLUSTERED 
(
	[action_name] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_s_action_list_act_type] ON [dbo].[s_action_list] 
(
	[action_type] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_s_action_list_id_name_type] ON [dbo].[s_action_list] 
(
	[action_id] ASC,
	[action_name] ASC,
	[action_type] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  StoredProcedure [dbo].[test_sp]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[test_sp]
	@pi_input1 VARCHAR(10),
	@pi_input2 VARCHAR(10),
	@po_output VARCHAR(100) OUTPUT
AS
BEGIN
	SET NOCOUNT ON;

  DECLARE @hasil VARCHAR(100)
  SET @hasil = COALESCE(@pi_input1,'') + ' ' + COALESCE(@pi_input2,'') + ' ' + CAST(GETDATE() AS VARCHAR(24))
  
  INSERT INTO dbo.s_log
           ([date]
           ,[request_parameter]
           ,[response])
     VALUES
           (GETDATE()
           ,'test SP'
           ,@hasil)

  SET @po_output = @hasil
  
END
GO
/****** Object:  Table [dbo].[m_source_app]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_source_app](
	[source_app_id] [int] IDENTITY(1,1) NOT NULL,
	[source_app_code] [char](6) NOT NULL,
	[source_app_name] [varchar](50) NOT NULL,
	[source_main_url] [varchar](100) NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[source_app_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[s_m_log_type]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[s_m_log_type](
	[log_type_id] [tinyint] NOT NULL,
	[icon] [varchar](20) NULL,
	[description] [varchar](20) NULL,
 CONSTRAINT [PK_s_m_log_type] PRIMARY KEY CLUSTERED 
(
	[log_type_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_workflow_template]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_workflow_template](
	[workflow_template_id] [int] IDENTITY(1,1) NOT NULL,
	[workflow_template_code] [varchar](8) NOT NULL,
	[description] [varchar](225) NOT NULL,
	[remarks] [varchar](225) NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[workflow_template_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[t_transaction_history_workflow]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[t_transaction_history_workflow](
	[transaction_history_workflow_id] [bigint] IDENTITY(1,1) NOT NULL,
	[transaction_id] [bigint] NOT NULL,
	[workflow_template_stage_id] [bigint] NULL,
	[last_user] [varchar](8) NOT NULL,
	[last_submit_date] [datetime] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [tinyint] NULL,
 CONSTRAINT [PK__t_transa__E535BB3F6418C597] PRIMARY KEY CLUSTERED 
(
	[transaction_history_workflow_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_document_type]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_document_type](
	[document_type_id] [int] IDENTITY(1,1) NOT NULL,
	[document_type_code] [varchar](50) NOT NULL,
	[description] [varchar](255) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [varchar](1) NOT NULL,
	[is_delete] [char](1) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[document_type_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_data_type]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_data_type](
	[data_type_id] [int] IDENTITY(1,1) NOT NULL,
	[data_type_code] [varchar](50) NOT NULL,
	[description] [varchar](255) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [varchar](1) NOT NULL,
	[is_delete] [char](1) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[data_type_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_currency]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_currency](
	[currency_id] [int] NOT NULL,
	[currency_code] [varchar](50) NOT NULL,
	[description] [varchar](255) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_curren__C7F543D34B8221F7] PRIMARY KEY CLUSTERED 
(
	[currency_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_m_currency] ON [dbo].[m_currency] 
(
	[currency_code] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[m_city]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_city](
	[city_id] [int] IDENTITY(1,1) NOT NULL,
	[city_code] [char](6) NULL,
	[province_id] [int] NOT NULL,
	[city_name] [varchar](160) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_city__031491A8629A9179] PRIMARY KEY CLUSTERED 
(
	[city_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_m_city_name] ON [dbo].[m_city] 
(
	[city_name] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_m_city_prov_id] ON [dbo].[m_city] 
(
	[province_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[m_approv_matrix]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_approv_matrix](
	[approv_matrix_id] [int] IDENTITY(1,1) NOT NULL,
	[approv_matrix_code] [char](8) NULL,
	[description] [varchar](225) NOT NULL,
	[remarks] [varchar](225) NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_approv__9B24A9281308BEAA] PRIMARY KEY CLUSTERED 
(
	[approv_matrix_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_menu]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_menu](
	[menu_id] [int] IDENTITY(1,1) NOT NULL,
	[menu_category_id] [int] NOT NULL,
	[url] [varchar](400) NULL,
	[description] [varchar](255) NOT NULL,
	[level] [int] NOT NULL,
	[menu_id_parent] [int] NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NULL,
	[modified_date] [datetime] NULL,
	[icon_class] [varchar](50) NULL,
	[is_delete] [char](1) NULL,
 CONSTRAINT [PK__m_menu__4CA0FADC550B8C31] PRIMARY KEY CLUSTERED 
(
	[menu_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_level_type]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_level_type](
	[level_type_id] [int] IDENTITY(1,1) NOT NULL,
	[level_type_code] [varchar](8) NOT NULL,
	[description] [varchar](60) NOT NULL,
	[head_description] [varchar](60) NOT NULL,
	[seq_number] [numeric](18, 0) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[level_type_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_m_level_type] ON [dbo].[m_level_type] 
(
	[level_type_code] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_m_level_type_1] ON [dbo].[m_level_type] 
(
	[seq_number] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[m_group_pic_detail]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_group_pic_detail](
	[group_pic_detail_id] [bigint] IDENTITY(1,1) NOT NULL,
	[group_pic_id] [int] NOT NULL,
	[source_app_id] [int] NULL,
	[branch_id] [varchar](5) NOT NULL,
	[transaction_type_id] [varchar](5) NOT NULL,
	[user_id_group_pic_detail] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_group___5A68E5336DD739FB] PRIMARY KEY CLUSTERED 
(
	[group_pic_detail_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_group_menu]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_group_menu](
	[group_menu_id] [int] IDENTITY(1,1) NOT NULL,
	[group_menu_code] [varchar](6) NULL,
	[description] [varchar](255) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NULL,
	[modified_date] [datetime] NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_group___771EF6A619EAC663] PRIMARY KEY CLUSTERED 
(
	[group_menu_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [IX_m_group_menu_menu_code] UNIQUE NONCLUSTERED 
(
	[group_menu_code] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_m_group_menu_desc] ON [dbo].[m_group_menu] 
(
	[description] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[t_transaction_document_file_history]    Script Date: 11/10/2017 15:12:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[t_transaction_document_file_history](
	[transaction_document_file_history_id] [bigint] IDENTITY(1,1) NOT NULL,
	[transaction_document_file_id] [bigint] NOT NULL,
	[workflow_template_stage_id] [bigint] NOT NULL,
	[process_date] [datetime] NOT NULL,
	[user_id] [int] NOT NULL,
	[token] [varchar](30) NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[transaction_document_file_history_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  StoredProcedure [dbo].[proc_takeover_transaction]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		KHU
-- Create date: 2017-10-24
-- Description:	
-- =============================================
CREATE PROCEDURE [dbo].[proc_takeover_transaction] 
	
	@pi_transaction_task_id BIGINT,
	@pi_username VARCHAR(8),
	@po_message VARCHAR(400) OUTPUT

AS
BEGIN

	SET NOCOUNT ON;
	
	DECLARE @v_result BIT = 0

	BEGIN TRY
	BEGIN TRANSACTION proses_takeover

		DECLARE
			@v_transaction_task_id BIGINT,
			@v_transaction_id BIGINT,
			@v_process_sequence INT,
			@v_user_id VARCHAR(8),
			@status_id INT
	
		SELECT
			@v_transaction_task_id = transaction_task_id,
			@v_transaction_id = transaction_id,
			@v_process_sequence = process_sequence, 
			@v_user_id = user_id, 
			@status_id = status_id
		FROM
			dbo.v_t_transaction_task
		WHERE
			transaction_task_id = @pi_transaction_task_id

		IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			BEGIN
				SET @po_message = 'Transaction not found'
				GOTO ERROR_OUT
			END

		BEGIN


			UPDATE
					t_transaction_task
				SET
					status_id = 3,    -- 3 : Not Available
					modified_by = @pi_username,
					modified_date = GETDATE()
				WHERE
					transaction_task_id = @pi_transaction_task_id AND
					is_delete = 0
			
				IF (@@ERROR <> 0)
					BEGIN
						SET @po_message = 'Failed Update Task Status Not Available'
						GOTO ERROR_OUT
					END

			INSERT INTO dbo.t_transaction_history_process
					(transaction_id,
					process_date,
					[user_id],
					workflow_template_action_id,
					action_description,
					reason_id,
					remarks,
					created_by,
					created_date,
					modified_by,
					modified_date,
					is_delete)

			SELECT
					@v_transaction_id, -- transaction_id - bigint
					GETDATE(), -- process_date - datetime
					@pi_username, -- user_id - int
					NULL, --@pi_workflow_template_action_id, -- workflow_template_action_id - bigint
					'Take Over Task from '+user_fullname,   -- action_description gunakan nama lengkap
					NULL, -- reason_id - int
					NULL, -- remarks - varchar(255)
					@pi_username, -- created_by - varchar(8)
					GETDATE(), -- created_date - datetime
					@pi_username, -- modified_by - varchar(8)
					GETDATE(), -- modified_date - datetime
					'0'  -- is_delete - char(1)
				FROM
					dbo.m_user
				WHERE
					username = @pi_username

				IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
					BEGIN
						SET @po_message = 'Failed insert transaction history process'
						GOTO ERROR_OUT
					END


			 INSERT INTO dbo.t_transaction_task
                           ([transaction_id]
                           ,[process_sequence]
                           ,[user_id]
                           ,[status_id]
                           ,[created_by]
                           ,[created_date]
                           ,[modified_by]
                           ,[modified_date]
                           ,[is_delete])
                     VALUES
                           (@v_transaction_id -- transaction_id - bigint
                           ,@v_process_sequence -- process_sequence - int
                           ,@pi_username
                           ,2    -- 2 : Available  -- status_id - int  
                           ,@pi_username -- created_by - varchar(8)
                           ,GETDATE() -- created_date - datetime
                           ,@pi_username -- modified_by - varchar(8)
                           ,GETDATE() -- modified_date - datetime
                           ,'0')
		            IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			            BEGIN
				            SET @po_message = 'Failed insert transaction task'
				            GOTO ERROR_OUT
			            END

		END


	COMMIT TRANSACTION proses_takeover
	SET @po_message = 'Sukses'  -- variable output 
		SET @v_result = 1
		SELECT @v_result AS [status], @po_message AS [message]  -- dual output stored procedure, variable output dan table output
		GOTO CLEAN_OUT

	END TRY
	BEGIN CATCH	-- tangkap error message selain yg sudah dideskripsikan
		SET @po_message = LEFT(ERROR_MESSAGE(),400)	-- berhubung variable diatas diset 400, maka potong error message
		GOTO ERROR_OUT
	END CATCH

	ERROR_OUT: 
		ROLLBACK TRANSACTION proses_takeover
		SELECT @v_result AS [status], @po_message AS [message]  -- dual output stored procedure, variable output dan table output
		--RETURN 1

	CLEAN_OUT:
		--RETURN 0

END
GO
/****** Object:  StoredProcedure [dbo].[proc_delegate_transaction]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		KHU
-- Create date: 2017-10-24
-- Description:	
-- =============================================
CREATE PROCEDURE [dbo].[proc_delegate_transaction] 
	
	@pi_transaction_task_id BIGINT,
	--@pi_transaction_id BIGINT,
	@pi_username VARCHAR(8),
	@pi_user_id VARCHAR(8),
	@pi_remarks VARCHAR(255),

	@po_message VARCHAR(400) OUTPUT

AS
BEGIN

	SET NOCOUNT ON;
	
	DECLARE @v_result BIT = 0

	BEGIN TRY
	BEGIN TRANSACTION proses_delegate

		DECLARE
			@v_transaction_task_id BIGINT,
			@v_transaction_id BIGINT,
			@v_process_sequence INT,
			@v_user_id VARCHAR(8),
			@status_id INT
	
		SELECT
			@v_transaction_task_id = transaction_task_id,
			@v_transaction_id = transaction_id,
			@v_process_sequence = process_sequence, 
			@v_user_id = user_id, 
			@status_id = status_id
		FROM
			dbo.v_t_transaction_task
		WHERE
			transaction_task_id = @pi_transaction_task_id

		IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			BEGIN
				SET @po_message = 'Transaction not found'
				GOTO ERROR_OUT
			END

		BEGIN


			UPDATE
					t_transaction_task
				SET
					status_id = 3,    -- 3 : Not Available
					modified_by = @pi_username,
					modified_date = GETDATE()
				WHERE
					transaction_task_id = @pi_transaction_task_id AND
					is_delete = 0
			
				IF (@@ERROR <> 0)
					BEGIN
						SET @po_message = 'Failed Update Task Status Not Available'
						GOTO ERROR_OUT
					END

			INSERT INTO dbo.t_transaction_history_process
					(transaction_id,
					process_date,
					[user_id],
					workflow_template_action_id,
					action_description,
					reason_id,
					remarks,
					created_by,
					created_date,
					modified_by,
					modified_date,
					is_delete)

			SELECT
					@v_transaction_id, -- transaction_id - bigint
					GETDATE(), -- process_date - datetime
					@pi_username, -- user_id - int
					NULL, --@pi_workflow_template_action_id, -- workflow_template_action_id - bigint
					'Delegate task to '+user_fullname,   -- action_description gunakan nama lengkap
					NULL, -- reason_id - int
					@pi_remarks, --@pi_remarks, -- remarks - varchar(255)
					@pi_username, -- created_by - varchar(8)
					GETDATE(), -- created_date - datetime
					@pi_username, -- modified_by - varchar(8)
					GETDATE(), -- modified_date - datetime
					'0'  -- is_delete - char(1)
				FROM
					dbo.m_user
				WHERE
					username = @pi_user_id

				IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
					BEGIN
						SET @po_message = 'Failed insert transaction history process'
						GOTO ERROR_OUT
					END


			 INSERT INTO dbo.t_transaction_task
                           ([transaction_id]
                           ,[process_sequence]
                           ,[user_id]
                           ,[status_id]
                           ,[created_by]
                           ,[created_date]
                           ,[modified_by]
                           ,[modified_date]
                           ,[is_delete])
                     VALUES
                           (@v_transaction_id -- transaction_id - bigint
                           ,@v_process_sequence -- process_sequence - int
                           ,@pi_user_id
                           ,2    -- 2 : Available  -- status_id - int  
                           ,@pi_username -- created_by - varchar(8)
                           ,GETDATE() -- created_date - datetime
                           ,@pi_username -- modified_by - varchar(8)
                           ,GETDATE() -- modified_date - datetime
                           ,'0')
		            IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			            BEGIN
				            SET @po_message = 'Failed insert transaction task'
				            GOTO ERROR_OUT
			            END

		END


	COMMIT TRANSACTION proses_delegate
	SET @po_message = 'Sukses'  -- variable output 
		SET @v_result = 1
		SELECT @v_result AS [status], @po_message AS [message]  -- dual output stored procedure, variable output dan table output
		GOTO CLEAN_OUT

	END TRY
	BEGIN CATCH	-- tangkap error message selain yg sudah dideskripsikan
		SET @po_message = LEFT(ERROR_MESSAGE(),400)	-- berhubung variable diatas diset 400, maka potong error message
		GOTO ERROR_OUT
	END CATCH

	ERROR_OUT: 
		ROLLBACK TRANSACTION proses_delegate
		SELECT @v_result AS [status], @po_message AS [message]  -- dual output stored procedure, variable output dan table output
		--RETURN 1

	CLEAN_OUT:
		--RETURN 0

END
GO
/****** Object:  Table [dbo].[m_reason_category]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_reason_category](
	[reason_category_id] [int] IDENTITY(1,1) NOT NULL,
	[reason_category_code] [varchar](10) NULL,
	[description] [varchar](100) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_reason__681B69C15F891AA4] PRIMARY KEY CLUSTERED 
(
	[reason_category_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_reason]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_reason](
	[reason_id] [int] IDENTITY(1,1) NOT NULL,
	[reason_category_id] [int] NOT NULL,
	[description] [varchar](225) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_reason__846BB554644DCFC1] PRIMARY KEY CLUSTERED 
(
	[reason_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_group_menu_mapping]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_group_menu_mapping](
	[group_menu_mapping_id] [int] IDENTITY(1,1) NOT NULL,
	[group_menu_id] [int] NOT NULL,
	[menu_id] [int] NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_group___7A5E481A58DC1D15] PRIMARY KEY CLUSTERED 
(
	[group_menu_mapping_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_m_group_menu_mapping] ON [dbo].[m_group_menu_mapping] 
(
	[group_menu_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_m_group_menu_mapping_1] ON [dbo].[m_group_menu_mapping] 
(
	[menu_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_m_group_menu_mapping_2] ON [dbo].[m_group_menu_mapping] 
(
	[status_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  UserDefinedFunction [dbo].[func_list_transaction_type]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- by Pribadi Pamungkas. 2017-08-07
CREATE FUNCTION [dbo].[func_list_transaction_type]
(	
	@pi_transaction_type_id VARCHAR(5)
)
RETURNS TABLE 
AS
RETURN 
(
  SELECT
  	*
  FROM
  	dbo.m_transaction_type
  WHERE
    --transaction_type_id = CAST((CASE @pi_transaction_type_id WHEN '*' THEN '0' ELSE @pi_transaction_type_id END) AS INT)
    transaction_type_id = CAST(REPLACE(@pi_transaction_type_id,'*','0') AS INT)
    OR @pi_transaction_type_id = '*'
)
GO
/****** Object:  UserDefinedFunction [dbo].[get_group_menu_to_add]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[get_group_menu_to_add]
(	
	@pi_user_id VARCHAR(3)
)
RETURNS TABLE 
AS
RETURN 
(
	SELECT DISTINCT
		g.group_menu_id, 
		g.group_menu_code, 
		g.description,
		g.created_by,
		g.created_date,
		g.modified_by,
		g.modified_date,
		g.is_delete
	FROM m_group_menu g
	WHERE g.is_delete = '0'
	AND g.group_menu_id NOT IN (
		SELECT group_menu_id
		FROM m_user_group_menu_mapping
		WHERE user_id = @pi_user_id
		AND is_delete = 0
	)
)
GO
/****** Object:  View [dbo].[v_t_receipt_form]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- by Pribadi Pamungkas. 2017-10-10
-- last modified by Pribadi Pamungkas. 2017-11-01. Tambah alamat, kota, dan propinsi cabang
CREATE VIEW [dbo].[v_t_receipt_form]
AS
    SELECT
      tt.transaction_id,
      mb.description AS elise,
      mb.address AS elise_address,
      ct.city_name,
      pv.province_name,
      CONVERT(varchar, tt.transaction_date, 103) +' '+ CONVERT(varchar(15),CAST(tt.transaction_date AS TIME),108) AS tanggal,
      tt.register_number AS no_registrasi,
      mt.description AS jenis_transaksi,
      tt.policy_id_reference,
      tt.policy_holder_reference,
      mt.footnote_receipt_form
    FROM
	    dbo.t_transaction AS tt INNER JOIN
	    dbo.m_branch AS mb ON tt.branch_id = mb.branch_id INNER JOIN
	    dbo.m_transaction_type AS mt ON tt.transaction_type_id = mt.transaction_type_id LEFT JOIN
	    dbo.m_city AS ct ON mb.city_id = ct.city_id LEFT JOIN
	    dbo.m_province AS pv ON mb.province_id = pv.province_id
    WHERE
      tt.is_delete = 0
      AND mb.is_delete = '0'
      AND mt.is_delete = '0'
GO
/****** Object:  Table [dbo].[m_approv_matrix_detail]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_approv_matrix_detail](
	[approv_matrix_detail_id] [bigint] IDENTITY(1,1) NOT NULL,
	[approv_matrix_id] [int] NOT NULL,
	[maximum_value] [decimal](12, 2) NOT NULL,
	[level_type_id] [int] NOT NULL,
	[currency_id] [int] NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_approv__193C3FDE17CD73C7] PRIMARY KEY CLUSTERED 
(
	[approv_matrix_detail_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  View [dbo].[v_user_group_menu]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_user_group_menu] AS
SELECT m.id
      ,m.user_id
      ,m.group_menu_id
      ,m.created_by
      ,m.created_date
      ,m.modified_by
      ,m.modified_date
      ,m.is_delete
	  ,g.description
  FROM m_user_group_menu_mapping m
  JOIN m_group_menu g ON g.group_menu_id = m.group_menu_id
  WHERE g.status_id = 5
  AND g.is_delete = '0'
  AND m.is_delete = '0'
  AND g.is_delete = 0
GO
/****** Object:  View [dbo].[v_m_workflow_template_stage]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_m_workflow_template_stage]
AS

SELECT
	wts.workflow_template_stage_id, 
	wts.workflow_template_id,
	wt.description AS workflow_template_desc,  
	wts.description, 
	wts.is_stage_approval, 
	wts.approv_matrix_id, 
	am.description AS approv_matrix_desc,
	wts.pic_is_requester, 
	wts.group_pic_id,
	wts.user_workflow_id,
	CASE WHEN wts.user_workflow_id = 1 THEN 'Requester'
		 WHEN wts.user_workflow_id = 2 THEN 'Head Requester'
		 WHEN wts.approv_matrix_id IS NOT NULL  THEN 'Approver'
		 WHEN wts.pic_is_requester = 1 THEN 'Requester'
	ELSE gp.description
	END AS pic_desc,
	--gp.description AS pic_desc, 
	wts.can_edit_information, 
	wts.can_upload_document, 
	wts.can_delete_document, 
	wts.seq_number

FROM 	
	m_workflow_template_stage AS wts
	INNER JOIN dbo.m_workflow_template wt ON wts.workflow_template_id = wt.workflow_template_id AND wt.is_delete = 0
	LEFT JOIN dbo.m_group_pic gp ON gp.group_pic_id = wts.group_pic_id AND gp.is_delete = 0
	LEFT JOIN dbo.m_approv_matrix am ON am.approv_matrix_id = wts.approv_matrix_id AND gp.is_delete = 0
WHERE
	wts.is_delete = 0
GO
/****** Object:  Table [dbo].[m_workflow_template_action]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_workflow_template_action](
	[workflow_template_action_id] [bigint] IDENTITY(1,1) NOT NULL,
	[workflow_template_stage_id] [bigint] NULL,
	[description] [varchar](255) NOT NULL,
	[is_final_action] [char](1) NOT NULL,
	[workflow_template_id] [int] NULL,
	[destination_stage_id] [int] NULL,
	[is_send_notif_to_requester] [char](1) NOT NULL,
	[is_send_notif_to_next_pic] [char](1) NOT NULL,
	[is_notif_document_completion] [char](1) NOT NULL,
	[is_check_mandatory_document] [char](1) NOT NULL,
	[is_remarks_mandatory] [char](1) NULL,
	[is_fill_reference_number] [char](1) NOT NULL,
	[is_check_transaction_status_core] [char](1) NULL,
	[code_transaction_status] [varchar](50) NULL,
	[is_need_reason] [char](1) NOT NULL,
	[reason_category_id] [int] NULL,
	[transaction_status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_workfl__D25E975224334AAC] PRIMARY KEY CLUSTERED 
(
	[workflow_template_action_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  View [dbo].[v_t_transaction_history_process]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_t_transaction_history_process] AS
SELECT
	th.transaction_history_process_id, 
	th.transaction_id,
	t.register_number,
	t.transaction_date,
	t.policy_id_reference,
	t.policy_holder_reference,
	t.status_desc,
	t.type_desc,
	t.stage_desc,
	th.process_date, 
	th.user_id,
	u.user_fullname, 
	th.workflow_template_action_id, 
	wt.description AS workflow_action_desc,
	th.action_description,
	th.reason_id,
	r.description AS reason_desc, 
	th.remarks,  
	th.is_delete
FROM
	t_transaction_history_process AS th
	LEFT JOIN
	v_get_transaction_all AS t ON t.transaction_id = th.transaction_id
	LEFT JOIN
	dbo.m_workflow_template_action wt ON wt.workflow_template_action_id = th.workflow_template_action_id
	LEFT JOIN
	dbo.m_reason AS r ON r.reason_id = th.reason_id
	LEFT JOIN
	dbo.m_user AS u ON u.username = th.user_id
WHERE 
	th.is_delete = 0 and
	u.is_delete in (0,99)
GO
/****** Object:  Table [dbo].[m_user_menu_mapping]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_user_menu_mapping](
	[id] [bigint] IDENTITY(1,1) NOT NULL,
	[user_id] [varchar](3) NOT NULL,
	[menu_id] [int] NOT NULL,
	[is_exclude] [tinyint] NOT NULL,
	[created_by] [varchar](3) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](3) NULL,
	[modified_date] [datetime] NULL,
	[is_delete] [tinyint] NOT NULL,
 CONSTRAINT [PK_m_user_menu_mapping] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_m_user_menu_mapping] ON [dbo].[m_user_menu_mapping] 
(
	[user_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[m_user_menu]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_user_menu](
	[user_menu_id] [int] IDENTITY(1,1) NOT NULL,
	[user_menu] [varchar](8) NOT NULL,
	[is_group_menu] [char](1) NOT NULL,
	[group_menu_id] [int] NOT NULL,
	[is_include] [char](1) NOT NULL,
	[is_exclude] [char](1) NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[user_menu_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  StoredProcedure [dbo].[proc_submit_transaction]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Note: Saat script ini dicreate di Production, mohon diubah bagian exec SP sbb:
--       - proc_get_new_business_information
--       - proc_get_alteration_information
--       - proc_get_claim_information
-- SP tsb saat di server Development menggunakan link server ke [ELI-DB10\EQUITY].[db_link_project_uat]
-- Saat dicreate di Production, tidak perlu lagi menggunakan link server, cukup [db_link_project] saja


-- last modified by Pribadi Pamungkas. 2017-11-06 => - apakah sudah digunakan di transaksi edits yang lain? Jika sudah maka sistem akan menolak proses dan mengeluarkan pesan "Duplicate Reference No. with Register No [RegisterNumber EDITS]! "
--																									 - apakah nomor transaksi sudah terdaftar di aplikasi core sistem? Jika sudah maka sistem akan menolak proses dan mengeluarkan pesan "Reference No. not found. Please input Register No. from Core System!"
--
-- todo: - cek status tiap tabel (active) dan is_delete
--       - informasi mandatory
CREATE PROCEDURE [dbo].[proc_submit_transaction]
	@pi_transaction_id BIGINT,
	@pi_workflow_template_action_id BIGINT,  -- next workflow_template_action_id yg akan dituju
	@pi_reason_id INT,
	@pi_reference_number VARCHAR(50),
	@pi_remarks VARCHAR(255),
	@pi_user_id VARCHAR(8),
	@po_message VARCHAR(400) OUTPUT
AS
BEGIN
	SET NOCOUNT ON;	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	DECLARE 
	  @v_result BIT = 0,
	  @v_current_datetime DATETIME = GETDATE()
	

	BEGIN TRY
	BEGIN TRANSACTION proses_utama

		--====================== get transaction ======================
		DECLARE
			@v_current_workflow_template_stage_id BIGINT,
			@v_requester_id VARCHAR(8),
			@v_branch_id INT,
			@v_transaction_type_id INT,
			@v_currency_id INT,
			@v_currency_code VARCHAR(3),
			@v_transaction_value DECIMAL(12,2),
			@v_is_policy_holder TINYINT = 0
		SELECT
			@v_current_workflow_template_stage_id = workflow_template_stage_id,
			@v_requester_id = [user_id],
			@v_branch_id = branch_id,
			@v_transaction_type_id = transaction_type_id,
			@v_currency_id = currency_id,
			@v_currency_code = currency_code,
			@v_transaction_value = COALESCE(transaction_value,0),
			@v_is_policy_holder = COALESCE(is_policy_holder,0)
		FROM
			dbo.t_transaction
		WHERE
			transaction_id = @pi_transaction_id
		IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			BEGIN
				SET @po_message = 'Transaction not found'
				GOTO ERROR_OUT
			END
	

		--====================== validasi next workflow stage adalah benar lanjutan dari workflow stage saat ini ======================
		DECLARE 
			@v_destination_stage_id INT,
			@v_is_check_mandatory_document BIT = 0,
			@v_workflow_template_stage_id BIGINT,
			@v_action_description VARCHAR(255),
			@v_is_stage_approval VARCHAR(1),
			@v_approv_matrix_id INT,
			@v_is_final_action VARCHAR(1) = '0',
			@v_transaction_status_id INT,
			@v_is_fill_reference_number VARCHAR(1), -- new
			@v_is_need_reason VARCHAR(1), -- new
			@v_pic_is_requester VARCHAR(1), -- new
			--sudah di-declare dibawah  --@v_group_pic_id INT -- new
			@v_is_remarks_mandatory VARCHAR(1) = '0'
		SELECT
			@v_destination_stage_id = act.destination_stage_id,
			@v_is_check_mandatory_document = CASE act.is_check_mandatory_document
												                WHEN '1' THEN 1
												                ELSE 0
											                 END,
			@v_workflow_template_stage_id = act.workflow_template_stage_id,
			@v_action_description = act.[description],
			--@v_is_stage_approval = COALESCE(stg.is_stage_approval,'0'),
			@v_is_stage_approval = COALESCE(next_stg.is_stage_approval,'0'),  -- modified by Pribadi Pamungkas. 2017-10-11. fix ke next stage
			--@v_approv_matrix_id = stg.approv_matrix_id,
			@v_approv_matrix_id = next_stg.approv_matrix_id,      -- modified by Pribadi Pamungkas. 2017-10-11. fix ke next stage
			@v_is_final_action = COALESCE(act.is_final_action,'0'),
			@v_transaction_status_id = act.transaction_status_id,
	    @v_is_fill_reference_number = COALESCE(act.is_fill_reference_number,'0'), -- new
	    @v_is_need_reason = COALESCE(act.is_need_reason,'0'), -- new
	    @v_pic_is_requester = COALESCE(next_stg.pic_is_requester,'0'), -- new
	    --sudah di-declare dibawah  --@v_group_pic_id = next_stg.group_pic_id, -- new
	    @v_is_remarks_mandatory = COALESCE(act.is_remarks_mandatory,'0')
		FROM
			dbo.m_workflow_template_action AS act INNER JOIN
			dbo.m_workflow_template_stage AS stg ON act.workflow_template_stage_id = stg.workflow_template_stage_id LEFT JOIN
			dbo.m_workflow_template_stage AS next_stg ON act.destination_stage_id = next_stg.workflow_template_stage_id
		WHERE
			act.workflow_template_stage_id = @v_current_workflow_template_stage_id  -- workflow_template_stage_id saat ini
			AND act.workflow_template_action_id = @pi_workflow_template_action_id   -- 
			AND act.is_delete = '0'
			AND stg.is_delete = '0'
		IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			BEGIN
				SET @po_message = 'Destination Action is not valid'
				GOTO ERROR_OUT
			END

    IF (@v_is_final_action = '0') AND (@v_destination_stage_id IS NULL) -- jika bukan final action, tetapi tidak ada destination_stage
			BEGIN
				SET @po_message = 'Missing Destination Stage'
				GOTO ERROR_OUT
			END


		--====================== cek remarks ======================
		IF (@v_is_remarks_mandatory = '1')	-- jika remarks wajib diisi
			BEGIN
				IF (LEN(COALESCE(@pi_remarks,''))<=1)	-- jika remaks berisi 1 karakter atau kurang
					BEGIN
			      SET @po_message = 'Please fill in the remarks field'
			      GOTO ERROR_OUT
					END
			END


		--====================== cek informasi transaksi mandatory ======================
		IF (@v_is_fill_reference_number = '1')
		  BEGIN
		    IF (COALESCE(@pi_reference_number,'') = '')
		      BEGIN
			      SET @po_message = 'Reference Number is empty'
			      GOTO ERROR_OUT
		      END
		    ELSE
		      BEGIN
						IF EXISTS(SELECT
												* 
											FROM 
												dbo.t_transaction 
											WHERE 
												reference_number = @pi_reference_number 
												AND status_id NOT IN (3,5)	-- Reject / Cancel
												AND is_delete = 0
												AND transaction_id <> @pi_transaction_id)
							BEGIN		-- jika sudah terpakai
								DECLARE @register_number_terpakai VARCHAR(9)
								SELECT
									@register_number_terpakai = register_number 
								FROM 
									dbo.t_transaction 
								WHERE 
									reference_number = @pi_reference_number 
									AND status_id NOT IN (3,5)	-- Reject / Cancel
									AND is_delete = 0
									AND transaction_id <> @pi_transaction_id
								SET @po_message = 'Duplicate Reference No. with Register No '+@register_number_terpakai
								GOTO ERROR_OUT
							END
						ELSE
							BEGIN
								-- note: saat live (pindah ke Production), ini di-enable-kan lagi, dan tidak menggunakan linked server
								---- cek di PL ada gak
								DECLARE @v_transaction_category_code VARCHAR(6)
								SELECT
									@v_transaction_category_code = mtc.transaction_category_code
								FROM
									dbo.m_transaction_type AS mtt INNER JOIN
									dbo.m_transaction_category AS mtc ON mtt.transaction_category_id = mtc.transaction_category_id
								WHERE
									mtt.transaction_type_id = @v_transaction_type_id

								DECLARE
									@return_value_core INT,
									@return_numrow_core INT = 0 

								IF @v_transaction_category_code = 'NB'	-- New Business
									BEGIN
										EXEC @return_value_core = [db_link_project].[EDITS].[proc_get_new_business_information] @pi_proposal_no = @pi_reference_number
										SELECT @return_numrow_core = @@ROWCOUNT	-- step ini hanya mengecek apakah data tsb ada atau tidak di PL, jadi cukup pake @@ROWCOUNT aja, gak usah melihat isi data hasilnya
										IF (@return_value_core = 0) OR (@@ERROR = 0)	-- jika tidak ada error pas execute
											BEGIN
												IF (@return_numrow_core = 0)
													BEGIN
														SET @po_message = 'Reference No. not found. Please input Register No. from Core System!'
														GOTO ERROR_OUT
													END
											END
										ELSE
											BEGIN
												SET @po_message = 'Failed to get data from core system'
												GOTO ERROR_OUT
											END
									END
								ELSE IF @v_transaction_category_code = 'PS'	-- Policy Service
									BEGIN
										EXEC @return_value_core = [db_link_project].[EDITS].[proc_get_alteration_information] @pi_registration_no = @pi_reference_number
										SELECT @return_numrow_core = @@ROWCOUNT	-- step ini hanya mengecek apakah data tsb ada atau tidak di PL, jadi cukup pake @@ROWCOUNT aja, gak usah melihat isi data hasilnya
										IF (@return_value_core = 0) OR (@@ERROR = 0)	-- jika tidak ada error pas execute
											BEGIN
												IF (@return_numrow_core = 0)
													BEGIN
														SET @po_message = 'Reference No. not found. Please input Register No. from Core System!'
														GOTO ERROR_OUT
													END
											END
										ELSE
											BEGIN
												SET @po_message = 'Failed to get data from core system'
												GOTO ERROR_OUT
											END
									END
								ELSE IF @v_transaction_category_code = 'CL'	-- Claim
									BEGIN
										EXEC @return_value_core = [db_link_project].[EDITS].[proc_get_claim_information] @pi_claim_no = @pi_reference_number
										SELECT @return_numrow_core = @@ROWCOUNT	-- step ini hanya mengecek apakah data tsb ada atau tidak di PL, jadi cukup pake @@ROWCOUNT aja, gak usah melihat isi data hasilnya
										IF (@return_value_core = 0) OR (@@ERROR = 0)	-- jika tidak ada error pas execute
											BEGIN
												IF (@return_numrow_core = 0)
													BEGIN
														SET @po_message = 'Reference No. not found. Please input Register No. from Core System!'
														GOTO ERROR_OUT
													END
											END
										ELSE
											BEGIN
												SET @po_message = 'Failed to get data from core system'
												GOTO ERROR_OUT
											END
									END
								--ELSE IF @v_transaction_category_code = 'CO'	-- Collection
								--	BEGIN
									
								--	END
								ELSE
									BEGIN
										SET @po_message = 'Unknown transaction category'
										GOTO ERROR_OUT
									END

								UPDATE
									dbo.t_transaction
								SET
									reference_number = @pi_reference_number
								WHERE
									transaction_id = @pi_transaction_id
								IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
									BEGIN
										SET @po_message = 'Failed update reference_number'
										GOTO ERROR_OUT
									END
							END
		      END
		  END


		IF (@v_is_need_reason = '1')
		  IF (COALESCE(@pi_reason_id,0) = 0)
		    BEGIN
			    SET @po_message = 'Reason is empty'
			    GOTO ERROR_OUT
		    END


		DECLARE @v_information_valid BIT = 1  -- nanti kalau sudah dikasih pengecekan, ganti dengan 0
		--bla bla bla
		IF @v_information_valid = 0
			BEGIN
				SET @po_message = 'Document can not be processed. Please check mandatory Information'
				GOTO ERROR_OUT
			END


		--====================== cek dokumen transaksi mandatory ======================
		IF @v_is_check_mandatory_document=1
			BEGIN
				DECLARE @v_document_valid BIT = 0
				SELECT @v_document_valid = dbo.func_is_document_complete(@pi_transaction_id)
				IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
					BEGIN
						SET @po_message = 'Failed check document'
						GOTO ERROR_OUT
					END
    	
				IF @v_document_valid = 0
					BEGIN
						SET @po_message = 'Document can not be processed. Please check mandatory Document'
						GOTO ERROR_OUT
					END
			END
	
	
		--====================== update transaction task sebelumnya menjadi not available ======================
		UPDATE
			tt
		SET
			status_id = 3,    -- 3:Not Available
			modified_by = @pi_user_id,
			modified_date = @v_current_datetime
		FROM
			dbo.t_transaction_task AS tt INNER JOIN
			(
				SELECT
					MAX(process_sequence) AS process_sequence,
					transaction_id
				FROM
					dbo.t_transaction_task
				WHERE
					transaction_id = @pi_transaction_id
				GROUP BY
					transaction_id
			) AS tt_max ON tt.transaction_id = tt_max.transaction_id
							AND tt.process_sequence = tt_max.process_sequence
		IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			BEGIN
				SET @po_message = 'Failed update t_transaction_task'
				GOTO ERROR_OUT
			END


		--====================== history workflow ======================
		-- added by Pribadi Pamungkas. 2017-10-12
    IF EXISTS(SELECT
              	*
              FROM
              	dbo.t_transaction_history_workflow
              WHERE
                transaction_id = @pi_transaction_id
                AND workflow_template_stage_id = @v_workflow_template_stage_id  -- cek stage awal
                AND is_delete = 0)
      BEGIN  -- jika di stage awal (stage saat ini) sudah ada di tabel history_workflow

        UPDATE
          dbo.t_transaction_history_workflow
        SET
          last_user = @pi_user_id,                -- update user
          last_submit_date = @v_current_datetime  -- update tanggal
        WHERE
          transaction_id = @pi_transaction_id
          AND workflow_template_stage_id = @v_workflow_template_stage_id  -- update stage awal
          AND is_delete = 0

      END
    ELSE
      BEGIN  -- jika di stage awal (stage saat ini) BELUM ada di tabel history_workflow
        -- insert stage awal di tabel history_workflow
        INSERT INTO dbo.t_transaction_history_workflow
                   ([transaction_id]
                   ,[workflow_template_stage_id]
                   ,[last_user]
                   ,[last_submit_date]
                   ,[created_by]
                   ,[created_date]
                   ,[modified_by]
                   ,[modified_date]
                   ,[is_delete])
             VALUES
                   (@pi_transaction_id
                   ,@v_workflow_template_stage_id
                   ,@pi_user_id
                   ,@v_current_datetime
                   ,@pi_user_id
                   ,@v_current_datetime
                   ,@pi_user_id
                   ,@v_current_datetime
                   ,0)
      END



    -- insert transaction task
    DECLARE
      @v_next_process_sequence INT,
      @v_next_task_status_id INT = 2 --         <------------------ [1:Taken, 2:Available, 3:Not Available]
    SELECT
      @v_next_process_sequence = MAX(process_sequence) + 1
    FROM
      dbo.t_transaction_task
    WHERE
      transaction_id = @pi_transaction_id
    GROUP BY
      transaction_id

    IF (@v_is_final_action = '0') -- jika bukan final
      BEGIN
		    --====================== APPROVAL ======================
		    IF @v_is_stage_approval = '1'
		      BEGIN
		        IF @v_approv_matrix_id IS NULL
		          BEGIN
				        SET @po_message = 'Approval Matrix is not defined'
				        GOTO ERROR_OUT
		          END
            
            DECLARE @v_level_type_id INT
            SELECT
	            @v_level_type_id = mtd.level_type_id
            FROM
	            m_approv_matrix AS mtx INNER JOIN
	            m_approv_matrix_detail AS mtd ON mtx.approv_matrix_id = mtd.approv_matrix_id
            WHERE
              mtx.approv_matrix_id = @v_approv_matrix_id
              AND mtd.maximum_value >= @v_transaction_value
              AND mtd.currency_id = @v_currency_id


            -- list head (masih hardcode menggunakan SELECT satu-satu, harusnya menggunakan looping hierarki)
            DECLARE @list_user_approval TABLE ( userid VARCHAR(8))
            
            DECLARE 
              @v_organization_id INT,
              @v_organization_id_parent_1 INT,
              @v_organization_id_parent_2 INT,
              @v_organization_id_parent_3 INT,
              @v_organization_id_parent_4 INT,
              @v_organization_id_parent_5 INT,
              @v_level_type_id_1 INT,
              @v_level_type_id_2 INT,
              @v_level_type_id_3 INT,
              @v_level_type_id_4 INT,
              @v_level_type_id_5 INT,
              @v_user_id_head_1 VARCHAR(8),
              @v_user_id_head_2 VARCHAR(8),
              @v_user_id_head_3 VARCHAR(8),
              @v_user_id_head_4 VARCHAR(8),
              @v_user_id_head_5 VARCHAR(8)
            DECLARE list_organization_cursor CURSOR FAST_FORWARD FOR  
            SELECT
	            org.organization_id
            FROM
	            m_user AS usr INNER JOIN
	            m_user_organization AS org ON usr.username = org.user_id_organization
            WHERE
              usr.username = @pi_user_id
            
            OPEN list_organization_cursor  
            FETCH NEXT FROM list_organization_cursor INTO @v_organization_id
            
            WHILE @@FETCH_STATUS = 0  
            BEGIN    
            
              -- reset
              SET @v_organization_id_parent_1 = NULL
              SET @v_organization_id_parent_2 = NULL
              SET @v_organization_id_parent_3 = NULL
              SET @v_organization_id_parent_4 = NULL
              SET @v_organization_id_parent_5 = NULL
              SET @v_level_type_id_1 = NULL
              SET @v_level_type_id_2 = NULL
              SET @v_level_type_id_3 = NULL
              SET @v_level_type_id_4 = NULL
              SET @v_level_type_id_5 = NULL
              SET @v_user_id_head_1 = NULL
              SET @v_user_id_head_2 = NULL
              SET @v_user_id_head_3 = NULL
              SET @v_user_id_head_4 = NULL
              SET @v_user_id_head_5 = NULL

              IF (@v_organization_id IS NOT NULL) AND (@v_level_type_id IS NOT NULL)
                BEGIN
                  SELECT
	                  @v_level_type_id_1 = level_type_id,
	                  @v_user_id_head_1 = user_id_head,
	                  @v_organization_id_parent_1 = organization_id_parent
                  FROM
	                  m_organization
                  WHERE
                    organization_id = @v_organization_id
                    AND level_type_id >= @v_level_type_id
                  IF (@@ROWCOUNT > 0) AND (COALESCE(@v_user_id_head_1,'')<>'')
                    INSERT INTO @list_user_approval ( userid ) VALUES (@v_user_id_head_1)
                END
              
              IF (@v_organization_id_parent_1 IS NOT NULL) AND (@v_level_type_id_1 IS NOT NULL)
                BEGIN
                  SELECT
	                  @v_level_type_id_2 = level_type_id,
	                  @v_user_id_head_2 = user_id_head,
	                  @v_organization_id_parent_2 = organization_id_parent
                  FROM
	                  m_organization
                  WHERE
                    organization_id = @v_organization_id_parent_1
                    AND level_type_id < @v_level_type_id_1
                  IF (@@ROWCOUNT > 0) AND (COALESCE(@v_user_id_head_2,'')<>'')
                    INSERT INTO @list_user_approval ( userid ) VALUES (@v_user_id_head_2)
                END

              IF (@v_organization_id_parent_2 IS NOT NULL) AND (@v_level_type_id_2 IS NOT NULL)
                BEGIN
                  SELECT
	                  @v_level_type_id_3 = level_type_id,
	                  @v_user_id_head_3 = user_id_head,
	                  @v_organization_id_parent_3 = organization_id_parent
                  FROM
	                  m_organization
                  WHERE
                    organization_id = @v_organization_id_parent_2
                    AND level_type_id < @v_level_type_id_2
                  IF (@@ROWCOUNT > 0) AND (COALESCE(@v_user_id_head_3,'')<>'')
                    INSERT INTO @list_user_approval ( userid ) VALUES (@v_user_id_head_3)
                END

              IF (@v_organization_id_parent_3 IS NOT NULL) AND (@v_level_type_id_3 IS NOT NULL)
                BEGIN
                  SELECT
	                  @v_level_type_id_4 = level_type_id,
	                  @v_user_id_head_4 = user_id_head,
	                  @v_organization_id_parent_4 = organization_id_parent
                  FROM
	                  m_organization
                  WHERE
                    organization_id = @v_organization_id_parent_3
                    AND level_type_id < @v_level_type_id_3
                  IF (@@ROWCOUNT > 0) AND (COALESCE(@v_user_id_head_4,'')<>'')
                    INSERT INTO @list_user_approval ( userid ) VALUES (@v_user_id_head_4)
                END

              IF (@v_organization_id_parent_4 IS NOT NULL) AND (@v_level_type_id_4 IS NOT NULL)
                BEGIN
                  SELECT
	                  @v_level_type_id_5 = level_type_id,
	                  @v_user_id_head_5 = user_id_head,
	                  @v_organization_id_parent_5 = organization_id_parent
                  FROM
	                  m_organization
                  WHERE
                    organization_id = @v_organization_id_parent_4
                    AND level_type_id < @v_level_type_id_4
                  IF (@@ROWCOUNT > 0) AND (COALESCE(@v_user_id_head_5,'')<>'')
                    INSERT INTO @list_user_approval ( userid ) VALUES (@v_user_id_head_5)
                END
      
              FETCH NEXT FROM list_organization_cursor INTO @v_organization_id  
            END  

            CLOSE list_organization_cursor  
            DEALLOCATE list_organization_cursor 


    			        
            IF EXISTS(SELECT * FROM @list_user_approval)
              BEGIN
		            -- insert history proses untuk pergantian action
		            -- Saat user memproses task transaksi untuk berpindah stage
		            -- Proses ini dijalankan saat user menekan tombol submit pada layar task information untuk memproses transaksi ke tahapan selanjutnya. Berikut informasi histori yang dibentuk oleh sistem:
		            -- 1.	Process Date = get date
		            -- 2.	Process Time = get time
		            -- 3.	User = get user
		            -- 4.	Action = get from field action
		            -- 5.	Reason = get from field reason
		            -- 6.	Remarks = get from field remarks
		            INSERT INTO dbo.t_transaction_history_process
				            (transaction_id,
				            process_date,
				            [user_id],
				            workflow_template_action_id,
				            action_description,
				            reason_id,
				            remarks,
				            created_by,
				            created_date,
				            modified_by,
				            modified_date,
				            is_delete)
		            VALUES
				            (@pi_transaction_id, -- transaction_id - bigint
				            @v_current_datetime, -- process_date - datetime
				            @pi_user_id, -- user_id - int
				            @pi_workflow_template_action_id, -- workflow_template_action_id - bigint
				            @v_action_description,
				            @pi_reason_id, -- reason_id - int
				            @pi_remarks, -- remarks - varchar(255)
				            @pi_user_id, -- created_by - varchar(8)
				            @v_current_datetime, -- created_date - datetime
				            @pi_user_id, -- modified_by - varchar(8)
				            @v_current_datetime, -- modified_date - datetime
				            '0'  -- is_delete - char(1)
				            )
		            IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			            BEGIN
				            SET @po_message = 'Failed insert t_transaction_history_process'
				            GOTO ERROR_OUT
			            END


				        INSERT INTO dbo.t_transaction_task
							        (transaction_id,
							        process_sequence,
							        [user_id],
							        status_id,
							        created_by,
							        created_date,
							        modified_by,
							        modified_date,
							        is_delete)
			          SELECT
			            @pi_transaction_id, -- transaction_id - bigint
			            @v_next_process_sequence, -- process_sequence - int
			            mu.username,   -- 
			            @v_next_task_status_id, -- status_id - int
			      	    @pi_user_id, -- created_by - varchar(8)
					        @v_current_datetime, -- created_date - datetime
					        @pi_user_id, -- modified_by - varchar(8)
					        @v_current_datetime, -- modified_date - datetime
					        '0'  -- is_delete - char(1)
			          FROM
			      	    @list_user_approval AS usr_appr INNER JOIN
			      	    dbo.m_user AS mu ON usr_appr.userid = mu.username
			          WHERE
			            mu.is_delete = 0
    			        
				        -- insert history pengiriman task ke pic yang dituju 
				        -- Saat sistem mengirimkan task ke pic yang dituju 
				        -- Proses ini dijalankan otomatis saat sistem sudah mengirimkan task ke PIC yang akan mengerjakan task tersebut. Berikut informasi histori yang dibentuk oleh sistem:
				        -- 1.	Process Date = get date
				        -- 2.	Process Time = get time
				        -- 3.	User = System
				        -- 4.	Action = ‘Assign task to  [Destination PIC]’
				        -- 5.	Reason = ‘NULL’
				        -- 6.	Remarks = ‘NULL’
				        INSERT INTO dbo.t_transaction_history_process
					        (transaction_id,
					        process_date,
					        [user_id],
					        workflow_template_action_id,
					        action_description,
					        reason_id,
					        remarks,
					        created_by,
					        created_date,
					        modified_by,
					        modified_date,
					        is_delete)
			          SELECT
			            @pi_transaction_id, -- transaction_id - bigint
			            @v_current_datetime, -- process_date - datetime
					        @pi_user_id, -- user_id - int
					        @pi_workflow_template_action_id, -- workflow_template_action_id - bigint
					        'Send approval to '+mu.user_fullname,  -- gunakan nama lengkap
                  @pi_reason_id, -- reason_id - int
					        @pi_remarks, -- remarks - varchar(255)
					        @pi_user_id, -- created_by - varchar(8)
					        @v_current_datetime, -- created_date - datetime
					        @pi_user_id, -- modified_by - varchar(8)
					        @v_current_datetime, -- modified_date - datetime
					        '0'  -- is_delete - char(1)
			          FROM
			      	    @list_user_approval AS usr_appr INNER JOIN
			      	    dbo.m_user AS mu ON usr_appr.userid = mu.username
			          WHERE
			            mu.is_delete = 0
    			      
                --Send approval to john
              END
            ELSE
              BEGIN
				        SET @po_message = 'There is no approver is intended'
				        GOTO ERROR_OUT
              END
            
		      END
        ELSE
		    --====================== BUKAN APPROVAL, ALIAS STAGE NORMAL ======================
          BEGIN

            -- tambahin: jika stage next sudah ada di tabel t_transaction_history_workflow, maka gunakan pic di tabel tsb,
            --           jika belum ada, insert
            DECLARE @v_user_history_workflow VARCHAR(8) = NULL
            IF EXISTS(SELECT
                  	    *
                      FROM
                  	    dbo.t_transaction_history_workflow
                      WHERE
                        transaction_id = @pi_transaction_id
                        AND workflow_template_stage_id = @v_destination_stage_id  --stage tujuan
                        AND is_delete = 0)
              BEGIN
                -- gunakan pic ini
                SELECT
            	    @v_user_history_workflow = last_user
                FROM
            	    dbo.t_transaction_history_workflow
                WHERE
                  transaction_id = @pi_transaction_id
                  AND workflow_template_stage_id = @v_destination_stage_id  --stage tujuan
                  AND is_delete = 0

              END
            ELSE
              BEGIN
                -- get/cari pic
                SET @v_user_history_workflow = NULL
              END
     
            IF (COALESCE(@v_user_history_workflow,'') <> '')  -- jika user pada history_workflow sudah ada, maka gunakan user tsb sebagai pic
              BEGIN
		            -- insert history proses untuk pergantian action
		            -- Saat user memproses task transaksi untuk berpindah stage
		            -- Proses ini dijalankan saat user menekan tombol submit pada layar task information untuk memproses transaksi ke tahapan selanjutnya. Berikut informasi histori yang dibentuk oleh sistem:
		            -- 1.	Process Date = get date
		            -- 2.	Process Time = get time
		            -- 3.	User = get user
		            -- 4.	Action = get from field action
		            -- 5.	Reason = get from field reason
		            -- 6.	Remarks = get from field remarks
		            INSERT INTO dbo.t_transaction_history_process
				            (transaction_id,
				            process_date,
				            [user_id],
				            workflow_template_action_id,
				            action_description,
				            reason_id,
				            remarks,
				            created_by,
				            created_date,
				            modified_by,
				            modified_date,
				            is_delete)
		            VALUES
				            (@pi_transaction_id, -- transaction_id - bigint
				            @v_current_datetime, -- process_date - datetime
				            @pi_user_id, -- user_id - int
				            @pi_workflow_template_action_id, -- workflow_template_action_id - bigint
				            @v_action_description,
				            @pi_reason_id, -- reason_id - int
				            @pi_remarks, -- remarks - varchar(255)
				            @pi_user_id, -- created_by - varchar(8)
				            @v_current_datetime, -- created_date - datetime
				            @pi_user_id, -- modified_by - varchar(8)
				            @v_current_datetime, -- modified_date - datetime
				            '0'  -- is_delete - char(1)
				            )
		            IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			            BEGIN
				            SET @po_message = 'Failed insert t_transaction_history_process'
				            GOTO ERROR_OUT
			            END

                INSERT INTO dbo.t_transaction_task
                           ([transaction_id]
                           ,[process_sequence]
                           ,[user_id]
                           ,[status_id]
                           ,[created_by]
                           ,[created_date]
                           ,[modified_by]
                           ,[modified_date]
                           ,[is_delete])
                     VALUES
                           (@pi_transaction_id -- transaction_id - bigint
                           ,@v_next_process_sequence -- process_sequence - int
                           ,@v_user_history_workflow
                           ,@v_next_task_status_id -- status_id - int  
                           ,@pi_user_id -- created_by - varchar(8)
                           ,@v_current_datetime -- created_date - datetime
                           ,@pi_user_id -- modified_by - varchar(8)
                           ,@v_current_datetime -- modified_date - datetime
                           ,'0')
		            IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			            BEGIN
				            SET @po_message = 'Failed insert t_transaction_task'
				            GOTO ERROR_OUT
			            END

		            -- insert history pengiriman task ke pic yang dituju 
		            -- Saat sistem mengirimkan task ke pic yang dituju 
		            -- Proses ini dijalankan otomatis saat sistem sudah mengirimkan task ke PIC yang akan mengerjakan task tersebut. Berikut informasi histori yang dibentuk oleh sistem:
		            -- 1.	Process Date = get date
		            -- 2.	Process Time = get time
		            -- 3.	User = System
		            -- 4.	Action = ‘Assign task to  [Destination PIC]’
		            -- 5.	Reason = ‘NULL’
		            -- 6.	Remarks = ‘NULL’
		            INSERT INTO dbo.t_transaction_history_process
			            (transaction_id,
			            process_date,
			            [user_id],
			            workflow_template_action_id,
			            action_description,
			            reason_id,
			            remarks,
			            created_by,
			            created_date,
			            modified_by,
			            modified_date,
			            is_delete)
		            SELECT
			            @pi_transaction_id, -- transaction_id - bigint
			            @v_current_datetime, -- process_date - datetime
			            'SYS', -- user_id - int
			            NULL, --@pi_workflow_template_action_id, -- workflow_template_action_id - bigint
			            'Assign task to '+user_fullname,  -- gunakan nama lengkap
			            @pi_reason_id, -- reason_id - int
			            NULL, --@pi_remarks, -- remarks - varchar(255)
			            @pi_user_id, -- created_by - varchar(8)
			            @v_current_datetime, -- created_date - datetime
			            @pi_user_id, -- modified_by - varchar(8)
			            @v_current_datetime, -- modified_date - datetime
			            '0'  -- is_delete - char(1)
		            FROM
			            dbo.m_user
		            WHERE
			            username = @v_user_history_workflow
		            IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			            BEGIN
				            SET @po_message = 'Transaction not found'
				            GOTO ERROR_OUT
			            END

              END
            ELSE  -- jika belum ada, maka cari pic
              BEGIN
		            -- check pic yg dibutuhkan
		            DECLARE @v_pic_yg_dibutuhkan TINYINT = 0
		            -- 1: user dia sendiri
		            -- 2: head requester
		            -- 3: grup pic id
              
		            DECLARE @v_group_pic_id INT
		            SELECT
			            @v_pic_yg_dibutuhkan = CASE
										            WHEN pic_is_requester = '1' THEN 1--'user dia sendiri'
										            ELSE CASE user_workflow_id
												            WHEN 2 THEN 2 --'head requester'
												            WHEN 3 THEN 3 --'gunakan grup pic id'
											            END
									            END,
			            @v_group_pic_id = group_pic_id
		            FROM
			            dbo.m_workflow_template_stage
		            WHERE
			            workflow_template_stage_id = @v_destination_stage_id  --@v_workflow_template_stage_id
                
		            -- head requester
		            DECLARE @v_head_requester_user_id VARCHAR(8)
		            SELECT
			            @v_head_requester_user_id = org.user_id_head
		            FROM
			            dbo.m_user_organization AS muo INNER JOIN 
			            dbo.m_status AS ms ON muo.status_id = ms.status_id INNER JOIN
			            dbo.m_organization AS org ON muo.organization_id = org.organization_id
		            WHERE
			            muo.user_id_organization = @pi_user_id
			            AND muo.status_id = 5
			            AND ms.status_id = 5


			          IF (@v_pic_yg_dibutuhkan = 3)
			            IF (@v_group_pic_id IS NULL)
			              BEGIN
				              SET @po_message = 'Group PIC undefined'
				              GOTO ERROR_OUT
			              END
			          ELSE IF (@v_pic_yg_dibutuhkan = 2)
			            IF (COALESCE(@v_head_requester_user_id,'') = '')
			              BEGIN
				              SET @po_message = 'Head Requester PIC is empty'
				              GOTO ERROR_OUT
			              END

			          IF (@v_pic_yg_dibutuhkan <> 3)
			            BEGIN
		                -- get PIC
		                DECLARE @v_user_pic VARCHAR(8) = NULL
		                SELECT
			                @v_user_pic = CASE
							                WHEN pic_is_requester = '1' THEN @v_requester_id--'user dia sendiri'
							                ELSE CASE user_workflow_id
									                WHEN 2 THEN @v_head_requester_user_id --'head requester' (tambahin, jika kosong maka error, tidak bisa submit)
									                --WHEN 3 THEN @v_user_group_pic_id  --'gunakan grup pic id'
									                ELSE NULL
								                END
							                END
		                FROM
			                dbo.m_workflow_template_stage
		                WHERE
			                workflow_template_stage_id = @v_destination_stage_id  --@v_workflow_template_stage_id
		                IF @v_user_pic IS NULL
			                BEGIN
				                SET @po_message = 'PIC is empty'
				                GOTO ERROR_OUT
			                END
			            END


		            -- insert history proses untuk pergantian action
		            -- Saat user memproses task transaksi untuk berpindah stage
		            -- Proses ini dijalankan saat user menekan tombol submit pada layar task information untuk memproses transaksi ke tahapan selanjutnya. Berikut informasi histori yang dibentuk oleh sistem:
		            -- 1.	Process Date = get date
		            -- 2.	Process Time = get time
		            -- 3.	User = get user
		            -- 4.	Action = get from field action
		            -- 5.	Reason = get from field reason
		            -- 6.	Remarks = get from field remarks
		            INSERT INTO dbo.t_transaction_history_process
				            (transaction_id,
				            process_date,
				            [user_id],
				            workflow_template_action_id,
				            action_description,
				            reason_id,
				            remarks,
				            created_by,
				            created_date,
				            modified_by,
				            modified_date,
				            is_delete)
		            VALUES
				            (@pi_transaction_id, -- transaction_id - bigint
				            @v_current_datetime, -- process_date - datetime
				            @pi_user_id, -- user_id - int
				            @pi_workflow_template_action_id, -- workflow_template_action_id - bigint
				            @v_action_description,
				            @pi_reason_id, -- reason_id - int
				            @pi_remarks, -- remarks - varchar(255)
				            @pi_user_id, -- created_by - varchar(8)
				            @v_current_datetime, -- created_date - datetime
				            @pi_user_id, -- modified_by - varchar(8)
				            @v_current_datetime, -- modified_date - datetime
				            '0'  -- is_delete - char(1)
				            )
		            IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			            BEGIN
				            SET @po_message = 'Failed insert t_transaction_history_process'
				            GOTO ERROR_OUT
			            END
              
              
		            --==================================================
		            -- insert transaction task
		            --DECLARE
			          --  @v_next_process_sequence INT,
			          --  @v_next_task_status_id INT = 2 --         <------------------ [1:Taken, 2:Available, 3:Not Available]
		            SELECT
			            @v_next_process_sequence = MAX(process_sequence) + 1
		            FROM
			            dbo.t_transaction_task
		            WHERE
			            transaction_id = @pi_transaction_id
		            GROUP BY
			            transaction_id

		            -- jika user_workflow_id = 5, maka pakai group_pic_id
		            IF @v_pic_yg_dibutuhkan=3 --(jika yg dibutuhkan adalah group_pic_id)
			            BEGIN
				            -- cek ada grup pic enggak?
				            IF NOT EXISTS(
						            SELECT
							            mu.username
						            FROM
							            dbo.m_group_pic AS mgp INNER JOIN
							            dbo.m_group_pic_detail AS mgpd ON mgp.group_pic_id = mgpd.group_pic_id INNER JOIN
							            dbo.m_user AS mu ON mgpd.user_id_group_pic_detail = mu.[user_id]
						            WHERE
							            mgp.group_pic_id = @v_group_pic_id
							            AND (mgpd.branch_id = CAST(@v_branch_id AS VARCHAR(5)) OR mgpd.branch_id = '*')
							            AND (mgpd.transaction_type_id = CAST(@v_transaction_type_id AS VARCHAR(5)) OR mgpd.transaction_type_id = '*')
							            AND mgp.is_delete = '0'
							            AND mgpd.is_delete = '0'
							            AND mu.is_delete = 0
						            )
					            BEGIN
						            SET @po_message = 'Group PIC is empty'
						            GOTO ERROR_OUT
					            END

				            INSERT INTO dbo.t_transaction_task
							            (transaction_id,
							            process_sequence,
							            [user_id],
							            status_id,
							            created_by,
							            created_date,
							            modified_by,
							            modified_date,
							            is_delete)
				            SELECT DISTINCT
					            @pi_transaction_id, -- transaction_id - bigint
					            @v_next_process_sequence, -- process_sequence - int
					            mu.username,   -- gunakan user group pic (bisa lebih dari 1)
					            @v_next_task_status_id, -- status_id - int                             <----------------------- [1:Taken, 2:Available, 3:Not Available]
					            @pi_user_id, -- created_by - varchar(8)
					            @v_current_datetime, -- created_date - datetime
					            @pi_user_id, -- modified_by - varchar(8)
					            @v_current_datetime, -- modified_date - datetime
					            '0'  -- is_delete - char(1)
				            FROM
					            dbo.m_group_pic AS mgp INNER JOIN
					            dbo.m_group_pic_detail AS mgpd ON mgp.group_pic_id = mgpd.group_pic_id INNER JOIN
					            dbo.m_user AS mu ON mgpd.user_id_group_pic_detail = mu.[user_id]
				            WHERE
					            mgp.group_pic_id = @v_group_pic_id
					            AND (mgpd.branch_id = CAST(@v_branch_id AS VARCHAR(5)) OR mgpd.branch_id = '*')
					            AND (mgpd.transaction_type_id = CAST(@v_transaction_type_id AS VARCHAR(5)) OR mgpd.transaction_type_id = '*')
					            AND mgp.is_delete = '0'
					            AND mgpd.is_delete = '0'
					            AND mu.is_delete = 0
				            IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
					            BEGIN
						            SET @po_message = 'Group PIC is empty'
						            GOTO ERROR_OUT
					            END
            	    
				            -- insert history pengiriman task ke pic yang dituju 
				            -- Saat sistem mengirimkan task ke pic yang dituju 
				            -- Proses ini dijalankan otomatis saat sistem sudah mengirimkan task ke PIC yang akan mengerjakan task tersebut. Berikut informasi histori yang dibentuk oleh sistem:
				            -- 1.	Process Date = get date
				            -- 2.	Process Time = get time
				            -- 3.	User = System
				            -- 4.	Action = ‘Assign task to  [Destination PIC]’
				            -- 5.	Reason = ‘NULL’
				            -- 6.	Remarks = ‘NULL’
				            INSERT INTO dbo.t_transaction_history_process
					            (transaction_id,
					            process_date,
					            [user_id],
					            workflow_template_action_id,
					            action_description,
					            reason_id,
					            remarks,
					            created_by,
					            created_date,
					            modified_by,
					            modified_date,
					            is_delete)
				            SELECT DISTINCT
					            @pi_transaction_id, -- transaction_id - bigint
					            @v_current_datetime, -- process_date - datetime
					            'SYS', -- user_id - int
					            NULL, --@pi_workflow_template_action_id, -- workflow_template_action_id - bigint
					            'Assign task to '+mu.user_fullname,  -- gunakan nama lengkap
					            @pi_reason_id, -- reason_id - int
					            NULL, --@pi_remarks, -- remarks - varchar(255)
					            @pi_user_id, -- created_by - varchar(8)
					            @v_current_datetime, -- created_date - datetime
					            @pi_user_id, -- modified_by - varchar(8)
					            @v_current_datetime, -- modified_date - datetime
					            '0'  -- is_delete - char(1)
				            FROM
					            dbo.m_group_pic AS mgp INNER JOIN
					            dbo.m_group_pic_detail AS mgpd ON mgp.group_pic_id = mgpd.group_pic_id INNER JOIN
					            dbo.m_user AS mu ON mgpd.user_id_group_pic_detail = mu.[user_id]
				            WHERE
					            mgp.group_pic_id = @v_group_pic_id
					            AND (mgpd.branch_id = CAST(@v_branch_id AS VARCHAR(5)) OR mgpd.branch_id = '*')
					            AND (mgpd.transaction_type_id = CAST(@v_transaction_type_id AS VARCHAR(5)) OR mgpd.transaction_type_id = '*')
					            AND mgp.is_delete = '0'
					            AND mgpd.is_delete = '0'
					            AND mu.is_delete = 0
				            IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
					            BEGIN
						            SET @po_message = 'Transaction not found'
						            GOTO ERROR_OUT
					            END

			            END
		            ELSE -- jika yang dibutuhkan adalah SELAIN group_pic_id
			            BEGIN

				            INSERT INTO dbo.t_transaction_task
							            (transaction_id,
							            process_sequence,
							            [user_id],
							            status_id,
							            created_by,
							            created_date,
							            modified_by,
							            modified_date,
							            is_delete)
				            SELECT
					            @pi_transaction_id, -- transaction_id - bigint
					            MAX(process_sequence) + 1, -- process_sequence - int
					            @v_user_pic, -- user_id - varchar(8)  
					            @v_next_task_status_id, -- status_id - int 
					            @pi_user_id, -- created_by - varchar(8)
					            @v_current_datetime, -- created_date - datetime
					            @pi_user_id, -- modified_by - varchar(8)
					            @v_current_datetime, -- modified_date - datetime
					            '0'  -- is_delete - char(1)
				            FROM
					            dbo.t_transaction_task
				            WHERE
					            transaction_id = @pi_transaction_id
				            GROUP BY
					            transaction_id
				            IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
					            BEGIN
						            SET @po_message = 'Failed insert t_transaction_task'
						            GOTO ERROR_OUT
					            END

				            -- insert history pengiriman task ke pic yang dituju 
				            -- Saat sistem mengirimkan task ke pic yang dituju 
				            -- Proses ini dijalankan otomatis saat sistem sudah mengirimkan task ke PIC yang akan mengerjakan task tersebut. Berikut informasi histori yang dibentuk oleh sistem:
				            -- 1.	Process Date = get date
				            -- 2.	Process Time = get time
				            -- 3.	User = System
				            -- 4.	Action = ‘Assign task to  [Destination PIC]’
				            -- 5.	Reason = ‘NULL’
				            -- 6.	Remarks = ‘NULL’
				            INSERT INTO dbo.t_transaction_history_process
							            (transaction_id,
							            process_date,
							            [user_id],
							            workflow_template_action_id,
							            action_description,
							            reason_id,
							            remarks,
							            created_by,
							            created_date,
							            modified_by,
							            modified_date,
							            is_delete)
				            SELECT
					            @pi_transaction_id, -- transaction_id - bigint
					            @v_current_datetime, -- process_date - datetime
					            'SYS', -- user_id - int
					            NULL, --@pi_workflow_template_action_id, -- workflow_template_action_id - bigint
					            'Assign task to '+user_fullname,  -- gunakan nama lengkap
					            @pi_reason_id, -- reason_id - int
					            NULL, --@pi_remarks, -- remarks - varchar(255)
					            @pi_user_id, -- created_by - varchar(8)
					            @v_current_datetime, -- created_date - datetime
					            @pi_user_id, -- modified_by - varchar(8)
					            @v_current_datetime, -- modified_date - datetime
					            '0'  -- is_delete - char(1)
				            FROM
					            dbo.m_user
				            WHERE
					            username = @v_user_pic
				            IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
					            BEGIN
						            SET @po_message = 'Transaction not found'
						            GOTO ERROR_OUT
					            END

			            END
              END

          END
      END
    ELSE  -- jika final
      BEGIN
        -- insert history proses untuk pergantian action
        -- Saat user memproses task transaksi untuk berpindah stage
        -- Proses ini dijalankan saat user menekan tombol submit pada layar task information untuk memproses transaksi ke tahapan selanjutnya. Berikut informasi histori yang dibentuk oleh sistem:
        -- 1.	Process Date = get date
        -- 2.	Process Time = get time
        -- 3.	User = get user
        -- 4.	Action = get from field action
        -- 5.	Reason = get from field reason
        -- 6.	Remarks = get from field remarks
        INSERT INTO dbo.t_transaction_history_process
            (transaction_id,
            process_date,
            [user_id],
            workflow_template_action_id,
            action_description,
            reason_id,
            remarks,
            created_by,
            created_date,
            modified_by,
            modified_date,
            is_delete)
        VALUES
            (@pi_transaction_id, -- transaction_id - bigint
            @v_current_datetime, -- process_date - datetime
            @pi_user_id, -- user_id - int
            @pi_workflow_template_action_id, -- workflow_template_action_id - bigint
            @v_action_description,
            @pi_reason_id, -- reason_id - int
            @pi_remarks, -- remarks - varchar(255)
            @pi_user_id, -- created_by - varchar(8)
            @v_current_datetime, -- created_date - datetime
            @pi_user_id, -- modified_by - varchar(8)
            @v_current_datetime, -- modified_date - datetime
            '0'  -- is_delete - char(1)
            )
        IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
          BEGIN
            SET @po_message = 'Failed insert t_transaction_history_process'
            GOTO ERROR_OUT
          END
      END


		

		--====================== update t_transaction ======================
		DECLARE @v_final_destination_stage_id INT
		IF (@v_is_final_action = '1')
		  SET @v_final_destination_stage_id = @v_current_workflow_template_stage_id -- jika Final Action, maka biarkan menggunakan stage_id sebelumnya (maksudnya saat ini)
		ELSE
		  SET @v_final_destination_stage_id = @v_destination_stage_id

		UPDATE
			dbo.t_transaction
		SET
			workflow_template_stage_id = @v_final_destination_stage_id,
			status_id = @v_transaction_status_id,
			modified_by = @pi_user_id,
			modified_date = @v_current_datetime
		WHERE
			transaction_id = @pi_transaction_id
		IF (@@ROWCOUNT = 0) OR (@@ERROR <> 0)
			BEGIN
				SET @po_message = 'Failed update t_transaction'
				GOTO ERROR_OUT
			END

		COMMIT TRANSACTION proses_utama
		SET @po_message = 'Sukses'  -- variable output 
		SET @v_result = 1
		SELECT @v_result AS [status], @po_message AS [message]  -- dual output stored procedure, variable output dan table output
		GOTO CLEAN_OUT

	END TRY
	BEGIN CATCH	-- tangkap error message selain yg sudah dideskripsikan
		SET @po_message = LEFT(ERROR_MESSAGE(),400)	-- berhubung variable diatas diset 400, maka potong error message
		GOTO ERROR_OUT
	END CATCH

	
	ERROR_OUT: 
		ROLLBACK TRANSACTION proses_utama
		SELECT @v_result AS [status], @po_message AS [message]  -- dual output stored procedure, variable output dan table output
		--RETURN 1

	CLEAN_OUT:
		--RETURN 0
			
END
GO
/****** Object:  View [dbo].[v_m_approv_matrix_detail]    Script Date: 11/10/2017 15:12:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_m_approv_matrix_detail]
AS
    SELECT  amd.approv_matrix_detail_id ,
            amd.approv_matrix_id ,
            amd.maximum_value ,
            amd.level_type_id ,
            lt.head_description AS level_type_desc, 
            amd.currency_id,
			cu.currency_code,
			cu.description as currency_description

    FROM    m_approv_matrix_detail amd
            INNER JOIN m_level_type lt ON lt.level_type_id = amd.level_type_id
			left JOIN m_currency cu on cu.currency_id =  amd.currency_id
    WHERE   lt.is_delete = 0
            AND amd.is_delete = 0
GO
/****** Object:  View [dbo].[v_group_menu]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_group_menu] AS
SELECT 
	m.menu_id,
	m.description AS menu_desc,
	m.menu_category_id,
	c.description AS cat_desc,
	m.url,
	m.level,
	m.menu_id_parent,
	m.created_by,
	m.created_date,
	m.modified_by,
	m.modified_date,
	g.group_menu_id,
	g.group_menu_code,
	g.description AS group_desc,
	p1.group_menu_mapping_id
FROM m_menu m
INNER JOIN m_menu_category c ON c.menu_category_id = m.menu_category_id
INNER JOIN m_group_menu_mapping p1 ON p1.menu_id = m.menu_id
INNER JOIN m_group_menu g ON g.group_menu_id = p1.group_menu_id
WHERE m.is_delete = 0
AND c.is_delete = 0
AND p1.is_delete = 0
AND g.is_delete = 0
GO
/****** Object:  View [dbo].[v_organization]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_organization]
AS SELECT 
	dbo.m_organization.organization_id, 
	dbo.m_organization.organization_code, 
	dbo.m_organization.description, 
	dbo.m_organization.level_type_id, 
    dbo.m_organization.organization_id_parent, 
	dbo.m_organization.status_id, 
	dbo.m_organization.is_delete, 
	dbo.m_status.status_code, 
    dbo.m_status.description AS status_desc, 
	m_organization_1.description AS parent_desc, 
	m_organization_1.organization_code AS parent_code, 
	m_organization_1.level_type_id AS parent_level,
	dbo.m_level_type.level_type_code, 
    dbo.m_level_type.description AS level_desc, 
	dbo.m_level_type.head_description, 
	dbo.m_level_type.seq_number,
	(
		SELECT COUNT(organization_id) 
		FROM m_organization 
		WHERE organization_id_parent = dbo.m_organization.organization_id
		AND status_id = 5 AND is_delete = 0
		) AS has_child,
	dbo.m_user.username,
	dbo.m_user.user_fullname
FROM dbo.m_organization 
INNER JOIN
	dbo.m_level_type ON dbo.m_organization.level_type_id = dbo.m_level_type.level_type_id 
LEFT OUTER JOIN
    dbo.m_organization AS m_organization_1 ON dbo.m_organization.organization_id_parent = m_organization_1.organization_id 
INNER JOIN
    dbo.m_status ON dbo.m_organization.status_id = dbo.m_status.status_id
LEFT JOIN dbo.m_user ON dbo.m_user.username = dbo.m_organization.user_id_head
GO
/****** Object:  View [dbo].[v_m_workflow_template_action]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--USE db_edits

CREATE VIEW [dbo].[v_m_workflow_template_action]
 AS
	SELECT 
		wta.workflow_template_action_id, 
		wta.workflow_template_stage_id, 
		wts.description AS workflow_template_stage_desc,
		wta.description, 
		wta.is_final_action, 
		wta.workflow_template_id, 
		wt.description AS workflow_template_desc,
		wta.destination_stage_id,
		wtss.description AS destination_stage_desc,
		wta.is_send_notif_to_requester, 
		wta.is_send_notif_to_next_pic, 
		wta.is_notif_document_completion, 
		wta.is_check_mandatory_document, 
		wta.is_remarks_mandatory,
		wta.is_fill_reference_number, 
		wta.is_check_transaction_status_core,
		wta.code_transaction_status,
		wta.is_need_reason, 
		wta.reason_category_id, 
		wta.transaction_status_id, 
		wta.is_delete,
		r.description AS reason_desc
	FROM m_workflow_template_action wta
	INNER JOIN dbo.m_workflow_template wt ON wt.workflow_template_id = wta.workflow_template_id AND wt.is_delete = 0
	LEFT JOIN dbo.m_workflow_template_stage wts ON wts.workflow_template_stage_id = wta.workflow_template_stage_id AND wts.is_delete = 0
	LEFT JOIN dbo.m_workflow_template_stage wtss ON wta.destination_stage_id = wtss.workflow_template_stage_id AND wtss.is_delete = 0
	LEFT JOIN dbo.m_reason_category r ON r.reason_category_id = wta.reason_category_id
	WHERE wta.is_delete = 0
GO
/****** Object:  StoredProcedure [dbo].[RemoveUserGroupMenu]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[RemoveUserGroupMenu] (
	@pi_user_id VARCHAR(3),
	@pi_group_menu_id INT,
	@pi_created_by VARCHAR(3)
)
	
AS
BEGIN

	UPDATE m_user_group_menu_mapping
	SET is_delete = 1,
		modified_by = @pi_created_by,
		modified_date = GETDATE()
	WHERE user_id = @pi_user_id
	AND group_menu_id = @pi_group_menu_id;

	UPDATE m_user_menu_mapping
	SET is_delete = 1,
		modified_by = @pi_created_by,
		modified_date = GETDATE()
	WHERE user_id = @pi_user_id
	AND menu_id IN (
		SELECT menu_id
		FROM m_group_menu_mapping
		WHERE group_menu_id = @pi_group_menu_id
		AND is_delete = '0'
	);

END
GO
/****** Object:  Table [dbo].[m_eform_setting]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_eform_setting](
	[eform_setting_id] [bigint] IDENTITY(1,1) NOT NULL,
	[eform_id] [int] NOT NULL,
	[description] [varchar](50) NOT NULL,
	[effective_date] [date] NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[eform_setting_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_eform_form_template]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_eform_form_template](
	[eform_form_template_id] [int] IDENTITY(1,1) NOT NULL,
	[eform_id] [int] NULL,
	[transaction_form_template_id] [smallint] NULL,
	[effective_date] [date] NULL,
	[status_id] [int] NULL,
	[created_by] [varchar](8) NULL,
	[created_date] [datetime] NULL,
	[modified_by] [varchar](8) NULL,
	[modified_date] [datetime] NULL,
	[is_delete] [char](1) NULL,
 CONSTRAINT [PK_m_eform_form_template] PRIMARY KEY CLUSTERED 
(
	[eform_form_template_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  View [dbo].[v_m_transaction_document]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_m_transaction_document]
AS
SELECT
  t.transaction_id,
  md.document_id,
  md.document_code,
  md.description,
  md.remarks,
  med.eform_document_id,
  med.eform_id,
  med.minimum_files,
  med.maximum_files,
  med.seq_number,
  med.status_id,
  COALESCE(df.jumlah_file,0) AS jumlah_file
FROM
  dbo.t_transaction AS t
  INNER JOIN 
  dbo.m_eform AS me ON t.transaction_type_id = me.transaction_type_id
  INNER JOIN 
  dbo.m_eform_document AS med ON me.eform_id = med.eform_id
  INNER JOIN 
  dbo.m_document AS md ON med.document_id = md.document_id
  LEFT JOIN
  (
    SELECT
	    ttd.transaction_id,
	    ttd.document_type_id,
	    COUNT(*) AS jumlah_file
    FROM
	    t_transaction_document AS ttd INNER JOIN
	    t_transaction_document_file AS ttdf ON ttd.transaction_document_id = ttdf.transaction_document_id
    WHERE
      ttd.is_delete = '0'
      AND ttdf.is_delete = '0'
    GROUP BY
	    ttd.transaction_id,
	    ttd.document_type_id
  ) AS df ON t.transaction_id = df.transaction_id
             AND md.document_id = df.document_type_id
WHERE
  (t.is_delete = 0)
  AND (me.is_delete = '0')
  AND (med.is_delete = '0')
  AND (md.is_delete = '0')
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "t"
            Begin Extent = 
               Top = 0
               Left = 0
               Bottom = 339
               Right = 240
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "me"
            Begin Extent = 
               Top = 6
               Left = 316
               Bottom = 125
               Right = 507
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "med"
            Begin Extent = 
               Top = 6
               Left = 545
               Bottom = 125
               Right = 729
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "md"
            Begin Extent = 
               Top = 6
               Left = 767
               Bottom = 125
               Right = 932
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_m_transaction_document'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'v_m_transaction_document'
GO
/****** Object:  UserDefinedFunction [dbo].[get_HigherOrganization]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[get_HigherOrganization] (@pi_level_id int)  
RETURNS TABLE  
AS  
RETURN
(
	SELECT 
		o.organization_id, 
		o.organization_code, 
		o.description,
		o.organization_id_parent,
		o.level_type_id,
		l.seq_number 
	FROM m_organization o,
		(
			SELECT 
				level_type_id,
				level_type_code,
				description,
				seq_number
			FROM m_level_type
		) l,
		(
			SELECT seq_number FROM m_level_type
			WHERE level_type_id = @pi_level_id
		) s
	  WHERE o.level_type_id = l.level_type_id

	  AND l.seq_number < s.seq_number
)
GO
/****** Object:  UserDefinedFunction [dbo].[get_higher_organization]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[get_higher_organization] (@pi_level_id int)  
RETURNS TABLE  
AS  
RETURN
(
	SELECT 
		o.organization_id, 
		o.organization_code, 
		o.description,
		o.organization_id_parent,
		o.level_type_id,
		l.seq_number 
	FROM m_organization o,
		(
			SELECT 
				level_type_id,
				level_type_code,
				description,
				seq_number
			FROM m_level_type
		) l,
		(
			SELECT seq_number FROM m_level_type
			WHERE level_type_id = @pi_level_id
		) s
	  WHERE o.level_type_id = l.level_type_id
	  AND l.seq_number < s.seq_number
	  AND o.status_id = 5
)
GO
/****** Object:  UserDefinedFunction [dbo].[get_user_menu_all_not_excluded]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[get_user_menu_all_not_excluded]
(	
	@pi_user_id VARCHAR(3)
)
RETURNS TABLE 
AS
RETURN 
(
	
	  SELECT * FROM
		  (SELECT 
			DISTINCT
			m.menu_id, 
			m.menu_category_id,
			m.url,
			m.description AS menu_desc,
			m.icon_class,
			m.menu_id_parent,
			c.description AS cat_desc
			--g.group_menu_id,
			--g.description AS group_desc
		  FROM m_menu m
		  JOIN m_group_menu_mapping p ON p.menu_id = m.menu_id
		  JOIN m_group_menu g ON g.group_menu_id = p.group_menu_id
		  JOIN m_user_group_menu_mapping mp ON mp.group_menu_id = p.group_menu_id
		  JOIN m_menu_category c ON c.menu_category_id = m.menu_category_id
		  WHERE m.is_delete = '0'
		  AND p.is_delete = '0'
		  AND g.is_delete = '0'
		  AND mp.user_id = @pi_user_id
		  AND mp.is_delete = 0) AS a 
		  FULL OUTER JOIN (
			SELECT 
				m_user_menu_mapping.user_id, 
				m_user_menu_mapping.menu_id AS menuid,
				m_menu_category.description AS catdesc, 
				m_menu.description AS menudesc,
				m_user_menu_mapping.is_exclude,
				m_user_menu_mapping.id AS mapping_id,
				m_menu.menu_id_parent AS menuidparent,
				m_menu.url AS menuurl,
				m_menu.icon_class AS iconclass 
			FROM m_user_menu_mapping
			JOIN m_menu ON m_menu.menu_id = m_user_menu_mapping.menu_id
			JOIN m_menu_category ON m_menu_category.menu_category_id = m_menu.menu_category_id
			WHERE m_user_menu_mapping.user_id = @pi_user_id
			AND m_user_menu_mapping.is_delete = 0
			AND m_menu.is_delete = '0'

		  ) b ON a.menu_id = b.menuid
		  WHERE is_exclude = 0 OR is_exclude IS NULL
	
)
GO
/****** Object:  UserDefinedFunction [dbo].[get_user_menu_all_excluded]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[get_user_menu_all_excluded]
(	
	@pi_user_id VARCHAR(3)
)
RETURNS TABLE 
AS
RETURN 
(
	
	  SELECT * FROM
		  (SELECT 
			DISTINCT
			m.menu_id, 
			m.menu_category_id,
			m.url,
			m.description AS menu_desc,
			m.menu_id_parent,
			c.description AS cat_desc
			--g.group_menu_id,
			--g.description AS group_desc
		  FROM m_menu m
		  JOIN m_group_menu_mapping p ON p.menu_id = m.menu_id
		  JOIN m_group_menu g ON g.group_menu_id = p.group_menu_id
		  JOIN m_user_group_menu_mapping mp ON mp.group_menu_id = p.group_menu_id
		  JOIN m_menu_category c ON c.menu_category_id = m.menu_category_id
		  WHERE m.is_delete = '0'
		  AND p.is_delete = '0'
		  AND g.is_delete = '0'
		  AND mp.user_id = @pi_user_id
		  AND mp.is_delete = 0) AS a 
		  FULL OUTER JOIN (
			SELECT 
				m_user_menu_mapping.user_id, 
				m_user_menu_mapping.menu_id AS menuid,
				m_menu_category.description AS catdesc, 
				m_menu.description AS menudesc,
				m_user_menu_mapping.is_exclude,
				m_user_menu_mapping.id AS mapping_id 
			FROM m_user_menu_mapping
			JOIN m_menu ON m_menu.menu_id = m_user_menu_mapping.menu_id
			JOIN m_menu_category ON m_menu_category.menu_category_id = m_menu.menu_category_id
			WHERE m_user_menu_mapping.user_id = @pi_user_id
			AND m_user_menu_mapping.is_delete = 0
			AND m_menu.is_delete = '0'

		  ) b ON a.menu_id = b.menuid
		  WHERE is_exclude = 1
	
)
GO
/****** Object:  UserDefinedFunction [dbo].[get_user_menu_all]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[get_user_menu_all]
(	
	@pi_user_id VARCHAR(3)
)
RETURNS TABLE 
AS
RETURN 
(
	
	  SELECT * FROM
		  (SELECT 
			DISTINCT
			m.menu_id, 
			m.menu_category_id,
			m.url,
			m.description AS menu_desc,
			m.menu_id_parent,
			mg.description AS parent_desc,
			c.description AS cat_desc
			--g.group_menu_id,
			--g.description AS group_desc
		  FROM m_menu m
		  JOIN m_group_menu_mapping p ON p.menu_id = m.menu_id
		  JOIN m_group_menu g ON g.group_menu_id = p.group_menu_id
		  JOIN m_user_group_menu_mapping mp ON mp.group_menu_id = p.group_menu_id
		  JOIN m_menu_category c ON c.menu_category_id = m.menu_category_id
		  LEFT JOIN m_menu mg ON m.menu_id_parent = mg.menu_id
		  WHERE m.is_delete = '0'
		  AND p.is_delete = '0'
		  AND g.is_delete = '0'
		  AND mp.user_id = @pi_user_id
		  AND mp.is_delete = 0) AS a 
		  FULL OUTER JOIN (
			SELECT 
				m_user_menu_mapping.user_id, 
				m_user_menu_mapping.menu_id AS menuid,
				m_menu_category.description AS catdesc, 
				m_menu.description AS menudesc,
				m_menu.menu_id_parent AS menuidparent,
				m2.description AS parentdesc,
				m_user_menu_mapping.is_exclude,
				m_user_menu_mapping.id AS mapping_id 
			FROM m_user_menu_mapping
			JOIN m_menu ON m_menu.menu_id = m_user_menu_mapping.menu_id
			JOIN m_menu_category ON m_menu_category.menu_category_id = m_menu.menu_category_id
			LEFT JOIN m_menu m2 ON m_menu.menu_id_parent = m2.menu_id
			WHERE m_user_menu_mapping.user_id = @pi_user_id
			AND m_user_menu_mapping.is_delete = 0
			AND m_menu.is_delete = '0'

		  ) b ON a.menu_id = b.menuid
	
)
GO
/****** Object:  UserDefinedFunction [dbo].[get_reason_by_action]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[get_reason_by_action] 
(	
	@pi_action_id BIGINT
)
RETURNS TABLE 
AS
RETURN 
(
	SELECT
		r.reason_id, 
		r.reason_category_id, 
		r.description, 
		r.status_id, 
		r.created_by, 
		r.created_date, 
		r.modified_by, 
		r.modified_date, 
		r.is_delete
	FROM m_reason r,
	(
		SELECT
			reason_category_id
		FROM m_workflow_template_action 
		WHERE workflow_template_action_id = @pi_action_id
		AND is_delete = 0
		AND is_need_reason = 1
	) a
	WHERE a.reason_category_id = r.reason_category_id
	AND r.is_delete = 0
)
GO
/****** Object:  UserDefinedFunction [dbo].[get_menu_for_add_no_category]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[get_menu_for_add_no_category]
(	
	@pi_user_id VARCHAR(3)
)
RETURNS TABLE 
AS
RETURN 
(

SELECT 
	mm.*, 
	c.description AS category_desc,
	mp.description AS parent_desc
FROM m_menu mm
JOIN m_menu_category c ON mm.menu_category_id = c.menu_category_id
LEFT JOIN m_menu mp ON mp.menu_id = mm.menu_id_parent
WHERE mm.is_delete = '0'
AND c.is_delete = '0'
AND mm.menu_id NOT IN
(
	SELECT COALESCE(menu_id, menuid) 
	FROM
		  (SELECT 
			DISTINCT
			m.menu_id 
		  FROM m_menu m
		  JOIN m_group_menu_mapping p ON p.menu_id = m.menu_id
		  JOIN m_group_menu g ON g.group_menu_id = p.group_menu_id
		  JOIN m_user_group_menu_mapping mp ON mp.group_menu_id = p.group_menu_id
		  JOIN m_menu_category c ON c.menu_category_id = m.menu_category_id
		  WHERE m.is_delete = '0'
		  AND p.is_delete = '0'
		  AND g.is_delete = '0'
		  AND mp.user_id = @pi_user_id
		  AND mp.is_delete = 0) AS a 
		  FULL OUTER JOIN (
			SELECT 
				m_user_menu_mapping.menu_id AS menuid
			FROM m_user_menu_mapping
			JOIN m_menu ON m_menu.menu_id = m_user_menu_mapping.menu_id
			JOIN m_menu_category ON m_menu_category.menu_category_id = m_menu.menu_category_id
			WHERE m_user_menu_mapping.user_id = @pi_user_id
			AND m_user_menu_mapping.is_delete = 0
			AND m_menu.is_delete = '0'

		  ) b ON a.menu_id = b.menuid

)

)
GO
/****** Object:  UserDefinedFunction [dbo].[get_menu_for_add]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[get_menu_for_add]
(	
	@pi_user_id VARCHAR(3),
	@pi_menu_category_id INT
)
RETURNS TABLE 
AS
RETURN 
(

SELECT 
	mm.*, 
	c.description AS category_desc,
	mp.description AS parent_desc
FROM m_menu mm
JOIN m_menu_category c ON mm.menu_category_id = c.menu_category_id
LEFT JOIN m_menu mp ON mp.menu_id = mm.menu_id_parent
WHERE mm.is_delete = '0'
AND c.is_delete = '0'
AND mm.menu_category_id = @pi_menu_category_id
AND mm.menu_id NOT IN
(
	SELECT COALESCE(menu_id, menuid) 
	FROM
		  (SELECT 
			DISTINCT
			m.menu_id 
		  FROM m_menu m
		  JOIN m_group_menu_mapping p ON p.menu_id = m.menu_id
		  JOIN m_group_menu g ON g.group_menu_id = p.group_menu_id
		  JOIN m_user_group_menu_mapping mp ON mp.group_menu_id = p.group_menu_id
		  JOIN m_menu_category c ON c.menu_category_id = m.menu_category_id
		  WHERE m.is_delete = '0'
		  AND p.is_delete = '0'
		  AND g.is_delete = '0'
		  AND mp.user_id = @pi_user_id
		  AND mp.is_delete = 0) AS a 
		  FULL OUTER JOIN (
			SELECT 
				m_user_menu_mapping.menu_id AS menuid
			FROM m_user_menu_mapping
			JOIN m_menu ON m_menu.menu_id = m_user_menu_mapping.menu_id
			JOIN m_menu_category ON m_menu_category.menu_category_id = m_menu.menu_category_id
			WHERE m_user_menu_mapping.user_id = @pi_user_id
			AND m_user_menu_mapping.is_delete = 0
			AND m_menu.is_delete = '0'

		  ) b ON a.menu_id = b.menuid

)

)
GO
/****** Object:  UserDefinedFunction [dbo].[get_menu_by_category_excl_group]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE FUNCTION [dbo].[get_menu_by_category_excl_group] 
(	
	@pi_group_menu_id INT,
	@pi_menu_category_id INT
)
RETURNS TABLE 
AS
RETURN 
(
	SELECT m.* FROM m_menu m
	WHERE m.menu_id NOT IN (
		SELECT g.menu_id FROM m_group_menu_mapping g
		WHERE g.group_menu_id = @pi_group_menu_id
		AND g.is_delete = '0'
	)
	AND m.menu_category_id = @pi_menu_category_id
)
GO
/****** Object:  UserDefinedFunction [dbo].[get_group_menu_member_user]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[get_group_menu_member_user]
(	
	@pi_group_menu_id INT,
	@pi_user_id VARCHAR(3)
)
RETURNS TABLE 
AS
RETURN 
(
	
	SELECT * FROM 
	(
	SELECT 
		mp.menu_id,
		mp.group_menu_id,
		m.description AS menu_desc,
		g.description AS group_desc
		FROM m_group_menu_mapping mp
		JOIN m_menu m ON m.menu_id = mp.menu_id
		JOIN m_group_menu g ON g.group_menu_id = mp.group_menu_id
		WHERE mp.is_delete = '0'
		AND m.is_delete = '0'
		AND g.is_delete = '0'
		AND mp.status_id = 5
		AND g.status_id = 5
		AND mp.group_menu_id = @pi_group_menu_id) AS tbl_a
	
	LEFT JOIN 
	(
		SELECT * FROM 
		(
			SELECT 
				mp.menu_id AS menu_id_2,
				mp.group_menu_id AS group_menu_id_2,
				m.description AS menu_desc_2,
				g.description AS group_desc_2
			FROM m_group_menu_mapping mp
			JOIN m_menu m ON m.menu_id = mp.menu_id
			JOIN m_group_menu g ON g.group_menu_id = mp.group_menu_id
			WHERE mp.is_delete = '0'
			AND m.is_delete = '0'
			AND g.is_delete = '0'
			AND mp.status_id = 5
			AND g.status_id = 5
			AND mp.group_menu_id = @pi_group_menu_id
		) AS gmenu,
		(
			SELECT
			menu_id AS user_menu_id,
			is_delete,
			is_exclude,
			user_id
			FROM
			m_user_menu_mapping
			WHERE is_delete = 0 AND user_id = @pi_user_id
		) AS umenu
	WHERE gmenu.menu_id_2 = umenu.user_menu_id
	) AS tbl_b ON tbl_b.menu_id_2 = tbl_a.menu_id
	
)
GO
/****** Object:  UserDefinedFunction [dbo].[func_list_branch]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- by Pribadi Pamungkas. 2017-08-07
CREATE FUNCTION [dbo].[func_list_branch]
(	
	@pi_branch_id VARCHAR(5)
)
RETURNS TABLE 
AS
RETURN 
(
  SELECT
  	*
  FROM
  	dbo.m_branch
  WHERE
    --branch_id = CAST((CASE @pi_branch_id WHEN '*' THEN '0' ELSE @pi_branch_id END) AS INT)
    branch_id = CAST(REPLACE(@pi_branch_id,'*','0') AS INT)
    OR @pi_branch_id = '*'
)
GO
/****** Object:  StoredProcedure [dbo].[AddUserMenuMapping]    Script Date: 11/10/2017 15:12:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[AddUserMenuMapping]
	@pi_menu_id INT,
	@pi_user_id VARCHAR(3),
	@pi_created_by VARCHAR(3)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    DECLARE @idexist INT;

	SELECT @idexist = COUNT(id) 
	FROM m_user_menu_mapping
	WHERE menu_id = @pi_menu_id
	AND user_id = @pi_user_id
	AND is_delete = 0;

	IF @idexist = 0
		
		BEGIN

			BEGIN TRANSACTION

				INSERT INTO m_user_menu_mapping
				(
					user_id,
					menu_id,
					is_exclude,
					created_by,
					created_date,
					modified_by,
					modified_date,
					is_delete
				)
				VALUES
				(
					@pi_user_id,
					@pi_menu_id,
					0,
					@pi_created_by,
					GETDATE(),
					@pi_created_by,
					GETDATE(),
					0
				);

				IF @@ERROR <> 0
					 BEGIN
						-- Rollback the transaction
						ROLLBACK

						-- Raise an error and return
						--RAISERROR ('Error in deleting employees in DeleteDepartment.', 16, 1)
						RETURN
					 END

				DECLARE @parents CURSOR;
				DECLARE @menu_id INT;
				DECLARE @parent_id INT;

				SET @parents = CURSOR FOR
				WITH Menus AS 
					( 
					--initialization 
						SELECT menu_id, menu_id_parent 
						FROM m_menu 
						WHERE menu_id = @pi_menu_id
						UNION ALL 
					--recursive execution 
						SELECT e.menu_id, e.menu_id_parent
						FROM m_menu e INNER JOIN Menus m  
						ON e.menu_id = m.menu_id_parent 
					)
			
				SELECT * FROM Menus;			
	
				OPEN @parents
					FETCH NEXT FROM @parents into @menu_id, @parent_id

						WHILE @@fetch_status = 0
			  
						  BEGIN
				  
							  IF @menu_id <> @pi_menu_id
					
								BEGIN

									DECLARE @pidexist INT;

									SELECT @pidexist = COUNT(id) 
									FROM m_user_menu_mapping
									WHERE menu_id = @menu_id
									AND user_id = @pi_user_id
									AND is_delete = 0;	

									IF @pidexist = 0
							
										INSERT INTO m_user_menu_mapping
										(
											user_id,
											menu_id,
											is_exclude,
											created_by,
											created_date,
											modified_by,
											modified_date,
											is_delete
										)
										VALUES
										(
											@pi_user_id,
											@menu_id,
											0,
											@pi_created_by,
											GETDATE(),
											@pi_created_by,
											GETDATE(),
											0
										);

										IF @@ERROR <> 0
											 BEGIN
												-- Rollback the transaction
												ROLLBACK

												-- Raise an error and return
												--RAISERROR ('Error in deleting employees in DeleteDepartment.', 16, 1)
												RETURN
											 END

								END

							  FETCH NEXT FROM @parents into @menu_id, @parent_id
						  END

			COMMIT
		END


END
GO
/****** Object:  StoredProcedure [dbo].[AddUserGroupMenuMapping]    Script Date: 11/10/2017 15:12:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[AddUserGroupMenuMapping]
	@pi_group_menu_id INT,
	@pi_user_id VARCHAR(3),
	@pi_created_by VARCHAR(3)
AS

BEGIN

DECLARE @groupcursor CURSOR;
DECLARE @m_menu_id int;

BEGIN TRANSACTION

INSERT INTO m_user_group_menu_mapping
(
	user_id,
	group_menu_id,
	created_by,
	created_date,
	modified_by,
	modified_date,
	is_delete
)
VALUES
(
	@pi_user_id,
	@pi_group_menu_id,
	@pi_created_by,
	GETDATE(),
	@pi_created_by,
	GETDATE(),
	0
);

IF @@ERROR <> 0
	BEGIN
						-- Rollback the transaction
		ROLLBACK

						-- Raise an error and return
						--RAISERROR ('Error in deleting employees in DeleteDepartment.', 16, 1)
		RETURN
	END

SET @groupcursor = CURSOR FOR SELECT menu_id FROM m_group_menu_mapping WHERE group_menu_id = @pi_group_menu_id AND is_delete=0;

OPEN @groupcursor
FETCH NEXT FROM @groupcursor into @m_menu_id

WHILE @@fetch_status = 0
	BEGIN

			DECLARE @parents CURSOR;
			DECLARE @c_menu_id INT;
			DECLARE @parent_id INT;

			SET @parents = CURSOR FOR
			WITH Menus AS 
				( 
				--initialization 
					SELECT menu_id, menu_id_parent 
					FROM m_menu 
					WHERE menu_id = @m_menu_id
					UNION ALL 
				--recursive execution 
					SELECT e.menu_id, e.menu_id_parent
					FROM m_menu e INNER JOIN Menus m  
					ON e.menu_id = m.menu_id_parent 
				) 
			
			SELECT * FROM Menus;			
	
			OPEN @parents
			FETCH NEXT FROM @parents into @c_menu_id, @parent_id

			WHILE @@fetch_status = 0
			  
			  BEGIN
				  
				  IF @c_menu_id <> @m_menu_id
					
					BEGIN

						DECLARE @pidexist INT;

						SELECT @pidexist = COUNT(id) 
						FROM m_user_menu_mapping
						WHERE menu_id = @c_menu_id
						AND user_id = @pi_user_id
						AND is_delete = 0;	

						IF @pidexist = 0
							
							BEGIN
							
								INSERT INTO m_user_menu_mapping
								(
									user_id,
									menu_id,
									is_exclude,
									created_by,
									created_date,
									modified_by,
									modified_date,
									is_delete
								)
								VALUES
								(
									@pi_user_id,
									@c_menu_id,
									0,
									@pi_created_by,
									GETDATE(),
									@pi_created_by,
									GETDATE(),
									0
								);

								IF @@ERROR <> 0
								 BEGIN
									-- Rollback the transaction
									ROLLBACK

									-- Raise an error and return
									--RAISERROR ('Error in deleting employees in DeleteDepartment.', 16, 1)
									RETURN
								 END
							
							END

					END

				  FETCH NEXT FROM @parents into @c_menu_id, @parent_id
			  END

	FETCH NEXT FROM @groupcursor into @m_menu_id;
	END

	COMMIT

END
GO
/****** Object:  StoredProcedure [dbo].[IncludeExcludeUserMenu]    Script Date: 11/10/2017 15:12:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[IncludeExcludeUserMenu] (
	@pi_user_id VARCHAR(3),
	@pi_menu_id INT,
	@pi_act VARCHAR(10),
	@pi_created_by VARCHAR(3)
)
AS
BEGIN

	SET NOCOUNT ON;

	DECLARE @cnt INT;

	SET @cnt = (
		SELECT 
			COUNT(id)
		FROM m_user_menu_mapping
		WHERE user_id = @pi_user_id
		AND menu_id = @pi_menu_id
		AND is_delete = 0
	);
	
	BEGIN TRANSACTION

	IF @cnt > 0
		
		BEGIN
			
			IF @pi_act = 'include'

				BEGIN

					UPDATE m_user_menu_mapping
					SET is_exclude = 0,
						modified_by = @pi_created_by,
						modified_date = GETDATE()
					WHERE user_id = @pi_user_id
					AND menu_id = @pi_menu_id;

					IF @@ERROR <> 0
					 BEGIN
						-- Rollback the transaction
						ROLLBACK

						-- Raise an error and return
						--RAISERROR ('Error in deleting employees in DeleteDepartment.', 16, 1)
						RETURN
					 END

					DECLARE @parents CURSOR;
					DECLARE @menu_id INT;
					DECLARE @parent_id INT;

					SET @parents = CURSOR FOR
					WITH Menus AS 
					( 
					--initialization 
						SELECT menu_id, menu_id_parent 
						FROM m_menu 
						WHERE menu_id = @pi_menu_id
						UNION ALL 
					--recursive execution 
						SELECT e.menu_id, e.menu_id_parent
						FROM m_menu e INNER JOIN Menus m  
						ON e.menu_id = m.menu_id_parent 
					) 
			
					SELECT * FROM Menus;			
	
					OPEN @parents
					FETCH NEXT FROM @parents into @menu_id, @parent_id

					WHILE @@fetch_status = 0
			  
						BEGIN
				  
							IF @menu_id <> @pi_menu_id
					
								BEGIN

									DECLARE @pidexist INT;

									SELECT @pidexist = COUNT(id) 
									FROM m_user_menu_mapping
									WHERE menu_id = @menu_id
									AND user_id = @pi_user_id
									AND is_delete = 0;	

									IF @pidexist = 0
							
										INSERT INTO m_user_menu_mapping
										(
											user_id,
											menu_id,
											is_exclude,
											created_by,
											created_date,
											modified_by,
											modified_date,
											is_delete
										)
										VALUES
										(
											@pi_user_id,
											@menu_id,
											0,
											@pi_created_by,
											GETDATE(),
											@pi_created_by,
											GETDATE(),
											0
										);

									ELSE

										UPDATE m_user_menu_mapping
										SET is_exclude = 0,
											modified_by = @pi_created_by,
											modified_date = GETDATE()
										WHERE user_id = @pi_user_id
										AND menu_id = @menu_id;
										
									IF @@ERROR <> 0
										 BEGIN
											-- Rollback the transaction
											ROLLBACK

											-- Raise an error and return
											--RAISERROR ('Error in deleting employees in DeleteDepartment.', 16, 1)
											RETURN
										 END
								END --END IF @menu_id <> @pi_menu_id

					  FETCH NEXT FROM @parents into @menu_id, @parent_id
				  END --END WHILE

				END --IF @pi_act = 'include'

			ELSE

				BEGIN
					
					UPDATE m_user_menu_mapping
					SET is_exclude = 1,
						modified_by = @pi_created_by,
						modified_date = GETDATE()
					WHERE user_id = @pi_user_id
					AND menu_id = @pi_menu_id;

					IF @@ERROR <> 0
					 BEGIN
						-- Rollback the transaction
						ROLLBACK

						-- Raise an error and return
						--RAISERROR ('Error in deleting employees in DeleteDepartment.', 16, 1)
						RETURN
					 END

				END
		END 

	ELSE

		BEGIN

			IF @pi_act = 'include'
				
				BEGIN
				
					INSERT INTO m_user_menu_mapping (
						user_id,
						menu_id,
						is_exclude,
						created_by,
						created_date,
						modified_by,
						modified_date
					)
					VALUES (
						@pi_user_id,
						@pi_menu_id,
						0,
						@pi_created_by,
						GETDATE(),
						@pi_created_by,
						GETDATE()
					);

					IF @@ERROR <> 0
					 BEGIN
						-- Rollback the transaction
						ROLLBACK

						-- Raise an error and return
						--RAISERROR ('Error in deleting employees in DeleteDepartment.', 16, 1)
						RETURN
					 END
					

					DECLARE @parents2 CURSOR;
					DECLARE @menu_id2 INT;
					DECLARE @parent_id2 INT;

					SET @parents = CURSOR FOR
					WITH Menus AS 
					( 
					--initialization 
						SELECT menu_id, menu_id_parent 
						FROM m_menu 
						WHERE menu_id = @pi_menu_id
						UNION ALL 
					--recursive execution 
						SELECT e.menu_id, e.menu_id_parent
						FROM m_menu e INNER JOIN Menus m  
						ON e.menu_id = m.menu_id_parent 
					) 
			
					SELECT * FROM Menus;			
	
					OPEN @parents2
					FETCH NEXT FROM @parents2 into @menu_id2, @parent_id2

					WHILE @@fetch_status = 0
			  
						BEGIN
				  
							IF @menu_id2 <> @pi_menu_id
					
								BEGIN

									DECLARE @pidexists INT;

									SELECT @pidexists = COUNT(id) 
									FROM m_user_menu_mapping
									WHERE menu_id = @menu_id2
									AND user_id = @pi_user_id
									AND is_delete = 0;	

									IF @pidexists = 0
							
										INSERT INTO m_user_menu_mapping
										(
											user_id,
											menu_id,
											is_exclude,
											created_by,
											created_date,
											modified_by,
											modified_date,
											is_delete
										)
										VALUES
										(
											@pi_user_id,
											@menu_id2,
											0,
											@pi_created_by,
											GETDATE(),
											@pi_created_by,
											GETDATE(),
											0
										);

									ELSE

										UPDATE m_user_menu_mapping
										SET is_exclude = 0,
											modified_by = @pi_created_by,
											modified_date = GETDATE()
										WHERE user_id = @pi_user_id
										AND menu_id = @menu_id2
										AND is_delete = 0;

									IF @@ERROR <> 0
									 BEGIN
										-- Rollback the transaction
										ROLLBACK

										-- Raise an error and return
										--RAISERROR ('Error in deleting employees in DeleteDepartment.', 16, 1)
										RETURN
									 END

								END --END IF @menu_id <> @pi_menu_id

					  FETCH NEXT FROM @parents2 into @menu_id2, @parent_id2
				  END --END WHILE
				
				END -- END IF @pi_act = 'include'

			ELSE

				BEGIN
				
					INSERT INTO m_user_menu_mapping (
						user_id,
						menu_id,
						is_exclude,
						created_by,
						created_date,
						modified_by,
						modified_date,
						is_delete
					)
					VALUES (
						@pi_user_id,
						@pi_menu_id,
						1,
						@pi_created_by,
						GETDATE(),
						@pi_created_by,
						GETDATE(),
						0
					);

					IF @@ERROR <> 0
						 BEGIN
										-- Rollback the transaction
							ROLLBACK

										-- Raise an error and return
										--RAISERROR ('Error in deleting employees in DeleteDepartment.', 16, 1)
							RETURN
						END			

				END
		
		END 

	COMMIT
	
END
GO
/****** Object:  Table [dbo].[m_eform_workflow]    Script Date: 11/10/2017 15:12:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_eform_workflow](
	[eform_workflow_id] [int] IDENTITY(1,1) NOT NULL,
	[eform_id] [int] NOT NULL,
	[workflow_template_id] [int] NOT NULL,
	[source_transaction_id] [smallint] NOT NULL,
	[effective_date] [date] NOT NULL,
	[status_id] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
 CONSTRAINT [PK__m_eform___0F44CEBC0C5BC11B] PRIMARY KEY CLUSTERED 
(
	[eform_workflow_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[m_eform_setting_detail]    Script Date: 11/10/2017 15:12:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[m_eform_setting_detail](
	[eform_setting_detail_id] [int] IDENTITY(1,1) NOT NULL,
	[eform_setting_id] [bigint] NOT NULL,
	[component_type_id] [bigint] NOT NULL,
	[label] [varchar](50) NULL,
	[row] [numeric](18, 0) NULL,
	[column_order] [numeric](18, 0) NULL,
	[width] [numeric](18, 0) NULL,
	[height] [numeric](18, 0) NULL,
	[data_type_id] [int] NULL,
	[source_data] [varchar](50) NULL,
	[saved_value] [varchar](50) NULL,
	[view_value] [varchar](50) NULL,
	[data_lenght] [varchar](50) NULL,
	[is_mandatory_field] [char](1) NOT NULL,
	[is_used_for_approval] [char](1) NOT NULL,
	[is_sub_question] [char](1) NOT NULL,
	[component_id_parent] [int] NOT NULL,
	[created_by] [varchar](8) NOT NULL,
	[created_date] [datetime] NOT NULL,
	[modified_by] [varchar](8) NOT NULL,
	[modified_date] [datetime] NOT NULL,
	[is_delete] [char](1) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[eform_setting_detail_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  StoredProcedure [dbo].[GetUserMenuForAdd]    Script Date: 11/10/2017 15:12:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetUserMenuForAdd] (
	@pi_user_id VARCHAR(3),
	@pi_pagenumber INT,
	@pi_rowsperpage INT,
	@pi_menu_category_id INT = NULL
)
AS
BEGIN
	
	IF @pi_menu_category_id IS NULL
		
		SELECT * FROM (
			SELECT ROW_NUMBER() OVER(ORDER BY menu_id) AS IdOrder,
				get_menu_for_add_no_category.* FROM get_menu_for_add_no_category(@pi_user_id) 
			) AS TBL 
		WHERE IdOrder BETWEEN ((@pi_pagenumber - 1) * @pi_rowsperpage + 1) AND (@pi_pagenumber * @pi_rowsperpage) ORDER BY description;

	ELSE
		
		SELECT * FROM (
			SELECT ROW_NUMBER() OVER(ORDER BY menu_id) AS IdOrder,
				get_menu_for_add.* FROM get_menu_for_add(@pi_user_id, @pi_menu_category_id) 
			) AS TBL 
		WHERE IdOrder BETWEEN ((@pi_pagenumber - 1) * @pi_rowsperpage + 1) AND (@pi_pagenumber * @pi_rowsperpage) ORDER BY description;
		

END
GO
/****** Object:  StoredProcedure [dbo].[GetUserMenuAll]    Script Date: 11/10/2017 15:12:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetUserMenuAll] (
	@pi_userid VARCHAR(3),
	@pi_pagenumber INT,
	@pi_rowsperpage INT
)
AS
BEGIN
SELECT * FROM (
                    SELECT ROW_NUMBER() OVER(ORDER BY menu_id) AS IdOrder,
                        get_user_menu_all.* FROM get_user_menu_all(@pi_userid) 
  
                ) AS TBL 
               WHERE IdOrder BETWEEN ((@pi_pagenumber - 1) * @pi_rowsperpage + 1) AND (@pi_pagenumber * @pi_rowsperpage) ORDER BY menu_desc;
END
GO
/****** Object:  UserDefinedFunction [dbo].[get_view_file_by_trans_type]    Script Date: 11/10/2017 15:12:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[get_view_file_by_trans_type]
(	
	@pi_transaction_type_id INT
)
RETURNS TABLE 
AS
RETURN 
(
	  
	SELECT TOP 1
		me.eform_id,
		meft.effective_date,
		meft.status_id,
		mtft.*
	FROM
		dbo.m_eform AS me INNER JOIN
		dbo.m_eform_form_template AS meft ON me.eform_id = meft.eform_id INNER JOIN
		dbo.m_transaction_form_template AS mtft ON meft.transaction_form_template_id = mtft.transaction_form_template_id
	WHERE
		meft.effective_date <= CAST(GETDATE() AS DATE)
		AND me.is_delete = '0'
		AND meft.is_delete = '0'
		AND mtft.is_delete = '0'
		AND me.transaction_type_id = @pi_transaction_type_id
		AND me.status_id = 5
		AND meft.status_id = 5
		-- tambahkan status aktif kalau perlu
	ORDER BY
		meft.effective_date DESC

	 
)
GO
/****** Object:  StoredProcedure [dbo].[UpdateUserActiveOrganization]    Script Date: 11/10/2017 15:12:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[UpdateUserActiveOrganization]
AS
BEGIN
	
	SET NOCOUNT ON;

	UPDATE m_user_organization
	SET status_id = 5
	WHERE user_organization_id IN (
		SELECT g.user_organization_id FROM m_user_organization g
		WHERE g.effective_date = CONVERT(DATE, GETDATE()) 
		AND g.status_id = 7
		AND g.is_delete = '0'
	)

	UPDATE m_user_organization
	SET status_id = 6
	WHERE user_organization_id IN (
		SELECT u.user_organization_id FROM m_user_organization u
		WHERE u.status_id = 5
		AND u.is_delete = '0'
		AND u.effective_date < CONVERT(DATE, GETDATE())
		AND u.user_id_organization IN (
			SELECT g.user_id_organization FROM m_user_organization g
			WHERE g.effective_date = CONVERT(DATE, GETDATE()) 
		)
	)

END
GO
/****** Object:  View [dbo].[v_user_organization]    Script Date: 11/10/2017 15:12:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_user_organization] AS

SELECT 
	  o.user_organization_id	
	  ,u.username
	  ,u.user_fullname
	  ,g.description AS organization_name	
      ,o.user_id_organization
      ,o.effective_date
      ,o.organization_id
	  ,g.level_type_id
	  ,t.description AS level_description
      ,o.status_id
      ,o.created_by
      ,o.created_date
      ,o.modified_by
      ,o.modified_date
      ,o.is_delete
  FROM m_user_organization o
  JOIN m_user u ON o.user_id_organization = u.username
  JOIN m_organization g ON o.organization_id = g.organization_id
  JOIN m_level_type t ON t.level_type_id = g.level_type_id
GO
/****** Object:  View [dbo].[v_m_transaction_type_2]    Script Date: 11/10/2017 15:12:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_m_transaction_type_2]
AS
    SELECT  mtt.transaction_type_id ,
            mtt.transaction_type_code ,
            mtt.description AS transaction_type_desc ,
            mtt.remarks AS transaction_type_remarks ,
            mtt.transaction_category_id ,
            (
				SELECT TOP 1
					mew.eform_workflow_id
				FROM
					dbo.m_eform AS me INNER JOIN
					dbo.m_eform_workflow AS mew ON me.eform_id = mew.eform_id INNER JOIN
					dbo.m_workflow_template AS mwt ON mew.workflow_template_id = mwt.workflow_template_id
				WHERE
					mew.effective_date <= CONVERT (date, GETDATE())
					AND me.is_delete = '0'
					AND mew.is_delete = '0'
					AND mwt.is_delete = '0'
					AND mew.status_id = 5
					AND me.transaction_type_id =  mtt.transaction_type_id
					AND mew.source_transaction_id = 2
					-- tambahkan status aktif kalau perlu
				ORDER BY
					mew.effective_date DESC

			) AS eform_workflow_id,
            --mtc.transaction_category_id ,
            mtc.transaction_category_code ,
            mtc.description AS transaction_category_desc,
            mtt.is_delete
    FROM    m_transaction_type mtt
            INNER JOIN m_transaction_category mtc ON mtc.transaction_category_id = mtt.transaction_category_id
    WHERE   mtt.is_delete = 0
            AND mtc.is_delete = 0
GO
/****** Object:  View [dbo].[v_m_transaction_type]    Script Date: 11/10/2017 15:12:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_m_transaction_type]
AS
    SELECT  mtt.transaction_type_id ,
            mtt.transaction_type_code ,
            mtt.description AS transaction_type_desc ,
            mtt.remarks AS transaction_type_remarks ,
            mtt.footnote_receipt_form AS transaction_type_footnote_receipt_form ,
            mtt.transaction_category_id ,
            (
				SELECT TOP 1
					mew.eform_workflow_id
				FROM
					dbo.m_eform AS me INNER JOIN
					dbo.m_eform_workflow AS mew ON me.eform_id = mew.eform_id INNER JOIN
					dbo.m_workflow_template AS mwt ON mew.workflow_template_id = mwt.workflow_template_id
				WHERE
					mew.effective_date <= CONVERT (date, GETDATE())
					AND me.is_delete = '0'
					AND mew.is_delete = '0'
					AND mwt.is_delete = '0'
					AND mew.status_id = 5
					AND me.transaction_type_id =  mtt.transaction_type_id
					AND mew.source_transaction_id = 1
					-- tambahkan status aktif kalau perlu
				ORDER BY
					mew.effective_date DESC

			) AS eform_workflow_id,
            --mtc.transaction_category_id ,
            mtc.transaction_category_code ,
            mtc.description AS transaction_category_desc,
            mtt.is_delete
    FROM    m_transaction_type mtt
            INNER JOIN m_transaction_category mtc ON mtc.transaction_category_id = mtt.transaction_category_id
    WHERE   mtt.is_delete = 0
            AND mtc.is_delete = 0
GO
/****** Object:  View [dbo].[v_m_group_pic_detail]    Script Date: 11/10/2017 15:12:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_m_group_pic_detail]
AS
    --SELECT
    --  gpd.group_pic_detail_id,
    --  gpd.group_pic_id,
    --  gpd.branch_id,
    --  bra.description AS branch_desc,
    --  gpd.transaction_type_id,
    --  trt.description AS transaction_desc,
    --  gpd.user_id_group_pic_detail,
    --  usr.user_fullname,
    --  gpd.is_delete
    --FROM
    --  dbo.m_group_pic_detail AS gpd
    --  INNER JOIN 
    --  dbo.m_group_pic AS gpi ON gpd.group_pic_id = gpi.group_pic_id
    --  INNER JOIN 
    --  dbo.m_branch AS bra ON gpd.branch_id = bra.branch_id
    --  INNER JOIN 
    --  dbo.m_transaction_type AS trt ON gpd.transaction_type_id = trt.transaction_type_id
    --  INNER JOIN 
    --  dbo.m_user AS usr ON gpd.user_id_group_pic_detail = usr.user_id
    --WHERE
    --  gpd.is_delete = '0'
    --  AND gpi.is_delete = '0'
    --  AND bra.is_delete = '0'
    --  AND trt.is_delete = '0'

    SELECT DISTINCT
      gpd.group_pic_detail_id,
      gpd.group_pic_id,
      gpd.branch_id,
      --flb.branch_id,
      --flb.description AS branch_desc,
      CASE gpd.branch_id
        WHEN '*' THEN 'All'
        ELSE flb.description
      END AS branch_desc,
      gpd.transaction_type_id,
      --fltt.transaction_type_id,
      --fltt.description AS transaction_desc,
      CASE gpd.transaction_type_id
        WHEN '*' THEN 'All'
        ELSE fltt.description
      END AS transaction_desc,
      gpd.user_id_group_pic_detail,
      usr.user_fullname,
      gpd.is_delete
    FROM
      dbo.m_group_pic_detail AS gpd
      INNER JOIN 
      dbo.m_group_pic AS gpi ON gpd.group_pic_id = gpi.group_pic_id
      CROSS APPLY
      dbo.func_list_branch(gpd.branch_id) AS flb
      CROSS APPLY
      dbo.func_list_transaction_type(gpd.transaction_type_id) AS fltt
      INNER JOIN 
      dbo.m_user AS usr ON gpd.user_id_group_pic_detail = usr.user_id
    WHERE
      gpd.is_delete = '0'
      AND gpi.is_delete = '0'
      AND flb.is_delete = '0'
      AND fltt.is_delete = '0'
GO
/****** Object:  View [dbo].[v_m_eform_workflow]    Script Date: 11/10/2017 15:12:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- by Pribadi Pamungkas. 2017-07-26
-- last modified by Pribadi Pamungkas. 2017-10-30
CREATE VIEW [dbo].[v_m_eform_workflow]
AS
    SELECT
  	  ewf.eform_workflow_id,
  	  ewf.eform_id,
  	  ewf.workflow_template_id,
  	  wft.workflow_template_code,
  	  wft.description,
	    wft.remarks,
	    ewf.source_transaction_id,
	    mst.description AS transaction_source,
  	  ewf.effective_date,
  	  ewf.status_id,
  	  ewf.created_by,
  	  ewf.created_date,
  	  ewf.modified_by,
  	  ewf.modified_date
    FROM
    	dbo.m_eform_workflow AS ewf INNER JOIN
    	dbo.m_workflow_template AS wft ON ewf.workflow_template_id = wft.workflow_template_id LEFT JOIN
    	dbo.m_source_transaction AS mst ON ewf.source_transaction_id = mst.source_transaction_id
    WHERE
      ewf.is_delete = '0'
      AND wft.is_delete = '0'
GO
/****** Object:  View [dbo].[v_m_eform_form]    Script Date: 11/10/2017 15:12:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- by Pribadi Pamungkas. 2017-08-29
CREATE VIEW [dbo].[v_m_eform_form]
AS
    SELECT
      me.eform_id,
      meft.eform_form_template_id,
      meft.effective_date,
      meft.status_id,
      mtft.transaction_form_template_id,
      mtft.description,
      mtft.filename
    FROM
	    dbo.m_eform AS me INNER JOIN
	    dbo.m_eform_form_template AS meft ON me.eform_id = meft.eform_id INNER JOIN
	    dbo.m_transaction_form_template AS mtft ON meft.transaction_form_template_id = mtft.transaction_form_template_id
    WHERE
      me.is_delete = '0'
      AND meft.is_delete = '0'
      AND mtft.is_delete = '0'
GO
/****** Object:  View [dbo].[v_m_eform_document]    Script Date: 11/10/2017 15:12:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- by Pribadi Pamungkas. 2017-07-23
CREATE VIEW [dbo].[v_m_eform_document]
AS
    SELECT
  	  med.eform_document_id,
  	  med.eform_id,
  	  med.document_id,
  	  md.document_code,
  	  md.description,
  	  med.minimum_files,
  	  med.maximum_files,
  	  med.seq_number,
  	  med.status_id,
  	  med.created_by,
  	  med.created_date,
  	  med.modified_by,
  	  med.modified_date,
  	  med.is_delete
    FROM
    	dbo.m_eform_document AS med INNER JOIN
    	dbo.m_document AS md ON med.document_id = md.document_id
    WHERE
      med.is_delete = '0'
      AND md.is_delete = '0'
GO
/****** Object:  View [dbo].[v_m_e_form]    Script Date: 11/10/2017 15:12:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- last modified by Pribadi Pamungkas. 2017-07-22
CREATE VIEW [dbo].[v_m_e_form]
AS
    SELECT  
      e.eform_id,
			e.eform_code, 
			e.transaction_type_id,
			mtt.transaction_type_desc,
			mtt.transaction_category_id,
			mtt.transaction_category_desc, 
			e.is_need_surat_kuasa, 
			e.remarks, 
			--e.transaction_form_template_id,
			--mtft.transaction_form_template_id,
			e.status_id, 
			e.is_delete
    FROM    
      dbo.m_eform AS e
      INNER JOIN 
      v_m_transaction_type AS mtt ON e.transaction_type_id = mtt.transaction_type_id
      --LEFT JOIN
      ----m_transaction_form_template AS tpl ON e.transaction_form_template_id = tpl.transaction_form_template_id
      
      --dbo.m_eform_form_template AS meft ON e.eform_id = meft.eform_id LEFT JOIN
	    --dbo.m_transaction_form_template AS mtft ON meft.transaction_form_template_id = mtft.transaction_form_template_id
    WHERE   
      e.is_delete = 0
GO
/****** Object:  View [dbo].[v_get_transaction_category_register]    Script Date: 11/10/2017 15:12:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_get_transaction_category_register] AS

SELECT
	me.eform_id,
	mtt.transaction_type_id,
	mtt.transaction_type_code,
	mtt.transaction_type_desc,
	mtt.transaction_category_id,
	mtt.transaction_category_code,
	mtt.transaction_category_desc,
	meft.effective_date,
	meft.status_id,
	mtft.*
FROM
	dbo.m_eform AS me INNER JOIN
	dbo.m_eform_form_template AS meft ON me.eform_id = meft.eform_id INNER JOIN
	dbo.m_transaction_form_template AS mtft ON meft.transaction_form_template_id = mtft.transaction_form_template_id INNER JOIN
	v_m_transaction_type AS mtt ON mtt.transaction_type_id = me.transaction_type_id
WHERE
	me.is_delete = '0'
	AND meft.is_delete = '0'
	AND mtft.is_delete = '0'
	AND me.status_id = 5
	AND meft.effective_date <= CONVERT (date, GETDATE())
GO
/****** Object:  DdlTrigger [tri_DDLEvents]    Script Date: 11/10/2017 15:12:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [tri_DDLEvents]
    ON DATABASE
    FOR CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE,
        ALTER_SCHEMA, RENAME, CREATE_TABLE, ALTER_TABLE,DROP_TABLE,CREATE_VIEW, ALTER_VIEW, DROP_VIEW
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE
        @EventData XML = EVENTDATA();
 
    DECLARE 
        @ip VARCHAR(32) =
        (
            SELECT client_net_address
                FROM sys.dm_exec_connections
                WHERE session_id = @@SPID
        );
 
    INSERT log_DDLEvents
    (
        EventType,
        EventDDL,
        EventXML,
        DatabaseName,
        SchemaName,
        ObjectName,
        HostName,
        IPAddress,
        ProgramName,
        LoginName
    )
    SELECT
        @EventData.value('(/EVENT_INSTANCE/EventType)[1]',   'NVARCHAR(100)'), 
        @EventData.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'NVARCHAR(MAX)'),
        @EventData,
        DB_NAME(),
        @EventData.value('(/EVENT_INSTANCE/SchemaName)[1]',  'NVARCHAR(255)'), 
        @EventData.value('(/EVENT_INSTANCE/ObjectName)[1]',  'NVARCHAR(255)'),
        HOST_NAME(),
        @ip,
        PROGRAM_NAME(),
        SUSER_SNAME();
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
DISABLE TRIGGER [tri_DDLEvents] ON DATABASE
GO
/****** Object:  Default [DF_t_transaction_history_process_is_delete]    Script Date: 11/10/2017 15:12:24 ******/
ALTER TABLE [dbo].[t_transaction_history_process] ADD  CONSTRAINT [DF_t_transaction_history_process_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_t_transaction_document_receive_doc_check]    Script Date: 11/10/2017 15:12:25 ******/
ALTER TABLE [dbo].[t_transaction_document] ADD  CONSTRAINT [DF_t_transaction_document_receive_doc_check]  DEFAULT ((0)) FOR [receive_doc_check]
GO
/****** Object:  Default [DF_t_transaction_transaction_date]    Script Date: 11/10/2017 15:12:26 ******/
ALTER TABLE [dbo].[t_transaction] ADD  CONSTRAINT [DF_t_transaction_transaction_date]  DEFAULT (getdate()) FOR [transaction_date]
GO
/****** Object:  Default [DF_t_transaction_source_app_id]    Script Date: 11/10/2017 15:12:26 ******/
ALTER TABLE [dbo].[t_transaction] ADD  CONSTRAINT [DF_t_transaction_source_app_id]  DEFAULT ((1)) FOR [source_app_id]
GO
/****** Object:  Default [DF_t_transaction_is_policy_holder]    Script Date: 11/10/2017 15:12:26 ******/
ALTER TABLE [dbo].[t_transaction] ADD  CONSTRAINT [DF_t_transaction_is_policy_holder]  DEFAULT ((1)) FOR [is_policy_holder]
GO
/****** Object:  Default [DF_t_transaction_is_notify_document_completion]    Script Date: 11/10/2017 15:12:26 ******/
ALTER TABLE [dbo].[t_transaction] ADD  CONSTRAINT [DF_t_transaction_is_notify_document_completion]  DEFAULT ((1)) FOR [is_notify_document_completion]
GO
/****** Object:  Default [DF_t_transaction_source_transaction_id]    Script Date: 11/10/2017 15:12:26 ******/
ALTER TABLE [dbo].[t_transaction] ADD  CONSTRAINT [DF_t_transaction_source_transaction_id]  DEFAULT ((1)) FOR [source_transaction_id]
GO
/****** Object:  Default [DF_t_transaction_is_delete]    Script Date: 11/10/2017 15:12:26 ******/
ALTER TABLE [dbo].[t_transaction] ADD  CONSTRAINT [DF_t_transaction_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_m_branch_is_delete]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_branch] ADD  CONSTRAINT [DF_m_branch_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_m_user_is_delete]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_user] ADD  CONSTRAINT [DF_m_user_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_m_transaction_task_status_is_delete]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_transaction_task_status] ADD  CONSTRAINT [DF_m_transaction_task_status_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_t_transaction_task_is_delete]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[t_transaction_task] ADD  CONSTRAINT [DF_t_transaction_task_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_m_workflow_template_stage_group_pic_id]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_workflow_template_stage] ADD  CONSTRAINT [DF_m_workflow_template_stage_group_pic_id]  DEFAULT (NULL) FOR [group_pic_id]
GO
/****** Object:  Default [DF_m_workflow_template_stage_user_workflow_id]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_workflow_template_stage] ADD  CONSTRAINT [DF_m_workflow_template_stage_user_workflow_id]  DEFAULT (NULL) FOR [user_workflow_id]
GO
/****** Object:  Default [DF_m_source_transaction_modified_date]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_source_transaction] ADD  CONSTRAINT [DF_m_source_transaction_modified_date]  DEFAULT (getdate()) FOR [modified_date]
GO
/****** Object:  Default [DF_m_source_transaction_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_source_transaction] ADD  CONSTRAINT [DF_m_source_transaction_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_m_source_of_funds_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_source_of_funds] ADD  CONSTRAINT [DF_m_source_of_funds_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_t_transaction_log_source_transaction_id]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[t_transaction_log] ADD  CONSTRAINT [DF_t_transaction_log_source_transaction_id]  DEFAULT ((1)) FOR [source_transaction_id]
GO
/****** Object:  Default [DF_user_menu_access_is_exclude]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[user_menu_access] ADD  CONSTRAINT [DF_user_menu_access_is_exclude]  DEFAULT ((0)) FOR [is_exclude]
GO
/****** Object:  Default [DF_user_menu_access_created_date]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[user_menu_access] ADD  CONSTRAINT [DF_user_menu_access_created_date]  DEFAULT (getdate()) FOR [created_date]
GO
/****** Object:  Default [DF_user_menu_access_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[user_menu_access] ADD  CONSTRAINT [DF_user_menu_access_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_s_log_date]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[s_log] ADD  CONSTRAINT [DF_s_log_date]  DEFAULT (getdate()) FOR [date]
GO
/****** Object:  Default [DF_t_transaction_form_log_created_date]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[t_transaction_form_log] ADD  CONSTRAINT [DF_t_transaction_form_log_created_date]  DEFAULT (getdate()) FOR [created_date]
GO
/****** Object:  Default [DF_t_transaction_form_log_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[t_transaction_form_log] ADD  CONSTRAINT [DF_t_transaction_form_log_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_m_alteration_type_created_date]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_alteration_type] ADD  CONSTRAINT [DF_m_alteration_type_created_date]  DEFAULT (getdate()) FOR [created_date]
GO
/****** Object:  Default [DF_m_alteration_type_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_alteration_type] ADD  CONSTRAINT [DF_m_alteration_type_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF__log_DDLEv__Event__7913E27D]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[log_DDLEvents] ADD  DEFAULT (getdate()) FOR [EventDate]
GO
/****** Object:  Default [DF_m_gross_income_year_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_gross_income_year] ADD  CONSTRAINT [DF_m_gross_income_year_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_m_gross_income_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_gross_income] ADD  CONSTRAINT [DF_m_gross_income_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_m_user_group_menu_mapping_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_user_group_menu_mapping] ADD  CONSTRAINT [DF_m_user_group_menu_mapping_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_m_reinstatement_type_created_date]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_reinstatement_type] ADD  CONSTRAINT [DF_m_reinstatement_type_created_date]  DEFAULT (getdate()) FOR [created_date]
GO
/****** Object:  Default [DF_m_reinstatement_type_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_reinstatement_type] ADD  CONSTRAINT [DF_m_reinstatement_type_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_m_transaction_form_template_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_transaction_form_template] ADD  CONSTRAINT [DF_m_transaction_form_template_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_m_status_created_date]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_status] ADD  CONSTRAINT [DF_m_status_created_date]  DEFAULT (getdate()) FOR [created_date]
GO
/****** Object:  Default [DF_m_menu_category_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_menu_category] ADD  CONSTRAINT [DF_m_menu_category_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_t_transaction_form_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[t_transaction_form] ADD  CONSTRAINT [DF_t_transaction_form_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_s_action_list_datasource]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[s_action_list] ADD  CONSTRAINT [DF_s_action_list_datasource]  DEFAULT ('default') FOR [datasource]
GO
/****** Object:  Default [DF_s_action_list_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[s_action_list] ADD  CONSTRAINT [DF_s_action_list_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_m_workflow_template_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_workflow_template] ADD  CONSTRAINT [DF_m_workflow_template_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_t_transaction_history_workflow_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[t_transaction_history_workflow] ADD  CONSTRAINT [DF_t_transaction_history_workflow_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_m_currency_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_currency] ADD  CONSTRAINT [DF_m_currency_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_m_menu_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_menu] ADD  CONSTRAINT [DF_m_menu_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_m_group_menu_created_date]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_group_menu] ADD  CONSTRAINT [DF_m_group_menu_created_date]  DEFAULT (getdate()) FOR [created_date]
GO
/****** Object:  Default [DF_m_group_menu_is_delete]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_group_menu] ADD  CONSTRAINT [DF_m_group_menu_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  Default [DF_m_user_menu_mapping_is_exclude]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_user_menu_mapping] ADD  CONSTRAINT [DF_m_user_menu_mapping_is_exclude]  DEFAULT ((0)) FOR [is_exclude]
GO
/****** Object:  Default [DF_m_user_menu_mapping_is_delete]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_user_menu_mapping] ADD  CONSTRAINT [DF_m_user_menu_mapping_is_delete]  DEFAULT ((0)) FOR [is_delete]
GO
/****** Object:  ForeignKey [FK_t_transaction_history_process_reason]    Script Date: 11/10/2017 15:12:24 ******/
ALTER TABLE [dbo].[t_transaction_history_process]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_history_process_reason] FOREIGN KEY([reason_id])
REFERENCES [dbo].[m_reason] ([reason_id])
GO
ALTER TABLE [dbo].[t_transaction_history_process] CHECK CONSTRAINT [FK_t_transaction_history_process_reason]
GO
/****** Object:  ForeignKey [FK_t_transaction_history_process_transaction]    Script Date: 11/10/2017 15:12:24 ******/
ALTER TABLE [dbo].[t_transaction_history_process]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_history_process_transaction] FOREIGN KEY([transaction_id])
REFERENCES [dbo].[t_transaction] ([transaction_id])
GO
ALTER TABLE [dbo].[t_transaction_history_process] CHECK CONSTRAINT [FK_t_transaction_history_process_transaction]
GO
/****** Object:  ForeignKey [FK_t_transaction_document_transaction]    Script Date: 11/10/2017 15:12:25 ******/
ALTER TABLE [dbo].[t_transaction_document]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_document_transaction] FOREIGN KEY([transaction_id])
REFERENCES [dbo].[t_transaction] ([transaction_id])
GO
ALTER TABLE [dbo].[t_transaction_document] CHECK CONSTRAINT [FK_t_transaction_document_transaction]
GO
/****** Object:  ForeignKey [FK_t_transaction_document_file_transaction_document]    Script Date: 11/10/2017 15:12:25 ******/
ALTER TABLE [dbo].[t_transaction_document_file]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_document_file_transaction_document] FOREIGN KEY([transaction_document_id])
REFERENCES [dbo].[t_transaction_document] ([transaction_document_id])
GO
ALTER TABLE [dbo].[t_transaction_document_file] CHECK CONSTRAINT [FK_t_transaction_document_file_transaction_document]
GO
/****** Object:  ForeignKey [FK_t_transaction_currency]    Script Date: 11/10/2017 15:12:26 ******/
ALTER TABLE [dbo].[t_transaction]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_currency] FOREIGN KEY([currency_id])
REFERENCES [dbo].[m_currency] ([currency_id])
GO
ALTER TABLE [dbo].[t_transaction] CHECK CONSTRAINT [FK_t_transaction_currency]
GO
/****** Object:  ForeignKey [FK_t_transaction_m_user]    Script Date: 11/10/2017 15:12:26 ******/
ALTER TABLE [dbo].[t_transaction]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_m_user] FOREIGN KEY([transaction_id])
REFERENCES [dbo].[t_transaction] ([transaction_id])
GO
ALTER TABLE [dbo].[t_transaction] CHECK CONSTRAINT [FK_t_transaction_m_user]
GO
/****** Object:  ForeignKey [FK_t_transaction_source_app]    Script Date: 11/10/2017 15:12:26 ******/
ALTER TABLE [dbo].[t_transaction]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_source_app] FOREIGN KEY([source_app_id])
REFERENCES [dbo].[m_source_app] ([source_app_id])
GO
ALTER TABLE [dbo].[t_transaction] CHECK CONSTRAINT [FK_t_transaction_source_app]
GO
/****** Object:  ForeignKey [FK_t_transaction_status]    Script Date: 11/10/2017 15:12:26 ******/
ALTER TABLE [dbo].[t_transaction]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_transaction_status] ([transaction_status_id])
GO
ALTER TABLE [dbo].[t_transaction] CHECK CONSTRAINT [FK_t_transaction_status]
GO
/****** Object:  ForeignKey [FK_t_transaction_transaction_type]    Script Date: 11/10/2017 15:12:26 ******/
ALTER TABLE [dbo].[t_transaction]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_transaction_type] FOREIGN KEY([transaction_type_id])
REFERENCES [dbo].[m_transaction_type] ([transaction_type_id])
GO
ALTER TABLE [dbo].[t_transaction] CHECK CONSTRAINT [FK_t_transaction_transaction_type]
GO
/****** Object:  ForeignKey [FK_t_transaction_workflow_template_stage]    Script Date: 11/10/2017 15:12:26 ******/
ALTER TABLE [dbo].[t_transaction]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_workflow_template_stage] FOREIGN KEY([workflow_template_stage_id])
REFERENCES [dbo].[m_workflow_template_stage] ([workflow_template_stage_id])
GO
ALTER TABLE [dbo].[t_transaction] CHECK CONSTRAINT [FK_t_transaction_workflow_template_stage]
GO
/****** Object:  ForeignKey [FK_m_user_organization_organization]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_user_organization]  WITH CHECK ADD  CONSTRAINT [FK_m_user_organization_organization] FOREIGN KEY([organization_id])
REFERENCES [dbo].[m_organization] ([organization_id])
GO
ALTER TABLE [dbo].[m_user_organization] CHECK CONSTRAINT [FK_m_user_organization_organization]
GO
/****** Object:  ForeignKey [FK_m_user_organization_status]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_user_organization]  WITH CHECK ADD  CONSTRAINT [FK_m_user_organization_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_user_organization] CHECK CONSTRAINT [FK_m_user_organization_status]
GO
/****** Object:  ForeignKey [FK_m_branch_city]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_branch]  WITH CHECK ADD  CONSTRAINT [FK_m_branch_city] FOREIGN KEY([city_id])
REFERENCES [dbo].[m_city] ([city_id])
GO
ALTER TABLE [dbo].[m_branch] CHECK CONSTRAINT [FK_m_branch_city]
GO
/****** Object:  ForeignKey [FK_m_branch_province]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_branch]  WITH CHECK ADD  CONSTRAINT [FK_m_branch_province] FOREIGN KEY([province_id])
REFERENCES [dbo].[m_province] ([province_id])
GO
ALTER TABLE [dbo].[m_branch] CHECK CONSTRAINT [FK_m_branch_province]
GO
/****** Object:  ForeignKey [FK_m_branch_status]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_branch]  WITH CHECK ADD  CONSTRAINT [FK_m_branch_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_branch] CHECK CONSTRAINT [FK_m_branch_status]
GO
/****** Object:  ForeignKey [FK_m_organization_level_type]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_organization]  WITH CHECK ADD  CONSTRAINT [FK_m_organization_level_type] FOREIGN KEY([level_type_id])
REFERENCES [dbo].[m_level_type] ([level_type_id])
GO
ALTER TABLE [dbo].[m_organization] CHECK CONSTRAINT [FK_m_organization_level_type]
GO
/****** Object:  ForeignKey [FK_m_organization_m_organization]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_organization]  WITH CHECK ADD  CONSTRAINT [FK_m_organization_m_organization] FOREIGN KEY([organization_id_parent])
REFERENCES [dbo].[m_organization] ([organization_id])
GO
ALTER TABLE [dbo].[m_organization] CHECK CONSTRAINT [FK_m_organization_m_organization]
GO
/****** Object:  ForeignKey [FK_m_organization_status]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_organization]  WITH CHECK ADD  CONSTRAINT [FK_m_organization_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_organization] CHECK CONSTRAINT [FK_m_organization_status]
GO
/****** Object:  ForeignKey [FK_m_user_status]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_user]  WITH CHECK ADD  CONSTRAINT [FK_m_user_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_user] CHECK CONSTRAINT [FK_m_user_status]
GO
/****** Object:  ForeignKey [FK_t_transaction_task_status]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[t_transaction_task]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_task_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_transaction_task_status] ([id])
GO
ALTER TABLE [dbo].[t_transaction_task] CHECK CONSTRAINT [FK_t_transaction_task_status]
GO
/****** Object:  ForeignKey [FK_t_transaction_task_transaction]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[t_transaction_task]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_task_transaction] FOREIGN KEY([transaction_id])
REFERENCES [dbo].[t_transaction] ([transaction_id])
GO
ALTER TABLE [dbo].[t_transaction_task] CHECK CONSTRAINT [FK_t_transaction_task_transaction]
GO
/****** Object:  ForeignKey [FK_m_transaction_type_transaction_category]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_transaction_type]  WITH CHECK ADD  CONSTRAINT [FK_m_transaction_type_transaction_category] FOREIGN KEY([transaction_category_id])
REFERENCES [dbo].[m_transaction_category] ([transaction_category_id])
GO
ALTER TABLE [dbo].[m_transaction_type] CHECK CONSTRAINT [FK_m_transaction_type_transaction_category]
GO
/****** Object:  ForeignKey [FK_m_workflow_template_stage_m_group_pic]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_workflow_template_stage]  WITH CHECK ADD  CONSTRAINT [FK_m_workflow_template_stage_m_group_pic] FOREIGN KEY([group_pic_id])
REFERENCES [dbo].[m_group_pic] ([group_pic_id])
GO
ALTER TABLE [dbo].[m_workflow_template_stage] CHECK CONSTRAINT [FK_m_workflow_template_stage_m_group_pic]
GO
/****** Object:  ForeignKey [FK_m_eform_document_document]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_eform_document]  WITH CHECK ADD  CONSTRAINT [FK_m_eform_document_document] FOREIGN KEY([document_id])
REFERENCES [dbo].[m_document] ([document_id])
GO
ALTER TABLE [dbo].[m_eform_document] CHECK CONSTRAINT [FK_m_eform_document_document]
GO
/****** Object:  ForeignKey [FK_m_eform_document_eform]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_eform_document]  WITH CHECK ADD  CONSTRAINT [FK_m_eform_document_eform] FOREIGN KEY([eform_id])
REFERENCES [dbo].[m_eform] ([eform_id])
GO
ALTER TABLE [dbo].[m_eform_document] CHECK CONSTRAINT [FK_m_eform_document_eform]
GO
/****** Object:  ForeignKey [FK_m_eform_document_status]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_eform_document]  WITH CHECK ADD  CONSTRAINT [FK_m_eform_document_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_eform_document] CHECK CONSTRAINT [FK_m_eform_document_status]
GO
/****** Object:  ForeignKey [FK_m_eform_status]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_eform]  WITH CHECK ADD  CONSTRAINT [FK_m_eform_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_eform] CHECK CONSTRAINT [FK_m_eform_status]
GO
/****** Object:  ForeignKey [FK_m_eform_transaction_type]    Script Date: 11/10/2017 15:12:28 ******/
ALTER TABLE [dbo].[m_eform]  WITH CHECK ADD  CONSTRAINT [FK_m_eform_transaction_type] FOREIGN KEY([transaction_type_id])
REFERENCES [dbo].[m_transaction_type] ([transaction_type_id])
GO
ALTER TABLE [dbo].[m_eform] CHECK CONSTRAINT [FK_m_eform_transaction_type]
GO
/****** Object:  ForeignKey [FK_m_group_pic_m_group_pic]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_group_pic]  WITH CHECK ADD  CONSTRAINT [FK_m_group_pic_m_group_pic] FOREIGN KEY([group_pic_id])
REFERENCES [dbo].[m_group_pic] ([group_pic_id])
GO
ALTER TABLE [dbo].[m_group_pic] CHECK CONSTRAINT [FK_m_group_pic_m_group_pic]
GO
/****** Object:  ForeignKey [FK_m_workflow_template_status]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_workflow_template]  WITH CHECK ADD  CONSTRAINT [FK_m_workflow_template_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_workflow_template] CHECK CONSTRAINT [FK_m_workflow_template_status]
GO
/****** Object:  ForeignKey [FK_t_transaction_history_workflow_transaction]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[t_transaction_history_workflow]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_history_workflow_transaction] FOREIGN KEY([transaction_id])
REFERENCES [dbo].[t_transaction] ([transaction_id])
GO
ALTER TABLE [dbo].[t_transaction_history_workflow] CHECK CONSTRAINT [FK_t_transaction_history_workflow_transaction]
GO
/****** Object:  ForeignKey [FK_t_transaction_history_workflow_workflow_template_stage]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[t_transaction_history_workflow]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_history_workflow_workflow_template_stage] FOREIGN KEY([workflow_template_stage_id])
REFERENCES [dbo].[m_workflow_template_stage] ([workflow_template_stage_id])
GO
ALTER TABLE [dbo].[t_transaction_history_workflow] CHECK CONSTRAINT [FK_t_transaction_history_workflow_workflow_template_stage]
GO
/****** Object:  ForeignKey [FK_m_document_type_status]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_document_type]  WITH CHECK ADD  CONSTRAINT [FK_m_document_type_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_document_type] CHECK CONSTRAINT [FK_m_document_type_status]
GO
/****** Object:  ForeignKey [FK_m_data_type_status]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_data_type]  WITH CHECK ADD  CONSTRAINT [FK_m_data_type_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_data_type] CHECK CONSTRAINT [FK_m_data_type_status]
GO
/****** Object:  ForeignKey [FK_m_currency_status]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_currency]  WITH CHECK ADD  CONSTRAINT [FK_m_currency_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_currency] CHECK CONSTRAINT [FK_m_currency_status]
GO
/****** Object:  ForeignKey [FK_m_city_province]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_city]  WITH CHECK ADD  CONSTRAINT [FK_m_city_province] FOREIGN KEY([province_id])
REFERENCES [dbo].[m_province] ([province_id])
GO
ALTER TABLE [dbo].[m_city] CHECK CONSTRAINT [FK_m_city_province]
GO
/****** Object:  ForeignKey [FK_m_approv_matrix_status]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_approv_matrix]  WITH CHECK ADD  CONSTRAINT [FK_m_approv_matrix_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_approv_matrix] CHECK CONSTRAINT [FK_m_approv_matrix_status]
GO
/****** Object:  ForeignKey [FK_m_menu_m_menu]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_menu]  WITH CHECK ADD  CONSTRAINT [FK_m_menu_m_menu] FOREIGN KEY([menu_id_parent])
REFERENCES [dbo].[m_menu] ([menu_id])
GO
ALTER TABLE [dbo].[m_menu] CHECK CONSTRAINT [FK_m_menu_m_menu]
GO
/****** Object:  ForeignKey [FK_m_menu_m_menu_category]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_menu]  WITH CHECK ADD  CONSTRAINT [FK_m_menu_m_menu_category] FOREIGN KEY([menu_category_id])
REFERENCES [dbo].[m_menu_category] ([menu_category_id])
GO
ALTER TABLE [dbo].[m_menu] CHECK CONSTRAINT [FK_m_menu_m_menu_category]
GO
/****** Object:  ForeignKey [FK_m_level_type_status]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_level_type]  WITH CHECK ADD  CONSTRAINT [FK_m_level_type_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_level_type] CHECK CONSTRAINT [FK_m_level_type_status]
GO
/****** Object:  ForeignKey [FK_m_group_pic_detail_group_pic]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_group_pic_detail]  WITH CHECK ADD  CONSTRAINT [FK_m_group_pic_detail_group_pic] FOREIGN KEY([group_pic_id])
REFERENCES [dbo].[m_group_pic] ([group_pic_id])
GO
ALTER TABLE [dbo].[m_group_pic_detail] CHECK CONSTRAINT [FK_m_group_pic_detail_group_pic]
GO
/****** Object:  ForeignKey [FK_m_group_menu_status]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[m_group_menu]  WITH CHECK ADD  CONSTRAINT [FK_m_group_menu_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_group_menu] CHECK CONSTRAINT [FK_m_group_menu_status]
GO
/****** Object:  ForeignKey [FK_t_transaction_document_file_history_transaction_document_file]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[t_transaction_document_file_history]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_document_file_history_transaction_document_file] FOREIGN KEY([transaction_document_file_id])
REFERENCES [dbo].[t_transaction_document_file] ([transaction_document_file_id])
GO
ALTER TABLE [dbo].[t_transaction_document_file_history] CHECK CONSTRAINT [FK_t_transaction_document_file_history_transaction_document_file]
GO
/****** Object:  ForeignKey [FK_t_transaction_document_file_history_user]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[t_transaction_document_file_history]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_document_file_history_user] FOREIGN KEY([user_id])
REFERENCES [dbo].[m_user] ([user_id])
GO
ALTER TABLE [dbo].[t_transaction_document_file_history] CHECK CONSTRAINT [FK_t_transaction_document_file_history_user]
GO
/****** Object:  ForeignKey [FK_t_transaction_document_file_history_workflow_template_stage]    Script Date: 11/10/2017 15:12:29 ******/
ALTER TABLE [dbo].[t_transaction_document_file_history]  WITH CHECK ADD  CONSTRAINT [FK_t_transaction_document_file_history_workflow_template_stage] FOREIGN KEY([workflow_template_stage_id])
REFERENCES [dbo].[m_workflow_template_stage] ([workflow_template_stage_id])
GO
ALTER TABLE [dbo].[t_transaction_document_file_history] CHECK CONSTRAINT [FK_t_transaction_document_file_history_workflow_template_stage]
GO
/****** Object:  ForeignKey [FK_m_reason_category_status]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_reason_category]  WITH CHECK ADD  CONSTRAINT [FK_m_reason_category_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_reason_category] CHECK CONSTRAINT [FK_m_reason_category_status]
GO
/****** Object:  ForeignKey [FK_m_reason_reason_category]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_reason]  WITH CHECK ADD  CONSTRAINT [FK_m_reason_reason_category] FOREIGN KEY([reason_category_id])
REFERENCES [dbo].[m_reason_category] ([reason_category_id])
GO
ALTER TABLE [dbo].[m_reason] CHECK CONSTRAINT [FK_m_reason_reason_category]
GO
/****** Object:  ForeignKey [FK_m_reason_status]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_reason]  WITH CHECK ADD  CONSTRAINT [FK_m_reason_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_reason] CHECK CONSTRAINT [FK_m_reason_status]
GO
/****** Object:  ForeignKey [FK_m_group_menu_mapping_group_menu]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_group_menu_mapping]  WITH CHECK ADD  CONSTRAINT [FK_m_group_menu_mapping_group_menu] FOREIGN KEY([group_menu_id])
REFERENCES [dbo].[m_group_menu] ([group_menu_id])
GO
ALTER TABLE [dbo].[m_group_menu_mapping] CHECK CONSTRAINT [FK_m_group_menu_mapping_group_menu]
GO
/****** Object:  ForeignKey [FK_m_group_menu_mapping_menu]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_group_menu_mapping]  WITH CHECK ADD  CONSTRAINT [FK_m_group_menu_mapping_menu] FOREIGN KEY([menu_id])
REFERENCES [dbo].[m_menu] ([menu_id])
GO
ALTER TABLE [dbo].[m_group_menu_mapping] CHECK CONSTRAINT [FK_m_group_menu_mapping_menu]
GO
/****** Object:  ForeignKey [FK_m_group_menu_mapping_status]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_group_menu_mapping]  WITH CHECK ADD  CONSTRAINT [FK_m_group_menu_mapping_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_group_menu_mapping] CHECK CONSTRAINT [FK_m_group_menu_mapping_status]
GO
/****** Object:  ForeignKey [FK_m_approv_matrix_detail_approv_matrix]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_approv_matrix_detail]  WITH CHECK ADD  CONSTRAINT [FK_m_approv_matrix_detail_approv_matrix] FOREIGN KEY([approv_matrix_id])
REFERENCES [dbo].[m_approv_matrix] ([approv_matrix_id])
GO
ALTER TABLE [dbo].[m_approv_matrix_detail] CHECK CONSTRAINT [FK_m_approv_matrix_detail_approv_matrix]
GO
/****** Object:  ForeignKey [FK_m_approv_matrix_detail_currency]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_approv_matrix_detail]  WITH CHECK ADD  CONSTRAINT [FK_m_approv_matrix_detail_currency] FOREIGN KEY([currency_id])
REFERENCES [dbo].[m_currency] ([currency_id])
GO
ALTER TABLE [dbo].[m_approv_matrix_detail] CHECK CONSTRAINT [FK_m_approv_matrix_detail_currency]
GO
/****** Object:  ForeignKey [FK_m_approv_matrix_detail_level_type]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_approv_matrix_detail]  WITH CHECK ADD  CONSTRAINT [FK_m_approv_matrix_detail_level_type] FOREIGN KEY([level_type_id])
REFERENCES [dbo].[m_level_type] ([level_type_id])
GO
ALTER TABLE [dbo].[m_approv_matrix_detail] CHECK CONSTRAINT [FK_m_approv_matrix_detail_level_type]
GO
/****** Object:  ForeignKey [FK_m_workflow_template_action_reason_category]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_workflow_template_action]  WITH CHECK ADD  CONSTRAINT [FK_m_workflow_template_action_reason_category] FOREIGN KEY([reason_category_id])
REFERENCES [dbo].[m_reason_category] ([reason_category_id])
GO
ALTER TABLE [dbo].[m_workflow_template_action] CHECK CONSTRAINT [FK_m_workflow_template_action_reason_category]
GO
/****** Object:  ForeignKey [FK_m_workflow_template_action_status]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_workflow_template_action]  WITH CHECK ADD  CONSTRAINT [FK_m_workflow_template_action_status] FOREIGN KEY([transaction_status_id])
REFERENCES [dbo].[m_transaction_status] ([transaction_status_id])
GO
ALTER TABLE [dbo].[m_workflow_template_action] CHECK CONSTRAINT [FK_m_workflow_template_action_status]
GO
/****** Object:  ForeignKey [FK_m_workflow_template_action_workflow_template]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_workflow_template_action]  WITH CHECK ADD  CONSTRAINT [FK_m_workflow_template_action_workflow_template] FOREIGN KEY([workflow_template_id])
REFERENCES [dbo].[m_workflow_template] ([workflow_template_id])
GO
ALTER TABLE [dbo].[m_workflow_template_action] CHECK CONSTRAINT [FK_m_workflow_template_action_workflow_template]
GO
/****** Object:  ForeignKey [FK_m_workflow_template_action_workflow_template_stage]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_workflow_template_action]  WITH CHECK ADD  CONSTRAINT [FK_m_workflow_template_action_workflow_template_stage] FOREIGN KEY([workflow_template_stage_id])
REFERENCES [dbo].[m_workflow_template_stage] ([workflow_template_stage_id])
GO
ALTER TABLE [dbo].[m_workflow_template_action] CHECK CONSTRAINT [FK_m_workflow_template_action_workflow_template_stage]
GO
/****** Object:  ForeignKey [FK_m_user_menu_mapping_m_menu]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_user_menu_mapping]  WITH CHECK ADD  CONSTRAINT [FK_m_user_menu_mapping_m_menu] FOREIGN KEY([menu_id])
REFERENCES [dbo].[m_menu] ([menu_id])
GO
ALTER TABLE [dbo].[m_user_menu_mapping] CHECK CONSTRAINT [FK_m_user_menu_mapping_m_menu]
GO
/****** Object:  ForeignKey [FK_m_user_menu_group_menu]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_user_menu]  WITH CHECK ADD  CONSTRAINT [FK_m_user_menu_group_menu] FOREIGN KEY([group_menu_id])
REFERENCES [dbo].[m_group_menu] ([group_menu_id])
GO
ALTER TABLE [dbo].[m_user_menu] CHECK CONSTRAINT [FK_m_user_menu_group_menu]
GO
/****** Object:  ForeignKey [FK_m_user_menu_status]    Script Date: 11/10/2017 15:12:30 ******/
ALTER TABLE [dbo].[m_user_menu]  WITH CHECK ADD  CONSTRAINT [FK_m_user_menu_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_user_menu] CHECK CONSTRAINT [FK_m_user_menu_status]
GO
/****** Object:  ForeignKey [FK_m_eform_setting_eform]    Script Date: 11/10/2017 15:12:31 ******/
ALTER TABLE [dbo].[m_eform_setting]  WITH CHECK ADD  CONSTRAINT [FK_m_eform_setting_eform] FOREIGN KEY([eform_id])
REFERENCES [dbo].[m_eform] ([eform_id])
GO
ALTER TABLE [dbo].[m_eform_setting] CHECK CONSTRAINT [FK_m_eform_setting_eform]
GO
/****** Object:  ForeignKey [FK_m_eform_setting_status]    Script Date: 11/10/2017 15:12:31 ******/
ALTER TABLE [dbo].[m_eform_setting]  WITH CHECK ADD  CONSTRAINT [FK_m_eform_setting_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_eform_setting] CHECK CONSTRAINT [FK_m_eform_setting_status]
GO
/****** Object:  ForeignKey [FK_m_eform_form_template_m_eform]    Script Date: 11/10/2017 15:12:31 ******/
ALTER TABLE [dbo].[m_eform_form_template]  WITH CHECK ADD  CONSTRAINT [FK_m_eform_form_template_m_eform] FOREIGN KEY([eform_id])
REFERENCES [dbo].[m_eform] ([eform_id])
GO
ALTER TABLE [dbo].[m_eform_form_template] CHECK CONSTRAINT [FK_m_eform_form_template_m_eform]
GO
/****** Object:  ForeignKey [FK_m_eform_form_template_m_status]    Script Date: 11/10/2017 15:12:31 ******/
ALTER TABLE [dbo].[m_eform_form_template]  WITH CHECK ADD  CONSTRAINT [FK_m_eform_form_template_m_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_eform_form_template] CHECK CONSTRAINT [FK_m_eform_form_template_m_status]
GO
/****** Object:  ForeignKey [FK_m_eform_form_template_m_transaction_form_template]    Script Date: 11/10/2017 15:12:31 ******/
ALTER TABLE [dbo].[m_eform_form_template]  WITH CHECK ADD  CONSTRAINT [FK_m_eform_form_template_m_transaction_form_template] FOREIGN KEY([transaction_form_template_id])
REFERENCES [dbo].[m_transaction_form_template] ([transaction_form_template_id])
GO
ALTER TABLE [dbo].[m_eform_form_template] CHECK CONSTRAINT [FK_m_eform_form_template_m_transaction_form_template]
GO
/****** Object:  ForeignKey [FK_m_eform_workflow_eform]    Script Date: 11/10/2017 15:12:32 ******/
ALTER TABLE [dbo].[m_eform_workflow]  WITH CHECK ADD  CONSTRAINT [FK_m_eform_workflow_eform] FOREIGN KEY([eform_id])
REFERENCES [dbo].[m_eform] ([eform_id])
GO
ALTER TABLE [dbo].[m_eform_workflow] CHECK CONSTRAINT [FK_m_eform_workflow_eform]
GO
/****** Object:  ForeignKey [FK_m_eform_workflow_status]    Script Date: 11/10/2017 15:12:32 ******/
ALTER TABLE [dbo].[m_eform_workflow]  WITH CHECK ADD  CONSTRAINT [FK_m_eform_workflow_status] FOREIGN KEY([status_id])
REFERENCES [dbo].[m_status] ([status_id])
GO
ALTER TABLE [dbo].[m_eform_workflow] CHECK CONSTRAINT [FK_m_eform_workflow_status]
GO
/****** Object:  ForeignKey [FK_m_eform_workflow_workflow_template]    Script Date: 11/10/2017 15:12:32 ******/
ALTER TABLE [dbo].[m_eform_workflow]  WITH CHECK ADD  CONSTRAINT [FK_m_eform_workflow_workflow_template] FOREIGN KEY([workflow_template_id])
REFERENCES [dbo].[m_workflow_template] ([workflow_template_id])
GO
ALTER TABLE [dbo].[m_eform_workflow] CHECK CONSTRAINT [FK_m_eform_workflow_workflow_template]
GO
/****** Object:  ForeignKey [FK_m_eform_setting_detail_component_type]    Script Date: 11/10/2017 15:12:32 ******/
ALTER TABLE [dbo].[m_eform_setting_detail]  WITH CHECK ADD  CONSTRAINT [FK_m_eform_setting_detail_component_type] FOREIGN KEY([component_type_id])
REFERENCES [dbo].[m_component_type] ([component_type_id])
GO
ALTER TABLE [dbo].[m_eform_setting_detail] CHECK CONSTRAINT [FK_m_eform_setting_detail_component_type]
GO
/****** Object:  ForeignKey [FK_m_eform_setting_detail_data_type]    Script Date: 11/10/2017 15:12:32 ******/
ALTER TABLE [dbo].[m_eform_setting_detail]  WITH CHECK ADD  CONSTRAINT [FK_m_eform_setting_detail_data_type] FOREIGN KEY([data_type_id])
REFERENCES [dbo].[m_data_type] ([data_type_id])
GO
ALTER TABLE [dbo].[m_eform_setting_detail] CHECK CONSTRAINT [FK_m_eform_setting_detail_data_type]
GO
/****** Object:  ForeignKey [FK_m_eform_setting_detail_eform_setting]    Script Date: 11/10/2017 15:12:32 ******/
ALTER TABLE [dbo].[m_eform_setting_detail]  WITH CHECK ADD  CONSTRAINT [FK_m_eform_setting_detail_eform_setting] FOREIGN KEY([eform_setting_id])
REFERENCES [dbo].[m_eform_setting] ([eform_setting_id])
GO
ALTER TABLE [dbo].[m_eform_setting_detail] CHECK CONSTRAINT [FK_m_eform_setting_detail_eform_setting]
GO
/****** Object:  DdlTrigger [tri_DDLEvents]    Script Date: 11/10/2017 15:12:33 ******/
Enable Trigger [tri_DDLEvents] ON Database
GO
