CREATE TABLE [dbo].[acc_mpay_bordereaux]
(
[mpayb_bordereau_id] [int] NOT NULL,
[mpayb_branch_id] [int] NOT NULL,
[mpayb_user_code] [dbo].[ais_code_medium] NOT NULL,
[mpayb_operation_utc_date] [datetime] NOT NULL,
[mpayb_reference] [nvarchar] (50) COLLATE French_CI_AS NULL,
[mpayb_bank_id] [int] NOT NULL,
[mpayb_currency_code] [dbo].[ais_code_short] NOT NULL,
[mpayb_sequence_code] [dbo].[ais_code_short] NULL,
[mpayb_date_from] [date] NOT NULL,
[mpayb_date_to] [date] NOT NULL,
[mpayb_status_code] [dbo].[ais_code_short] NOT NULL,
[mpayb_obs] [nvarchar] (64) COLLATE French_CI_AS NULL,
[mpayb_cr_date] [date] NOT NULL,
[mpayb_s_date] [date] NULL,
[mpayb_c_date] [date] NULL,
[mpayb_a_date] [date] NULL,
[mpayb_an_date] [date] NULL,
[mpayb_date] AS (case  when [mpayb_status_code]='CR' then [mpayb_cr_date] when [mpayb_status_code]='S' then [mpayb_s_date] when [mpayb_status_code]='C' then [mpayb_c_date] when [mpayb_status_code]='AN' then [mpayb_an_date] when [mpayb_status_code]='A' then [mpayb_a_date] end)
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[acc_mpay_bordereaux] ADD CONSTRAINT [PK_acc_mpay_bordereaux] PRIMARY KEY CLUSTERED  ([mpayb_bordereau_id]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[acc_mpay_bordereaux] ADD CONSTRAINT [FK_acc_mpay_bordereaux_acc_currencies] FOREIGN KEY ([mpayb_currency_code]) REFERENCES [dbo].[acc_currencies] ([cur_currency_code])
GO
ALTER TABLE [dbo].[acc_mpay_bordereaux] ADD CONSTRAINT [FK_acc_mpay_bordereaux_acc_mpay_bordereaux_statuses] FOREIGN KEY ([mpayb_status_code]) REFERENCES [dbo].[acc_mpay_bordereaux_statuses] ([mpays_status_code])
GO
ALTER TABLE [dbo].[acc_mpay_bordereaux] ADD CONSTRAINT [FK_acc_mpay_bordereaux_act_banks] FOREIGN KEY ([mpayb_bank_id]) REFERENCES [dbo].[act_banks] ([bk_actor_id])
GO
ALTER TABLE [dbo].[acc_mpay_bordereaux] ADD CONSTRAINT [FK_acc_mpay_bordereaux_act_branches] FOREIGN KEY ([mpayb_branch_id]) REFERENCES [dbo].[act_branches] ([br_actor_id])
GO
ALTER TABLE [dbo].[acc_mpay_bordereaux] ADD CONSTRAINT [FK_acc_mpay_bordereaux_sq_sequences] FOREIGN KEY ([mpayb_sequence_code]) REFERENCES [dbo].[sq_sequences] ([seq_sequence_code])
GO
ALTER TABLE [dbo].[acc_mpay_bordereaux] ADD CONSTRAINT [FK_acc_mpay_bordereaux_usr_users] FOREIGN KEY ([mpayb_user_code]) REFERENCES [dbo].[usr_users] ([usr_user_code])
GO
