public HttpResponseMessage AddStockWithdrawalItem(Entities.TrnStockWithdrawalItem objStockWithdrawalItem, String SWId)
        {
            try
            {
                var currentUser = from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d;
                if (currentUser.Any())
                {
                    Int32 currentUserId   = currentUser.FirstOrDefault().Id;
                    Int32 currentBranchId = currentUser.FirstOrDefault().BranchId;

                    IQueryable <Data.MstUserForm>         userForms       = from d in db.MstUserForms where d.UserId == currentUserId && d.SysForm.FormName.Equals("StockWithdrawalDetail") select d;
                    IQueryable <Data.TrnStockWithdrawal>  stockWithdrawal = from d in db.TrnStockWithdrawals where d.Id == Convert.ToInt32(SWId) select d;
                    IQueryable <Data.MstArticle>          item            = from d in db.MstArticles where d.Id == objStockWithdrawalItem.ItemId && d.IsInventory == true && d.Kitting != 2 && d.IsLocked == true select d;
                    IQueryable <Data.MstArticleInventory> itemInventories = from d in db.MstArticleInventories
                                                                            where d.ArticleId == objStockWithdrawalItem.ItemId && d.BranchId == currentBranchId &&
                                                                            d.Quantity > 0 && d.MstArticle.IsInventory == true && d.MstArticle.IsLocked == true
                                                                            select d;

                    Boolean isValid       = false;
                    String  returnMessage = "";

                    if (!userForms.Any())
                    {
                        returnMessage = "Sorry. You have no access for this stock withdrawal page.";
                    }
                    else if (!userForms.FirstOrDefault().CanAdd)
                    {
                        returnMessage = "Sorry. You have no rights to add stock withdrawal item.";
                    }
                    else if (!stockWithdrawal.Any())
                    {
                        returnMessage = "These current stock withdrawal details are not found in the server. Please add new stock withdrawal first before proceeding.";
                    }
                    else if (stockWithdrawal.FirstOrDefault().IsLocked)
                    {
                        returnMessage = "You cannot add new stock withdrawal item if the current stock withdrawal detail is locked.";
                    }
                    else if (!item.Any())
                    {
                        returnMessage = "The selected item was not found in the server.";
                    }
                    else if (!itemInventories.Any())
                    {
                        returnMessage = "The selected item has no inventory code.";
                    }
                    else
                    {
                        isValid = true;
                    }

                    if (isValid)
                    {
                        var conversionUnit = from d in db.MstArticleUnits
                                             where d.ArticleId == objStockWithdrawalItem.ItemId &&
                                             d.UnitId == objStockWithdrawalItem.UnitId &&
                                             d.MstArticle.IsLocked == true
                                             select d;

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

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

                            Data.TrnStockWithdrawalItem newStockWithdrawalItem = new Data.TrnStockWithdrawalItem
                            {
                                SWId            = Convert.ToInt32(SWId),
                                ItemId          = objStockWithdrawalItem.ItemId,
                                ItemInventoryId = objStockWithdrawalItem.ItemInventoryId,
                                Particulars     = objStockWithdrawalItem.Particulars,
                                UnitId          = objStockWithdrawalItem.UnitId,
                                Quantity        = objStockWithdrawalItem.Quantity,
                                Cost            = objStockWithdrawalItem.Cost,
                                Amount          = objStockWithdrawalItem.Amount,
                                BaseUnitId      = item.FirstOrDefault().UnitId,
                                BaseQuantity    = baseQuantity,
                                BaseCost        = baseCost,
                            };

                            db.TrnStockWithdrawalItems.InsertOnSubmit(newStockWithdrawalItem);
                            db.SubmitChanges();

                            return(Request.CreateResponse(HttpStatusCode.OK));
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "The selected item has no unit conversion."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, returnMessage));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Theres no current user logged in."));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }
Пример #2
0
        public HttpResponseMessage ApplyStockWithdrawalItemSalesInvoiceStatusItem(List <Entities.TrnSalesInvoiceItem> objSalesInvoiceItems, String SWId)
        {
            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;

                    IQueryable <Data.MstUserForm>        userForms       = from d in db.MstUserForms where d.UserId == currentUserId && d.SysForm.FormName.Equals("StockWithdrawalDetail") select d;
                    IQueryable <Data.TrnStockWithdrawal> stockWithdrawal = from d in db.TrnStockWithdrawals where d.Id == Convert.ToInt32(SWId) select d;

                    Boolean isValid       = false;
                    String  returnMessage = "";

                    if (!userForms.Any())
                    {
                        returnMessage = "Sorry. You have no access in this stock withdrawal detail page.";
                    }
                    else if (!userForms.FirstOrDefault().CanAdd)
                    {
                        returnMessage = "Sorry. You have no rights to add new stock withdrawal item in this stock withdrawal detail page.";
                    }
                    else if (!stockWithdrawal.Any())
                    {
                        returnMessage = "These current stock withdrawal details are not found in the server. Please add new stock withdrawal first before proceeding.";
                    }
                    else if (stockWithdrawal.FirstOrDefault().IsLocked)
                    {
                        returnMessage = "You cannot apply sales invoice items to stock withdrawal item if the current stock withdrawal detail is locked.";
                    }
                    else if (!objSalesInvoiceItems.Any())
                    {
                        returnMessage = "There are no sales invoice items.";
                    }
                    else
                    {
                        isValid = true;
                    }

                    if (isValid)
                    {
                        String newObject = "[";
                        Int32  count     = 0;

                        foreach (var objSalesInvoiceItem in objSalesInvoiceItems)
                        {
                            count += 1;

                            var item = from d in db.MstArticles
                                       where d.Id == objSalesInvoiceItem.ItemId &&
                                       d.ArticleTypeId == 1 &&
                                       d.IsInventory == true &&
                                       d.IsLocked == true
                                       select d;

                            if (item.Any())
                            {
                                var itemInventories = from d in db.MstArticleInventories
                                                      where d.ArticleId == objSalesInvoiceItem.ItemId && d.BranchId == stockWithdrawal.FirstOrDefault().BranchId
                                                      //&& d.Quantity > 0
                                                      && d.MstArticle.IsInventory == true && d.MstArticle.IsLocked == true
                                                      select d;

                                if (itemInventories.Any())
                                {
                                    var conversionUnit = from d in db.MstArticleUnits
                                                         where d.ArticleId == objSalesInvoiceItem.ItemId &&
                                                         d.UnitId == objSalesInvoiceItem.UnitId &&
                                                         d.MstArticle.IsLocked == true
                                                         select d;

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

                                        Decimal baseCost = objSalesInvoiceItem.Quantity * itemInventories.FirstOrDefault().Cost;
                                        if (baseQuantity > 0)
                                        {
                                            baseCost = (objSalesInvoiceItem.Quantity * itemInventories.FirstOrDefault().Cost) / baseQuantity;
                                        }

                                        Data.TrnStockWithdrawalItem newStockWithdrawalItem = new Data.TrnStockWithdrawalItem
                                        {
                                            SWId            = Convert.ToInt32(SWId),
                                            ItemId          = objSalesInvoiceItem.ItemId,
                                            ItemInventoryId = itemInventories.FirstOrDefault().Id,
                                            Particulars     = objSalesInvoiceItem.Particulars,
                                            UnitId          = item.FirstOrDefault().UnitId,
                                            Quantity        = objSalesInvoiceItem.Quantity,
                                            Cost            = itemInventories.FirstOrDefault().Cost,
                                            Amount          = objSalesInvoiceItem.Quantity * itemInventories.FirstOrDefault().Cost,
                                            BaseUnitId      = item.FirstOrDefault().UnitId,
                                            BaseQuantity    = baseQuantity,
                                            BaseCost        = baseCost,
                                        };

                                        db.TrnStockWithdrawalItems.InsertOnSubmit(newStockWithdrawalItem);

                                        if (objSalesInvoiceItems.Count() == 1)
                                        {
                                            newObject += at.GetObjectString(newStockWithdrawalItem);
                                        }
                                        else
                                        {
                                            if (count == objSalesInvoiceItems.Count())
                                            {
                                                newObject += at.GetObjectString(newStockWithdrawalItem);
                                            }
                                            else
                                            {
                                                newObject += at.GetObjectString(newStockWithdrawalItem) + ", ";
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        db.SubmitChanges();

                        newObject += "]";
                        at.InsertAuditTrail(currentUser.FirstOrDefault().Id, GetType().Name, MethodBase.GetCurrentMethod().Name, "NA", newObject);

                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotFound, returnMessage));
                    }
                }
                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."));
            }
        }