Exemple #1
0
        public Int32 insertStockTransfer(Models.TrnStockTransfer stockTransfer)
        {
            try
            {
                var userId = (from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d.Id).SingleOrDefault();

                var lastSTNumber   = from d in db.TrnStockTransfers.OrderByDescending(d => d.Id) where d.BranchId == currentBranchId() select d;
                var STNumberResult = "0000000001";

                if (lastSTNumber.Any())
                {
                    var STNumber = Convert.ToInt32(lastSTNumber.FirstOrDefault().STNumber) + 0000000001;
                    STNumberResult = zeroFill(STNumber, 10);
                }

                var users         = from d in db.MstUsers where d.Id == userId select d;
                var branches      = from d in db.MstBranches where d.Id != currentBranchId() && d.CompanyId == users.FirstOrDefault().CompanyId select d;
                var otherArticles = from d in db.MstArticles where d.ArticleTypeId == 6 select d;

                Data.TrnStockTransfer newStockTransfer = new Data.TrnStockTransfer();
                newStockTransfer.BranchId        = currentBranchId();
                newStockTransfer.STNumber        = STNumberResult;
                newStockTransfer.STDate          = DateTime.Today;
                newStockTransfer.ToBranchId      = branches.FirstOrDefault().Id;
                newStockTransfer.ArticleId       = otherArticles.FirstOrDefault().Id;
                newStockTransfer.Particulars     = "NA";
                newStockTransfer.ManualSTNumber  = "NA";
                newStockTransfer.PreparedById    = userId;
                newStockTransfer.CheckedById     = userId;
                newStockTransfer.ApprovedById    = userId;
                newStockTransfer.IsLocked        = false;
                newStockTransfer.CreatedById     = userId;
                newStockTransfer.CreatedDateTime = DateTime.Now;
                newStockTransfer.UpdatedById     = userId;
                newStockTransfer.UpdatedDateTime = DateTime.Now;

                db.TrnStockTransfers.InsertOnSubmit(newStockTransfer);
                db.SubmitChanges();

                return(newStockTransfer.Id);
            }
            catch
            {
                return(0);
            }
        }
        public HttpResponseMessage AddStockTransfer()
        {
            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 currentBranchId = currentUser.FirstOrDefault().BranchId;

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

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanAdd)
                        {
                            var defaultSTNumber   = "0000000001";
                            var lastStockTransfer = from d in db.TrnStockTransfers.OrderByDescending(d => d.Id)
                                                    where d.BranchId == currentBranchId
                                                    select d;

                            if (lastStockTransfer.Any())
                            {
                                var STNumber = Convert.ToInt32(lastStockTransfer.FirstOrDefault().STNumber) + 0000000001;
                                defaultSTNumber = FillLeadingZeroes(STNumber, 10);
                            }

                            var toBranches = from d in db.MstBranches
                                             where d.Id != currentBranchId
                                             select d;

                            if (toBranches.Any())
                            {
                                var articles = from d in db.MstArticles
                                               where d.ArticleTypeId == 6 &&
                                               d.IsLocked == true
                                               select d;

                                if (articles.Any())
                                {
                                    var users = from d in db.MstUsers.OrderBy(d => d.FullName)
                                                where d.IsLocked == true
                                                select d;

                                    if (users.Any())
                                    {
                                        Data.TrnStockTransfer newStockTransfer = new Data.TrnStockTransfer
                                        {
                                            BranchId        = currentBranchId,
                                            STNumber        = defaultSTNumber,
                                            STDate          = DateTime.Today,
                                            ToBranchId      = toBranches.FirstOrDefault().Id,
                                            ArticleId       = articles.FirstOrDefault().Id,
                                            Particulars     = "NA",
                                            ManualSTNumber  = "NA",
                                            PreparedById    = currentUserId,
                                            CheckedById     = currentUserId,
                                            ApprovedById    = currentUserId,
                                            IsPrinted       = false,
                                            IsLocked        = false,
                                            CreatedById     = currentUserId,
                                            CreatedDateTime = DateTime.Now,
                                            UpdatedById     = currentUserId,
                                            UpdatedDateTime = DateTime.Now
                                        };

                                        db.TrnStockTransfers.InsertOnSubmit(newStockTransfer);
                                        db.SubmitChanges();

                                        String newObject = at.GetObjectString(newStockTransfer);
                                        at.InsertAuditTrail(currentUser.FirstOrDefault().Id, GetType().Name, MethodBase.GetCurrentMethod().Name, "NA", newObject);

                                        return(Request.CreateResponse(HttpStatusCode.OK, newStockTransfer.Id));
                                    }
                                    else
                                    {
                                        return(Request.CreateResponse(HttpStatusCode.NotFound, "No user found. Please setup more users for all transactions."));
                                    }
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.NotFound, "No article found. Please setup more articles for all transactions."));
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "No branch found. Please setup more branches for all transactions."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to add stock transfer."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this stock transfer 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."));
            }
        }
        public HttpResponseMessage AddFolderMonitoringStockTransfer(List <Entities.FolderMonitoringTrnStockTransfer> folderMonitoringTrnStockTransferObjects)
        {
            try
            {
                if (folderMonitoringTrnStockTransferObjects.Any())
                {
                    foreach (var folderMonitoringTrnStockTransferObject in folderMonitoringTrnStockTransferObjects)
                    {
                        Boolean isBranchExist    = false,
                                 isToBranchExist = false,
                                 isArticleExist  = false,
                                 isUserExist     = false,
                                 isItemExist     = false;

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

                        var toBranch = from d in db.MstBranches where d.BranchCode.Equals(folderMonitoringTrnStockTransferObject.ToBranchCode) select d;
                        if (toBranch.Any())
                        {
                            isToBranchExist = true;
                        }

                        var article = from d in db.MstArticles where d.ArticleTypeId == 6 && d.ManualArticleCode.Equals(folderMonitoringTrnStockTransferObject.ArticleCode) select d;
                        if (article.Any())
                        {
                            isArticleExist = true;
                        }

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

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

                        if (isBranchExist && isToBranchExist && isUserExist && isArticleExist && isItemExist)
                        {
                            Int32 STId = 0;

                            var currentStockTransfer = from d in db.TrnStockTransfers where d.BranchId == branch.FirstOrDefault().Id&& d.ManualSTNumber.Equals(folderMonitoringTrnStockTransferObject.ManualSTNumber) && d.IsLocked == true select d;
                            if (currentStockTransfer.Any())
                            {
                                STId = currentStockTransfer.FirstOrDefault().Id;

                                var unlockStockTransfer = currentStockTransfer.FirstOrDefault();
                                unlockStockTransfer.IsLocked        = false;
                                unlockStockTransfer.UpdatedById     = user.FirstOrDefault().Id;
                                unlockStockTransfer.UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnStockTransferObject.CreatedDateTime);
                                db.SubmitChanges();

                                journal.DeleteStockTransferJournal(STId);
                                inventory.DeleteStockTransferInventory(STId);
                            }
                            else
                            {
                                var defaultSTNumber   = "0000000001";
                                var lastStockTransfer = from d in db.TrnStockTransfers.OrderByDescending(d => d.Id) where d.BranchId == branch.FirstOrDefault().Id select d;
                                if (lastStockTransfer.Any())
                                {
                                    var STNumber = Convert.ToInt32(lastStockTransfer.FirstOrDefault().STNumber) + 0000000001;
                                    defaultSTNumber = FillLeadingZeroes(STNumber, 10);
                                }

                                Data.TrnStockTransfer newStockTransfer = new Data.TrnStockTransfer
                                {
                                    BranchId        = branch.FirstOrDefault().Id,
                                    STNumber        = defaultSTNumber,
                                    STDate          = Convert.ToDateTime(folderMonitoringTrnStockTransferObject.STDate),
                                    ToBranchId      = toBranch.FirstOrDefault().Id,
                                    ArticleId       = article.FirstOrDefault().Id,
                                    Particulars     = folderMonitoringTrnStockTransferObject.Remarks,
                                    ManualSTNumber  = folderMonitoringTrnStockTransferObject.ManualSTNumber,
                                    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(folderMonitoringTrnStockTransferObject.CreatedDateTime),
                                    UpdatedById     = user.FirstOrDefault().Id,
                                    UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnStockTransferObject.CreatedDateTime)
                                };

                                db.TrnStockTransfers.InsertOnSubmit(newStockTransfer);
                                db.SubmitChanges();

                                STId = newStockTransfer.Id;
                            }

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

                                Decimal baseCost = folderMonitoringTrnStockTransferObject.Amount;
                                if (baseQuantity > 0)
                                {
                                    baseCost = folderMonitoringTrnStockTransferObject.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.TrnStockTransferItem newStockTransferItem = new Data.TrnStockTransferItem
                                    {
                                        STId            = STId,
                                        ItemId          = item.FirstOrDefault().Id,
                                        ItemInventoryId = itemInventoryId,
                                        Particulars     = folderMonitoringTrnStockTransferObject.Particulars,
                                        UnitId          = unitConversion.FirstOrDefault().UnitId,
                                        Quantity        = folderMonitoringTrnStockTransferObject.Quantity,
                                        Cost            = folderMonitoringTrnStockTransferObject.Cost,
                                        Amount          = folderMonitoringTrnStockTransferObject.Amount,
                                        BaseUnitId      = item.FirstOrDefault().UnitId,
                                        BaseQuantity    = baseQuantity,
                                        BaseCost        = baseCost
                                    };

                                    db.TrnStockTransferItems.InsertOnSubmit(newStockTransferItem);
                                    db.SubmitChanges();
                                }

                                var stockTransfer = from d in db.TrnStockTransfers where d.Id == STId && d.IsLocked == false select d;
                                if (stockTransfer.Any())
                                {
                                    var lockStockTransfer = stockTransfer.FirstOrDefault();
                                    lockStockTransfer.IsLocked        = true;
                                    lockStockTransfer.UpdatedById     = user.FirstOrDefault().Id;
                                    lockStockTransfer.UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnStockTransferObject.CreatedDateTime);
                                    db.SubmitChanges();

                                    journal.InsertStockTransferJournal(STId);
                                    inventory.InsertStockTransferInventory(STId);
                                }
                            }
                        }
                    }

                    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 Int32 insertStockTransfer(Models.TrnStockTransfer stockTransfer)
        {
            try
            {
                var userId = (from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d.Id).SingleOrDefault();

                var lastSTNumber = from d in db.TrnStockTransfers.OrderByDescending(d => d.Id) select d;
                var STNumberResult = "0000000001";

                if (lastSTNumber.Any())
                {
                    var STNumber = Convert.ToInt32(lastSTNumber.FirstOrDefault().STNumber) + 0000000001;
                    STNumberResult = zeroFill(STNumber, 10);
                }

                var users = from d in db.MstUsers where d.Id == userId select d;
                var branches = from d in db.MstBranches where d.Id != currentBranchId() && d.CompanyId == users.FirstOrDefault().CompanyId select d;

                Data.TrnStockTransfer newStockTransfer = new Data.TrnStockTransfer();
                newStockTransfer.BranchId = currentBranchId();
                newStockTransfer.STNumber = STNumberResult;
                newStockTransfer.STDate = DateTime.Today;
                newStockTransfer.ToBranchId = branches.FirstOrDefault().Id;
                newStockTransfer.Particulars = "NA";
                newStockTransfer.ManualSTNumber = "NA";
                newStockTransfer.PreparedById = userId;
                newStockTransfer.CheckedById = userId;
                newStockTransfer.ApprovedById = userId;
                newStockTransfer.IsLocked = false;
                newStockTransfer.CreatedById = userId;
                newStockTransfer.CreatedDateTime = DateTime.Now;
                newStockTransfer.UpdatedById = userId;
                newStockTransfer.UpdatedDateTime = DateTime.Now;

                db.TrnStockTransfers.InsertOnSubmit(newStockTransfer);
                db.SubmitChanges();

                return newStockTransfer.Id;
            }
            catch
            {
                return 0;
            }
        }