public ActionResult AddEditPO(Int32?PurcherseOrderId, Int32?PrePurcherseOrderId, Int32?FatherId)
        {
            var model = new AddEditPOViewModel();

            model.Fill(CargarDatosContext(), PurcherseOrderId, PrePurcherseOrderId, FatherId);
            return(View(model));
        }
        public ActionResult AddEditPO(AddEditPOViewModel model, FormCollection frm)
        {
            try
            {
                String poCode = String.Empty;

                using (var transaction = new TransactionScope())
                {
                    PurcherseOrder purcharseOrder = null;

                    if (model.PurcherseOrderId.HasValue)
                    {
                        purcharseOrder = context.PurcherseOrder.FirstOrDefault(x => x.PurcherseOrderId == model.PurcherseOrderId);
                    }
                    else
                    {
                        purcharseOrder       = new PurcherseOrder();
                        purcharseOrder.State = ConstantHelpers.ESTADO.ACTIVO;
                        context.PurcherseOrder.Add(purcharseOrder);
                    }

                    purcharseOrder.RegistrationDate        = model.RegistrationDate.ToDateTime();
                    purcharseOrder.CurrencyId              = model.CurrencyId;
                    purcharseOrder.IncotermsId             = model.IncotermsId;
                    purcharseOrder.Total                   = model.Total;
                    purcharseOrder.ToleranceId             = model.ToleranceId;
                    purcharseOrder.ToleranceRemark         = model.ToleranceRemark;
                    purcharseOrder.ShipmentSample          = model.ShipmentSample;
                    purcharseOrder.TreasuryId              = model.TreasuryId;
                    purcharseOrder.PortId                  = model.PortId;
                    purcharseOrder.BeneficiarySupply       = model.BeneficiarySupply;
                    purcharseOrder.BeneficiarySupplyAdress = model.BeneficiarySupplyAdress;
                    purcharseOrder.BeneficiaryBankAdress   = model.BeneficiaryBankAdress;
                    purcharseOrder.BankAccountnumber       = model.BankAccountnumber;
                    purcharseOrder.SwiftCode               = model.SwiftCode;
                    purcharseOrder.IntermediaryBank        = model.IntermediaryBank;
                    purcharseOrder.SwiftIntermediaryBank   = model.SwiftIntermediaryBank;
                    purcharseOrder.SupplierEmail           = model.SupplierEmail;
                    purcharseOrder.CEOEmail                = model.CEOEmail;
                    purcharseOrder.Code                = model.Code ?? String.Empty;
                    purcharseOrder.OrderDate           = model.OrderDate.ToDateTime();
                    purcharseOrder.PrePurcherseOrderId = model.PrePurcherseOrderId.Value;
                    purcharseOrder.DocumentTypeId      = context.DocumentType.FirstOrDefault(x => x.Acronym == ConstantHelpers.TIPODOCUMENTO.PURCHARSE_ORDER).DocumentTypeId;

                    Decimal Total    = 0;
                    var     lstPrice = frm.AllKeys.Where(x => x.StartsWith("price-")).ToList();
                    foreach (var quantity in lstPrice)
                    {
                        var prePurcherseOrderDetailId = quantity.Replace("price-", String.Empty).ToInteger();

                        PrePurcherseOrderDetail detail = context.PrePurcherseOrderDetail.FirstOrDefault(x => x.PrePurcherseOrderDetailId == prePurcherseOrderDetailId);


                        var price = frm[quantity].ToDecimal();
                        Total += price;

                        detail.Price = price;
                    }

                    context.SaveChanges();

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


                    transaction.Complete();
                }

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