Exemplo n.º 1
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.InsertOTInventory(Convert.ToInt32(id));
                    journal.insertOTJournal(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.deleteOTInventory(Convert.ToInt32(id));
                        journal.deleteOTJournal(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));
            }
        }