public HttpResponseMessage AddFolderMonitoringStockOut(List <Entities.FolderMonitoringTrnStockOut> folderMonitoringTrnStockOutObjects)
        {
            try
            {
                if (folderMonitoringTrnStockOutObjects.Any())
                {
                    String returnMessage = "";

                    List <Entities.FolderMonitoringTrnStockOutHeader> newFolderMonitoringTrnStockOutHeaders = new List <Entities.FolderMonitoringTrnStockOutHeader>();
                    List <Entities.FolderMonitoringTrnStockOutItem>   newFolderMonitoringTrnStockOutItems   = new List <Entities.FolderMonitoringTrnStockOutItem>();

                    Boolean isDataValid = false;

                    foreach (var folderMonitoringTrnStockOutObject in folderMonitoringTrnStockOutObjects)
                    {
                        Boolean isBranchExist   = false,
                                 isAccountExist = false,
                                 isArticleExist = false,
                                 isUserExist    = false,
                                 isItemExist    = false,
                                 isUnitExist    = false;

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

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

                        var account = from d in db.MstAccounts where d.AccountCode.Equals(folderMonitoringTrnStockOutObject.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(folderMonitoringTrnStockOutObject.ArticleCode) select d;
                        if (article.Any())
                        {
                            isArticleExist = true;
                        }

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

                        var item = from d in db.MstArticles where d.ArticleTypeId == 1 && d.ManualArticleCode.Equals(folderMonitoringTrnStockOutObject.ItemCode) && d.IsInventory == true && d.IsLocked == true select d;
                        if (item.Any())
                        {
                            isItemExist = true;
                        }

                        var unit = from d in db.MstUnits where d.Unit.Equals(folderMonitoringTrnStockOutObject.Unit) select d;
                        if (unit.Any())
                        {
                            isUnitExist = true;
                        }

                        if (!isBranchExist)
                        {
                            returnMessage = "Invalid Branch Code: " + folderMonitoringTrnStockOutObject.BranchCode; isDataValid = false; break;
                        }
                        else if (!isAccountExist)
                        {
                            returnMessage = "Invalid Account Code: " + folderMonitoringTrnStockOutObject.AccountCode; isDataValid = false; break;
                        }
                        else if (!isArticleExist)
                        {
                            returnMessage = "Invalid Article Code: " + folderMonitoringTrnStockOutObject.ArticleCode; isDataValid = false; break;
                        }
                        else if (!isUserExist)
                        {
                            returnMessage = "Invalid User Code: " + folderMonitoringTrnStockOutObject.UserCode; isDataValid = false; break;
                        }
                        else if (!isItemExist)
                        {
                            returnMessage = "Invalid Item Code: " + folderMonitoringTrnStockOutObject.ItemCode; isDataValid = false; break;
                        }
                        else if (!isUnitExist)
                        {
                            returnMessage = "Invalid Unit: " + folderMonitoringTrnStockOutObject.Unit; isDataValid = false; break;
                        }
                        else
                        {
                            var currentStockOut = from d in db.TrnStockOuts
                                                  where d.BranchId == branch.FirstOrDefault().Id &&
                                                  d.ManualOTNumber.Equals(folderMonitoringTrnStockOutObject.ManualOTNumber)
                                                  select d;

                            if (currentStockOut.Any())
                            {
                                returnMessage = "This Manual OT No. " + folderMonitoringTrnStockOutObject.ManualOTNumber + " is already exist!";
                                isDataValid   = false;

                                break;
                            }
                            else
                            {
                                var unitConversion = from d in item.FirstOrDefault().MstArticleUnits where d.UnitId == unit.FirstOrDefault().Id select d;
                                if (unitConversion.Any())
                                {
                                    Decimal baseQuantity = folderMonitoringTrnStockOutObject.Quantity * 1;
                                    if (unitConversion.FirstOrDefault().Multiplier > 0)
                                    {
                                        baseQuantity = folderMonitoringTrnStockOutObject.Quantity * (1 / unitConversion.FirstOrDefault().Multiplier);
                                    }

                                    Decimal baseCost = folderMonitoringTrnStockOutObject.Amount;
                                    if (baseQuantity > 0)
                                    {
                                        baseCost = folderMonitoringTrnStockOutObject.Amount / baseQuantity;
                                    }

                                    Int32   itemInventoryId      = 0;
                                    Boolean isValidInventoryItem = false;
                                    if (item.FirstOrDefault().IsInventory)
                                    {
                                        var itemInventory = from d in db.MstArticleInventories
                                                            where d.BranchId == branch.FirstOrDefault().Id &&
                                                            d.ArticleId == item.FirstOrDefault().Id
                                                            select d;

                                        if (itemInventory.Any())
                                        {
                                            itemInventoryId      = itemInventory.FirstOrDefault().Id;
                                            isValidInventoryItem = true;
                                        }
                                    }
                                    else
                                    {
                                        isValidInventoryItem = true;
                                    }

                                    if (isValidInventoryItem)
                                    {
                                        var folderMonitoringTrnStockOutHeader = from d in newFolderMonitoringTrnStockOutHeaders
                                                                                where d.BranchId == branch.FirstOrDefault().Id &&
                                                                                d.ManualOTNumber.Equals(folderMonitoringTrnStockOutObject.ManualOTNumber)
                                                                                select d;

                                        if (folderMonitoringTrnStockOutHeader.Any())
                                        {
                                            var currentFolderMonitoringTrnStockOutHeader = folderMonitoringTrnStockOutHeader.FirstOrDefault();
                                            currentFolderMonitoringTrnStockOutHeader.ListFolderMonitoringTrnStockOutItems.Add(new Entities.FolderMonitoringTrnStockOutItem()
                                            {
                                                ManualOTNumber   = folderMonitoringTrnStockOutObject.ManualOTNumber,
                                                ExpenseAccountId = account.FirstOrDefault().Id,
                                                ItemId           = item.FirstOrDefault().Id,
                                                ItemInventoryId  = itemInventoryId,
                                                Particulars      = folderMonitoringTrnStockOutObject.Particulars,
                                                UnitId           = unit.FirstOrDefault().Id,
                                                Quantity         = folderMonitoringTrnStockOutObject.Quantity,
                                                Cost             = folderMonitoringTrnStockOutObject.Cost,
                                                Amount           = folderMonitoringTrnStockOutObject.Amount,
                                                BaseUnitId       = item.FirstOrDefault().UnitId,
                                                BaseQuantity     = baseQuantity,
                                                BaseCost         = baseCost
                                            });
                                        }
                                        else
                                        {
                                            newFolderMonitoringTrnStockOutItems = new List <Entities.FolderMonitoringTrnStockOutItem>
                                            {
                                                new Entities.FolderMonitoringTrnStockOutItem()
                                                {
                                                    ManualOTNumber   = folderMonitoringTrnStockOutObject.ManualOTNumber,
                                                    ExpenseAccountId = account.FirstOrDefault().Id,
                                                    ItemId           = item.FirstOrDefault().Id,
                                                    ItemInventoryId  = itemInventoryId,
                                                    Particulars      = folderMonitoringTrnStockOutObject.Particulars,
                                                    UnitId           = unit.FirstOrDefault().Id,
                                                    Quantity         = folderMonitoringTrnStockOutObject.Quantity,
                                                    Cost             = folderMonitoringTrnStockOutObject.Cost,
                                                    Amount           = folderMonitoringTrnStockOutObject.Amount,
                                                    BaseUnitId       = item.FirstOrDefault().UnitId,
                                                    BaseQuantity     = baseQuantity,
                                                    BaseCost         = baseCost
                                                }
                                            };

                                            if (newFolderMonitoringTrnStockOutItems.Any())
                                            {
                                                newFolderMonitoringTrnStockOutHeaders.Add(new Entities.FolderMonitoringTrnStockOutHeader()
                                                {
                                                    BranchId        = branch.FirstOrDefault().Id,
                                                    OTNumber        = "0000000000",
                                                    OTDate          = Convert.ToDateTime(folderMonitoringTrnStockOutObject.OTDate),
                                                    AccountId       = account.FirstOrDefault().Id,
                                                    ArticleId       = article.FirstOrDefault().Id,
                                                    Particulars     = folderMonitoringTrnStockOutObject.Remarks,
                                                    ManualOTNumber  = folderMonitoringTrnStockOutObject.ManualOTNumber,
                                                    PreparedById    = user.FirstOrDefault().Id,
                                                    CheckedById     = user.FirstOrDefault().Id,
                                                    ApprovedById    = user.FirstOrDefault().Id,
                                                    Status          = null,
                                                    IsPrinted       = false,
                                                    IsLocked        = false,
                                                    CreatedById     = user.FirstOrDefault().Id,
                                                    CreatedDateTime = Convert.ToDateTime(folderMonitoringTrnStockOutObject.CreatedDateTime),
                                                    UpdatedById     = user.FirstOrDefault().Id,
                                                    UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnStockOutObject.CreatedDateTime),
                                                    ListFolderMonitoringTrnStockOutItems = newFolderMonitoringTrnStockOutItems
                                                });
                                            }
                                        }

                                        isDataValid = true;
                                    }
                                    else
                                    {
                                        returnMessage = "This item code: " + folderMonitoringTrnStockOutObject.ItemCode + " has no Inventory";
                                        isDataValid   = false;

                                        break;
                                    }
                                }
                                else
                                {
                                    returnMessage = "This item code: " + folderMonitoringTrnStockOutObject.ItemCode + " has invalid unit conversion. Unit: " + folderMonitoringTrnStockOutObject.Unit;
                                    isDataValid   = false;

                                    break;
                                }

                                isDataValid = true;
                            }
                        }
                    }

                    if (isDataValid)
                    {
                        if (newFolderMonitoringTrnStockOutHeaders.Any())
                        {
                            foreach (var newFolderMonitoringTrnStockOutHeader in newFolderMonitoringTrnStockOutHeaders)
                            {
                                Int32 OTId = 0;

                                var defaultOTNumber = "0000000001";
                                var lastStockOut    = from d in db.TrnStockOuts.OrderByDescending(d => d.Id) where d.BranchId == newFolderMonitoringTrnStockOutHeader.BranchId select d;
                                if (lastStockOut.Any())
                                {
                                    var OTNumber = Convert.ToInt32(lastStockOut.FirstOrDefault().OTNumber) + 0000000001;
                                    defaultOTNumber = FillLeadingZeroes(OTNumber, 10);
                                }

                                Data.TrnStockOut newStockOut = new Data.TrnStockOut
                                {
                                    BranchId        = newFolderMonitoringTrnStockOutHeader.BranchId,
                                    OTNumber        = defaultOTNumber,
                                    OTDate          = newFolderMonitoringTrnStockOutHeader.OTDate,
                                    AccountId       = newFolderMonitoringTrnStockOutHeader.AccountId,
                                    ArticleId       = newFolderMonitoringTrnStockOutHeader.ArticleId,
                                    Particulars     = newFolderMonitoringTrnStockOutHeader.Particulars,
                                    ManualOTNumber  = newFolderMonitoringTrnStockOutHeader.ManualOTNumber,
                                    PreparedById    = newFolderMonitoringTrnStockOutHeader.PreparedById,
                                    CheckedById     = newFolderMonitoringTrnStockOutHeader.CheckedById,
                                    ApprovedById    = newFolderMonitoringTrnStockOutHeader.ApprovedById,
                                    Status          = newFolderMonitoringTrnStockOutHeader.Status,
                                    IsPrinted       = newFolderMonitoringTrnStockOutHeader.IsPrinted,
                                    IsLocked        = newFolderMonitoringTrnStockOutHeader.IsLocked,
                                    CreatedById     = newFolderMonitoringTrnStockOutHeader.CreatedById,
                                    CreatedDateTime = newFolderMonitoringTrnStockOutHeader.CreatedDateTime,
                                    UpdatedById     = newFolderMonitoringTrnStockOutHeader.UpdatedById,
                                    UpdatedDateTime = newFolderMonitoringTrnStockOutHeader.UpdatedDateTime,
                                };

                                db.TrnStockOuts.InsertOnSubmit(newStockOut);
                                db.SubmitChanges();

                                OTId = newStockOut.Id;

                                if (newFolderMonitoringTrnStockOutHeader.ListFolderMonitoringTrnStockOutItems.Any())
                                {
                                    foreach (var listFolderMonitoringTrnStockOutItem in newFolderMonitoringTrnStockOutHeader.ListFolderMonitoringTrnStockOutItems)
                                    {
                                        Data.TrnStockOutItem newStockOutItem = new Data.TrnStockOutItem
                                        {
                                            OTId             = OTId,
                                            ExpenseAccountId = listFolderMonitoringTrnStockOutItem.ExpenseAccountId,
                                            ItemId           = listFolderMonitoringTrnStockOutItem.ItemId,
                                            ItemInventoryId  = listFolderMonitoringTrnStockOutItem.ItemInventoryId,
                                            Particulars      = listFolderMonitoringTrnStockOutItem.Particulars,
                                            UnitId           = listFolderMonitoringTrnStockOutItem.UnitId,
                                            Quantity         = listFolderMonitoringTrnStockOutItem.Quantity,
                                            Cost             = listFolderMonitoringTrnStockOutItem.Cost,
                                            Amount           = listFolderMonitoringTrnStockOutItem.Amount,
                                            BaseUnitId       = listFolderMonitoringTrnStockOutItem.BaseUnitId,
                                            BaseQuantity     = listFolderMonitoringTrnStockOutItem.BaseQuantity,
                                            BaseCost         = listFolderMonitoringTrnStockOutItem.BaseCost
                                        };
                                    }
                                }

                                var stockIn = from d in db.TrnStockOuts
                                              where d.Id == OTId
                                              select d;

                                if (stockIn.Any())
                                {
                                    var lockStockOut = stockIn.FirstOrDefault();
                                    lockStockOut.IsLocked = true;
                                    db.SubmitChanges();

                                    journal.InsertStockOutJournal(OTId);
                                    inventory.InsertStockOutInventory(OTId);
                                }
                            }
                        }
                    }

                    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));
            }
        }
        public HttpResponseMessage LockStockOut(Entities.TrnStockOut objStockOut, String id)
        {
            try
            {
                var currentUser = from d in db.MstUsers
                                  where d.UserId == User.Identity.GetUserId()
                                  select d;

                if (currentUser.Any())
                {
                    var currentUserId = currentUser.FirstOrDefault().Id;

                    var userForms = from d in db.MstUserForms
                                    where d.UserId == currentUserId &&
                                    d.SysForm.FormName.Equals("StockOutDetail")
                                    select d;

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanLock)
                        {
                            var stockOut = from d in db.TrnStockOuts
                                           where d.Id == Convert.ToInt32(id)
                                           select d;

                            if (stockOut.Any())
                            {
                                if (!stockOut.FirstOrDefault().IsLocked)
                                {
                                    String oldObject = at.GetObjectString(stockOut.FirstOrDefault());

                                    var lockStockOut = stockOut.FirstOrDefault();
                                    lockStockOut.OTDate          = Convert.ToDateTime(objStockOut.OTDate);
                                    lockStockOut.AccountId       = objStockOut.AccountId;
                                    lockStockOut.ArticleId       = objStockOut.ArticleId;
                                    lockStockOut.Particulars     = objStockOut.Particulars;
                                    lockStockOut.ManualOTNumber  = objStockOut.ManualOTNumber;
                                    lockStockOut.CheckedById     = objStockOut.CheckedById;
                                    lockStockOut.ApprovedById    = objStockOut.ApprovedById;
                                    lockStockOut.Status          = objStockOut.Status;
                                    lockStockOut.IsLocked        = true;
                                    lockStockOut.UpdatedById     = currentUserId;
                                    lockStockOut.UpdatedDateTime = DateTime.Now;

                                    db.SubmitChanges();

                                    // =====================
                                    // Journal and Inventory
                                    // =====================
                                    Business.Journal   journal   = new Business.Journal();
                                    Business.Inventory inventory = new Business.Inventory();

                                    if (lockStockOut.IsLocked)
                                    {
                                        journal.InsertStockOutJournal(Convert.ToInt32(id));
                                        inventory.InsertStockOutInventory(Convert.ToInt32(id));
                                    }

                                    String newObject = at.GetObjectString(stockOut.FirstOrDefault());
                                    at.InsertAuditTrail(currentUser.FirstOrDefault().Id, GetType().Name, MethodBase.GetCurrentMethod().Name, oldObject, newObject);

                                    return(Request.CreateResponse(HttpStatusCode.OK));
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Locking Error. These stock out details are already locked."));
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "Data not found. These stock out details are not found in the server."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to lock stock out."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this stock out page."));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Theres no current user logged in."));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }
Exemplo n.º 3
0
        public HttpResponseMessage AddFolderMonitoringStockOut(List <Entities.FolderMonitoringTrnStockOut> folderMonitoringTrnStockOutObjects)
        {
            try
            {
                if (folderMonitoringTrnStockOutObjects.Any())
                {
                    foreach (var folderMonitoringTrnStockOutObject in folderMonitoringTrnStockOutObjects)
                    {
                        Boolean isBranchExist   = false,
                                 isAccountExist = false,
                                 isArticleExist = false,
                                 isUserExist    = false,
                                 isItemExist    = false;

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

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

                        var account = from d in db.MstAccounts where d.AccountCode.Equals(folderMonitoringTrnStockOutObject.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(folderMonitoringTrnStockOutObject.ArticleCode) select d;
                        if (article.Any())
                        {
                            isArticleExist = true;
                        }

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

                        var item = from d in db.MstArticles where d.ArticleTypeId == 1 && d.ManualArticleCode.Equals(folderMonitoringTrnStockOutObject.ItemCode) && d.IsInventory == true && d.IsLocked == true select d;
                        if (item.Any())
                        {
                            isItemExist = true;
                        }

                        if (isBranchExist && isUserExist && isAccountExist && isArticleExist && isItemExist)
                        {
                            Int32 OTId = 0;

                            var currentStockOut = from d in db.TrnStockOuts where d.BranchId == branch.FirstOrDefault().Id&& d.ManualOTNumber.Equals(folderMonitoringTrnStockOutObject.ManualOTNumber) && d.IsLocked == true select d;
                            if (currentStockOut.Any())
                            {
                                OTId = currentStockOut.FirstOrDefault().Id;

                                var unlockStockOut = currentStockOut.FirstOrDefault();
                                unlockStockOut.IsLocked        = false;
                                unlockStockOut.UpdatedById     = user.FirstOrDefault().Id;
                                unlockStockOut.UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnStockOutObject.CreatedDateTime);
                                db.SubmitChanges();

                                journal.DeleteStockOutJournal(OTId);
                                inventory.DeleteStockOutInventory(OTId);
                            }
                            else
                            {
                                var defaultOTNumber = "0000000001";
                                var lastStockOut    = from d in db.TrnStockOuts.OrderByDescending(d => d.Id) where d.BranchId == branch.FirstOrDefault().Id select d;
                                if (lastStockOut.Any())
                                {
                                    var OTNumber = Convert.ToInt32(lastStockOut.FirstOrDefault().OTNumber) + 0000000001;
                                    defaultOTNumber = FillLeadingZeroes(OTNumber, 10);
                                }

                                Data.TrnStockOut newStockOut = new Data.TrnStockOut
                                {
                                    BranchId        = branch.FirstOrDefault().Id,
                                    OTNumber        = defaultOTNumber,
                                    OTDate          = Convert.ToDateTime(folderMonitoringTrnStockOutObject.OTDate),
                                    AccountId       = account.FirstOrDefault().Id,
                                    ArticleId       = article.FirstOrDefault().Id,
                                    Particulars     = folderMonitoringTrnStockOutObject.Remarks,
                                    ManualOTNumber  = folderMonitoringTrnStockOutObject.ManualOTNumber,
                                    PreparedById    = user.FirstOrDefault().Id,
                                    CheckedById     = user.FirstOrDefault().Id,
                                    ApprovedById    = user.FirstOrDefault().Id,
                                    Status          = null,
                                    IsPrinted       = false,
                                    IsLocked        = false,
                                    CreatedById     = user.FirstOrDefault().Id,
                                    CreatedDateTime = Convert.ToDateTime(folderMonitoringTrnStockOutObject.CreatedDateTime),
                                    UpdatedById     = user.FirstOrDefault().Id,
                                    UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnStockOutObject.CreatedDateTime)
                                };

                                db.TrnStockOuts.InsertOnSubmit(newStockOut);
                                db.SubmitChanges();

                                OTId = newStockOut.Id;
                            }

                            var unitConversion = from d in item.FirstOrDefault().MstArticleUnits where d.MstUnit.Unit.Equals(folderMonitoringTrnStockOutObject.Unit) select d;
                            if (unitConversion.Any())
                            {
                                Decimal baseQuantity = folderMonitoringTrnStockOutObject.Quantity * 1;
                                if (unitConversion.FirstOrDefault().Multiplier > 0)
                                {
                                    baseQuantity = folderMonitoringTrnStockOutObject.Quantity * (1 / unitConversion.FirstOrDefault().Multiplier);
                                }

                                Decimal baseCost = folderMonitoringTrnStockOutObject.Amount;
                                if (baseQuantity > 0)
                                {
                                    baseCost = folderMonitoringTrnStockOutObject.Amount / baseQuantity;
                                }

                                Int32 itemInventoryId = 0;

                                var itemInventory = from d in db.MstArticleInventories where d.BranchId == branch.FirstOrDefault().Id&& d.ArticleId == item.FirstOrDefault().Id select d;
                                if (itemInventory.Any())
                                {
                                    itemInventoryId = itemInventory.FirstOrDefault().Id;
                                }

                                if (itemInventoryId > 0)
                                {
                                    Data.TrnStockOutItem newStockOutItem = new Data.TrnStockOutItem
                                    {
                                        OTId             = OTId,
                                        ItemId           = item.FirstOrDefault().Id,
                                        ItemInventoryId  = itemInventoryId,
                                        Particulars      = folderMonitoringTrnStockOutObject.Particulars,
                                        UnitId           = unitConversion.FirstOrDefault().UnitId,
                                        Quantity         = folderMonitoringTrnStockOutObject.Quantity,
                                        Cost             = folderMonitoringTrnStockOutObject.Cost,
                                        Amount           = folderMonitoringTrnStockOutObject.Amount,
                                        BaseUnitId       = item.FirstOrDefault().UnitId,
                                        BaseQuantity     = baseQuantity,
                                        BaseCost         = baseCost,
                                        ExpenseAccountId = account.FirstOrDefault().Id
                                    };

                                    db.TrnStockOutItems.InsertOnSubmit(newStockOutItem);
                                    db.SubmitChanges();
                                }

                                var stockOut = from d in db.TrnStockOuts where d.Id == OTId && d.IsLocked == false select d;
                                if (stockOut.Any())
                                {
                                    var lockStockOut = stockOut.FirstOrDefault();
                                    lockStockOut.IsLocked        = true;
                                    lockStockOut.UpdatedById     = user.FirstOrDefault().Id;
                                    lockStockOut.UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnStockOutObject.CreatedDateTime);
                                    db.SubmitChanges();

                                    journal.InsertStockOutJournal(OTId);
                                    inventory.InsertStockOutInventory(OTId);
                                }
                            }
                        }
                    }

                    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."));
            }
        }
Exemplo n.º 4
0
        public HttpResponseMessage updateStockOut(String id, Models.TrnStockOut stockOut)
        {
            try
            {
                var userId = (from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d.Id).SingleOrDefault();

                var stockOuts = from d in db.TrnStockOuts where d.Id == Convert.ToInt32(id) select d;
                if (stockOuts.Any())
                {
                    var updateStockOut = stockOuts.FirstOrDefault();
                    updateStockOut.BranchId        = stockOut.BranchId;
                    updateStockOut.OTNumber        = stockOut.OTNumber;
                    updateStockOut.OTDate          = Convert.ToDateTime(stockOut.OTDate);
                    updateStockOut.AccountId       = stockOut.AccountId;
                    updateStockOut.ArticleId       = stockOut.ArticleId;
                    updateStockOut.Particulars     = stockOut.Particulars;
                    updateStockOut.ManualOTNumber  = stockOut.ManualOTNumber;
                    updateStockOut.PreparedById    = stockOut.PreparedById;
                    updateStockOut.CheckedById     = stockOut.CheckedById;
                    updateStockOut.ApprovedById    = stockOut.ApprovedById;
                    updateStockOut.IsLocked        = true;
                    updateStockOut.UpdatedById     = userId;
                    updateStockOut.UpdatedDateTime = DateTime.Now;
                    db.SubmitChanges();

                    inventory.InsertStockOutInventory(Convert.ToInt32(id));
                    journal.InsertStockOutJournal(Convert.ToInt32(id));

                    // Check for negative inventory
                    bool foundNegativeQuantity = false;
                    if (updateStockOut.TrnStockOutItems.Any())
                    {
                        foreach (var stockOutItem in updateStockOut.TrnStockOutItems)
                        {
                            if (stockOutItem.MstArticle.IsInventory)
                            {
                                var mstArticleInventory = from d in db.MstArticleInventories
                                                          where d.TrnStockOutItems.Contains(stockOutItem)
                                                          select d;

                                if (mstArticleInventory.Any())
                                {
                                    if (stockOutItem.MstArticleInventory.Quantity < 0)
                                    {
                                        foundNegativeQuantity = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    if (!foundNegativeQuantity)
                    {
                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                    else
                    {
                        inventory.DeleteStockOutInventory(Convert.ToInt32(id));
                        journal.DeleteStockOutJournal(Convert.ToInt32(id));

                        updateStockOut.IsLocked = false;
                        db.SubmitChanges();

                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Negative Inventory Found!"));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
            }
            catch
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }