public bool UpdateOrder(OrderReqExtend items)
        {
            bool result = true;

            try
            {
                if (items != null)
                {
                    var getforUpdate = context.OrderReqs.FirstOrDefault(a => a.Code == items.Code);

                    if (getforUpdate != null)
                    {
                        getforUpdate.Status               = items.Status;
                        getforUpdate.Observation          = items.Observation;
                        context.Entry(getforUpdate).State = EntityState.Modified;
                        context.SaveChanges();
                    }
                }
                else
                {
                    result = false;
                }
            }
            catch (Exception ex)
            {
                result = false;
                Debug.Write("Error " + ex.Message);
            }
            return(result);
        }
        public async Task <bool> UpdateOrder(OrderReqExtend codes)
        {
            bool   result = false;
            string url    = "http://" + PATHSERVER + "/tshirt/productchange/SaveOrder";

            try
            {
                var json    = JsonConvert.SerializeObject(codes);
                var content = new StringContent(json, Encoding.UTF8, "application/json");

                HttpResponseMessage request = null;

                request = await client.PostAsync(url, content);

                if (request.IsSuccessStatusCode)
                {
                    var x = await request.Content.ReadAsStringAsync();

                    result = JsonConvert.DeserializeObject <bool>(x);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(@"				ERROR {0}", ex.Message);
            }
            return(result);
        }
        private async void Save()
        {
            bool result = false;
            var  items  = new OrderReqExtend();

            items.Code        = Pedido;
            items.Status      = "1";
            items.Observation = Observation;


            var answer = await App.Current.MainPage.DisplayAlert("TSHIRT", "Desea Guardar Cambio de Productos?", "SI", "NO");

            if (answer)
            {
                result = await services.UpdateOrder(items);

                if (result)
                {
                    await Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new Result());
                }
                else
                {
                    App.Current.MainPage.DisplayAlert("TSHIRT", "Error guardando registro", "OK");
                }
            }
        }
        public bool Save(OrderReqExtend items)
        {
            XmlChange xml       = new XmlChange();
            var       order     = new OrderReqExtend();
            var       detail    = new OrderReqDetailExtend();
            bool      isUpdated = true;

            try
            {
                //delete Order
                OrderReq ordertoDelete = context.OrderReqs.FirstOrDefault(a => a.Code == items.Code);
                context.Entry(ordertoDelete).State = EntityState.Deleted;
                context.SaveChanges();


                //delete OrderDetail
                List <OrderReqDetail> listOrderDetailToDelete = context.OrderReqDetails.Where(a => a.OrderReqCode == items.Code).ToList();
                context.OrderReqDetails.RemoveRange(listOrderDetailToDelete);
                context.SaveChanges();

                //Update status OrderReqDetailProduct
                var detailforUpdate = context.OrderReqDetailProducts.Where(a => a.OrderReqCode == items.Code && a.Status == 0).ToList();
                detailforUpdate.ForEach(a =>
                {
                    a.Status = 1;
                });
                context.SaveChanges();

                bool resultCreatexml = xml.Create(items.Code); //Crea XML

                //if (result)
                //{
                //    var detailforUpdate = context.OrderReqDetailProducts.Where(a=> a.OrderReqCode == items.Code && a.Status == 0).ToList();
                //    detailforUpdate.ForEach(a =>
                //    {
                //        a.Status = 1;
                //    });


                //    foreach (OrderReqDetailProduct row in detailforUpdate)
                //    {
                //        row.Status = 1;
                //        context.Entry(row).State = EntityState.Modified;
                //        context.SaveChanges();
                //    }

                //   // context.SaveChanges();

                //    bool resultCreatexml = xml.Create(items.Code); //Crea XML
                //}
            }
            catch (Exception)
            {
                isUpdated = false;
                throw;
            }
            return(isUpdated);
        }
        public async Task <ActionResult> Add(OrderReqExtend aModel)
        {
            _services = new ProductChangeServices();
            var order = new OrderReqExtend();

            if (aModel != null)
            {
                var result = await _services.Save(aModel);

                if (result)
                {
                    return(RedirectToAction("Index"));
                }
            }

            return(View("Index"));
        }
        public async Task <ActionResult> Add(string code)
        {
            _services = new ProductChangeServices();
            var order = new OrderReqExtend();

            if (!string.IsNullOrEmpty(code))
            {
                order = await _services.GetDetailByCode(code);
            }
            else
            {
                return(View("Index"));
            }


            return(View(order));
        }
        private async void SearchImage()
        {
            var result = new OrderReqExtend();

            result = await services.GetOrderByCode(NroPedido);

            if (result.Id > 0)
            {
                Visible    = true;
                ClientName = result.ClientName;
            }
            else
            {
                Visible    = false;
                ClientName = "Pedido NO encontrado";
            }
            OnPropertyChanged("SearchImage");
        }
        public async Task <ActionResult> OrderDetails(List <OrderReqDetailExtend> aModel, string order, string prod, string prodChan, string prodChanNam, int?qty)
        {
            if (!string.IsNullOrEmpty(order))
            {
                _services = new ProductChangeServices();
                var result = new OrderReqExtend();
                var list   = new List <OrderReqDetailExtend>();
                result = await _services.GetDetailByCode(order);

                if (result != null)
                {
                    var op = Session["OptionCRUD"].ToString();

                    if (op == "INS")
                    {
                        result.Detail.Where(a => a.ProductCode == prod).ForEach(p =>
                        {
                            p.ProductCodeChanged = prodChan;
                            p.ProductNameChanged = prodChanNam;
                            if (qty != null)
                            {
                                p.Quantity = (int)qty;
                            }
                        });
                    }
                    else if (op == "DEL")
                    {
                        result.Detail.Where(a => a.ProductCode == prod).ForEach(p =>
                        {
                            p.ProductCodeChanged = null;
                            p.ProductNameChanged = null;
                            p.Quantity           = null;
                        });
                    }


                    //return PartialView("_Details", (IEnumerable<OrderReqDetailExtend>)result.Detail);
                    return(PartialView("_Details", result.Detail));
                }
            }

            //return PartialView("_Details", (IEnumerable<OrderReqDetailExtend>)aModel);
            return(PartialView("_Details", aModel));
        }
        public async Task <OrderReqExtend> GetOrderByCode(string code)
        {
            var    items = new OrderReqExtend();
            string url   = "http://" + PATHSERVER + "/tshirt/productchange/GetOrderByCode?code=";
            string uri   = string.Concat(url, code);

            try
            {
                var result = await client.GetAsync(uri);

                if (result.IsSuccessStatusCode)
                {
                    var content = await result.Content.ReadAsStringAsync();

                    items = JsonConvert.DeserializeObject <OrderReqExtend>(content);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(@"				ERROR {0}", ex.Message);
            }
            return(items);
        }
        public OrderReqExtend GetOrderByCode(string code)
        {
            var order = new OrderReqExtend();

            try
            {
                order = (from or in context.OrderReqs
                         where or.Code == code
                         select new OrderReqExtend()
                {
                    Id = or.Id,
                    Code = or.Code,
                    Description = or.Description,
                    Status = or.Status,
                    ClientCode = or.ClientCode,
                    DateCreated = or.DateCreated,
                    Value1 = or.Value1,
                    Value2 = or.Value2,
                    Value3 = or.Value3,
                    Value4 = or.Value4,
                    Value5 = or.Value5,
                    Observation = or.Observation,
                    ClientName = or.ClientCode
                }).FirstOrDefault();

                if (order == null) //si no se encuentra en la bd intermedia busca en GP
                {
                    using (TSGVLEntities db = new TSGVLEntities())
                    {
                        var _sop10100 = db.SOP10100.FirstOrDefault(a => a.SOPNUMBE.Trim().Equals(code));

                        if (_sop10100 == null)
                        {
                            var _sop30200 = db.SOP30200.FirstOrDefault(a => a.SOPNUMBE.Trim() == code);
                            order = new OrderReqExtend
                            {
                                Id          = 1,
                                ClientCode  = _sop30200.CUSTNAME.Trim().Replace("'", "''"),
                                Code        = _sop30200.SOPNUMBE.Trim(),
                                ClientName  = _sop30200.CUSTNAME.Trim().Replace("'", "''"),
                                DateCreated = _sop30200.DOCDATE.ToString("dd/MM/yyyy"),
                                Status      = _sop30200.SOPSTATUS.ToString()
                            };
                        }
                        else
                        {
                            order = new OrderReqExtend
                            {
                                Id          = 1,
                                ClientCode  = _sop10100.CUSTNAME.Trim().Replace("'", "''"),
                                Code        = _sop10100.SOPNUMBE.Trim(),
                                ClientName  = _sop10100.CUSTNAME.Trim().Replace("'", "''"),
                                DateCreated = _sop10100.DOCDATE.ToString("dd/MM/yyyy"),
                                Status      = _sop10100.SOPSTATUS.ToString()
                            };
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Write("Error " + ex.InnerException.Message);
                return(null);
            }
            return(order);
        }
        public OrderReqExtend GetDetailByCode(string code)
        {
            try
            {
                var order = new OrderReqExtend();

                List <OrderReqDetailExtend> list         = new List <OrderReqDetailExtend>();
                List <OrderReqDetail>       detailInsert = new List <OrderReqDetail>();
                using (TSGVLEntities db = new TSGVLEntities())
                {
                    var anyOrder = context.OrderReqs.Any(a => a.Code == code);
                    if (!anyOrder)
                    {
                        var master = db.SOP10100.FirstOrDefault(p => p.SOPNUMBE.Trim().Equals(code));

                        if (master != null)
                        {
                            var _master = new OrderReq()
                            {
                                Code        = master.SOPNUMBE.Trim(),
                                ClientCode  = master.CUSTNAME.Trim().Replace("'", "''"),
                                DateCreated = master.DOCDATE.ToString("dd/MM/yyyy"),
                                Status      = master.SOPSTATUS.ToString(),
                            };

                            context.OrderReqs.Add(_master);
                            context.SaveChanges();

                            order.Id          = _master.Id;
                            order.Code        = _master.Code;
                            order.DateCreated = _master.DateCreated;
                            order.Status      = _master.Status;
                            order.ClientCode  = _master.ClientCode;
                            order.ClientName  = _master.ClientCode;

                            var detail = db.SOP10200.Where(p => p.SOPNUMBE.Trim().Equals(code)).ToList();
                            if (detail.Any())
                            {
                                foreach (var row in detail)
                                {
                                    var _detail = new OrderReqDetail()
                                    {
                                        OrderReqCode = row.SOPNUMBE.Trim(),
                                        Observation  = row.ITEMDESC.Trim(),
                                        ProductCode  = row.ITEMNMBR.Trim().Replace("'", "''"),
                                        Quantity     = Convert.ToInt32(row.QUANTITY),
                                        Warehouse    = row.LOCNCODE.Trim()
                                    };
                                    detailInsert.Add(_detail);

                                    context.OrderReqDetails.Add(_detail);
                                    context.SaveChanges();
                                }

                                list.AddRange(detailInsert.Select(items => new OrderReqDetailExtend()
                                {
                                    OrderReqCode       = items.OrderReqCode,
                                    Observation        = string.Empty,
                                    ProductCode        = items.ProductCode,
                                    ProductCodeChanged = string.Empty,
                                    Quantity           = items.Quantity,
                                    DateProductChanged = string.Empty,
                                    UserUpdated        = string.Empty,
                                    ProductName        = items.Observation,
                                    ProductNameChanged = string.Empty,
                                    QuantityChanged    = 0,
                                    Warehouse          = items.Warehouse
                                }));
                                order.Detail = list;
                            }
                        }
                        else
                        {
                            var master30200 = db.SOP30200.FirstOrDefault(p => p.SOPNUMBE.Trim().Equals(code));
                            var _master2    = new OrderReq()
                            {
                                Code        = master30200.SOPNUMBE.Trim(),
                                ClientCode  = master30200.CUSTNAME.Trim().Replace("'", "''"),
                                DateCreated = master30200.DOCDATE.ToString("dd/MM/yyyy"),
                                Status      = master30200.SOPSTATUS.ToString(),
                            };

                            context.OrderReqs.Add(_master2);
                            context.SaveChanges();

                            order.Id          = _master2.Id;
                            order.Code        = _master2.Code;
                            order.DateCreated = _master2.DateCreated;
                            order.Status      = _master2.Status;
                            order.ClientCode  = _master2.ClientCode;
                            order.ClientName  = _master2.ClientCode;

                            var detail = db.SOP30300.Where(p => p.SOPNUMBE.Trim().Equals(code)).ToList();
                            if (detail.Any())
                            {
                                foreach (var row in detail)
                                {
                                    var _detail = new OrderReqDetail()
                                    {
                                        OrderReqCode = row.SOPNUMBE.Trim(),
                                        Observation  = row.ITEMDESC.Trim(),
                                        ProductCode  = row.ITEMNMBR.Trim().Replace("'", "''"),
                                        Quantity     = Convert.ToInt32(row.QUANTITY),
                                        Warehouse    = row.LOCNCODE.Trim()
                                    };
                                    detailInsert.Add(_detail);

                                    context.OrderReqDetails.Add(_detail);
                                    context.SaveChanges();
                                }

                                list.AddRange(detailInsert.Select(items => new OrderReqDetailExtend()
                                {
                                    OrderReqCode       = items.OrderReqCode,
                                    Observation        = string.Empty,
                                    ProductCode        = items.ProductCode,
                                    ProductCodeChanged = string.Empty,
                                    Quantity           = items.Quantity,
                                    DateProductChanged = string.Empty,
                                    UserUpdated        = string.Empty,
                                    ProductName        = items.Observation,
                                    ProductNameChanged = string.Empty,
                                    QuantityChanged    = 0,
                                    Warehouse          = items.Warehouse
                                }));
                                order.Detail = list;
                            }
                        }
                    }
                    else
                    {
                        order = GetOrderByCode(code);

                        var qyDetail = (from or in context.OrderReqDetails
                                        join orp in context.OrderReqDetailProducts on new { or.OrderReqCode, or.ProductCode } equals new { orp.OrderReqCode, orp.ProductCode }
                                        into rel
                                        from relleft in rel.DefaultIfEmpty()
                                        where or.OrderReqCode.Trim() == code
                                        select new OrderReqDetailExtend
                        {
                            Id = or.Id,
                            OrderReqCode = or.OrderReqCode.Trim(),
                            Observation = or.Observation,
                            ProductCode = or.ProductCode,
                            ProductName = or.Observation,
                            ProductCodeChanged = relleft.ProductCodeChanged,
                            Quantity = or.Quantity,
                            DateProductChanged = relleft.DateProductChanged,
                            UserUpdated = relleft.UserUpdated,
                            QuantityChanged = relleft.Quantity == null ? 0 : relleft.Quantity,
                            Warehouse = or.Warehouse
                        }).ToList();

                        list.AddRange(qyDetail.Select(item => new OrderReqDetailExtend
                        {
                            Id                 = item.Id,
                            OrderReqCode       = item.OrderReqCode.Trim(),
                            Observation        = item.Observation,
                            ProductCode        = item.ProductCode,
                            ProductCodeChanged = item.ProductCodeChanged,
                            Quantity           = item.Quantity,
                            DateProductChanged = item.DateProductChanged,
                            UserUpdated        = item.UserUpdated,
                            ProductName        = item.ProductName, //db.IV00101.FirstOrDefault(a => a.ITEMNMBR.Trim() == item.ProductCode).ITEMDESC.Trim(),
                            ProductNameChanged = (item.ProductCodeChanged != null) ? db.IV00101.FirstOrDefault(a => a.ITEMNMBR.Trim() == item.ProductCodeChanged.ToString()).ITEMDESC.Trim() : string.Empty,
                            QuantityChanged    = item.QuantityChanged,
                            Warehouse          = item.Warehouse
                        }));

                        order.Detail = list;
                    }
                }

                return(order);
            }
            catch (Exception ex)
            {
                return(null);

                Debug.Write("Error " + ex.InnerException.Message);
            }
        }
 public bool Save(OrderReqExtend items)
 {
     return(_productRepository.Save(items));
 }