public void ValidateSCGDocument(SCGDocument document, bool haveReceiver, bool haveApprover)
        {
            Spring.Validation.ValidationErrors errors = new Spring.Validation.ValidationErrors();
            // DocumentSubTypeID = ใบของบประมาณระหว่างงวด. Not use in eAccounting.
            // Must specific Approver.
            if (document.CompanyID == null)
            {
                errors.AddError("Provider.Error", new Spring.Validation.ErrorMessage("CompanyIsRequired"));
            }
            else
            {
                DbCompany company = ScgDbQueryProvider.DbCompanyQuery.FindByIdentity(document.CompanyID.CompanyID);
                if (company == null)
                {
                    errors.AddError("Provider.Error", new Spring.Validation.ErrorMessage("CompanyIsRequired"));
                }
            }
            if (document.PaymentMethodID == 39)
            {
                if (document.RequesterID.Userid != document.ReceiverID.Userid)
                {
                    errors.AddError("Provider.Error", new Spring.Validation.ErrorMessage("ReceiverCannotBeDifferentRequester"));
                }
            }

            if (string.IsNullOrEmpty(document.Subject))
            {
                errors.AddError("Provider.Error", new Spring.Validation.ErrorMessage("SubjectIsRequired"));
            }

            if (haveReceiver)
            {
                if (document.ReceiverID == null)
                {
                    errors.AddError("Provider.Error", new Spring.Validation.ErrorMessage("ReceiverIsRequired"));
                }
                else
                {
                    SuUser receiver = QueryProvider.SuUserQuery.FindByIdentity(document.ReceiverID.Userid);

                    if (document.ReceiverID == null)
                    {
                        errors.AddError("Provider.Error", new Spring.Validation.ErrorMessage("ReceiverIsRequired"));
                    }
                }
            }

            if (haveApprover)
            {
                if (document.ApproverID == null)
                {
                    errors.AddError("Provider.Error", new Spring.Validation.ErrorMessage("ApproverIsRequired"));
                }
                else
                {
                    SuUser approver = QueryProvider.SuUserQuery.FindByIdentity(document.ApproverID.Userid);

                    if (approver == null)
                    {
                        errors.AddError("Provider.Error", new Spring.Validation.ErrorMessage("ApproverIsRequired"));
                    }
                }
            }

            if (document.RequesterID == null)
            {
                errors.AddError("Provider.Error", new Spring.Validation.ErrorMessage("RequesterIsRequired"));
            }
            else
            {
                SuUser requester = QueryProvider.SuUserQuery.FindByIdentity(document.RequesterID.Userid);

                if (requester == null)
                {
                    errors.AddError("Provider.Error", new Spring.Validation.ErrorMessage("RequesterIsRequired"));
                }
            }
            //add new field
            //IList<object> advanceEditableField = AvAdvanceDocumentService.GetEditableFields(document.DocumentID, null);
            IList <object> expenseEditableField    = FnExpenseDocumentService.GetEditableFields(document.DocumentID);
            IList <object> remittanceEditableField = FnRemittanceService.GetEditableFields(document.DocumentID);

            if (/*advanceEditableField.Contains(AdvanceFieldGroup.VerifyDetail) ||*/ expenseEditableField.Contains(ExpenseFieldGroup.VerifyDetail) || remittanceEditableField.Contains(RemittanceFieldGroup.VerifyDetail))
            {
                if (string.IsNullOrEmpty(document.BranchCode))
                {
                    errors.AddError("Provider.Error", new Spring.Validation.ErrorMessage("BranchCodeIsRequired"));
                }
                //if (!document.PaymentMethodID.HasValue)
                //{
                //    errors.AddError("Provider.Error", new Spring.Validation.ErrorMessage("PaymentMethodID_IsRequired"));
                //}
                if (!document.PostingDate.HasValue)
                {
                    errors.AddError("Provider.Error", new Spring.Validation.ErrorMessage("PostingDateIsRequired"));
                }
                if (!document.BaseLineDate.HasValue)
                {
                    errors.AddError("Provider.Error", new Spring.Validation.ErrorMessage("BaseLineDateIsRequired"));
                }
            }
            if (!errors.IsEmpty)
            {
                throw new ServiceValidationException(errors);
            }
        }
        public void UpdateTransactionDocument(Guid TxID, SCGDocument document, bool haveReceiver, bool haveApprover)
        {
            Spring.Validation.ValidationErrors errors = new Spring.Validation.ValidationErrors();
            try
            {
                this.ValidateSCGDocument(document, haveReceiver, haveApprover);
            }
            catch (ServiceValidationException ex)
            {
                errors.MergeErrors(ex.ValidationErrors);
            }

            if (!errors.IsEmpty)
            {
                throw new ServiceValidationException(errors);
            }

            DataSet   ds            = TransactionService.GetDS(TxID);
            DataTable documentTable = ds.Tables["Document"];

            if (documentTable.Rows.Count > 0)
            {
                DataRow documentRow = documentTable.Rows[0];
                documentRow.BeginEdit();

                if (document.CompanyID != null)
                {
                    documentRow["CompanyID"] = document.CompanyID.CompanyID;
                }
                if (document.CreatorID != null)
                {
                    documentRow["CreatorID"] = document.CreatorID.Userid;
                }
                if (document.RequesterID != null)
                {
                    documentRow["RequesterID"] = document.RequesterID.Userid;
                }
                if (document.ApproverID != null)
                {
                    documentRow["ApproverID"] = document.ApproverID.Userid;
                }
                if (document.ReceiverID != null)
                {
                    documentRow["ReceiverID"] = document.ReceiverID.Userid;
                }
                //documentRow["DocumentNo"] = document.DocumentNo;
                if (document.DocumentType != null)
                {
                    documentRow["DocumentTypeID"] = document.DocumentType.DocumentTypeID;
                }
                IList <object> advanceEditableField      = AvAdvanceDocumentService.GetEditableFields(document.DocumentID, null);
                IList <object> expenseEditableField      = FnExpenseDocumentService.GetEditableFields(document.DocumentID);
                IList <object> remittanceEditableField   = FnRemittanceService.GetEditableFields(document.DocumentID);
                IList <object> fixedAdvanceEditableField = FixedAdvanceDocumentService.GetEditableFields(document.DocumentID);
                if (advanceEditableField.Contains(AdvanceFieldGroup.VerifyDetail) || expenseEditableField.Contains(ExpenseFieldGroup.VerifyDetail) || remittanceEditableField.Contains(RemittanceFieldGroup.VerifyDetail) || fixedAdvanceEditableField.Contains(FixedAdvanceFieldGroup.VerifyDetail))
                {
                    //add new field
                    if (!string.IsNullOrEmpty(document.BranchCode))
                    {
                        documentRow["BranchCode"] = document.BranchCode;
                    }
                    if (!string.IsNullOrEmpty(document.BusinessArea))
                    {
                        documentRow["BusinessArea"] = document.BusinessArea;
                    }
                    else
                    {
                        documentRow["BusinessArea"] = string.Empty;
                    }

                    if (!string.IsNullOrEmpty(document.Supplementary))
                    {
                        documentRow["Supplementary"] = document.Supplementary;
                    }
                    else
                    {
                        documentRow["Supplementary"] = string.Empty;
                    }

                    if (document.PaymentMethodID.HasValue && document.PaymentMethodID > 0)
                    {
                        documentRow["PaymentMethodID"] = document.PaymentMethodID.Value;
                    }
                    if (document.PostingDate.HasValue && document.PostingDate.Value != DateTime.MinValue)
                    {
                        documentRow["PostingDate"] = document.PostingDate;
                    }
                    if (document.BaseLineDate.HasValue && document.BaseLineDate.Value != DateTime.MinValue)
                    {
                        documentRow["BaseLineDate"] = document.BaseLineDate;
                    }
                    if (!string.IsNullOrEmpty(document.BankAccount))
                    {
                        documentRow["BankAccount"] = document.BankAccount;
                    }
                }
                if (!string.IsNullOrEmpty(document.ReferenceNo))
                {
                    documentRow["ReferenceNo"] = document.ReferenceNo;
                }
                else
                {
                    if (!documentRow["DocumentNo"].ToString().StartsWith("EHR"))
                    {
                        documentRow["ReferenceNo"] = document.ReferenceNo;
                    }
                }

                documentRow["IsVerifyImage"] = document.IsVerifyImage;
                documentRow["Subject"]       = document.Subject;
                documentRow["Memo"]          = document.Memo;
                documentRow["Active"]        = document.Active;
                documentRow["CreBy"]         = UserAccount.UserID;
                documentRow["CreDate"]       = DateTime.Now;
                documentRow["UpdBy"]         = UserAccount.UserID;
                documentRow["UpdDate"]       = DateTime.Now;
                documentRow["UpdPgm"]        = UserAccount.CurrentProgramCode;

                documentRow.EndEdit();
            }
        }