Esempio n. 1
0
        public ActionResult AddEditEntryNote(Int32?EntryNoteId, Int32?FatherId)
        {
            var model = new AddEditEntryNoteViewModel();

            model.Fill(CargarDatosContext(), EntryNoteId, FatherId);
            return(View(model));
        }
Esempio n. 2
0
        public ActionResult AddEditEntryNote(AddEditEntryNoteViewModel model, FormCollection frm)
        {
            try
            {
                String entryCode = String.Empty;
                using (var transaction = new TransactionScope())
                {
                    EntryNote entryNote = null;

                    if (model.EntryNoteId.HasValue)
                    {
                        PostMessage(MessageType.Warning, "No se puede editar una nota de ingreso.");
                        return(RedirectToAction("ListEntryNote", new { FatherId = model.FatherId }));
                        //entryNote = context.EntryNote.FirstOrDefault(x => x.EntryNoteId == model.EntryNoteId);
                    }
                    else
                    {
                        entryNote       = new EntryNote();
                        entryNote.State = ConstantHelpers.ESTADO.ACTIVO;
                        entryNote.Date  = DateTime.Now;
                        entryNote.Time  = DateTime.Now.TimeOfDay;
                        context.EntryNote.Add(entryNote);
                    }

                    entryNote.MovementTypeId       = model.MovementTypeId;
                    entryNote.SupplierGuideNumber  = model.SupplierGuideNumber;
                    entryNote.TransportGuideNumber = model.TransportGuideNumber;
                    entryNote.Seal               = model.Seal;
                    entryNote.TransportTime      = model.TransportTime;
                    entryNote.DestinationStoreId = model.DestinationStoreId;
                    entryNote.SupplierId         = model.SupplierId;
                    entryNote.DocumentCode       = model.DocumentCode;
                    entryNote.DocumentId         = model.DocumentId;
                    entryNote.Code               = model.Code ?? String.Empty;

                    List <StockProduct> lstStockProduct = new List <StockProduct>();
                    var lstQuantity = frm.AllKeys.Where(x => x.StartsWith("qreal-")).ToList();
                    foreach (var q in lstQuantity)
                    {
                        var data = q.Replace("qreal-", String.Empty).Split('-');
                        var PrePurcherseOrderDetailId = data[1].ToInteger();
                        var index = data[0].ToInteger();

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

                        var stockProductDetail = new StockProductDetail();
                        context.StockProductDetail.Add(stockProductDetail);
                        stockProductDetail.EntryNote = entryNote;
                        stockProductDetail.State     = ConstantHelpers.ESTADO.ACTIVO;
                        stockProductDetail.Operation = ConstantHelpers.OPERATION.ENTRY;
                        stockProductDetail.Value     = frm["qreal-" + index + "-" + PrePurcherseOrderDetailId].ToDecimal();
                        stockProductDetail.Date      = DateTime.Now;
                        //stockProductDetail.CUPrice = detail.Price;
                        //stockProductDetail.CurrencyId = detail.PrePurcherseOrder.PurcherseOrder.FirstOrDefault(x => x.State == ConstantHelpers.ESTADO.ACTIVO).CurrencyId;

                        var stock = context.StockProduct.FirstOrDefault(x => x.ProductId == detail.ProductId && x.StoreId == model.DestinationStoreId && x.State == ConstantHelpers.ESTADO.ACTIVO);
                        if (stock != null)
                        {
                            stock.Quantity += stockProductDetail.Value;
                        }
                        else
                        {
                            stock = lstStockProduct.FirstOrDefault(x => x.ProductId == detail.ProductId && x.StoreId == model.DestinationStoreId && x.State == ConstantHelpers.ESTADO.ACTIVO);
                            if (stock != null)
                            {
                                stock.Quantity += stockProductDetail.Value;
                            }
                            else
                            {
                                stock           = new StockProduct();
                                stock.Quantity  = stockProductDetail.Value;
                                stock.State     = ConstantHelpers.ESTADO.ACTIVO;
                                stock.ProductId = detail.ProductId;
                                stock.StoreId   = model.DestinationStoreId;
                                lstStockProduct.Add(stock);
                            }
                            //context.StockProduct.Add(stock);
                        }

                        stockProductDetail.StockProduct = stock;



                        EntryNoteDetail entryNoteDetail = new EntryNoteDetail();
                        entryNoteDetail.EntryNote                 = entryNote;
                        entryNoteDetail.State                     = ConstantHelpers.ESTADO.ACTIVO;
                        entryNoteDetail.TypePayment               = (frm["typepayment-" + index + "-" + PrePurcherseOrderDetailId] ?? "TOT").ToString();
                        entryNoteDetail.RealQuantity              = frm["qreal-" + index + "-" + PrePurcherseOrderDetailId].ToDecimal();
                        entryNoteDetail.RealMeasureUnit           = "m";
                        entryNoteDetail.PrePurcherseOrderDetailId = PrePurcherseOrderDetailId;
                        entryNoteDetail.SecRealMeasureUnit        = "rollos";
                        //entryNoteDetail.ProductId = detail.ProductId;
                        entryNoteDetail.SecRealQuantity = frm["qsreal-" + index + "-" + PrePurcherseOrderDetailId].ToDecimal();
                        context.EntryNoteDetail.Add(entryNoteDetail);
                    }

                    context.StockProduct.AddRange(lstStockProduct);
                    context.SaveChanges();

                    if (String.IsNullOrEmpty(model.Code))
                    {
                        entryNote.Code = entryNote.EntryNoteId.ToString();
                        entryCode      = entryNote.Code;
                        context.SaveChanges();
                    }

                    transaction.Complete();
                }


                PostMessage(MessageType.Success, "Los datos se guardaron exitosamente. Nota de Salida: " + entryCode);
                return(RedirectToAction("ListEntryNote", new { FatherId = model.FatherId }));
            }
            catch (Exception ex)
            {
                PostMessage(MessageType.Error);
                model.Fill(CargarDatosContext(), model.EntryNoteId, model.FatherId);
                return(View(model));
            }
        }