public ActionResult _AddEditDocumentPrePO(_AddEditDocumentPrePOViewModel model)
        {
            try
            {
                PrePurcherseOrder prePurcherseOrder = context.PrePurcherseOrder.FirstOrDefault(x => x.PrePurcherseOrderId == model.PrePurcherseOrderId);
                if (prePurcherseOrder != null)
                {
                    byte[] data;
                    using (Stream inputStream = model.File.InputStream)
                    {
                        MemoryStream memoryStream = inputStream as MemoryStream;
                        if (memoryStream == null)
                        {
                            memoryStream = new MemoryStream();
                            inputStream.CopyTo(memoryStream);
                        }

                        data = memoryStream.ToArray();
                    }

                    prePurcherseOrder.DocumentContentType = model.File.ContentType;
                    prePurcherseOrder.DocumentName        = model.DocumentName;
                    prePurcherseOrder.DocumentPath        = data;
                    prePurcherseOrder.DocumentExtension   = Path.GetExtension(model.File.FileName);

                    context.SaveChanges();
                    PostMessage(MessageType.Success);
                }
                else
                {
                    PostMessage(MessageType.Error);
                }
            }
            catch (Exception ex)
            {
                PostMessage(MessageType.Error);
            }

            return(RedirectToAction("ListPrePO", new { FatherId = model.FatherId }));
        }
        public ActionResult _DeletePrePO(_DeletePrePOViewModel model)
        {
            try
            {
                PrePurcherseOrder prePurcherseOrder = null;

                if (model.PrePurcherseOrderId > 0)
                {
                    prePurcherseOrder       = context.PrePurcherseOrder.FirstOrDefault(x => x.PrePurcherseOrderId == model.PrePurcherseOrderId);
                    prePurcherseOrder.State = ConstantHelpers.ESTADO.INACTIVO;
                }

                context.SaveChanges();
                PostMessage(MessageType.Success);
            }
            catch (Exception ex)
            {
                PostMessage(MessageType.Error);
            }

            return(RedirectToAction("ListPrePO", new { FatherId = model.FatherId }));
        }
        public ActionResult AddEditPrePO(AddEditPrePOViewModel model, FormCollection frm)
        {
            try
            {
                String prePOCode = String.Empty;

                using (var transaction = new TransactionScope())
                {
                    PrePurcherseOrder prePO = null;

                    if (model.PrePurcherseOrderId.HasValue)
                    {
                        prePO = context.PrePurcherseOrder.Include(x => x.PrePurcherseOrderDetail).FirstOrDefault(x => x.PrePurcherseOrderId == model.PrePurcherseOrderId);

                        var lstPrePurcherseOrderDetail = prePO.PrePurcherseOrderDetail.ToList();
                        foreach (var d in lstPrePurcherseOrderDetail)
                        {
                            context.PrePurcherseOrderDetail.Remove(d);
                        }
                    }
                    else
                    {
                        prePO       = new PrePurcherseOrder();
                        prePO.State = ConstantHelpers.ESTADO.ACTIVO;
                        context.PrePurcherseOrder.Add(prePO);
                    }

                    prePO.RegistrationDate = model.Registration.ToDateTime();
                    prePO.SupplierId       = model.SupplierId;
                    prePO.CountryId        = model.CountryId;
                    prePO.ShipmentDate     = model.ShipmentDate.ToDateTime();
                    prePO.Text             = model.Text;
                    prePO.SendSupply       = model.SendSupply;
                    prePO.Code             = model.Code;
                    prePO.DateOrder        = model.DateOrder.ToDateTime();


                    var lstQuantity = frm.AllKeys.Where(x => x.StartsWith("quantity-")).ToList();
                    foreach (var quantity in lstQuantity)
                    {
                        PrePurcherseOrderDetail detail = new PrePurcherseOrderDetail();

                        var productId   = quantity.Replace("quantity-", String.Empty).ToInteger();
                        var intQuantity = frm[quantity].ToInteger();
                        var measureunit = frm["measureunit-" + productId].ToInteger();

                        detail.ProductId         = productId;
                        detail.Quantity          = intQuantity;
                        detail.MeasureUnitId     = measureunit;
                        detail.State             = ConstantHelpers.ESTADO.ACTIVO;
                        detail.PrePurcherseOrder = prePO;

                        context.PrePurcherseOrderDetail.Add(detail);
                    }

                    context.SaveChanges();

                    if (String.IsNullOrEmpty(model.Code))
                    {
                        prePO.Code = prePO.PrePurcherseOrderId.ToString();
                        prePOCode  = prePO.Code;
                        context.SaveChanges();
                    }

                    transaction.Complete();
                }

                PostMessage(MessageType.Success, "Los datos se guardaron exitosamente. Pre Purcharse Order: " + prePOCode);
                return(RedirectToAction("ListPrePO", new { FatherId = model.FatherId }));
            }
            catch (Exception ex)
            {
                PostMessage(MessageType.Error);
                model.Fill(CargarDatosContext(), model.PrePurcherseOrderId, model.FatherId);
                return(View(model));
            }
        }