/// <summary>
        /// Client Read
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <param name="eventClient"> </param>
        public static ClientReadResponse ClientRead(ClientReadRequest clientReadRequest)
        {
            var clientReadResponse = RepClient.Read(clientReadRequest.clientUID);

            clientReadResponse.client.clientExtraInformation             = new ClientExtraInformation();
            clientReadResponse.client.clientExtraInformation.FKClientUID = clientReadResponse.client.UID;

            // clientReadResponse.client.clientExtraInformation.Read();

            RepClientExtraInformation.Read(clientReadResponse.client.clientExtraInformation);


            return(clientReadResponse);
        }
        /// <summary>
        /// Client Read
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <param name="eventClient"> </param>
        public static ClientReadResponse ClientReadFull(ClientReadRequest clientReadRequest)
        {
            // Check if the user can access the client record
            //
            bool isadmin = false;

            string userid = clientReadRequest.headerInfo.UserID;
            var    sur    = new SecurityUserRole();

            var clientReadResponse = RepClient.Read(clientReadRequest.clientUID);

            if (sur.UserHasAccessToRole(userid, FCMConstant.UserRoleType.ADMIN))
            {
                // ok
            }
            else
            {
                if (sur.UserHasAccessToRole(userid, FCMConstant.UserRoleType.CLIENT))
                {
                    if (clientReadResponse.client.FKUserID.ToUpper() == clientReadRequest.headerInfo.UserID.ToUpper())
                    {
                        //ok
                    }
                    else
                    {
                        return(new ClientReadResponse());
                    }
                }
                else
                {
                    return(new ClientReadResponse());
                }
            }



            clientReadResponse.client.clientExtraInformation             = new ClientExtraInformation();
            clientReadResponse.client.clientExtraInformation.FKClientUID = clientReadResponse.client.UID;

            RepClientExtraInformation.Read(clientReadResponse.client.clientExtraInformation);

            clientReadResponse.client.clientEmployee = RepEmployee.ReadEmployees(clientReadRequest.clientUID);

            return(clientReadResponse);
        }
        /// <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);
        }