public HttpResponseMessage AddShopOrder(Entities.TrnShopOrder objShopOrder)
        {
            try
            {
                HttpStatusCode responseStatusCode = HttpStatusCode.OK;
                String         responseMessage    = "";

                var currentUser = from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d;
                var userForm    = from d in db.MstUserForms where d.UserId == currentUser.FirstOrDefault().Id&& d.SysForm.FormName.Equals("ShopOrderList") select d;
                var item        = from d in db.MstArticles where d.ArticleTypeId == 1 && d.IsLocked == true select d;
                IQueryable <Data.MstUnit> unit = null;
                if (item.Any())
                {
                    unit = from d in db.MstUnits where d.Id == item.FirstOrDefault().UnitId select d;
                }
                var shopOrderStatus = from d in db.MstShopOrderStatus select d;
                var shopGroup       = from d in db.MstShopGroups select d;

                if (!userForm.Any())
                {
                    responseStatusCode = HttpStatusCode.NotFound; responseMessage = "No rights.";
                }
                else if (!userForm.FirstOrDefault().CanAdd)
                {
                    responseStatusCode = HttpStatusCode.BadRequest; responseMessage = "No add rights.";
                }
                else if (!item.Any())
                {
                    responseStatusCode = HttpStatusCode.NotFound; responseMessage = "Item not found.";
                }
                else if (!unit.Any())
                {
                    responseStatusCode = HttpStatusCode.NotFound; responseMessage = "Unit not found.";
                }
                else if (!shopOrderStatus.Any())
                {
                    responseStatusCode = HttpStatusCode.NotFound; responseMessage = "Shop order status not found.";
                }
                else if (!shopGroup.Any())
                {
                    responseStatusCode = HttpStatusCode.NotFound; responseMessage = "Shop group not found.";
                }
                else
                {
                    Data.TrnShopOrder newShopOrder = new Data.TrnShopOrder
                    {
                        BranchId            = currentUser.FirstOrDefault().BranchId,
                        SPNumber            = "NA",
                        SPDate              = DateTime.Today,
                        ItemId              = item.OrderByDescending(d => d.Article).FirstOrDefault().Id,
                        Quantity            = 0,
                        UnitId              = unit.FirstOrDefault().Id,
                        Amount              = 0,
                        ShopOrderStatusId   = shopOrderStatus.FirstOrDefault().Id,
                        ShopOrderStatusDate = DateTime.Today,
                        ShopGroupId         = shopGroup.FirstOrDefault().Id,
                        Particulars         = "NA",
                        Status              = null,
                        IsPrinted           = false,
                        IsLocked            = false,
                        CreatedById         = currentUser.FirstOrDefault().Id,
                        CreatedDateTime     = DateTime.Now,
                        UpdatedById         = currentUser.FirstOrDefault().Id,
                        UpdatedDateTime     = DateTime.Now
                    };

                    db.TrnShopOrders.InsertOnSubmit(newShopOrder);
                    db.SubmitChanges();

                    responseMessage = newShopOrder.Id.ToString();
                }

                return(Request.CreateResponse(responseStatusCode, responseMessage));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
            }
        }
        public HttpResponseMessage ImportShopOrder(List <Entities.TrnShopOrder> objShopOrders)
        {
            try
            {
                HttpStatusCode responseStatusCode = HttpStatusCode.OK;
                String         responseMessage    = "";

                var currentUser = from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d;
                var userForm    = from d in db.MstUserForms where d.UserId == currentUser.FirstOrDefault().Id&& d.SysForm.FormName.Equals("ShopOrderList") select d;

                if (!userForm.Any())
                {
                    responseStatusCode = HttpStatusCode.NotFound; responseMessage = "No rights.";
                }
                else if (!userForm.FirstOrDefault().CanAdd)
                {
                    responseStatusCode = HttpStatusCode.BadRequest; responseMessage = "No add rights.";
                }
                else
                {
                    Debug.WriteLine(objShopOrders);

                    if (objShopOrders.Any())
                    {
                        Int32 count = 1;

                        List <Entities.TrnShopOrder> newShopOrders = new List <Entities.TrnShopOrder>();
                        foreach (var objShopOrder in objShopOrders)
                        {
                            Boolean isValid = true;

                            var shopOrderObjects = from d in objShopOrders
                                                   where d.SPNumber.Equals(objShopOrder.SPNumber)
                                                   select d;

                            if (shopOrderObjects.Any())
                            {
                                if (shopOrderObjects.Count() > 1)
                                {
                                    isValid = false;

                                    responseStatusCode = HttpStatusCode.NotFound;
                                    responseMessage    = "Duplicate order number: " + objShopOrder.SPNumber;

                                    newShopOrders = new List <Entities.TrnShopOrder>();
                                    break;
                                }
                            }

                            Int32 itemId = 0, unitId = 0;
                            var   item = from d in db.MstArticles
                                         where d.ManualArticleCode.Equals(objShopOrder.ItemCode) &&
                                         d.ArticleTypeId == 1 &&
                                         d.IsLocked == true
                                         select d;

                            if (item.Any())
                            {
                                itemId = item.FirstOrDefault().Id;
                                unitId = item.FirstOrDefault().UnitId;
                            }
                            else
                            {
                                isValid = false;

                                responseStatusCode = HttpStatusCode.NotFound;
                                responseMessage    = "Item code: " + objShopOrder.ItemCode + " not found.";

                                newShopOrders = new List <Entities.TrnShopOrder>();
                                break;
                            }

                            Int32 shopOrderStatusId = 0;
                            var   shopOrderStatus   = from d in db.MstShopOrderStatus select d;
                            if (shopOrderStatus.Any())
                            {
                                shopOrderStatusId = shopOrderStatus.FirstOrDefault().Id;
                            }
                            else
                            {
                                isValid = false;

                                responseStatusCode = HttpStatusCode.NotFound;
                                responseMessage    = "No shop order status";

                                newShopOrders = new List <Entities.TrnShopOrder>();
                                break;
                            }

                            Int32 shopGroupId = 0;
                            var   shopGroup   = from d in db.MstShopGroups select d;
                            if (shopGroup.Any())
                            {
                                shopGroupId = shopGroup.FirstOrDefault().Id;
                            }
                            else
                            {
                                isValid = false;

                                responseStatusCode = HttpStatusCode.NotFound;
                                responseMessage    = "No shop group";

                                newShopOrders = new List <Entities.TrnShopOrder>();
                                break;
                            }

                            var order = from d in db.TrnShopOrders
                                        where d.SPNumber.Equals(objShopOrder.SPNumber) &&
                                        d.IsLocked == true
                                        select d;

                            if (order.Any())
                            {
                                isValid = false;

                                responseStatusCode = HttpStatusCode.NotFound;
                                responseMessage    = "Shop order number: " + objShopOrder.SPNumber + " is already exist.";

                                newShopOrders = new List <Entities.TrnShopOrder>();
                                break;
                            }

                            if (isValid)
                            {
                                newShopOrders.Add(new Entities.TrnShopOrder()
                                {
                                    BranchId            = currentUser.FirstOrDefault().BranchId,
                                    SPNumber            = objShopOrder.SPNumber,
                                    SPDate              = objShopOrder.SPDate,
                                    ItemId              = itemId,
                                    Quantity            = objShopOrder.Quantity,
                                    UnitId              = unitId,
                                    Amount              = objShopOrder.Amount,
                                    ShopOrderStatusId   = shopOrderStatusId,
                                    ShopOrderStatusDate = objShopOrder.SPDate,
                                    ShopGroupId         = shopGroupId,
                                    Particulars         = objShopOrder.Particulars,
                                    Status              = null,
                                    IsPrinted           = false,
                                    IsLocked            = true,
                                    CreatedById         = currentUser.FirstOrDefault().Id,
                                    CreatedDateTime     = DateTime.Now.ToShortDateString(),
                                    UpdatedById         = currentUser.FirstOrDefault().Id,
                                    UpdatedDateTime     = DateTime.Now.ToShortDateString()
                                });

                                count += 1;
                            }
                        }

                        if ((count - 1) == objShopOrders.Count())
                        {
                            if (newShopOrders.Any())
                            {
                                foreach (var objNewShopOrder in newShopOrders)
                                {
                                    Data.TrnShopOrder newShopOrder = new Data.TrnShopOrder
                                    {
                                        BranchId            = objNewShopOrder.BranchId,
                                        SPNumber            = objNewShopOrder.SPNumber,
                                        SPDate              = Convert.ToDateTime(objNewShopOrder.SPDate),
                                        ItemId              = objNewShopOrder.ItemId,
                                        Quantity            = objNewShopOrder.Quantity,
                                        UnitId              = objNewShopOrder.UnitId,
                                        Amount              = objNewShopOrder.Amount,
                                        ShopOrderStatusId   = objNewShopOrder.ShopOrderStatusId,
                                        ShopOrderStatusDate = Convert.ToDateTime(objNewShopOrder.ShopOrderStatusDate),
                                        ShopGroupId         = objNewShopOrder.ShopGroupId,
                                        Particulars         = objNewShopOrder.Particulars,
                                        Status              = objNewShopOrder.Status,
                                        IsPrinted           = objNewShopOrder.IsPrinted,
                                        IsLocked            = objNewShopOrder.IsLocked,
                                        CreatedById         = objNewShopOrder.CreatedById,
                                        CreatedDateTime     = Convert.ToDateTime(objNewShopOrder.CreatedDateTime),
                                        UpdatedById         = objNewShopOrder.UpdatedById,
                                        UpdatedDateTime     = Convert.ToDateTime(objNewShopOrder.UpdatedDateTime),
                                    };

                                    db.TrnShopOrders.InsertOnSubmit(newShopOrder);
                                }

                                db.SubmitChanges();
                            }
                        }
                    }
                }

                return(Request.CreateResponse(responseStatusCode, responseMessage));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
            }
        }