示例#1
0
        public bool SetVenta(List <ETicketVenta> venta, string usr, ref ETransactionResult result)
        {
            bool res = true;

            EMovimiento mov   = new EMovimiento();
            Daproductos _prod = new Daproductos();
            List <EMovimientoDetalle> movdet = new List <EMovimientoDetalle>();
            int idDetalle = 0;

            mov.fecha            = DateTime.Now;
            mov.idMovimiento     = (int)getIdMovimiento();
            mov.idTipoMovimiento = "VTA";
            mov.observacion      = "";
            mov.usuario          = usr;

            idDetalle = (int)getIdMovimientoDetalle();
            foreach (ETicketVenta det in venta)
            {
                EMovimientoDetalle detalle = new EMovimientoDetalle();

                detalle.idDetalle      = idDetalle;
                detalle.idMovimiento   = mov.idMovimiento;
                detalle.tipoAfectacion = "S";
                detalle.idProducto     = det.Producto;
                detalle.cantidad       = det.Cantidad;

                idDetalle += 1;
                movdet.Add(detalle);
            }
            var prods = _prod.productos_GetAll(ref result).Cast <Eproductos>().ToList();

            var invInsuficiente = movdet.Join(prods, _mov => _mov.idProducto, _prods => _prods.idProducto,
                                              (_mov, _prods) => new { Cantidad = _prods.cantidad - _mov.cantidad }).Where(
                x => x.Cantidad < 0).ToList().Count;

            if (invInsuficiente > 0)
            {
                result.result  = -1;
                result.message = "No se puede vender mas producto que el existente.";
                return(false);
            }

            Eticket ticket = new Eticket();
            List <EdetalleTicket> detTic = new List <EdetalleTicket>();

            ticket.idTicket    = (int)getIdTicket();
            ticket.usuario     = usr;
            ticket.fecha       = DateTime.Now;
            ticket.total       = venta.Sum(p => p.Total);
            ticket.observacion = "";
            ticket.cancelado   = false;

            idDetalle = (int)getIdTicketDet();
            foreach (ETicketVenta det in venta)
            {
                EdetalleTicket detalle = new EdetalleTicket();

                detalle.idDetalle  = idDetalle;
                detalle.idTicket   = ticket.idTicket;
                detalle.fecha      = DateTime.Now;
                detalle.idProducto = det.Producto;
                detalle.cantidad   = det.Cantidad;
                detalle.precio     = det.Precio;
                detalle.total      = det.Total;

                idDetalle += 1;

                detTic.Add(detalle);
            }

            var _productos = _prod.productos_GetAll(ref result);

            _productos = _productos.Join(movdet, x => x.idProducto, y => y.idProducto,
                                         (x, y) => x).ToList();

            if (saveTicket(ticket, ref result))
            {
                if (saveDetalleTicket(detTic, ref result))
                {
                    if (saveMovimiento(mov, ref result))
                    {
                        if (!saveDetalleMov(movdet, ref result))
                        {
                            rollbackVenta(_productos, ticket.idTicket, mov.idMovimiento);
                        }
                        else
                        {
                            res = true;
                        }
                    }
                    else
                    {
                        rollbackVenta(_productos, ticket.idTicket, mov.idMovimiento);
                    }
                }
                else
                {
                    rollbackVenta(_productos, ticket.idTicket, mov.idMovimiento);
                }
            }
            else
            {
                rollbackVenta(_productos, ticket.idTicket, mov.idMovimiento);
            }

            return(res);
        }
示例#2
0
        public List <object> GetAll(ref ETransactionResult result)
        {
            Daproductos daLista = new Daproductos();

            return(daLista.productos_GetAll(ref result).ToList <object>());
        }
示例#3
0
        public void SetProductos(string directorio, ref ETransactionResult res)
        {
            res = new ETransactionResult();
            List <Eproductos> prods = new List <Eproductos>();

            try
            {
                using (var reader = new StreamReader(directorio))
                    using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                    {
                        prods = csv.GetRecords <Eproductos>().ToList();
                    }


                Daproductos db = new Daproductos();

                var prod  = db.productos_GetAll(ref res).Cast <Eproductos>().ToList();
                int maxId = 0;

                if (prod.Count != 0)
                {
                    maxId = prod.Select(x => x.idProducto).Max();
                }



                int codigoProducto = maxId + 1;

                bool          bandera = false;
                StringBuilder mensaje = new StringBuilder();

                foreach (Eproductos item in prods)
                {
                    item.idProducto = codigoProducto;
                    codigoProducto++;
                }

                foreach (Eproductos item in prods)
                {
                    res = new ETransactionResult();
                    saveProducto(item, ref res);

                    if (res.result == 1)
                    {
                        bandera = true;
                        mensaje.Append("\n No se pudo guardar el producto " + item.descripcion + " :" + res.message);
                    }
                }

                res = new ETransactionResult();
                if (!string.IsNullOrEmpty(mensaje.ToString()))
                {
                    res.result  = 1;
                    res.message = mensaje.ToString();
                }
            }
            catch (Exception ex)
            {
                res.result  = 1;
                res.message = ex.Message;
            }
        }