public HttpResponseMessage updatePurchaseOrderItem(String id, Models.TrnPurchaseOrderItem purchaseOrderItem)
        {
            try
            {
                var purchaseOrderItems = from d in db.TrnPurchaseOrderItems where d.Id == Convert.ToInt32(id) select d;
                if (purchaseOrderItems.Any())
                {
                    var updatePurchaseOrderItem = purchaseOrderItems.FirstOrDefault();
                    updatePurchaseOrderItem.POId        = purchaseOrderItem.POId;
                    updatePurchaseOrderItem.ItemId      = purchaseOrderItem.ItemId;
                    updatePurchaseOrderItem.Particulars = purchaseOrderItem.Particulars;
                    updatePurchaseOrderItem.UnitId      = purchaseOrderItem.UnitId;
                    updatePurchaseOrderItem.Quantity    = purchaseOrderItem.Quantity;
                    updatePurchaseOrderItem.Cost        = purchaseOrderItem.Cost;
                    updatePurchaseOrderItem.Amount      = purchaseOrderItem.Amount;

                    var mstArticleUnit = from d in db.MstArticles where d.Id == purchaseOrderItem.ItemId select d;
                    updatePurchaseOrderItem.BaseUnitId = mstArticleUnit.FirstOrDefault().UnitId;

                    var conversionUnit = from d in db.MstArticleUnits where d.ArticleId == purchaseOrderItem.ItemId && d.UnitId == purchaseOrderItem.UnitId select d;
                    if (conversionUnit.Any())
                    {
                        if (conversionUnit.FirstOrDefault().Multiplier > 0)
                        {
                            updatePurchaseOrderItem.BaseQuantity = purchaseOrderItem.Quantity * (1 / conversionUnit.FirstOrDefault().Multiplier);
                        }
                        else
                        {
                            updatePurchaseOrderItem.BaseQuantity = purchaseOrderItem.Quantity * 1;
                        }
                    }
                    else
                    {
                        updatePurchaseOrderItem.BaseQuantity = purchaseOrderItem.Quantity * 1;
                    }

                    if (conversionUnit.Any())
                    {
                        var baseQuantity = purchaseOrderItem.Quantity * (1 / conversionUnit.FirstOrDefault().Multiplier);
                        if (baseQuantity > 0)
                        {
                            updatePurchaseOrderItem.BaseCost = purchaseOrderItem.Amount / baseQuantity;
                        }
                        else
                        {
                            updatePurchaseOrderItem.BaseCost = purchaseOrderItem.Amount;
                        }
                    }
                    else
                    {
                        updatePurchaseOrderItem.BaseCost = purchaseOrderItem.Amount;
                    }

                    db.SubmitChanges();

                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
            }
            catch
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }
        public Int32 insertPurchaseOrderItem(Models.TrnPurchaseOrderItem purchaseOrderItem)
        {
            try
            {
                Data.TrnPurchaseOrderItem newPurchaseOrderItem = new Data.TrnPurchaseOrderItem();
                newPurchaseOrderItem.POId        = purchaseOrderItem.POId;
                newPurchaseOrderItem.ItemId      = purchaseOrderItem.ItemId;
                newPurchaseOrderItem.Particulars = purchaseOrderItem.Particulars;
                newPurchaseOrderItem.UnitId      = purchaseOrderItem.UnitId;
                newPurchaseOrderItem.Quantity    = purchaseOrderItem.Quantity;
                newPurchaseOrderItem.Cost        = purchaseOrderItem.Cost;
                newPurchaseOrderItem.Amount      = purchaseOrderItem.Amount;

                var mstArticleUnit = from d in db.MstArticles
                                     where d.Id == purchaseOrderItem.ItemId
                                     select d;

                newPurchaseOrderItem.BaseUnitId = mstArticleUnit.FirstOrDefault().UnitId;

                var conversionUnit = from d in db.MstArticleUnits
                                     where d.ArticleId == purchaseOrderItem.ItemId &&
                                     d.UnitId == purchaseOrderItem.UnitId
                                     select d;

                if (conversionUnit.Any())
                {
                    if (conversionUnit.FirstOrDefault().Multiplier > 0)
                    {
                        newPurchaseOrderItem.BaseQuantity = purchaseOrderItem.Quantity * (1 / conversionUnit.FirstOrDefault().Multiplier);
                    }
                    else
                    {
                        newPurchaseOrderItem.BaseQuantity = purchaseOrderItem.Quantity * 1;
                    }
                }
                else
                {
                    newPurchaseOrderItem.BaseQuantity = purchaseOrderItem.Quantity * 1;
                }

                if (conversionUnit.Any())
                {
                    var baseQuantity = purchaseOrderItem.Quantity * (1 / conversionUnit.FirstOrDefault().Multiplier);
                    if (baseQuantity > 0)
                    {
                        newPurchaseOrderItem.BaseCost = purchaseOrderItem.Amount / baseQuantity;
                    }
                    else
                    {
                        newPurchaseOrderItem.BaseCost = purchaseOrderItem.Amount;
                    }
                }
                else
                {
                    newPurchaseOrderItem.BaseCost = purchaseOrderItem.Amount;
                }

                db.TrnPurchaseOrderItems.InsertOnSubmit(newPurchaseOrderItem);
                db.SubmitChanges();

                return(newPurchaseOrderItem.Id);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(0);
            }
        }