public static List <ChartOfAccount_cu> GetChartOfAccountOfPreviousCodeMargin(
            DB_ChartOfAccountCodeMargin chartOfAccountCodeMargin, bool isDebit)
        {
            switch (chartOfAccountCodeMargin)
            {
            case DB_ChartOfAccountCodeMargin.FirstMargin:
                return(null);

            case DB_ChartOfAccountCodeMargin.SecondMargin:
                return
                    (ChartOfAccount_cu.ItemsList.FindAll(
                         item =>
                         Convert.ToInt32(item.ChartOfAccountCodeMargin_P_ID).Equals((int)DB_ChartOfAccountCodeMargin.FirstMargin) &&
                         Convert.ToBoolean(item.IsDebit).Equals(Convert.ToBoolean(isDebit)))
                     .OrderBy(item => item.Serial)
                     .OrderBy(item => item.ParentChartOfAccount_CU_ID).ToList());

            case DB_ChartOfAccountCodeMargin.ThirdMargin:
                return
                    (ChartOfAccount_cu.ItemsList.FindAll(
                         item =>
                         Convert.ToInt32(item.ChartOfAccountCodeMargin_P_ID).Equals((int)DB_ChartOfAccountCodeMargin.SecondMargin) &&
                         Convert.ToBoolean(item.IsDebit).Equals(Convert.ToBoolean(isDebit)))
                     .OrderBy(item => item.Serial)
                     .OrderBy(item => item.ParentChartOfAccount_CU_ID).ToList());

            case DB_ChartOfAccountCodeMargin.FourthMargin:
                return
                    (ChartOfAccount_cu.ItemsList.FindAll(
                         item =>
                         Convert.ToInt32(item.ChartOfAccountCodeMargin_P_ID).Equals((int)DB_ChartOfAccountCodeMargin.ThirdMargin) &&
                         Convert.ToBoolean(item.IsDebit).Equals(Convert.ToBoolean(isDebit)))
                     .OrderBy(item => item.Serial)
                     .OrderBy(item => item.ParentChartOfAccount_CU_ID).ToList());

            case DB_ChartOfAccountCodeMargin.FifthMargin:
                return
                    (ChartOfAccount_cu.ItemsList.FindAll(
                         item =>
                         Convert.ToInt32(item.ChartOfAccountCodeMargin_P_ID).Equals((int)DB_ChartOfAccountCodeMargin.FourthMargin) &&
                         Convert.ToBoolean(item.IsDebit).Equals(Convert.ToBoolean(isDebit)))
                     .OrderBy(item => item.Serial)
                     .OrderBy(item => item.ParentChartOfAccount_CU_ID).ToList());
            }

            return(null);
        }
        public static List <ChartOfAccount_cu> GetChartOfAccountOfPreviousCodeMargin(object chartOfAccountCodeMarginID, bool isDebit)
        {
            if (chartOfAccountCodeMarginID == null)
            {
                return(null);
            }

            ChartOfAccountCodeMargin_p codeMargin =
                ChartOfAccountCodeMargin_p.ItemsList.Find(
                    item => Convert.ToInt32(item.ID).Equals(Convert.ToInt32(chartOfAccountCodeMarginID)));

            if (codeMargin == null)
            {
                return(null);
            }
            DB_ChartOfAccountCodeMargin chartOfAccountCodeMargin = (DB_ChartOfAccountCodeMargin)Convert.ToInt32(codeMargin.ID);

            return(GetChartOfAccountOfPreviousCodeMargin(chartOfAccountCodeMargin, isDebit));
        }
        public static int GetChartOfAccountCodeMarginNumberOfDigits(object chartOfAccountCodeMarginID)
        {
            ChartOfAccountCodeMargin_p chartOfAccountCode = GetChartOfAccountCodeMargin(chartOfAccountCodeMarginID);

            if (chartOfAccountCode == null)
            {
                return(0);
            }

            int numberOfDigits = 0;
            DB_ChartOfAccountCodeMargin chosenAccountCodeMargin = (DB_ChartOfAccountCodeMargin)Convert.ToInt32(chartOfAccountCode.ID);

            switch (chosenAccountCodeMargin)
            {
            case DB_ChartOfAccountCodeMargin.FirstMargin:
                numberOfDigits = numberOfDigits + chartOfAccountCode.NumberOfDigits;
                break;

            case DB_ChartOfAccountCodeMargin.SecondMargin:
                numberOfDigits = numberOfDigits + Convert.ToInt32(chartOfAccountCode.NumberOfDigits) +
                                 GetChartOfAccountCodeMarginNumberOfDigits((int)DB_ChartOfAccountCodeMargin.FirstMargin);
                break;

            case DB_ChartOfAccountCodeMargin.ThirdMargin:
                numberOfDigits = numberOfDigits + Convert.ToInt32(chartOfAccountCode.NumberOfDigits) +
                                 GetChartOfAccountCodeMarginNumberOfDigits((int)DB_ChartOfAccountCodeMargin.SecondMargin);
                break;

            case DB_ChartOfAccountCodeMargin.FourthMargin:
                numberOfDigits = numberOfDigits + Convert.ToInt32(chartOfAccountCode.NumberOfDigits) +
                                 GetChartOfAccountCodeMarginNumberOfDigits((int)DB_ChartOfAccountCodeMargin.ThirdMargin);
                break;

            case DB_ChartOfAccountCodeMargin.FifthMargin:
                numberOfDigits = numberOfDigits + Convert.ToInt32(chartOfAccountCode.NumberOfDigits) +
                                 GetChartOfAccountCodeMarginNumberOfDigits((int)DB_ChartOfAccountCodeMargin.FourthMargin);
                break;
            }

            return(numberOfDigits);
        }
        public override bool Collect(AbstractDataCollector <TEntity> collector)
        {
            if (collector == null)
            {
                return(false);
            }

            ActiveCollector = collector;

            ID = ((IChartOfAccountViewer)ActiveCollector.ActiveViewer).ID;

            if (ActiveDBItem == null)
            {
                return(false);
            }

            if (Name_P != null)
            {
                ((ChartOfAccount_cu)ActiveDBItem).Name_P = Name_P.ToString();
            }

            if (Name_S != null)
            {
                ((ChartOfAccount_cu)ActiveDBItem).Name_S = Name_S.ToString();
            }

            if (Description != null)
            {
                ((ChartOfAccount_cu)ActiveDBItem).Description = Description.ToString();
            }

            DB_ChartOfAccountCodeMargin chartOfAccountCodeMargin = DB_ChartOfAccountCodeMargin.None;

            if (ChartOfAccountCodeMargin_P_ID != null)
            {
                ((ChartOfAccount_cu)ActiveDBItem).ChartOfAccountCodeMargin_P_ID = Convert.ToInt32(ChartOfAccountCodeMargin_P_ID);
                chartOfAccountCodeMargin = (DB_ChartOfAccountCodeMargin)Convert.ToInt32(ChartOfAccountCodeMargin_P_ID);
            }

            if (ParentChartOfAccount_CU_ID != null)
            {
                ((ChartOfAccount_cu)ActiveDBItem).ParentChartOfAccount_CU_ID = Convert.ToInt32(ParentChartOfAccount_CU_ID);
            }

            if (Serial != null)
            {
                switch (chartOfAccountCodeMargin)
                {
                case DB_ChartOfAccountCodeMargin.FirstMargin:
                    ((ChartOfAccount_cu)ActiveDBItem).Serial = Convert.ToInt64(Serial);
                    break;

                case DB_ChartOfAccountCodeMargin.SecondMargin:
                case DB_ChartOfAccountCodeMargin.ThirdMargin:
                case DB_ChartOfAccountCodeMargin.FourthMargin:
                case DB_ChartOfAccountCodeMargin.FifthMargin:
                    ((ChartOfAccount_cu)ActiveDBItem).Serial =
                        AccountingBusinessLogicEngine.GetChartOfAccountSerial(ParentChartOfAccount_CU_ID, Serial);
                    break;
                }
            }

            if (IsDebit != null)
            {
                ((ChartOfAccount_cu)ActiveDBItem).IsDebit = Convert.ToBoolean(IsDebit);
            }

            if (UserID != null)
            {
                ((ChartOfAccount_cu)ActiveDBItem).InsertedBy = Convert.ToInt32(UserID);
            }

            ((ChartOfAccount_cu)ActiveDBItem).IsOnDuty = true;
            switch (((IChartOfAccountViewer)ActiveCollector.ActiveViewer).CommonTransactionType)
            {
            case DB_CommonTransactionType.DeleteExisting:
                ((ChartOfAccount_cu)ActiveDBItem).IsOnDuty = false;
                break;
            }

            RelatedViewers = ((IChartOfAccountViewer)ActiveCollector.ActiveViewer).RelatedViewers;

            return(true);
        }
        public static string GetNextChartOfAccountSerial(DB_ChartOfAccountCodeMargin chartOfAccountCodeMargin,
                                                         object parentChartofAccountID)
        {
            string nextSerial = "1";

            if (ChartOfAccount_cu.ItemsList.Count == 0)
            {
                return(nextSerial);
            }

            ChartOfAccount_cu parentChartOfAccount = null;

            if (parentChartofAccountID != null)
            {
                parentChartOfAccount =
                    ChartOfAccount_cu.ItemsList.Find(item => Convert.ToInt32(item.ID).Equals(Convert.ToInt32(parentChartofAccountID)));
            }

            long parentSerial = -1;

            if (parentChartOfAccount != null)
            {
                parentSerial = parentChartOfAccount.Serial;
            }

            List <ChartOfAccount_cu> chartOfAccountsList =
                ChartOfAccount_cu.ItemsList.FindAll(
                    item => Convert.ToInt32(item.ChartOfAccountCodeMargin_P_ID).Equals((int)chartOfAccountCodeMargin));

            if (parentChartOfAccount != null)
            {
                chartOfAccountsList =
                    chartOfAccountsList.FindAll(
                        item =>
                        item.ParentChartOfAccount_CU_ID != null &&
                        Convert.ToInt32(item.ParentChartOfAccount_CU_ID).Equals(Convert.ToInt32(parentChartofAccountID)));
            }
            ChartOfAccountCodeMargin_p chartOfAccountCodeMarginP =
                ChartOfAccountCodeMargin_p.ItemsList.Find(
                    item => Convert.ToInt32(item.ID).Equals(Convert.ToInt32(chartOfAccountCodeMargin)));
            long              allowedNumerOfDigits                   = GetChartOfAccountCodeMarginNumberOfDigits(Convert.ToInt32(chartOfAccountCodeMargin));
            long              previousAllowedNumberOfDigits          = 0;
            long              chartOfAccountCodeMarginNumberOfDigits = 0;
            string            numberOfZeros      = "";
            ChartOfAccount_cu lastChartOfAccount = null;

            if (chartOfAccountsList.Count > 0)
            {
                lastChartOfAccount = chartOfAccountsList.OrderByDescending(item => item.Serial).ToList().First();
            }
            char[] arry  = null;
            int    index = 0;

            switch (chartOfAccountCodeMargin)
            {
                #region FirstMargin
            case DB_ChartOfAccountCodeMargin.FirstMargin:
                if (chartOfAccountsList.Count == 0)
                {
                    return(nextSerial);
                }
                if (lastChartOfAccount == null)
                {
                    return(nextSerial);
                }
                nextSerial = Convert.ToString(lastChartOfAccount.Serial + 1);
                if (nextSerial.Length > allowedNumerOfDigits)
                {
                    return(String.Empty);
                }
                break;
                #endregion

                #region SecondMargin
            case DB_ChartOfAccountCodeMargin.SecondMargin:
                if (chartOfAccountsList.Count == 0)
                {
                    return("0" + nextSerial);
                }
                if (lastChartOfAccount == null)
                {
                    return("0" + nextSerial);
                }
                nextSerial = Convert.ToString(lastChartOfAccount.Serial + 1);
                previousAllowedNumberOfDigits =
                    GetChartOfAccountCodeMarginNumberOfDigits(Convert.ToInt32(DB_ChartOfAccountCodeMargin.FirstMargin));
                arry = nextSerial.ToCharArray();
                if (arry.Length == 0)
                {
                    return(string.Empty);
                }
                index      = 0;
                nextSerial = "";
                foreach (char character in arry)
                {
                    index++;
                    if (index > previousAllowedNumberOfDigits)
                    {
                        nextSerial = nextSerial + character;
                    }
                }
                if (nextSerial.Length > allowedNumerOfDigits)
                {
                    return(String.Empty);
                }
                break;
                #endregion

                #region ThirdMargin
            case DB_ChartOfAccountCodeMargin.ThirdMargin:
                if (chartOfAccountsList.Count == 0)
                {
                    return("0" + nextSerial);
                }
                if (lastChartOfAccount == null)
                {
                    return("0" + nextSerial);
                }
                nextSerial = Convert.ToString(lastChartOfAccount.Serial + 1);
                previousAllowedNumberOfDigits =
                    GetChartOfAccountCodeMarginNumberOfDigits(Convert.ToInt32(DB_ChartOfAccountCodeMargin.SecondMargin));
                arry = nextSerial.ToCharArray();
                if (arry.Length == 0)
                {
                    return(string.Empty);
                }
                index      = 0;
                nextSerial = "";
                foreach (char character in arry)
                {
                    index++;
                    if (index > previousAllowedNumberOfDigits)
                    {
                        nextSerial = nextSerial + character;
                    }
                }
                if (nextSerial.Length > allowedNumerOfDigits)
                {
                    return(String.Empty);
                }
                break;
                #endregion

                #region FourthMargin
            case DB_ChartOfAccountCodeMargin.FourthMargin:
                if (chartOfAccountsList.Count == 0)
                {
                    return("0" + nextSerial);
                }
                if (lastChartOfAccount == null)
                {
                    return("0" + nextSerial);
                }
                nextSerial = Convert.ToString(lastChartOfAccount.Serial + 1);
                previousAllowedNumberOfDigits =
                    GetChartOfAccountCodeMarginNumberOfDigits(Convert.ToInt32(DB_ChartOfAccountCodeMargin.ThirdMargin));
                arry = nextSerial.ToCharArray();
                if (arry.Length == 0)
                {
                    return(string.Empty);
                }
                index      = 0;
                nextSerial = "";
                foreach (char character in arry)
                {
                    index++;
                    if (index > previousAllowedNumberOfDigits)
                    {
                        nextSerial = nextSerial + character;
                    }
                }
                if (nextSerial.Length > allowedNumerOfDigits)
                {
                    return(String.Empty);
                }
                break;
                #endregion

                #region FifthMargin
            case DB_ChartOfAccountCodeMargin.FifthMargin:
                if (chartOfAccountsList.Count == 0)
                {
                    return("00" + nextSerial);
                }
                if (lastChartOfAccount == null)
                {
                    return("00" + nextSerial);
                }

                previousAllowedNumberOfDigits =
                    GetChartOfAccountCodeMarginNumberOfDigits(Convert.ToInt32(DB_ChartOfAccountCodeMargin.FourthMargin));
                allowedNumerOfDigits = allowedNumerOfDigits - previousAllowedNumberOfDigits;

                for (int i = 0; i < allowedNumerOfDigits; i++)
                {
                    numberOfZeros = numberOfZeros + "0";
                }

                nextSerial = Convert.ToString(chartOfAccountsList.Count + 1);
                nextSerial = Convert.ToInt64(nextSerial).ToString(numberOfZeros);
                break;
                #endregion
            }

            return(nextSerial);
        }