/// <summary>
        /// Update client details
        /// </summary>
        /// <param name="headerInfo"> </param>
        /// <param name="eventClient"> </param>
        public ClientUpdateResponse ClientUpdateFull(ClientUpdateRequest clientUpdateRequest)
        {
            var clientUpdateResponse = new ClientUpdateResponse();

            bool contractorSizeFirstTime = false;

            // Check if contractor size has been set before
            //
            var clientRead = RepClient.Read(clientUpdateRequest.eventClient.UID);

            if (clientRead.client.FKDocumentSetUID == 0)
            {
                contractorSizeFirstTime = true;
            }

            clientUpdateResponse.response = new ResponseStatus();

            // --------------------------------------------------------------
            // Check if user ID is already connected to a client
            // --------------------------------------------------------------

            //var checkLinkedUser = new Client(clientUpdateRequest.headerInfo)
            //{ FKUserID = clientUpdateRequest.eventClient.FKUserID };

            var checkLinkedUser = new Model.ModelClient.Client(clientUpdateRequest.headerInfo)
            {
                FKUserID = clientUpdateRequest.eventClient.FKUserID,
                UID      = clientUpdateRequest.eventClient.UID
            };

            if (!string.IsNullOrEmpty(checkLinkedUser.FKUserID))
            {
                var responseLinked = RepClient.ReadLinkedUser(checkLinkedUser);
                // var responseLinked = checkLinkedUser.ReadLinkedUser();

                if (responseLinked.ReturnCode == 0001 && responseLinked.ReasonCode == 0001)
                {
                    clientUpdateResponse.response =
                        new ResponseStatus()
                    {
                        ReturnCode = -0010, ReasonCode = 0001, Message = "User ID is already linked to another client."
                    };
                    return(clientUpdateResponse);
                }

                if (responseLinked.ReturnCode == 0001 && responseLinked.ReasonCode == 0003)
                {
                    // All good. User ID is connected to Client Supplied.
                    //
                }
            }


            using (var connection = new MySqlConnection(ConnString.ConnectionString))
            {
                using (var tr = new TransactionScope(TransactionScopeOption.Required))
                {
                    connection.Open();
                    var newClient = RepClient.Update(clientUpdateRequest.headerInfo, clientUpdateRequest.eventClient, connection);

                    //var responseClientUpdate = clientUpdateRequest.eventClient.Update();
                    var responseClientUpdate = newClient;

                    if (!responseClientUpdate.Successful)
                    {
                        // Rollback
                        tr.Dispose();

                        clientUpdateResponse.response = new ResponseStatus(MessageType.Error);

                        clientUpdateResponse.response.Message    = responseClientUpdate.Message;
                        clientUpdateResponse.response.ReturnCode = responseClientUpdate.ReturnCode;
                        clientUpdateResponse.response.ReasonCode = responseClientUpdate.ReasonCode;

                        return(clientUpdateResponse);
                    }
                    // -------------------------------------------
                    // Call method to add client extra information
                    // -------------------------------------------

                    var ceiRead = new RepClientExtraInformation(clientUpdateRequest.headerInfo);
                    ceiRead.FKClientUID = clientUpdateRequest.eventClient.UID;

                    // var ceiResponse = ceiRead.Read();

                    var ceiResponse = RepClientExtraInformation.Read(ceiRead);

                    if (ceiResponse.ReturnCode != 1)
                    {
                        // Rollback
                        tr.Dispose();

                        clientUpdateResponse.response = new ResponseStatus(MessageType.Error);
                        return(clientUpdateResponse);
                    }

                    // Return Code = 1, Reason Code = 2 means "Record not found"
                    //
                    if (ceiResponse.ReturnCode == 1 && ceiResponse.ReasonCode == 1)
                    {
                        clientUpdateRequest.eventClient.clientExtraInformation.RecordVersion = ceiRead.RecordVersion;

                        var cei = RepClientExtraInformation.Update(clientUpdateRequest.headerInfo,
                                                                   clientUpdateRequest.eventClient.
                                                                   clientExtraInformation,
                                                                   connection);

                        // var cei = clientUpdateRequest.eventClient.clientExtraInformation.Update();

                        if (!cei.Successful)
                        {
                            clientUpdateResponse.response = new ResponseStatus();
                            clientUpdateResponse.response = cei;
                            return(clientUpdateResponse);
                        }
                    }

                    // Return Code = 1, Reason Code = 2 means "Record not found"
                    //
                    if (ceiResponse.ReturnCode == 1 && ceiResponse.ReasonCode == 2)
                    {
                        // Create new record

                        // -------------------------------------------
                        // Call method to add client extra information
                        // -------------------------------------------
                        clientUpdateRequest.eventClient.clientExtraInformation.FKClientUID = clientUpdateRequest.eventClient.UID;
                        var cei = RepClientExtraInformation.Insert(
                            HeaderInfo.Instance,
                            clientUpdateRequest.eventClient.clientExtraInformation,
                            connection);

                        if (cei.ReturnCode != 1)
                        {
                            // Rollback transaction
                            //
                            tr.Dispose();

                            clientUpdateResponse.response = new ResponseStatus();
                            clientUpdateResponse.response = cei;
                            return(clientUpdateResponse);
                        }
                    }

                    //tr.Complete();

                    // If this is the first time the users sets the contractor size, add documents.
                    //
                    if (contractorSizeFirstTime)
                    {
                        // --------------------------------------------
                        // Add first document set
                        // --------------------------------------------
                        var cds = new ClientDocumentSet();
                        cds.FKClientUID = clientUpdateRequest.eventClient.UID;

                        // cds.FolderOnly = "CLIENT" + newClientUID.ToString().Trim().PadLeft(4, '0');
                        cds.FolderOnly = "CLIENT" + clientUpdateRequest.eventClient.UID.ToString().Trim().PadLeft(4, '0');

                        // cds.Folder = FCMConstant.SYSFOLDER.CLIENTFOLDER + "\\CLIENT" + newClientUID.ToString().Trim().PadLeft(4, '0');
                        cds.Folder = FCMConstant.SYSFOLDER.CLIENTFOLDER + @"\" + cds.FolderOnly;

                        cds.SourceFolder = FCMConstant.SYSFOLDER.TEMPLATEFOLDER;
                        // cds.Add( clientUpdateRequest.headerInfo, connection );
                        cds.Add(clientUpdateRequest.headerInfo);

                        // --------------------------------------------
                        // Apply initial document set
                        // --------------------------------------------
                        BUSClientDocument.AssociateDocumentsToClient(
                            clientDocumentSet: cds,
                            documentSetUID: clientUpdateRequest.eventClient.FKDocumentSetUID,
                            headerInfo: clientUpdateRequest.headerInfo);

                        // Fix Destination Folder Location
                        //
                        BUSClientDocumentGeneration.UpdateLocation(cds.FKClientUID, cds.ClientSetID);
                    }

                    SaveEmployees(clientUpdateRequest.eventClient.UID, clientUpdateRequest.eventClient.clientEmployee, clientUpdateRequest.headerInfo.UserID);
                }
            }

            return(clientUpdateResponse);
        }
        /// <summary>
        /// Update client details
        /// </summary>
        /// <param name="headerInfo"> </param>
        /// <param name="eventClient"> </param>
        public ClientUpdateResponse ClientUpdate(ClientUpdateRequest clientUpdateRequest)
        {
            var clientUpdateResponse = new ClientUpdateResponse();

            clientUpdateResponse.response = new ResponseStatus();

            // --------------------------------------------------------------
            // Check if user ID is already connected to a client
            // --------------------------------------------------------------

            //var checkLinkedUser = new Client(clientUpdateRequest.headerInfo)
            //{ FKUserID = clientUpdateRequest.eventClient.FKUserID };

            var checkLinkedUser = new Model.ModelClient.Client(clientUpdateRequest.headerInfo)
            {
                FKUserID = clientUpdateRequest.eventClient.FKUserID,
                UID      = clientUpdateRequest.eventClient.UID
            };

            if (!string.IsNullOrEmpty(checkLinkedUser.FKUserID))
            {
                var responseLinked = RepClient.ReadLinkedUser(checkLinkedUser);
                // var responseLinked = checkLinkedUser.ReadLinkedUser();

                if (responseLinked.ReturnCode == 0001 && responseLinked.ReasonCode == 0001)
                {
                    clientUpdateResponse.response =
                        new ResponseStatus()
                    {
                        ReturnCode = -0010, ReasonCode = 0001, Message = "User ID is already linked to another client."
                    };
                    return(clientUpdateResponse);
                }

                if (responseLinked.ReturnCode == 0001 && responseLinked.ReasonCode == 0003)
                {
                    // All good. User ID is connected to Client Supplied.
                    //
                }
            }


            using (var connection = new MySqlConnection(ConnString.ConnectionString))
            {
                using (var tr = new TransactionScope(TransactionScopeOption.Required))
                {
                    connection.Open();
                    var newClient = RepClient.Update(clientUpdateRequest.headerInfo, clientUpdateRequest.eventClient, connection);

                    //var responseClientUpdate = clientUpdateRequest.eventClient.Update();
                    var responseClientUpdate = newClient;

                    if (!responseClientUpdate.Successful)
                    {
                        // Rollback
                        tr.Dispose();

                        clientUpdateResponse.response = new ResponseStatus(MessageType.Error);

                        clientUpdateResponse.response.Message    = responseClientUpdate.Message;
                        clientUpdateResponse.response.ReturnCode = responseClientUpdate.ReturnCode;
                        clientUpdateResponse.response.ReasonCode = responseClientUpdate.ReasonCode;

                        return(clientUpdateResponse);
                    }
                    // -------------------------------------------
                    // Call method to add client extra information
                    // -------------------------------------------

                    var ceiRead = new RepClientExtraInformation(clientUpdateRequest.headerInfo);
                    ceiRead.FKClientUID = clientUpdateRequest.eventClient.UID;

                    // var ceiResponse = ceiRead.Read();

                    var ceiResponse = RepClientExtraInformation.Read(ceiRead);

                    if (ceiResponse.ReturnCode != 1)
                    {
                        // Rollback
                        tr.Dispose();

                        clientUpdateResponse.response = new ResponseStatus(MessageType.Error);
                        return(clientUpdateResponse);
                    }

                    // Return Code = 1, Reason Code = 2 means "Record not found"
                    //
                    if (ceiResponse.ReturnCode == 1 && ceiResponse.ReasonCode == 1)
                    {
                        clientUpdateRequest.eventClient.clientExtraInformation.RecordVersion = ceiRead.RecordVersion;

                        var cei = RepClientExtraInformation.Update(clientUpdateRequest.headerInfo,
                                                                   clientUpdateRequest.eventClient.
                                                                   clientExtraInformation,
                                                                   connection);

                        // var cei = clientUpdateRequest.eventClient.clientExtraInformation.Update();

                        if (!cei.Successful)
                        {
                            clientUpdateResponse.response = new ResponseStatus();
                            clientUpdateResponse.response = cei;
                            return(clientUpdateResponse);
                        }
                    }

                    // Return Code = 1, Reason Code = 2 means "Record not found"
                    //
                    if (ceiResponse.ReturnCode == 1 && ceiResponse.ReasonCode == 2)
                    {
                        // Create new record

                        // -------------------------------------------
                        // Call method to add client extra information
                        // -------------------------------------------
                        clientUpdateRequest.eventClient.clientExtraInformation.FKClientUID = clientUpdateRequest.eventClient.UID;
                        var cei = RepClientExtraInformation.Insert(
                            HeaderInfo.Instance,
                            clientUpdateRequest.eventClient.clientExtraInformation,
                            connection);

                        if (cei.ReturnCode != 1)
                        {
                            // Rollback transaction
                            //
                            tr.Dispose();

                            clientUpdateResponse.response = new ResponseStatus();
                            clientUpdateResponse.response = cei;
                            return(clientUpdateResponse);
                        }
                    }

                    tr.Complete();
                }
            }

            return(clientUpdateResponse);
        }
        /// <summary>
        /// Add client
        /// </summary>
        /// <param name="headerInfo"> </param>
        /// <param name="eventClient"></param>
        /// <param name="linkInitialSet"> </param>
        /// <returns></returns>
        public ClientAddResponse ClientAddFull(ClientAddRequest clientAddRequest)
        {
            var response = new ClientAddResponse();

            // This is a new client.
            //
            if (string.IsNullOrEmpty(clientAddRequest.eventClient.Name))
            {
                response.responseStatus = new ResponseStatus()
                {
                    ReturnCode = -0010,
                    ReasonCode = 0001,
                    Message    = "Client Name is mandatory."
                };
                return(response);
            }
            // --------------------------------------------------------------
            // Check if user ID is already connected to a client
            // --------------------------------------------------------------
            #region Check if user is already connected to a client
            if (!string.IsNullOrEmpty(clientAddRequest.eventClient.FKUserID))
            {
                var checkLinkedUser = new Client(clientAddRequest.headerInfo)
                {
                    FKUserID = clientAddRequest.eventClient.FKUserID
                };
                //var responseLinked = checkLinkedUser.ReadLinkedUser();

                var responseLinked = RepClient.ReadLinkedUser(checkLinkedUser);

                if (!responseLinked.Successful)
                {
                    response.responseStatus = new ResponseStatus();
                    response.responseStatus = responseLinked;
                    return(response);
                }

                if (responseLinked.ReturnCode == 0001 && responseLinked.ReasonCode == 0001)
                {
                    response.responseStatus = new ResponseStatus()
                    {
                        ReturnCode = -0010,
                        ReasonCode = 0002,
                        Message    = "User ID is already linked to another client."
                    };
                    return(response);
                }
            }
            #endregion

            var newClientUid = 0;

            using (var connection = new MySqlConnection(ConnString.ConnectionString))
            {
                using (var tr = new TransactionScope(TransactionScopeOption.Required))
                {
                    connection.Open();

                    // -------------------------------
                    // Call method to add new client
                    // -------------------------------
                    //var newClient = clientAddRequest.eventClient.Insert(clientAddRequest.headerInfo, connection);

                    var newClient = RepClient.Insert(clientAddRequest.headerInfo, clientAddRequest.eventClient, connection);

                    //   var newClientX = eventClient.MySQLInsert(headerInfo);

                    newClientUid = Convert.ToInt32(newClient.Contents);

                    // -------------------------------------------
                    // Call method to add client extra information
                    // -------------------------------------------
                    clientAddRequest.eventClient.clientExtraInformation.FKClientUID = clientAddRequest.eventClient.UID;
                    var cei = RepClientExtraInformation.Insert(
                        HeaderInfo.Instance,
                        clientAddRequest.eventClient.clientExtraInformation,
                        connection);

                    if (cei.ReturnCode != 1)
                    {
                        // Rollback transaction
                        //
                        tr.Dispose();

                        response.responseStatus = new ResponseStatus();
                        response.responseStatus = cei;
                        return(response);
                    }

                    // Add user role
                    //
                    SecurityUserRole securityUserRole = new SecurityUserRole();
                    securityUserRole.FK_Role   = FCMConstant.UserRoleType.CLIENT;
                    securityUserRole.FK_UserID = clientAddRequest.headerInfo.UserID;
                    securityUserRole.IsActive  = "Y";
                    securityUserRole.IsVoid    = "N";
                    securityUserRole.StartDate = System.DateTime.Today;
                    securityUserRole.EndDate   = Convert.ToDateTime("9999-12-31");
                    securityUserRole.Add();

                    // --------------------------------------------
                    // Add List of Employees
                    // --------------------------------------------
                    SaveEmployees(clientAddRequest.eventClient.UID, clientAddRequest.eventClient.clientEmployee, clientAddRequest.headerInfo.UserID);

                    // 14/04/2013
                    // Not adding sets when client is created because the client is created just after the user ID is registered!
                    //

                    // 17/04/2013
                    // If the ADMINistrator creates the client the document set has to be created and documents added.
                    //
                    if (clientAddRequest.linkInitialSet == "Y")
                    {
                        // --------------------------------------------
                        // Add first document set
                        // --------------------------------------------
                        var cds = new ClientDocumentSet();
                        cds.FKClientUID = newClientUid;

                        // cds.FolderOnly = "CLIENT" + newClientUID.ToString().Trim().PadLeft(4, '0');
                        cds.FolderOnly = "CLIENT" + newClientUid.ToString().Trim().PadLeft(4, '0');

                        // cds.Folder = FCMConstant.SYSFOLDER.CLIENTFOLDER + "\\CLIENT" + newClientUID.ToString().Trim().PadLeft(4, '0');
                        cds.Folder = FCMConstant.SYSFOLDER.CLIENTFOLDER + @"\" + cds.FolderOnly;

                        cds.SourceFolder = FCMConstant.SYSFOLDER.TEMPLATEFOLDER;
                        cds.Add(clientAddRequest.headerInfo, connection);

                        // --------------------------------------------
                        // Apply initial document set
                        // --------------------------------------------
                        BUSClientDocument.AssociateDocumentsToClient(
                            clientDocumentSet: cds,
                            documentSetUID: clientAddRequest.eventClient.FKDocumentSetUID,
                            headerInfo: clientAddRequest.headerInfo);

                        // Fix Destination Folder Location
                        //
                        BUSClientDocumentGeneration.UpdateLocation(cds.FKClientUID, cds.ClientSetID);
                    }

                    // Commit transaction

                    tr.Complete();
                }
            }

            ClientList(clientAddRequest.headerInfo);

            // List();

            // Return new client id
            response.clientUID      = newClientUid;
            response.responseStatus = new ResponseStatus();

            return(response);
        }
        /// <summary>
        /// Add client
        /// </summary>
        /// <param name="headerInfo"> </param>
        /// <param name="eventClient"></param>
        /// <param name="linkInitialSet"> </param>
        /// <returns></returns>
        public ClientAddResponse ClientAdd(ClientAddRequest clientAddRequest)
        {
            var response = new ClientAddResponse();

            // This is a new client.
            //
            if (string.IsNullOrEmpty(clientAddRequest.eventClient.Name))
            {
                response.responseStatus = new ResponseStatus()
                {
                    ReturnCode = -0010,
                    ReasonCode = 0001,
                    Message    = "Client Name is mandatory."
                };
                return(response);
            }
            // --------------------------------------------------------------
            // Check if user ID is already connected to a client
            // --------------------------------------------------------------
            #region Check if user is already connected to a client
            if (!string.IsNullOrEmpty(clientAddRequest.eventClient.FKUserID))
            {
                var checkLinkedUser = new Client(clientAddRequest.headerInfo)
                {
                    FKUserID = clientAddRequest.eventClient.FKUserID
                };
                //var responseLinked = checkLinkedUser.ReadLinkedUser();

                var responseLinked = RepClient.ReadLinkedUser(checkLinkedUser);

                if (!responseLinked.Successful)
                {
                    response.responseStatus = new ResponseStatus();
                    response.responseStatus = responseLinked;
                    return(response);
                }

                if (responseLinked.ReturnCode == 0001 && responseLinked.ReasonCode == 0001)
                {
                    response.responseStatus = new ResponseStatus()
                    {
                        ReturnCode = -0010,
                        ReasonCode = 0002,
                        Message    = "User ID is already linked to another client."
                    };
                    return(response);
                }
            }
            #endregion

            var newClientUid = 0;

            using (var connection = new MySqlConnection(ConnString.ConnectionString))
            {
                using (var tr = new TransactionScope(TransactionScopeOption.Required))
                {
                    connection.Open();

                    // -------------------------------
                    // Call method to add new client
                    // -------------------------------
                    //var newClient = clientAddRequest.eventClient.Insert(clientAddRequest.headerInfo, connection);

                    var newClient = RepClient.Insert(clientAddRequest.headerInfo, clientAddRequest.eventClient, connection);

                    //   var newClientX = eventClient.MySQLInsert(headerInfo);

                    newClientUid = Convert.ToInt32(newClient.Contents);

                    // -------------------------------------------
                    // Call method to add client extra information
                    // -------------------------------------------
                    clientAddRequest.eventClient.clientExtraInformation.FKClientUID = clientAddRequest.eventClient.UID;
                    var cei = RepClientExtraInformation.Insert(
                        HeaderInfo.Instance,
                        clientAddRequest.eventClient.clientExtraInformation,
                        connection);

                    if (cei.ReturnCode != 1)
                    {
                        // Rollback transaction
                        //
                        tr.Dispose();

                        response.responseStatus = new ResponseStatus();
                        response.responseStatus = cei;
                        return(response);
                    }

                    // --------------------------------------------
                    // Add first document set
                    // --------------------------------------------
                    var cds = new ClientDocumentSet();
                    cds.FKClientUID = newClientUid;

                    // cds.FolderOnly = "CLIENT" + newClientUID.ToString().Trim().PadLeft(4, '0');
                    cds.FolderOnly = "CLIENT" + newClientUid.ToString().Trim().PadLeft(4, '0');

                    // cds.Folder = FCMConstant.SYSFOLDER.CLIENTFOLDER + "\\CLIENT" + newClientUID.ToString().Trim().PadLeft(4, '0');
                    cds.Folder = FCMConstant.SYSFOLDER.CLIENTFOLDER + @"\" + cds.FolderOnly;

                    cds.SourceFolder = FCMConstant.SYSFOLDER.TEMPLATEFOLDER;
                    cds.Add(clientAddRequest.headerInfo, connection);

                    // --------------------------------------------
                    // Apply initial document set
                    // --------------------------------------------
                    if (clientAddRequest.linkInitialSet == "Y")
                    {
                        BUSClientDocument.AssociateDocumentsToClient(
                            clientDocumentSet: cds,
                            documentSetUID: clientAddRequest.eventClient.FKDocumentSetUID,
                            headerInfo: clientAddRequest.headerInfo);

                        // Fix Destination Folder Location
                        //
                        BUSClientDocumentGeneration.UpdateLocation(cds.FKClientUID, cds.ClientSetID);
                    }

                    // Commit transaction
                    //
                    tr.Complete();
                }
            }

            ClientList(clientAddRequest.headerInfo);

            // List();

            // Return new client id
            response.clientUID      = newClientUid;
            response.responseStatus = new ResponseStatus();

            return(response);
        }