Exemple #1
0
        public HttpResponseMessage PostAllAR(Models.TrnSalesInvoice salesInvoice, String ORId)
        {
            try
            {
                Data.TrnCollectionLine newCollectionLine = new Data.TrnCollectionLine();

                newCollectionLine.ORId             = Convert.ToInt32(ORId);
                newCollectionLine.BranchId         = salesInvoice.BranchId;
                newCollectionLine.AccountId        = salesInvoice.AccountId;
                newCollectionLine.ArticleId        = salesInvoice.CustomerId;
                newCollectionLine.SIId             = salesInvoice.Id;
                newCollectionLine.Particulars      = salesInvoice.DocumentReference;
                newCollectionLine.Amount           = salesInvoice.Amount;
                newCollectionLine.PayTypeId        = (from d in db.MstPayTypes select d.Id).FirstOrDefault();
                newCollectionLine.CheckNumber      = "NA";
                newCollectionLine.CheckDate        = DateTime.Now;
                newCollectionLine.CheckBank        = "NA";
                newCollectionLine.DepositoryBankId = (from d in db.MstArticles where d.ArticleTypeId == 5 select d.Id).FirstOrDefault();
                newCollectionLine.IsClear          = true;

                db.TrnCollectionLines.InsertOnSubmit(newCollectionLine);
                db.SubmitChanges();

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }
Exemple #2
0
        public Int32 insertCollectionLine(Models.TrnCollectionLine collectionLine)
        {
            try
            {
                Data.TrnCollectionLine newCollectionLine = new Data.TrnCollectionLine();
                newCollectionLine.ORId             = collectionLine.ORId;
                newCollectionLine.BranchId         = collectionLine.BranchId;
                newCollectionLine.AccountId        = collectionLine.AccountId;
                newCollectionLine.ArticleId        = collectionLine.ArticleId;
                newCollectionLine.SIId             = collectionLine.SIId;
                newCollectionLine.Particulars      = collectionLine.Particulars;
                newCollectionLine.Amount           = collectionLine.Amount;
                newCollectionLine.PayTypeId        = collectionLine.PayTypeId;
                newCollectionLine.CheckNumber      = collectionLine.CheckNumber;
                newCollectionLine.CheckDate        = Convert.ToDateTime(collectionLine.CheckDate);
                newCollectionLine.CheckBank        = collectionLine.CheckBank;
                newCollectionLine.DepositoryBankId = collectionLine.DepositoryBankId;
                newCollectionLine.IsClear          = collectionLine.IsClear;

                db.TrnCollectionLines.InsertOnSubmit(newCollectionLine);
                db.SubmitChanges();

                return(newCollectionLine.Id);
            }
            catch
            {
                return(0);
            }
        }
        public HttpResponseMessage postCollectionLine(Entities.TrnCollectionLine collectionLine)
        {
            try
            {
                Data.TrnCollectionLine newCollectionLine = new Data.TrnCollectionLine();
                newCollectionLine.CollectionId = collectionLine.CollectionId;
                newCollectionLine.Amount       = collectionLine.Amount;
                newCollectionLine.PayTypeId    = collectionLine.PayTypeId;
                newCollectionLine.CheckNumber  = collectionLine.CheckNumber;
                newCollectionLine.CheckDate    = collectionLine.CheckDate;
                newCollectionLine.CheckBank    = collectionLine.CheckBank;
                newCollectionLine.CreditCardVerificationCode = collectionLine.CreditCardVerificationCode;
                newCollectionLine.CreditCardNumber           = collectionLine.CreditCardNumber;
                newCollectionLine.CreditCardType             = collectionLine.CreditCardType;
                newCollectionLine.CreditCardBank             = collectionLine.CreditCardBank;
                newCollectionLine.GiftCertificateNumber      = collectionLine.GiftCertificateNumber;
                newCollectionLine.OtherInformation           = collectionLine.OtherInformation;
                newCollectionLine.StockInId = collectionLine.StockInId;
                newCollectionLine.AccountId = collectionLine.AccountId;
                db.TrnCollectionLines.InsertOnSubmit(newCollectionLine);
                db.SubmitChanges();

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's wrong from the server."));
            }
        }
        public Int32 postCollectionLine()
        {
            try
            {
                Data.TrnCollectionLine newCollectionLine = new Data.TrnCollectionLine();
                newCollectionLine.CollectionId = CollectionId();
                newCollectionLine.Amount       = 0;
                newCollectionLine.PayTypeId    = PayTypeId();
                newCollectionLine.CheckNumber  = "n/a";
                newCollectionLine.CheckDate    = DateTime.Today;
                newCollectionLine.CheckBank    = "n/a";
                newCollectionLine.CreditCardVerificationCode = "n/a";
                newCollectionLine.CreditCardNumber           = "n/a";
                newCollectionLine.CreditCardType             = "n/a";
                newCollectionLine.CreditCardBank             = "n/a";
                newCollectionLine.GiftCertificateNumber      = "n/a";
                newCollectionLine.OtherInformation           = "n/a";
                newCollectionLine.StockInId = StockInId();
                newCollectionLine.AccountId = AccountId();
                db.TrnCollectionLines.InsertOnSubmit(newCollectionLine);
                db.SubmitChanges();

                return(newCollectionLine.Id);
            }
            catch (Exception e)
            {
                return(0);
            }
        }
Exemple #5
0
        public void InsertCollectionLines(int collectionId)
        {
            try
            {
                foreach (var trncollectionLine in trnListCollectionLines)
                {
                    Data.TrnCollectionLine collectionLine = new Data.TrnCollectionLine
                    {
                        CollectionId = collectionId,
                        Amount       = trncollectionLine.Amount,
                        PayTypeId    = trncollectionLine.PayTypeId,
                        CheckNumber  = trncollectionLine.CheckNumber,
                        CheckDate    = trncollectionLine.CheckDate,
                        CheckBank    = trncollectionLine.CheckBank,
                        CreditCardVerificationCode = trncollectionLine.CreditCardVerificationCode,
                        CreditCardNumber           = trncollectionLine.CreditCardNumber,
                        CreditCardType             = trncollectionLine.CreditCardType,
                        CreditCardBank             = trncollectionLine.CreditCardBank,
                        GiftCertificateNumber      = trncollectionLine.GiftCertificateNumber,
                        OtherInformation           = trncollectionLine.OtherInformation,
                        StockInId = trncollectionLine.StockInId,
                        AccountId = trncollectionLine.AccountId,
                        CreditCardReferenceNumber = trncollectionLine.CreditCardReferenceNumber,
                        CreditCardHolderName      = trncollectionLine.CreditCardHolderName,
                        CreditCardExpiry          = trncollectionLine.CreditCardExpiry
                    };

                    db.TrnCollectionLines.InsertOnSubmit(collectionLine);
                    db.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex);
            }
        }
Exemple #6
0
        public HttpResponseMessage PostAdvances(Models.TrnDisbursementLine disbursementLine, String articleId, String ORId)
        {
            try
            {
                var CustomerAdvancesAccountId = (from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d.CustomerAdvancesAccountId).SingleOrDefault();

                var journals = from d in db.TrnJournals
                               where d.ArticleId == Convert.ToInt32(articleId) &&
                               d.AccountId == CustomerAdvancesAccountId &&
                               d.BranchId == currentBranchId()
                               group d by new
                {
                    BranchId    = d.BranchId,
                    Branch      = d.MstBranch.Branch,
                    AccountId   = d.AccountId,
                    Account     = d.MstAccount.Account,
                    AccountCode = d.MstAccount.AccountCode,
                    ArticleId   = d.ArticleId,
                    Article     = d.MstArticle.Article,
                    RRId        = d.RRId
                } into g
                    select new Models.TrnJournal
                {
                    BranchId     = g.Key.BranchId,
                    Branch       = g.Key.Branch,
                    AccountId    = g.Key.AccountId,
                    Account      = g.Key.Account,
                    AccountCode  = g.Key.AccountCode,
                    ArticleId    = g.Key.ArticleId,
                    Article      = g.Key.Article,
                    RRId         = g.Key.RRId,
                    DebitAmount  = g.Sum(d => d.DebitAmount),
                    CreditAmount = g.Sum(d => d.CreditAmount),
                    Balance      = g.Sum(d => d.CreditAmount) - g.Sum(d => d.DebitAmount)
                };

                if (journals.Any())
                {
                    Data.TrnCollectionLine newCollectionLine = new Data.TrnCollectionLine();

                    var accountId = 0;
                    foreach (var journal in journals)
                    {
                        accountId = (from d in db.MstArticles where d.Id == journal.ArticleId select d.AccountId).SingleOrDefault();

                        newCollectionLine.ORId             = Convert.ToInt32(ORId);
                        newCollectionLine.BranchId         = journal.BranchId;
                        newCollectionLine.AccountId        = accountId;
                        newCollectionLine.ArticleId        = journal.ArticleId;
                        newCollectionLine.SIId             = (from d in db.TrnSalesInvoices select d.Id).FirstOrDefault();
                        newCollectionLine.Particulars      = "Customer Advances";
                        newCollectionLine.Amount           = journal.Balance;
                        newCollectionLine.PayTypeId        = (from d in db.MstPayTypes select d.Id).FirstOrDefault();
                        newCollectionLine.CheckNumber      = "NA";
                        newCollectionLine.CheckDate        = DateTime.Now;
                        newCollectionLine.CheckBank        = "NA";
                        newCollectionLine.DepositoryBankId = null;
                        newCollectionLine.IsClear          = true;
                    }

                    db.TrnCollectionLines.InsertOnSubmit(newCollectionLine);
                    db.SubmitChanges();

                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }
Exemple #7
0
        public HttpResponseMessage insertCollectionLineAccountsReceivable(String SalesId, String ORId)
        {
            try
            {
                var salesInvoices = from d in db.TrnSalesInvoices
                                    where d.BranchId == currentBranchId() &&
                                    d.Id == Convert.ToInt32(SalesId)
                                    select new Models.TrnSalesInvoice
                {
                    Id                = d.Id,
                    BranchId          = d.BranchId,
                    Branch            = d.MstBranch.Branch,
                    SINumber          = d.SINumber,
                    SIDate            = d.SIDate.ToShortDateString(),
                    CustomerId        = d.CustomerId,
                    Customer          = d.MstArticle.Article,
                    TermId            = d.TermId,
                    Term              = d.MstTerm.Term,
                    DocumentReference = d.DocumentReference,
                    ManualSINumber    = d.ManualSINumber,
                    Remarks           = d.Remarks,
                    Amount            = d.Amount,
                    PaidAmount        = d.PaidAmount,
                    AdjustmentAmount  = d.AdjustmentAmount,
                    BalanceAmount     = d.BalanceAmount,
                    AccountId         = d.MstArticle.MstAccount.Id
                };

                if (salesInvoices.Any())
                {
                    Data.TrnCollectionLine newCollectionLine = new Data.TrnCollectionLine();

                    foreach (var salesInvoice in salesInvoices)
                    {
                        newCollectionLine.ORId             = Convert.ToInt32(ORId);
                        newCollectionLine.BranchId         = salesInvoice.BranchId;
                        newCollectionLine.AccountId        = salesInvoice.AccountId;
                        newCollectionLine.ArticleId        = salesInvoice.CustomerId;
                        newCollectionLine.SIId             = Convert.ToInt32(SalesId);
                        newCollectionLine.Particulars      = salesInvoice.DocumentReference;
                        newCollectionLine.Amount           = salesInvoice.Amount;
                        newCollectionLine.PayTypeId        = (from d in db.MstPayTypes select d.Id).FirstOrDefault();
                        newCollectionLine.CheckNumber      = "NA";
                        newCollectionLine.CheckDate        = DateTime.Now;
                        newCollectionLine.CheckBank        = "NA";
                        newCollectionLine.DepositoryBankId = (from d in db.MstArticles where d.ArticleTypeId == 5 select d.Id).FirstOrDefault();
                        newCollectionLine.IsClear          = true;
                    }

                    db.TrnCollectionLines.InsertOnSubmit(newCollectionLine);
                    db.SubmitChanges();

                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
            }
            catch
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }
        public int Post(Models.TrnCollectionLine collectionline)
        {
            try
            {
                var isLocked = true;

                Data.TrnCollectionLine newCollectionLine = new Data.TrnCollectionLine();

                //
                newCollectionLine.CollectionId = collectionline.CollectionId;
                newCollectionLine.Amount = collectionline.Amount;
                newCollectionLine.PayTypeId = collectionline.PayTypeId;
                newCollectionLine.CheckNumber = collectionline.CheckNumber;
                newCollectionLine.CheckDate = Convert.ToDateTime(collectionline.CheckDate);
                newCollectionLine.CheckBank = collectionline.CheckBank;
                newCollectionLine.CreditCardVerificationCode = collectionline.CreditCardVerificationCode;
                newCollectionLine.CreditCardNumber = collectionline.CreditCardNumber;
                newCollectionLine.CreditCardType = collectionline.CreditCardType;
                newCollectionLine.CreditCardBank = collectionline.CreditCardBank;
                newCollectionLine.GiftCertificateNumber = collectionline.GiftCertificateNumber;
                newCollectionLine.OtherInformation = collectionline.OtherInformation;
                newCollectionLine.StockInId = collectionline.StockInId;
                newCollectionLine.AccountId = collectionline.AccountId;
                newCollectionLine.CreditCardReferenceNumber = collectionline.CreditCardReferenceNumber;
                newCollectionLine.CreditCardHolderName = collectionline.CreditCardHolderName;
                newCollectionLine.CreditCardExpiry = collectionline.CreditCardExpiry;
                //

                db.TrnCollectionLines.InsertOnSubmit(newCollectionLine);
                db.SubmitChanges();

                return newCollectionLine.Id;
            }
            catch
            {
                return 0;
            }
        }
Exemple #9
0
        public HttpResponseMessage AddFolderMonitoringCollection(List <Entities.FolderMonitoringTrnCollection> folderMonitoringTrnCollectionObjects)
        {
            try
            {
                if (folderMonitoringTrnCollectionObjects.Any())
                {
                    foreach (var folderMonitoringTrnCollectionObject in folderMonitoringTrnCollectionObjects)
                    {
                        Boolean isBranchExist          = false,
                                 isCustomerExist       = false,
                                 isUserExist           = false,
                                 isAccountExist        = false,
                                 isArticleExist        = false,
                                 isSalesInvoiceExist   = false,
                                 isPayTypeExist        = false,
                                 isDepositoryBankExist = false;

                        IQueryable <Data.TrnSalesInvoice> salesInvoice = null;

                        var branch = from d in db.MstBranches where d.BranchCode.Equals(folderMonitoringTrnCollectionObject.BranchCode) select d;
                        if (branch.Any())
                        {
                            isBranchExist = true;

                            if (!folderMonitoringTrnCollectionObject.SINumber.Equals("") || !folderMonitoringTrnCollectionObject.SINumber.Equals("NA"))
                            {
                                salesInvoice = from d in db.TrnSalesInvoices where d.BranchId == branch.FirstOrDefault().Id&& d.SINumber.Equals(folderMonitoringTrnCollectionObject.SINumber) && d.IsLocked == true select d;
                                if (salesInvoice.Any())
                                {
                                    isSalesInvoiceExist = true;
                                }
                            }
                        }

                        var customer = from d in db.MstArticles where d.ArticleTypeId == 2 && d.ManualArticleCode.Equals(folderMonitoringTrnCollectionObject.CustomerCode) && d.IsLocked == true select d;
                        if (customer.Any())
                        {
                            isCustomerExist = true;
                        }

                        var user = from d in db.MstUsers where d.UserName.Equals(folderMonitoringTrnCollectionObject.UserCode) select d;
                        if (user.Any())
                        {
                            isUserExist = true;
                        }

                        List <easyfis.Entities.MstArticle> listArticles = new List <easyfis.Entities.MstArticle>();

                        var account = from d in db.MstAccounts where d.AccountCode.Equals(folderMonitoringTrnCollectionObject.AccountCode) select d;
                        if (account.Any())
                        {
                            isAccountExist = true;

                            var accountArticleTypes = from d in db.MstAccountArticleTypes where d.AccountId == Convert.ToInt32(account.FirstOrDefault().Id) select d;
                            if (accountArticleTypes.Any())
                            {
                                foreach (var accountArticleType in accountArticleTypes)
                                {
                                    var articles = from d in db.MstArticles where d.ArticleTypeId == accountArticleType.ArticleTypeId && d.IsLocked == true select d;
                                    if (articles.Any())
                                    {
                                        foreach (var articleObject in articles)
                                        {
                                            listArticles.Add(new easyfis.Entities.MstArticle()
                                            {
                                                Id = articleObject.Id,
                                                ManualArticleCode = articleObject.ManualArticleCode,
                                                Article           = articleObject.Article
                                            });
                                        }
                                    }
                                }
                            }
                        }

                        var article = from d in listArticles where d.ManualArticleCode.Equals(folderMonitoringTrnCollectionObject.ArticleCode) select d;
                        if (article.Any())
                        {
                            isArticleExist = true;
                        }

                        var payType = from d in db.MstPayTypes where d.PayType.Equals(folderMonitoringTrnCollectionObject.PayType) select d;
                        if (payType.Any())
                        {
                            isPayTypeExist = true;
                        }

                        var depositoryBank = from d in db.MstArticles where d.ArticleTypeId == 5 && d.ManualArticleCode.Equals(folderMonitoringTrnCollectionObject.DepositoryBankCode) select d;
                        if (depositoryBank.Any())
                        {
                            isDepositoryBankExist = true;
                        }

                        if (isBranchExist && isCustomerExist && isUserExist && isAccountExist && isArticleExist && isPayTypeExist && isDepositoryBankExist)
                        {
                            Int32 ORId = 0;

                            var currentCollection = from d in db.TrnCollections where d.BranchId == branch.FirstOrDefault().Id&& d.ManualORNumber.Equals(folderMonitoringTrnCollectionObject.ManualORNumber) && d.IsLocked == true select d;
                            if (currentCollection.Any())
                            {
                                ORId = currentCollection.FirstOrDefault().Id;

                                var unlockCollection = currentCollection.FirstOrDefault();
                                unlockCollection.IsLocked        = false;
                                unlockCollection.UpdatedById     = user.FirstOrDefault().Id;
                                unlockCollection.UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnCollectionObject.CreatedDateTime);
                                db.SubmitChanges();

                                journal.DeleteOfficialReceiptJournal(ORId);
                            }
                            else
                            {
                                var defaultORNumber = "0000000001";
                                var lastCollection  = from d in db.TrnCollections.OrderByDescending(d => d.Id) where d.BranchId == branch.FirstOrDefault().Id select d;
                                if (lastCollection.Any())
                                {
                                    var ORNumber = Convert.ToInt32(lastCollection.FirstOrDefault().ORNumber) + 0000000001;
                                    defaultORNumber = FillLeadingZeroes(ORNumber, 10);
                                }

                                Data.TrnCollection newCollection = new Data.TrnCollection
                                {
                                    BranchId        = branch.FirstOrDefault().Id,
                                    ORNumber        = defaultORNumber,
                                    ORDate          = Convert.ToDateTime(folderMonitoringTrnCollectionObject.ORDate),
                                    ManualORNumber  = folderMonitoringTrnCollectionObject.ManualORNumber,
                                    CustomerId      = customer.FirstOrDefault().Id,
                                    Particulars     = folderMonitoringTrnCollectionObject.Remarks,
                                    PreparedById    = user.FirstOrDefault().Id,
                                    CheckedById     = user.FirstOrDefault().Id,
                                    ApprovedById    = user.FirstOrDefault().Id,
                                    Status          = null,
                                    IsCancelled     = false,
                                    IsPrinted       = false,
                                    IsLocked        = false,
                                    CreatedById     = user.FirstOrDefault().Id,
                                    CreatedDateTime = Convert.ToDateTime(folderMonitoringTrnCollectionObject.CreatedDateTime),
                                    UpdatedById     = user.FirstOrDefault().Id,
                                    UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnCollectionObject.CreatedDateTime)
                                };

                                db.TrnCollections.InsertOnSubmit(newCollection);
                                db.SubmitChanges();

                                ORId = newCollection.Id;
                            }

                            Int32?SIId = null;
                            if (isSalesInvoiceExist)
                            {
                                SIId = salesInvoice.FirstOrDefault().Id;
                            }

                            Data.TrnCollectionLine newCollectionLine = new Data.TrnCollectionLine
                            {
                                ORId             = ORId,
                                BranchId         = branch.FirstOrDefault().Id,
                                AccountId        = account.FirstOrDefault().Id,
                                ArticleId        = article.FirstOrDefault().Id,
                                SIId             = SIId,
                                Particulars      = folderMonitoringTrnCollectionObject.Particulars,
                                Amount           = folderMonitoringTrnCollectionObject.Amount,
                                PayTypeId        = payType.FirstOrDefault().Id,
                                CheckNumber      = folderMonitoringTrnCollectionObject.CheckNumber,
                                CheckDate        = Convert.ToDateTime(folderMonitoringTrnCollectionObject.CheckDate),
                                CheckBank        = folderMonitoringTrnCollectionObject.CheckBank,
                                DepositoryBankId = depositoryBank.FirstOrDefault().Id,
                                IsClear          = folderMonitoringTrnCollectionObject.IsClear
                            };

                            db.TrnCollectionLines.InsertOnSubmit(newCollectionLine);
                            db.SubmitChanges();

                            var collection = from d in db.TrnCollections where d.Id == ORId && d.IsLocked == false select d;
                            if (collection.Any())
                            {
                                var lockCollection = collection.FirstOrDefault();
                                lockCollection.IsLocked        = true;
                                lockCollection.UpdatedById     = user.FirstOrDefault().Id;
                                lockCollection.UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnCollectionObject.CreatedDateTime);
                                db.SubmitChanges();

                                var collectionLines = from d in lockCollection.TrnCollectionLines where d.SIId != null select d;
                                if (collectionLines.Any())
                                {
                                    foreach (var collectionLine in collectionLines)
                                    {
                                        accountsReceivable.UpdateAccountsReceivable(Convert.ToInt32(collectionLine.SIId));
                                    }
                                }

                                journal.InsertOfficialReceiptJournal(ORId);
                            }
                        }
                    }

                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound, "No data found."));
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }
        public List <Entities.ReturnedDocument> QuintaIntegrationAddSalesInvoice(Entities.RootObject objRoot)
        {
            try
            {
                List <Entities.ReturnedDocument> listSINumber = new List <Entities.ReturnedDocument>();

                var branch = from d in db.MstBranches select d;
                if (branch.Any())
                {
                    Int32 currentBranchId = branch.FirstOrDefault().Id;

                    if (objRoot.TRN.Any())
                    {
                        foreach (var objSales in objRoot.TRN)
                        {
                            if (objSales != null)
                            {
                                Boolean unitExist = false;
                                var     units     = from d in db.MstUnits select d;
                                if (units.Any())
                                {
                                    unitExist = true;
                                }

                                Boolean termExist = false;
                                var     terms     = from d in db.MstTerms where d.Term.Equals(objRoot.DefaultTerm) select d;
                                if (terms.Any())
                                {
                                    termExist = true;
                                }

                                Boolean userExist = false;
                                var     users     = from d in db.MstUsers where d.UserName.Equals("admin") select d;
                                if (users.Any())
                                {
                                    userExist = true;
                                }

                                Int32 customerId = 0;
                                var   customers  = from d in db.MstArticles where d.ManualArticleCode.Equals(objSales.ACI) && d.ArticleTypeId == 2 select d;
                                if (customers.Any())
                                {
                                    customerId = customers.FirstOrDefault().Id;
                                }
                                else
                                {
                                    var defaultCustomerCode = "0000000001";
                                    var lastCustomer        = from d in db.MstArticles.OrderByDescending(d => d.Id) where d.ArticleTypeId == 2 select d;
                                    if (lastCustomer.Any())
                                    {
                                        var customerCode = Convert.ToInt32(lastCustomer.FirstOrDefault().ArticleCode) + 0000000001;
                                        defaultCustomerCode = ZeroFill(customerCode, 10);
                                    }

                                    var articleGroups = from d in db.MstArticleGroups where d.ArticleTypeId == 2 select d;
                                    if (articleGroups.Any())
                                    {
                                        Data.MstArticle newCustomer = new Data.MstArticle
                                        {
                                            ArticleCode       = defaultCustomerCode,
                                            ManualArticleCode = objSales.ACI,
                                            Article           = objSales.ACC,
                                            Category          = "NA",
                                            ArticleTypeId     = 2,
                                            ArticleGroupId    = articleGroups.FirstOrDefault().Id,
                                            AccountId         = articleGroups.FirstOrDefault().AccountId,
                                            SalesAccountId    = articleGroups.FirstOrDefault().SalesAccountId,
                                            CostAccountId     = articleGroups.FirstOrDefault().CostAccountId,
                                            AssetAccountId    = articleGroups.FirstOrDefault().AssetAccountId,
                                            ExpenseAccountId  = articleGroups.FirstOrDefault().ExpenseAccountId,
                                            UnitId            = units.FirstOrDefault().Id,
                                            OutputTaxId       = db.MstTaxTypes.FirstOrDefault().Id,
                                            InputTaxId        = db.MstTaxTypes.FirstOrDefault().Id,
                                            WTaxTypeId        = db.MstTaxTypes.FirstOrDefault().Id,
                                            Price             = 0,
                                            Cost                      = 0,
                                            IsInventory               = false,
                                            IsConsignment             = false,
                                            ConsignmentCostPercentage = 0,
                                            ConsignmentCostValue      = 0,
                                            Particulars               = "NA",
                                            Address                   = "NA",
                                            TermId                    = terms.FirstOrDefault().Id,
                                            ContactNumber             = "NA",
                                            ContactPerson             = "NA",
                                            EmailAddress              = "NA",
                                            TaxNumber                 = "NA",
                                            CreditLimit               = 0,
                                            DateAcquired              = DateTime.Now,
                                            UsefulLife                = 0,
                                            SalvageValue              = 0,
                                            ManualArticleOldCode      = "NA",
                                            Kitting                   = 0,
                                            DefaultSupplierId         = null,
                                            StockLevelQuantity        = 0,
                                            ReorderQuantity           = 0,
                                            IsLocked                  = true,
                                            CreatedById               = users.FirstOrDefault().Id,
                                            CreatedDateTime           = DateTime.Now,
                                            UpdatedById               = users.FirstOrDefault().Id,
                                            UpdatedDateTime           = DateTime.Now
                                        };

                                        db.MstArticles.InsertOnSubmit(newCustomer);
                                        db.SubmitChanges();

                                        customerId = newCustomer.Id;
                                    }
                                }

                                Boolean taxTypeExist = false;
                                var     taxTypes     = from d in db.MstTaxTypes select d;
                                if (taxTypes.Any())
                                {
                                    taxTypeExist = true;
                                }

                                Boolean discountExist = false;
                                var     discounts     = from d in db.MstDiscounts where d.Discount.Equals(objRoot.DefaultDiscount) select d;
                                if (discounts.Any())
                                {
                                    discountExist = true;
                                }

                                if (userExist)
                                {
                                    if (termExist)
                                    {
                                        if (customerId != 0)
                                        {
                                            Int32 salesInvoiceId = 0;

                                            var SINumberResult   = "0000000001";
                                            var lastSalesInvoice = from d in db.TrnSalesInvoices.OrderByDescending(d => d.Id)
                                                                   where d.BranchId == currentBranchId
                                                                   select d;

                                            if (lastSalesInvoice.Any())
                                            {
                                                var SINumber = Convert.ToInt32(lastSalesInvoice.FirstOrDefault().SINumber) + 0000000001;
                                                SINumberResult = ZeroFill(SINumber, 10);
                                            }

                                            Data.TrnSalesInvoice addSalesInvoice = new Data.TrnSalesInvoice
                                            {
                                                BranchId          = currentBranchId,
                                                SINumber          = SINumberResult,
                                                SIDate            = Convert.ToDateTime(objSales.TDT),
                                                CustomerId        = customerId,
                                                TermId            = terms.FirstOrDefault().Id,
                                                DocumentReference = objSales.FTN,
                                                ManualSINumber    = objSales.FTN,
                                                Remarks           = "NA",
                                                Amount            = objSales.NAM,
                                                PaidAmount        = 0,
                                                AdjustmentAmount  = 0,
                                                BalanceAmount     = objSales.NAM,
                                                SoldById          = users.FirstOrDefault().Id,
                                                PreparedById      = users.FirstOrDefault().Id,
                                                CheckedById       = users.FirstOrDefault().Id,
                                                ApprovedById      = users.FirstOrDefault().Id,
                                                IsLocked          = true,
                                                CreatedById       = users.FirstOrDefault().Id,
                                                CreatedDateTime   = DateTime.Now,
                                                UpdatedById       = users.FirstOrDefault().Id,
                                                UpdatedDateTime   = DateTime.Now
                                            };

                                            db.TrnSalesInvoices.InsertOnSubmit(addSalesInvoice);
                                            db.SubmitChanges();

                                            salesInvoiceId = addSalesInvoice.Id;

                                            listSINumber.Add(new Entities.ReturnedDocument()
                                            {
                                                SINumber       = addSalesInvoice.SINumber,
                                                ManualSINumber = addSalesInvoice.ManualSINumber
                                            });

                                            if (salesInvoiceId != 0)
                                            {
                                                Int32 itemId = 0;
                                                var   items  = from d in db.MstArticles where d.ManualArticleCode.Equals(objSales.SAI) && d.ArticleTypeId == 1 select d;
                                                if (items.Any())
                                                {
                                                    itemId = items.FirstOrDefault().Id;
                                                }
                                                else
                                                {
                                                    var defaultItemCode = "0000000001";
                                                    var lastItem        = from d in db.MstArticles.OrderByDescending(d => d.Id) where d.ArticleTypeId == 1 select d;
                                                    if (lastItem.Any())
                                                    {
                                                        var itemCode = Convert.ToInt32(lastItem.FirstOrDefault().ArticleCode) + 0000000001;
                                                        defaultItemCode = ZeroFill(itemCode, 10);
                                                    }

                                                    var articleGroups = from d in db.MstArticleGroups where d.ArticleTypeId == 1 select d;
                                                    if (articleGroups.Any())
                                                    {
                                                        Data.MstArticle newItem = new Data.MstArticle
                                                        {
                                                            ArticleCode       = defaultItemCode,
                                                            ManualArticleCode = objSales.SAI,
                                                            Article           = objSales.SAM,
                                                            Category          = "NA",
                                                            ArticleTypeId     = 1,
                                                            ArticleGroupId    = articleGroups.FirstOrDefault().Id,
                                                            AccountId         = articleGroups.FirstOrDefault().AccountId,
                                                            SalesAccountId    = articleGroups.FirstOrDefault().SalesAccountId,
                                                            CostAccountId     = articleGroups.FirstOrDefault().CostAccountId,
                                                            AssetAccountId    = articleGroups.FirstOrDefault().AssetAccountId,
                                                            ExpenseAccountId  = articleGroups.FirstOrDefault().ExpenseAccountId,
                                                            UnitId            = units.FirstOrDefault().Id,
                                                            OutputTaxId       = taxTypes.Where(d => d.TaxType.Equals(objRoot.DefaultVatOutput)).FirstOrDefault().Id,
                                                            InputTaxId        = taxTypes.Where(d => d.TaxType.Equals(objRoot.DefaultVatInput)).FirstOrDefault().Id,
                                                            WTaxTypeId        = taxTypes.Where(d => d.TaxType.Equals(objRoot.DefaultWTax)).FirstOrDefault().Id,
                                                            Price             = objSales.NAM,
                                                            Cost                      = 0,
                                                            IsInventory               = false,
                                                            IsConsignment             = false,
                                                            ConsignmentCostPercentage = 0,
                                                            ConsignmentCostValue      = 0,
                                                            Particulars               = "NA",
                                                            Address                   = "NA",
                                                            TermId                    = terms.FirstOrDefault().Id,
                                                            ContactNumber             = "NA",
                                                            ContactPerson             = "NA",
                                                            EmailAddress              = "NA",
                                                            TaxNumber                 = "NA",
                                                            CreditLimit               = 0,
                                                            DateAcquired              = DateTime.Now,
                                                            UsefulLife                = 0,
                                                            SalvageValue              = 0,
                                                            ManualArticleOldCode      = "NA",
                                                            Kitting                   = 0,
                                                            DefaultSupplierId         = null,
                                                            StockLevelQuantity        = 0,
                                                            ReorderQuantity           = 0,
                                                            IsLocked                  = true,
                                                            CreatedById               = users.FirstOrDefault().Id,
                                                            CreatedDateTime           = DateTime.Now,
                                                            UpdatedById               = users.FirstOrDefault().Id,
                                                            UpdatedDateTime           = DateTime.Now
                                                        };

                                                        db.MstArticles.InsertOnSubmit(newItem);
                                                        db.SubmitChanges();

                                                        itemId = newItem.Id;

                                                        Data.MstArticleUnit newItemUnitConversion = new Data.MstArticleUnit
                                                        {
                                                            ArticleId   = Convert.ToInt32(itemId),
                                                            UnitId      = units.FirstOrDefault().Id,
                                                            Multiplier  = 1,
                                                            IsCountUnit = false
                                                        };

                                                        db.MstArticleUnits.InsertOnSubmit(newItemUnitConversion);
                                                        db.SubmitChanges();

                                                        Data.MstArticlePrice newItemPrice = new Data.MstArticlePrice
                                                        {
                                                            ArticleId        = Convert.ToInt32(itemId),
                                                            PriceDescription = "SRP",
                                                            Price            = objSales.NAM,
                                                            Remarks          = "NA"
                                                        };

                                                        db.MstArticlePrices.InsertOnSubmit(newItemPrice);
                                                        db.SubmitChanges();
                                                    }
                                                }

                                                if (itemId != 0)
                                                {
                                                    if (unitExist)
                                                    {
                                                        if (taxTypeExist)
                                                        {
                                                            if (discountExist)
                                                            {
                                                                Int32?itemInventoryId  = null;
                                                                var   articleInventory = from d in db.MstArticleInventories
                                                                                         where d.BranchId == currentBranchId &&
                                                                                         d.ArticleId == itemId
                                                                                         select d;

                                                                if (articleInventory.Any())
                                                                {
                                                                    itemInventoryId = articleInventory.FirstOrDefault().Id;
                                                                }

                                                                var conversionUnit = from d in db.MstArticleUnits
                                                                                     where d.ArticleId == itemId &&
                                                                                     d.UnitId == items.FirstOrDefault().UnitId
                                                                                     select d;

                                                                if (conversionUnit.Any())
                                                                {
                                                                    Decimal baseQuantity = 1;
                                                                    Decimal basePrice    = objSales.NAM;

                                                                    if (conversionUnit.FirstOrDefault().Multiplier > 0)
                                                                    {
                                                                        baseQuantity = 1 * (1 / conversionUnit.FirstOrDefault().Multiplier);
                                                                    }

                                                                    if (baseQuantity > 0)
                                                                    {
                                                                        basePrice = objSales.NAM / baseQuantity;
                                                                    }

                                                                    Data.TrnSalesInvoiceItem newSalesInvoiceItem = new Data.TrnSalesInvoiceItem
                                                                    {
                                                                        SIId               = salesInvoiceId,
                                                                        ItemId             = itemId,
                                                                        ItemInventoryId    = itemInventoryId,
                                                                        Particulars        = "NA",
                                                                        UnitId             = units.FirstOrDefault().Id,
                                                                        Quantity           = 1,
                                                                        Price              = objSales.NAM,
                                                                        DiscountId         = discounts.FirstOrDefault().Id,
                                                                        DiscountRate       = discounts.FirstOrDefault().DiscountRate,
                                                                        DiscountAmount     = 0,
                                                                        NetPrice           = objSales.NAM,
                                                                        Amount             = objSales.NAM,
                                                                        VATId              = items.FirstOrDefault().OutputTaxId,
                                                                        VATPercentage      = items.FirstOrDefault().MstTaxType.TaxRate,
                                                                        VATAmount          = (objSales.NAM / (1 + (items.FirstOrDefault().MstTaxType.TaxRate / 100))) * (items.FirstOrDefault().MstTaxType.TaxRate / 100),
                                                                        BaseUnitId         = items.FirstOrDefault().UnitId,
                                                                        BaseQuantity       = baseQuantity,
                                                                        BasePrice          = basePrice,
                                                                        SalesItemTimeStamp = DateTime.Now
                                                                    };

                                                                    db.TrnSalesInvoiceItems.InsertOnSubmit(newSalesInvoiceItem);
                                                                    db.SubmitChanges();
                                                                }
                                                            }
                                                        }
                                                    }
                                                }

                                                var currentSalesInvoice = from d in db.TrnSalesInvoices where d.Id == salesInvoiceId select d;
                                                if (currentSalesInvoice.Any())
                                                {
                                                    Decimal salesAmount      = 0;
                                                    Decimal paidAmount       = currentSalesInvoice.FirstOrDefault().PaidAmount;
                                                    Decimal adjustmentAmount = currentSalesInvoice.FirstOrDefault().AdjustmentAmount;

                                                    var salesInvoiceItems = from d in db.TrnSalesInvoiceItems where d.SIId == salesInvoiceId select d;
                                                    if (salesInvoiceItems.Any())
                                                    {
                                                        salesAmount = salesInvoiceItems.Sum(d => d.Amount);
                                                    }

                                                    var updateSalesInvoiceAmount = currentSalesInvoice.FirstOrDefault();
                                                    updateSalesInvoiceAmount.Amount        = salesAmount;
                                                    updateSalesInvoiceAmount.BalanceAmount = (salesAmount - paidAmount) + adjustmentAmount;

                                                    db.SubmitChanges();
                                                }

                                                inventory.InsertSalesInvoiceInventory(Convert.ToInt32(salesInvoiceId));
                                                journal.InsertSalesInvoiceJournal(Convert.ToInt32(salesInvoiceId));

                                                if (currentSalesInvoice.Any())
                                                {
                                                    var defaultORNumber = "0000000001";
                                                    var lastCollection  = from d in db.TrnCollections.OrderByDescending(d => d.Id)
                                                                          where d.BranchId == currentBranchId
                                                                          select d;

                                                    if (lastCollection.Any())
                                                    {
                                                        var ORNumber = Convert.ToInt32(lastCollection.FirstOrDefault().ORNumber) + 0000000001;
                                                        defaultORNumber = ZeroFill(ORNumber, 10);
                                                    }

                                                    Data.TrnCollection newCollection = new Data.TrnCollection
                                                    {
                                                        BranchId        = currentBranchId,
                                                        ORNumber        = defaultORNumber,
                                                        ORDate          = Convert.ToDateTime(objSales.ADT),
                                                        ManualORNumber  = "NA",
                                                        CustomerId      = customers.FirstOrDefault().Id,
                                                        Particulars     = "NA",
                                                        PreparedById    = users.FirstOrDefault().Id,
                                                        CheckedById     = users.FirstOrDefault().Id,
                                                        ApprovedById    = users.FirstOrDefault().Id,
                                                        IsLocked        = true,
                                                        CreatedById     = users.FirstOrDefault().Id,
                                                        CreatedDateTime = DateTime.Now,
                                                        UpdatedById     = users.FirstOrDefault().Id,
                                                        UpdatedDateTime = DateTime.Now
                                                    };

                                                    db.TrnCollections.InsertOnSubmit(newCollection);
                                                    db.SubmitChanges();

                                                    Boolean payTypeExist = false;
                                                    var     payTypes     = from d in db.MstPayTypes select d;
                                                    if (payTypes.Any())
                                                    {
                                                        payTypeExist = true;
                                                    }

                                                    Boolean bankExist = false;
                                                    var     banks     = from d in db.MstArticles where d.ArticleTypeId == 5 select d;
                                                    if (banks.Any())
                                                    {
                                                        bankExist = true;
                                                    }

                                                    if (payTypeExist)
                                                    {
                                                        if (bankExist)
                                                        {
                                                            Data.TrnCollectionLine newCollectionLine = new Data.TrnCollectionLine
                                                            {
                                                                ORId             = newCollection.Id,
                                                                BranchId         = currentBranchId,
                                                                AccountId        = currentSalesInvoice.FirstOrDefault().MstArticle.AccountId,
                                                                ArticleId        = currentSalesInvoice.FirstOrDefault().CustomerId,
                                                                SIId             = currentSalesInvoice.FirstOrDefault().Id,
                                                                Particulars      = currentSalesInvoice.FirstOrDefault().Remarks,
                                                                Amount           = objSales.NAM,
                                                                PayTypeId        = payTypes.FirstOrDefault().Id,
                                                                CheckNumber      = "NA",
                                                                CheckDate        = DateTime.Today,
                                                                CheckBank        = "NA",
                                                                DepositoryBankId = banks.FirstOrDefault().Id,
                                                                IsClear          = true
                                                            };

                                                            db.TrnCollectionLines.InsertOnSubmit(newCollectionLine);
                                                            db.SubmitChanges();

                                                            journal.InsertOfficialReceiptJournal(Convert.ToInt32(newCollection.Id));
                                                            accountsReceivable.UpdateAccountsReceivable(currentSalesInvoice.FirstOrDefault().Id);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    return(listSINumber.ToList());
                }
                else
                {
                    return(new List <Entities.ReturnedDocument>());
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(new List <Entities.ReturnedDocument>());
            }
        }
Exemple #11
0
        public HttpResponseMessage AddFolderMonitoringCollection(List <Entities.FolderMonitoringTrnCollection> folderMonitoringTrnCollectionObjects)
        {
            try
            {
                if (folderMonitoringTrnCollectionObjects.Any())
                {
                    String returnMessage = "";

                    List <Entities.FolderMonitoringTrnCollectionHeader> newFolderMonitoringTrnCollectionHeaders = new List <Entities.FolderMonitoringTrnCollectionHeader>();
                    List <Entities.FolderMonitoringTrnCollectionLine>   newFolderMonitoringTrnCollectionLines   = new List <Entities.FolderMonitoringTrnCollectionLine>();

                    Boolean isDataValid = false;

                    foreach (var folderMonitoringTrnCollectionObject in folderMonitoringTrnCollectionObjects)
                    {
                        Boolean isBranchExist          = false,
                                 isCustomerExist       = false,
                                 isUserExist           = false,
                                 isAccountExist        = false,
                                 isArticleExist        = false,
                                 isLineBranchExist     = false,
                                 isPayTypeExist        = false,
                                 isDepositoryBankExist = false;

                        var branch = from d in db.MstBranches where d.BranchCode.Equals(folderMonitoringTrnCollectionObject.BranchCode) select d;
                        if (branch.Any())
                        {
                            isBranchExist = true;
                        }

                        var customer = from d in db.MstArticles where d.ArticleTypeId == 2 && d.ManualArticleCode.Equals(folderMonitoringTrnCollectionObject.CustomerCode) && d.IsLocked == true select d;
                        if (customer.Any())
                        {
                            isCustomerExist = true;
                        }

                        var user = from d in db.MstUsers where d.UserName.Equals(folderMonitoringTrnCollectionObject.UserCode) select d;
                        if (user.Any())
                        {
                            isUserExist = true;
                        }

                        var lineBranch = from d in db.MstBranches where d.BranchCode.Equals(folderMonitoringTrnCollectionObject.LineBranchCode) select d;
                        if (lineBranch.Any())
                        {
                            isLineBranchExist = true;
                        }

                        List <easyfis.Entities.MstArticle> listArticles = new List <easyfis.Entities.MstArticle>();
                        var account = from d in db.MstAccounts where d.AccountCode.Equals(folderMonitoringTrnCollectionObject.AccountCode) select d;
                        if (account.Any())
                        {
                            isAccountExist = true;

                            var accountArticleTypes = from d in db.MstAccountArticleTypes
                                                      where d.AccountId == Convert.ToInt32(account.FirstOrDefault().Id)
                                                      group d by d.ArticleTypeId into g
                                                      select g;

                            if (accountArticleTypes.Any())
                            {
                                foreach (var accountArticleType in accountArticleTypes)
                                {
                                    var articles = from d in db.MstArticles
                                                   where d.ArticleTypeId == accountArticleType.Key &&
                                                   d.IsLocked == true
                                                   select d;

                                    if (articles.Any())
                                    {
                                        foreach (var articleObject in articles)
                                        {
                                            listArticles.Add(new easyfis.Entities.MstArticle()
                                            {
                                                Id = articleObject.Id,
                                                ManualArticleCode = articleObject.ManualArticleCode,
                                                Article           = articleObject.Article
                                            });
                                        }
                                    }
                                }
                            }
                        }

                        var article = from d in listArticles where d.ManualArticleCode.Equals(folderMonitoringTrnCollectionObject.ArticleCode) select d;
                        if (article.Any())
                        {
                            isArticleExist = true;
                        }

                        var payType = from d in db.MstPayTypes where d.PayType.Equals(folderMonitoringTrnCollectionObject.PayType) select d;
                        if (payType.Any())
                        {
                            isPayTypeExist = true;
                        }

                        var depositoryBank = from d in db.MstArticles where d.ArticleTypeId == 5 && d.ManualArticleCode.Equals(folderMonitoringTrnCollectionObject.DepositoryBankCode) select d;
                        if (depositoryBank.Any())
                        {
                            isDepositoryBankExist = true;
                        }

                        if (!isBranchExist)
                        {
                            returnMessage = "Invalid Branch Code: " + folderMonitoringTrnCollectionObject.BranchCode; isDataValid = false; break;
                        }
                        else if (!isCustomerExist)
                        {
                            returnMessage = "Invalid Customer Code: " + folderMonitoringTrnCollectionObject.CustomerCode; isDataValid = false; break;
                        }
                        else if (!isUserExist)
                        {
                            returnMessage = "Invalid User Code: " + folderMonitoringTrnCollectionObject.UserCode; isDataValid = false; break;
                        }
                        else if (!isLineBranchExist)
                        {
                            returnMessage = "Invalid Line Branch Code: " + folderMonitoringTrnCollectionObject.LineBranchCode; isDataValid = false; break;
                        }
                        else if (!isAccountExist)
                        {
                            returnMessage = "Invalid Account Code: " + folderMonitoringTrnCollectionObject.AccountCode; isDataValid = false; break;
                        }
                        else if (!isArticleExist)
                        {
                            returnMessage = "Invalid Article Code: " + folderMonitoringTrnCollectionObject.ArticleCode; isDataValid = false; break;
                        }
                        else if (!isPayTypeExist)
                        {
                            returnMessage = "Invalid Pay Type: " + folderMonitoringTrnCollectionObject.PayType; isDataValid = false; break;
                        }
                        else if (!isDepositoryBankExist)
                        {
                            returnMessage = "Invalid Depository Bank Code: " + folderMonitoringTrnCollectionObject.DepositoryBankCode; isDataValid = false; break;
                        }
                        else
                        {
                            var collection = from d in db.TrnCollections
                                             where d.BranchId == branch.FirstOrDefault().Id &&
                                             d.ManualORNumber.Equals(folderMonitoringTrnCollectionObject.ManualORNumber) &&
                                             d.IsLocked == true
                                             select d;

                            if (collection.Any())
                            {
                                returnMessage = "This Manual OR No. " + folderMonitoringTrnCollectionObject.ManualORNumber + " is already exist!";
                                isDataValid   = false;

                                break;
                            }
                            else
                            {
                                Int32?SIId = null;
                                if (!folderMonitoringTrnCollectionObject.ManualSINumber.Equals("") || !folderMonitoringTrnCollectionObject.ManualSINumber.Equals("NA"))
                                {
                                    var salesInvoice = from d in db.TrnSalesInvoices
                                                       where d.BranchId == branch.FirstOrDefault().Id &&
                                                       d.ManualSINumber.Equals(folderMonitoringTrnCollectionObject.ManualSINumber) &&
                                                       d.IsLocked == true
                                                       select d;

                                    if (salesInvoice.Any())
                                    {
                                        SIId = salesInvoice.FirstOrDefault().Id;
                                    }
                                }

                                var folderMonitoringTrnCollectionHeader = from d in newFolderMonitoringTrnCollectionHeaders
                                                                          where d.BranchId == branch.FirstOrDefault().Id &&
                                                                          d.ManualORNumber.Equals(folderMonitoringTrnCollectionObject.ManualORNumber)
                                                                          select d;

                                if (folderMonitoringTrnCollectionHeader.Any())
                                {
                                    var currentFolderMonitoringTrnCollectionHeader = folderMonitoringTrnCollectionHeader.FirstOrDefault();
                                    currentFolderMonitoringTrnCollectionHeader.ListFolderMonitoringTrnCollectionLines.Add(new Entities.FolderMonitoringTrnCollectionLine()
                                    {
                                        ManualORNumber = folderMonitoringTrnCollectionObject.ManualORNumber,
                                        BranchId       = lineBranch.FirstOrDefault().Id,
                                        AccountId      = account.FirstOrDefault().Id,
                                        ArticleId      = article.FirstOrDefault().Id,
                                        SIId           = SIId,
                                        Particulars    = folderMonitoringTrnCollectionObject.Particulars,
                                        Amount         = folderMonitoringTrnCollectionObject.Amount,
                                        PayTypeId      = payType.FirstOrDefault().Id,
                                        CheckNumber    = folderMonitoringTrnCollectionObject.CheckNumber,
                                        CheckDate      = Convert.ToDateTime(folderMonitoringTrnCollectionObject.CheckDate),
                                        CheckBank      = folderMonitoringTrnCollectionObject.CheckBank,
                                        IsClear        = folderMonitoringTrnCollectionObject.IsClear
                                    });
                                }
                                else
                                {
                                    newFolderMonitoringTrnCollectionLines = new List <Entities.FolderMonitoringTrnCollectionLine>
                                    {
                                        new Entities.FolderMonitoringTrnCollectionLine()
                                        {
                                            ManualORNumber = folderMonitoringTrnCollectionObject.ManualORNumber,
                                            BranchId       = lineBranch.FirstOrDefault().Id,
                                            AccountId      = account.FirstOrDefault().Id,
                                            ArticleId      = article.FirstOrDefault().Id,
                                            SIId           = SIId,
                                            Particulars    = folderMonitoringTrnCollectionObject.Particulars,
                                            Amount         = folderMonitoringTrnCollectionObject.Amount,
                                            PayTypeId      = payType.FirstOrDefault().Id,
                                            CheckNumber    = folderMonitoringTrnCollectionObject.CheckNumber,
                                            CheckDate      = Convert.ToDateTime(folderMonitoringTrnCollectionObject.CheckDate),
                                            CheckBank      = folderMonitoringTrnCollectionObject.CheckBank,
                                            IsClear        = folderMonitoringTrnCollectionObject.IsClear
                                        }
                                    };

                                    if (newFolderMonitoringTrnCollectionLines.Any())
                                    {
                                        newFolderMonitoringTrnCollectionHeaders.Add(new Entities.FolderMonitoringTrnCollectionHeader()
                                        {
                                            BranchId        = branch.FirstOrDefault().Id,
                                            ORNumber        = "0000000000",
                                            ORDate          = Convert.ToDateTime(folderMonitoringTrnCollectionObject.ORDate),
                                            CustomerId      = customer.FirstOrDefault().Id,
                                            Particulars     = folderMonitoringTrnCollectionObject.Remarks,
                                            ManualORNumber  = folderMonitoringTrnCollectionObject.ManualORNumber,
                                            PreparedById    = user.FirstOrDefault().Id,
                                            CheckedById     = user.FirstOrDefault().Id,
                                            ApprovedById    = user.FirstOrDefault().Id,
                                            Status          = null,
                                            IsCancelled     = false,
                                            IsPrinted       = false,
                                            IsLocked        = false,
                                            CreatedById     = user.FirstOrDefault().Id,
                                            CreatedDateTime = Convert.ToDateTime(folderMonitoringTrnCollectionObject.CreatedDateTime),
                                            UpdatedById     = user.FirstOrDefault().Id,
                                            UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnCollectionObject.CreatedDateTime),
                                            ListFolderMonitoringTrnCollectionLines = newFolderMonitoringTrnCollectionLines
                                        });
                                    }
                                }

                                isDataValid = true;
                            }
                        }
                    }

                    if (isDataValid)
                    {
                        if (newFolderMonitoringTrnCollectionHeaders.Any())
                        {
                            foreach (var newFolderMonitoringTrnCollectionHeader in newFolderMonitoringTrnCollectionHeaders)
                            {
                                Int32 ORId = 0;

                                var defaultORNumber = "0000000001";
                                var lastCollection  = from d in db.TrnCollections.OrderByDescending(d => d.Id) where d.BranchId == newFolderMonitoringTrnCollectionHeader.BranchId select d;
                                if (lastCollection.Any())
                                {
                                    var ORNumber = Convert.ToInt32(lastCollection.FirstOrDefault().ORNumber) + 0000000001;
                                    defaultORNumber = FillLeadingZeroes(ORNumber, 10);
                                }

                                Data.TrnCollection newCollection = new Data.TrnCollection
                                {
                                    BranchId        = newFolderMonitoringTrnCollectionHeader.BranchId,
                                    ORNumber        = defaultORNumber,
                                    ORDate          = newFolderMonitoringTrnCollectionHeader.ORDate,
                                    ManualORNumber  = newFolderMonitoringTrnCollectionHeader.ManualORNumber,
                                    CustomerId      = newFolderMonitoringTrnCollectionHeader.CustomerId,
                                    Particulars     = newFolderMonitoringTrnCollectionHeader.Particulars,
                                    PreparedById    = newFolderMonitoringTrnCollectionHeader.PreparedById,
                                    CheckedById     = newFolderMonitoringTrnCollectionHeader.CheckedById,
                                    ApprovedById    = newFolderMonitoringTrnCollectionHeader.ApprovedById,
                                    Status          = newFolderMonitoringTrnCollectionHeader.Status,
                                    IsCancelled     = newFolderMonitoringTrnCollectionHeader.IsCancelled,
                                    IsPrinted       = newFolderMonitoringTrnCollectionHeader.IsPrinted,
                                    IsLocked        = newFolderMonitoringTrnCollectionHeader.IsLocked,
                                    CreatedById     = newFolderMonitoringTrnCollectionHeader.CreatedById,
                                    CreatedDateTime = newFolderMonitoringTrnCollectionHeader.CreatedDateTime,
                                    UpdatedById     = newFolderMonitoringTrnCollectionHeader.UpdatedById,
                                    UpdatedDateTime = newFolderMonitoringTrnCollectionHeader.UpdatedDateTime
                                };

                                db.TrnCollections.InsertOnSubmit(newCollection);
                                db.SubmitChanges();

                                ORId = newCollection.Id;

                                if (newFolderMonitoringTrnCollectionHeader.ListFolderMonitoringTrnCollectionLines.Any())
                                {
                                    foreach (var listFolderMonitoringTrnCollectionLine in newFolderMonitoringTrnCollectionHeader.ListFolderMonitoringTrnCollectionLines)
                                    {
                                        Data.TrnCollectionLine newCollectionLine = new Data.TrnCollectionLine
                                        {
                                            ORId             = ORId,
                                            BranchId         = listFolderMonitoringTrnCollectionLine.BranchId,
                                            AccountId        = listFolderMonitoringTrnCollectionLine.AccountId,
                                            ArticleId        = listFolderMonitoringTrnCollectionLine.ArticleId,
                                            SIId             = listFolderMonitoringTrnCollectionLine.SIId,
                                            Particulars      = listFolderMonitoringTrnCollectionLine.Particulars,
                                            Amount           = listFolderMonitoringTrnCollectionLine.Amount,
                                            PayTypeId        = listFolderMonitoringTrnCollectionLine.PayTypeId,
                                            CheckNumber      = listFolderMonitoringTrnCollectionLine.CheckNumber,
                                            CheckDate        = listFolderMonitoringTrnCollectionLine.CheckDate,
                                            CheckBank        = listFolderMonitoringTrnCollectionLine.CheckBank,
                                            DepositoryBankId = listFolderMonitoringTrnCollectionLine.DepositoryBankId,
                                            IsClear          = listFolderMonitoringTrnCollectionLine.IsClear
                                        };

                                        db.TrnCollectionLines.InsertOnSubmit(newCollectionLine);
                                    }

                                    db.SubmitChanges();
                                }

                                var collection = from d in db.TrnCollections
                                                 where d.Id == ORId
                                                 select d;

                                if (collection.Any())
                                {
                                    var lockCollection = collection.FirstOrDefault();
                                    lockCollection.IsLocked = true;
                                    db.SubmitChanges();

                                    journal.InsertOfficialReceiptJournal(ORId);
                                }

                                var collectionLines = from d in db.TrnCollectionLines
                                                      where d.ORId == ORId &&
                                                      d.SIId != null &&
                                                      d.TrnCollection.IsLocked == true
                                                      select d;

                                if (collectionLines.Any())
                                {
                                    foreach (var collectionLine in collectionLines)
                                    {
                                        accountsReceivable.UpdateAccountsReceivable(Convert.ToInt32(collectionLine.SIId));
                                    }
                                }
                            }
                        }
                    }

                    return(Request.CreateResponse(HttpStatusCode.OK, returnMessage));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound, "Data Source is Invalid or Empty."));
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Backend Error: " + ex.Message));
            }
        }