Beispiel #1
0
        private void ImportItems()
        {
            using (TransactionScope scope = new TransactionScope())
            {

                List<StockDetailRecord> detail = new List<StockDetailRecord>();

                // Import Items
                Items.Clear();
                foreach (var i in importDataBindingSource.Cast<ImportData>())
                {
                    try
                    {
                        if (i.Import)
                        {
                            bool movesStock = i.MovesStock.ToLower().Trim() == "si" ? true : false;

                            Item item = new Item
                            {
                                ID = Guid.NewGuid(),
                                Name = i.ItemName,
                                ItemType = ItemTypes.Where(it => it.Name == i.ItemTypeName).Single(),
                                InternalCode = i.InternalCode,
                                ExternalCode = i.ExternalCode,
                                BaseMeasurement = Measurements.Where(m => m.Name == i.MeasurementName).Single(),
                                MinimumStock = !String.IsNullOrEmpty(i.MinimumStock) ? Double.Parse(i.MinimumStock) : 0.0,
                                MovesStock = movesStock,
                                CurrentPrice = new Price
                                {
                                    ID = Guid.NewGuid(),
                                    Date = DateTime.Now,
                                    Value = !String.IsNullOrEmpty(i.Price) ? Double.Parse(i.Price) : 0.0
                                }
                            };

                            ItemService.Save(item);

                            i.ImportStatus = ImportStatus.Importado;

                            Items.Add(item);

                            if (movesStock)
                                detail.Add(new StockDetailRecord
                                {
                                    Item = item,
                                    Amount = !String.IsNullOrEmpty(i.ActualStock) ? Double.Parse(i.ActualStock) : 0.0,
                                });

                            i.Import = false;
                        }
                        else
                            Items.Add(ItemService.GetByName(i.ItemName));
                    }
                    catch (Exception ex)
                    {
                        i.ImportStatus = ImportStatus.Error;
                        i.Message = ex.Message;
                    }
                }

                if (detail.Count > 0)
                {
                    StockMovement stockMovement = new StockMovement
                    {
                        ID = Guid.NewGuid(),
                        Comment = "Inventario de Carga Inicial de Datos",
                        Date = DateTime.Now,
                        User = UserService.LoggedInUser
                    };
                    foreach (var d in detail)
                        stockMovement.Details.Add(new StockMovementDetail
                        {
                            ID = Guid.NewGuid(),
                            Item = d.Item,
                            Amount = d.Amount,
                            StockMovement = stockMovement
                        });

                    StockMovementService.Save(stockMovement);
                }

                scope.Complete();
            }
        }
        internal void Save()
        {
            try
            {
                if (this.Items.Count > 0)
                {
                    var dr = MessageBox.Show(
                        "¿Confirma que desea guardar el movimiento?",
                        "Confirmar Movimiento",
                        MessageBoxButton.YesNo,
                        MessageBoxImage.Question,
                        MessageBoxResult.No);
                    if (dr == MessageBoxResult.No)
                        return;

                    StockMovement mov = new StockMovement
                    {
                        Comment = this.Comments,
                        Date = this.Date,
                        UserID = UserService.LoggedInUser.ID
                    };

                    var details = from i in Items
                                  select new StockMovementDetail
                                      {
                                          ItemID = i.ItemID,
                                          Amount = i.Amount * (this.IsInput ? 1 : -1)
                                      };
                    mov.Details.AddRange(details);

                    StockMovementService.Save(mov);

                }

                ClearAll();
            }
            catch (Exception ex)
            {
                ex.ShowMessageBox();
            }
        }
        internal void Save()
        {
            try
            {
                if (this.Items.Count > 0)
                {
                    var confirm = new ConfirmSaleView
                    {
                        Owner = this.view,
                        Total = this.Total,
                        Payment = this.Total
                    };

                    var dr = confirm.ShowDialog();
                    if (!dr.HasValue || dr.Value == false)
                        return;

                    List<SaleDetail> saleDetail = new List<SaleDetail>();
                    List<StockMovementDetail> stockDetail = new List<StockMovementDetail>();

                    Sale sale = new Sale
                    {
                        Total = this.Total,
                        Date = DateTime.Now,
                        UserID = UserService.LoggedInUser.ID,
                        InternalID = this.NextNumber
                    };

                    foreach (var i in Items)
                    {
                        SaleDetail detail = new SaleDetail
                          {
                              ID = Guid.NewGuid(),
                              ItemID = i.ItemID,
                              Amount = i.Amount,
                              Subtotal = i.Subtotal,
                              PriceID = i.PriceID
                          };
                        saleDetail.Add(detail);
                    }

                    sale.Details.AddRange(saleDetail);

                    SaleService.Save(sale);

                    foreach (var i in sale.Details)
                    {
                        if (i.Item.MovesStock)
                        {
                            stockDetail.Add(new StockMovementDetail
                            {
                                ItemID = i.ItemID,
                                Amount = i.Amount * -1,
                                SaleDetailID = i.ID
                            });
                        }
                    }

                    if (stockDetail.Count > 0)
                    {
                        StockMovement stockMovement = new StockMovement
                        {
                            Date = DateTime.Now,
                            Comment = String.Format("Generado desde la venta #{0}", sale.InternalID),
                            UserID = UserService.LoggedInUser.ID
                        };

                        stockMovement.Details.AddRange(stockDetail);

                        StockMovementService.Save(stockMovement);
                    }

                }

                ClearAll();
            }
            catch (Exception ex)
            {
                ex.ShowMessageBox();
            }
        }