Beispiel #1
0
        public async Task <IActionResult> GetKardexLineById(Int64 KardexLineId)
        {
            KardexLine Items = new KardexLine();

            try
            {
                Items = await _context.KardexLine.Where(q => q.KardexLineId == KardexLineId).FirstOrDefaultAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError($"Ocurrio un error: { ex.ToString() }");
                return(BadRequest($"Ocurrio un error:{ex.Message}"));
            }


            return(Ok(Items));
        }
Beispiel #2
0
        public async Task <ActionResult <KardexLine> > Insert([FromBody] KardexLine _KardexLine)
        {
            KardexLine _KardexLineq = new KardexLine();

            try
            {
                _KardexLineq = _KardexLine;
                _context.KardexLine.Add(_KardexLineq);
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError($"Ocurrio un error: { ex.ToString() }");
                return(BadRequest($"Ocurrio un error:{ex.Message}"));
            }

            return(await Task.Run(() => Ok(_KardexLineq)));
        }
Beispiel #3
0
        public async Task <IActionResult> Delete([FromBody] KardexLine _KardexLine)
        {
            KardexLine _KardexLineq = new KardexLine();

            try
            {
                _KardexLineq = _context.KardexLine
                               .Where(x => x.KardexLineId == (Int64)_KardexLine.KardexLineId)
                               .FirstOrDefault();

                _context.KardexLine.Remove(_KardexLineq);
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError($"Ocurrio un error: { ex.ToString() }");
                return(BadRequest($"Ocurrio un error:{ex.Message}"));
            }

            return(await Task.Run(() => Ok(_KardexLineq)));
        }
Beispiel #4
0
        public async Task <ActionResult <KardexLine> > Update([FromBody] KardexLine _KardexLine)
        {
            KardexLine _KardexLineq = _KardexLine;

            try
            {
                _KardexLineq = await(from c in _context.KardexLine
                                     .Where(q => q.KardexLineId == _KardexLine.KardexLineId)
                                     select c
                                     ).FirstOrDefaultAsync();

                _context.Entry(_KardexLineq).CurrentValues.SetValues((_KardexLine));

                //_context.KardexLine.Update(_KardexLineq);
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError($"Ocurrio un error: { ex.ToString() }");
                return(BadRequest($"Ocurrio un error:{ex.Message}"));
            }

            return(await Task.Run(() => Ok(_KardexLineq)));
        }
        public async Task <ActionResult <GoodsDelivered> > Insert([FromBody] GoodsDeliveredDTO _GoodsDelivered)
        {
            GoodsDelivered _GoodsDeliveredq = new GoodsDelivered();

            try
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        _GoodsDeliveredq = _GoodsDelivered;

                        BoletaDeSalida _boletadesalida = new BoletaDeSalida
                        {
                            BranchId                     = _GoodsDelivered.BranchId,
                            BranchName                   = _GoodsDelivered.BranchName,
                            CustomerId                   = _GoodsDelivered.CustomerId,
                            CustomerName                 = _GoodsDelivered.CustomerName,
                            DocumentDate                 = _GoodsDelivered.DocumentDate,
                            FechaCreacion                = DateTime.Now,
                            FechaModificacion            = DateTime.Now,
                            Marca                        = _GoodsDelivered.Marca,
                            Placa                        = _GoodsDelivered.Placa,
                            Motorista                    = _GoodsDelivered.Name,
                            Quantity                     = _GoodsDelivered._GoodsDeliveredLine.Select(q => q.QuantitySacos).Sum(),
                            SubProductId                 = _GoodsDelivered.SubProductId,
                            SubProductName               = _GoodsDelivered.SubProductName,
                            GoodsDeliveryAuthorizationId = _GoodsDelivered.GoodsDeliveryAuthorizationId,
                            GoodsDeliveredId             = _GoodsDeliveredq.GoodsDeliveredId,
                            CargadoId                    = 13,
                            Cargadoname                  = "Cargado",
                            UsuarioCreacion              = _GoodsDelivered.UsuarioCreacion,
                            UsuarioModificacion          = _GoodsDelivered.UsuarioModificacion,
                            UnitOfMeasureId              = _GoodsDelivered._GoodsDeliveredLine[0].UnitOfMeasureId,
                            UnitOfMeasureName            = _GoodsDelivered._GoodsDeliveredLine[0].UnitOfMeasureName,
                            WeightBallot                 = _GoodsDelivered.WeightBallot,
                        };

                        _context.BoletaDeSalida.Add(_boletadesalida);
                        await _context.SaveChangesAsync();

                        _GoodsDeliveredq.ExitTicket = _boletadesalida.BoletaDeSalidaId;

                        _context.GoodsDelivered.Add(_GoodsDeliveredq);

                        foreach (var item in _GoodsDeliveredq._GoodsDeliveredLine)
                        {
                            item.GoodsDeliveredId = _GoodsDeliveredq.GoodsDeliveredId;
                            _context.GoodsDeliveredLine.Add(item);

                            Kardex _kardexmax = await(from c in _context.Kardex
                                                      .OrderByDescending(q => q.DocumentDate)
                                                      // .Take(1)
                                                      join d in _context.KardexLine on c.KardexId equals d.KardexId
                                                      where c.CustomerId == _GoodsDeliveredq.CustomerId && d.SubProducId == item.SubProductId &&
                                                      c.DocumentName != "CD" && d.WareHouseId == item.WareHouseId
                                                      select c
                                                      )
                                                .FirstOrDefaultAsync();

                            if (_kardexmax == null)
                            {
                                _kardexmax = new Kardex();
                            }
                            KardexLine _KardexLine = await _context.KardexLine
                                                     .Where(q => q.KardexId == _kardexmax.KardexId)
                                                     .Where(q => q.SubProducId == item.SubProductId)
                                                     .Where(q => q.WareHouseId == item.WareHouseId)
                                                     .Where(q => q.BranchId == _GoodsDeliveredq.BranchId)
                                                     .OrderByDescending(q => q.KardexLineId)
                                                     .Take(1)
                                                     .FirstOrDefaultAsync();

                            SubProduct _subproduct = await(from c in _context.SubProduct
                                                           .Where(q => q.SubproductId == item.SubProductId)
                                                           select c
                                                           ).FirstOrDefaultAsync();

                            if (_KardexLine.Total > item.Quantity)
                            {
                                item.Total = _KardexLine.TotalCD - item.Quantity;
                            }
                            else
                            {
                                return(await Task.Run(() => BadRequest("Inventario insuficiente!")));
                            }

                            _GoodsDelivered.Kardex._KardexLine.Add(new KardexLine
                            {
                                DocumentDate      = _GoodsDeliveredq.DocumentDate,
                                ProducId          = _GoodsDeliveredq.ProductId,
                                ProductName       = _GoodsDeliveredq.ProductName,
                                SubProducId       = item.SubProductId,
                                SubProductName    = item.SubProductName,
                                QuantityEntry     = 0,
                                QuantityOut       = item.Quantity,
                                BranchId          = _GoodsDeliveredq.BranchId,
                                BranchName        = _GoodsDeliveredq.BranchName,
                                WareHouseId       = item.WareHouseId,
                                WareHouseName     = item.WareHouseName,
                                UnitOfMeasureId   = item.UnitOfMeasureId,
                                UnitOfMeasureName = item.UnitOfMeasureName,
                                TypeOperationId   = 1,
                                TypeOperationName = "Salida",
                                Total             = item.Total,
                                TotalBags         = _KardexLine.TotalBags - item.QuantitySacos,
                                QuantityOutCD     = item.Quantity - (item.Quantity * _subproduct.Merma),
                                TotalCD           = _KardexLine.TotalCD - (item.Quantity - (item.Quantity * _subproduct.Merma)),
                            });
                        }

                        await _context.SaveChangesAsync();

                        _GoodsDelivered.Kardex.DocType           = 0;
                        _GoodsDelivered.Kardex.DocName           = "EntregaMercaderia/GoodsDelivered";
                        _GoodsDelivered.Kardex.DocumentDate      = _GoodsDeliveredq.DocumentDate;
                        _GoodsDelivered.Kardex.FechaCreacion     = DateTime.Now;
                        _GoodsDelivered.Kardex.FechaModificacion = DateTime.Now;
                        _GoodsDelivered.Kardex.TypeOperationId   = 1;
                        _GoodsDelivered.Kardex.TypeOperationName = "Salida";
                        _GoodsDelivered.Kardex.KardexDate        = DateTime.Now;

                        _GoodsDelivered.Kardex.DocumentName = "CE";

                        _GoodsDelivered.Kardex.CustomerId          = _GoodsDeliveredq.CustomerId;
                        _GoodsDelivered.Kardex.CustomerName        = _GoodsDeliveredq.CustomerName;
                        _GoodsDelivered.Kardex.CurrencyId          = _GoodsDeliveredq.CurrencyId;
                        _GoodsDelivered.Kardex.CurrencyName        = _GoodsDeliveredq.CurrencyName;
                        _GoodsDelivered.Kardex.DocumentId          = _GoodsDeliveredq.GoodsDeliveredId;
                        _GoodsDelivered.Kardex.UsuarioCreacion     = _GoodsDeliveredq.UsuarioCreacion;
                        _GoodsDelivered.Kardex.UsuarioModificacion = _GoodsDeliveredq.UsuarioModificacion;
                        _context.Kardex.Add(_GoodsDelivered.Kardex);

                        await _context.SaveChangesAsync();

                        BitacoraWrite _write = new BitacoraWrite(_context, new Bitacora
                        {
                            IdOperacion  = _GoodsDelivered.GoodsDeliveredId,
                            DocType      = "GoodsDelivered",
                            ClaseInicial =
                                Newtonsoft.Json.JsonConvert.SerializeObject(_GoodsDelivered, new JsonSerializerSettings {
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                            }),
                            ResultadoSerializado = Newtonsoft.Json.JsonConvert.SerializeObject(_GoodsDelivered, new JsonSerializerSettings {
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                            }),
                            Accion              = "Insert",
                            FechaCreacion       = DateTime.Now,
                            FechaModificacion   = DateTime.Now,
                            UsuarioCreacion     = _GoodsDelivered.UsuarioCreacion,
                            UsuarioModificacion = _GoodsDelivered.UsuarioModificacion,
                            UsuarioEjecucion    = _GoodsDelivered.UsuarioModificacion,
                        });

                        await _context.SaveChangesAsync();



                        _boletadesalida.GoodsDeliveredId = _GoodsDeliveredq.GoodsDeliveredId;
                        _context.Entry(_boletadesalida).CurrentValues.SetValues((_boletadesalida));

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw ex;
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Ocurrio un error: { ex.ToString() }");
                return(await Task.Run(() => BadRequest($"Ocurrio un error:{ex.Message}")));
            }

            return(await Task.Run(() => Ok(_GoodsDeliveredq)));
        }
Beispiel #6
0
        public async Task <ActionResult <PurchaseOrder> > InsertWithInventory([FromBody] PurchaseOrderDTO _PurchaseOrders)
        {
            PurchaseOrder _PurchaseOrdersq = new PurchaseOrder();

            try
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        _context.PurchaseOrder.Add(_PurchaseOrders);
                        // await _context.SaveChangesAsync();

                        foreach (var item in _PurchaseOrders.PurchaseOrderLines)
                        {
                            item.Id = _PurchaseOrders.Id;
                            _context.PurchaseOrderLine.Add(item);

                            Kardex _kardexmax = await(from c in _context.Kardex
                                                      .OrderByDescending(q => q.DocumentDate)
                                                      // .Take(1)
                                                      join d in _context.KardexLine on c.KardexId equals d.KardexId
                                                      where d.ProducId == item.ProductId
                                                      select c
                                                      )
                                                .FirstOrDefaultAsync();

                            if (_kardexmax == null)
                            {
                                _kardexmax = new Kardex();
                            }
                            KardexLine _KardexLine = await _context.KardexLine
                                                     .Where(q => q.KardexId == _kardexmax.KardexId)
                                                     .Where(q => q.ProducId == item.ProductId)
                                                     //.Where(q => q.WareHouseId == item.WareHouseId)
                                                     //.Where(q => q.BranchId == _GoodsDeliveredq.BranchId)
                                                     .OrderByDescending(q => q.KardexLineId)
                                                     .Take(1)
                                                     .FirstOrDefaultAsync();

                            Product _subproduct = await(from c in _context.Product
                                                        .Where(q => q.ProductId == item.ProductId)
                                                        select c
                                                        ).FirstOrDefaultAsync();


                            item.QtyReceivedToDate = _KardexLine.Total + item.QtyReceived;



                            _PurchaseOrders.Kardex._KardexLine.Add(new KardexLine
                            {
                                DocumentDate   = _PurchaseOrders.DatePlaced,
                                ProducId       = Convert.ToInt64(item.ProductId),
                                ProductName    = item.ProductDescription,
                                SubProducId    = 0,
                                SubProductName = "N/A",
                                QuantityEntry  = Convert.ToDouble(item.QtyReceived),
                                QuantityOut    = 0,
                                BranchId       = Convert.ToInt64(_PurchaseOrders.BranchId),
                                //BranchName = _PurchaseOrders.Branch.BranchName,
                                WareHouseId       = 0,
                                WareHouseName     = "N/A",
                                UnitOfMeasureId   = item.UnitOfMeasureId,
                                UnitOfMeasureName = item.UnitOfMeasureName,
                                TypeOperationId   = 1,
                                TypeOperationName = "Orden de Compra",
                                Total             = Convert.ToDouble(item.QtyReceivedToDate),
                                // TotalBags = item.QuantitySacos - _KardexLine.TotalBags,
                                //QuantityOutCD = item.Quantity - (item.Quantity * _subproduct.Merma),
                                //TotalCD = _KardexLine.TotalCD - (item.Quantity - (item.Quantity * _subproduct.Merma)),
                            });
                        }
                        await _context.SaveChangesAsync();

                        _PurchaseOrders.Kardex.DocType           = 0;
                        _PurchaseOrders.Kardex.DocName           = "FacturaProforma/PurchaseOrders";
                        _PurchaseOrders.Kardex.DocumentDate      = _PurchaseOrders.DatePlaced;
                        _PurchaseOrders.Kardex.FechaCreacion     = DateTime.Now;
                        _PurchaseOrders.Kardex.FechaModificacion = DateTime.Now;
                        _PurchaseOrders.Kardex.TypeOperationId   = 1;
                        _PurchaseOrders.Kardex.TypeOperationName = "Salida";
                        _PurchaseOrders.Kardex.KardexDate        = DateTime.Now;

                        _PurchaseOrders.Kardex.DocumentName = "FacturaProforma";

                        _PurchaseOrders.Kardex.CustomerId          = 0;
                        _PurchaseOrders.Kardex.CustomerName        = "N/A";
                        _PurchaseOrders.Kardex.CurrencyId          = Convert.ToInt32(_PurchaseOrders.CurrencyId);
                        _PurchaseOrders.Kardex.CurrencyName        = _PurchaseOrders.CurrencyName;
                        _PurchaseOrders.Kardex.DocumentId          = _PurchaseOrders.Id;
                        _PurchaseOrders.Kardex.UsuarioCreacion     = _PurchaseOrders.UsuarioCreacion;
                        _PurchaseOrders.Kardex.UsuarioModificacion = _PurchaseOrders.UsuarioModificacion;
                        _context.Kardex.Add(_PurchaseOrders.Kardex);

                        await _context.SaveChangesAsync();

                        BitacoraWrite _write = new BitacoraWrite(_context, new Bitacora
                        {
                            IdOperacion  = _PurchaseOrders.VendorId,
                            DocType      = "PurchaseOrders",
                            ClaseInicial =
                                Newtonsoft.Json.JsonConvert.SerializeObject(_PurchaseOrders, new JsonSerializerSettings {
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                            }),
                            ResultadoSerializado = Newtonsoft.Json.JsonConvert.SerializeObject(_PurchaseOrders, new JsonSerializerSettings {
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                            }),
                            Accion              = "Insert",
                            FechaCreacion       = DateTime.Now,
                            FechaModificacion   = DateTime.Now,
                            UsuarioCreacion     = _PurchaseOrders.UsuarioCreacion,
                            UsuarioModificacion = _PurchaseOrders.UsuarioModificacion,
                            UsuarioEjecucion    = _PurchaseOrders.UsuarioModificacion,
                        });

                        await _context.SaveChangesAsync();

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw ex;
                    }
                }
                //_PurchaseOrdersq = _PurchaseOrders;
                //_context.PurchaseOrders.Add(_PurchaseOrdersq);
                //await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError($"Ocurrio un error: { ex.ToString() }");
                return(BadRequest($"Ocurrio un error:{ex.Message}"));
            }

            return(await Task.Run(() => Ok(_PurchaseOrdersq)));
        }
Beispiel #7
0
        public async Task <ActionResult <CertificadoDeposito> > Insert([FromBody] CertificadoDepositoDTO _CertificadoDeposito)
        {
            CertificadoDeposito          _CertificadoDepositoq = new CertificadoDeposito();
            SolicitudCertificadoDeposito _SolicitudCertificado = new SolicitudCertificadoDeposito();

            try
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        //Solicitud de certificado
                        //  _SolicitudCertificado = mapper.Map<SolicitudCertificadoDeposito>(_CertificadoDeposito);
                        _SolicitudCertificado = new SolicitudCertificadoDeposito
                        {
                            CurrencyId               = _CertificadoDeposito.CurrencyId,
                            CurrencyName             = _CertificadoDeposito.CurrencyName,
                            BankName                 = _CertificadoDeposito.BankName,
                            BankId                   = _CertificadoDeposito.BankId,
                            Almacenaje               = _CertificadoDeposito.Almacenaje,
                            CustomerId               = _CertificadoDeposito.CustomerId,
                            CustomerName             = _CertificadoDeposito.CustomerName,
                            Direccion                = _CertificadoDeposito.Direccion,
                            EmpresaSeguro            = _CertificadoDeposito.EmpresaSeguro,
                            Estado                   = _CertificadoDeposito.Estado,
                            FechaCertificado         = _CertificadoDeposito.FechaCertificado,
                            FechaFirma               = _CertificadoDeposito.FechaFirma,
                            FechaInicioComputo       = _CertificadoDeposito.FechaInicioComputo,
                            FechaVencimientoDeposito = _CertificadoDeposito.FechaVencimientoDeposito,
                            FechaVencimiento         = _CertificadoDeposito.FechaVencimiento,
                            NoCD                         = _CertificadoDeposito.NoCD,
                            FechaPagoBanco               = _CertificadoDeposito.FechaPagoBanco,
                            NombreEmpresa                = _CertificadoDeposito.NombreEmpresa,
                            LugarFirma                   = _CertificadoDeposito.LugarFirma,
                            MontoGarantia                = _CertificadoDeposito.MontoGarantia,
                            NoPoliza                     = _CertificadoDeposito.NoPoliza,
                            NombrePrestatario            = _CertificadoDeposito.NombrePrestatario,
                            NoTraslado                   = _CertificadoDeposito.NoTraslado,
                            OtrosCargos                  = _CertificadoDeposito.OtrosCargos,
                            PorcentajeInteresesInsolutos = _CertificadoDeposito.PorcentajeInteresesInsolutos,
                            Seguro                       = _CertificadoDeposito.Seguro,
                            ServicioId                   = _CertificadoDeposito.ServicioId,
                            ServicioName                 = _CertificadoDeposito.ServicioName,
                            Quantitysum                  = _CertificadoDeposito.Quantitysum,
                            Total                        = _CertificadoDeposito.Total,
                            SujetasAPago                 = _CertificadoDeposito.SujetasAPago,
                            WarehouseId                  = _CertificadoDeposito.WarehouseId,
                            WarehouseName                = _CertificadoDeposito.WarehouseName,
                            Aduana                       = _CertificadoDeposito.Aduana,
                            ManifiestoNo                 = _CertificadoDeposito.ManifiestoNo,
                        };

                        _context.SolicitudCertificadoDeposito.Add(_SolicitudCertificado);
                        foreach (var item in _CertificadoDeposito._CertificadoLine)
                        {
                            SolicitudCertificadoLine _SolicitudCertificadoLine = new SolicitudCertificadoLine {
                                Amount      = item.Amount,
                                Description = item.Description,
                                //  IdCD = item.IdCD,
                                Price          = item.Price,
                                Quantity       = item.Quantity,
                                SubProductId   = item.SubProductId,
                                SubProductName = item.SubProductName,
                                TotalCantidad  = item.TotalCantidad,
                                UnitMeasureId  = item.UnitMeasureId,
                                UnitMeasurName = item.UnitMeasurName,
                            };

                            //  _SolicitudCertificadoLine = mapper.Map<SolicitudCertificadoLine>(item);
                            _SolicitudCertificadoLine.IdSCD = _SolicitudCertificado.IdSCD;
                            _context.SolicitudCertificadoLine.Add(_SolicitudCertificadoLine);
                        }

                        await _context.SaveChangesAsync();

                        /////////////////////////////////////////////////////////////////////////
                        //////////////////Certificado////////////////////////////////////////////

                        _CertificadoDepositoq = _CertificadoDeposito;
                        _context.CertificadoDeposito.Add(_CertificadoDepositoq);
                        // await _context.SaveChangesAsync();

                        foreach (var item in _CertificadoDeposito._CertificadoLine)
                        {
                            item.IdCD = _CertificadoDepositoq.IdCD;
                            _context.CertificadoLine.Add(item);

                            //Kardex _kardexmax = await (from kdx in _context.Kardex
                            //          .Where(q => q.CustomerId == _CertificadoDepositoq.CustomerId)
                            //                           from kdxline in _context.KardexLine
                            //                             .Where(q => q.KardexId == kdx.KardexId)
                            //                               .Where(o => o.SubProducId == item.SubProductId)
                            //                               .OrderByDescending(o => o.DocumentDate).Take(1)
                            //                           select kdx).FirstOrDefaultAsync();

                            Kardex _kardexmax = await(from c in _context.Kardex
                                                      .OrderByDescending(q => q.DocumentDate)
                                                      // .Take(1)
                                                      join d in _context.KardexLine on c.KardexId equals d.KardexId
                                                      where c.CustomerId == _CertificadoDepositoq.CustomerId && d.SubProducId == item.SubProductId &&
                                                      c.DocumentName == "CD"
                                                      select c
                                                      )
                                                .FirstOrDefaultAsync();

                            if (_kardexmax == null)
                            {
                                _kardexmax = new Kardex();
                            }


                            KardexLine _KardexLine = await _context.KardexLine
                                                     .Where(q => q.KardexId == _kardexmax.KardexId)
                                                     .Where(q => q.SubProducId == item.SubProductId)
                                                     .OrderByDescending(q => q.KardexLineId)
                                                     .Take(1)
                                                     .FirstOrDefaultAsync();

                            if (_KardexLine == null)
                            {
                                _KardexLine = new KardexLine();
                            }

                            SubProduct _subproduct = await(from c in _context.SubProduct
                                                           .Where(q => q.SubproductId == item.SubProductId)
                                                           select c
                                                           ).FirstOrDefaultAsync();


                            //  _context.GoodsReceivedLine.Add(item);

                            item.Amount = item.Quantity + _KardexLine.Total;

                            _CertificadoDeposito.Kardex._KardexLine.Add(new KardexLine
                            {
                                DocumentDate = _CertificadoDeposito.FechaCertificado,
                                // ProducId = _CertificadoDeposito.,
                                // ProductName = _GoodsReceivedq.ProductName,
                                SubProducId       = item.SubProductId,
                                SubProductName    = item.SubProductName,
                                QuantityEntry     = item.Quantity,
                                QuantityOut       = 0,
                                QuantityEntryBags = item.TotalCantidad,
                                BranchId          = _CertificadoDeposito.BranchId,
                                BranchName        = _CertificadoDeposito.BranchName,
                                WareHouseId       = _CertificadoDeposito.WarehouseId,
                                WareHouseName     = _CertificadoDeposito.WarehouseName,
                                UnitOfMeasureId   = item.UnitMeasureId,
                                UnitOfMeasureName = item.UnitMeasurName,
                                TypeOperationId   = 1,
                                TypeOperationName = "Entrada",
                                // Total = item.Amount,
                                //TotalBags = item.QuantitySacos + _KardexLine.TotalBags,
                                //QuantityEntryCD = item.Quantity / (1 + _subproduct.Merma),
                                QuantityEntryCD = item.Quantity,
                                TotalCD         = _KardexLine.TotalCD + (item.Quantity),
                            });
                        }

                        await _context.SaveChangesAsync();

                        BitacoraWrite _write = new BitacoraWrite(_context, new Bitacora
                        {
                            IdOperacion  = _CertificadoDeposito.IdCD,
                            DocType      = "CertificadoDeposito",
                            ClaseInicial =
                                Newtonsoft.Json.JsonConvert.SerializeObject(_CertificadoDeposito, new JsonSerializerSettings {
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                            }),
                            ResultadoSerializado = Newtonsoft.Json.JsonConvert.SerializeObject(_CertificadoDeposito, new JsonSerializerSettings {
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                            }),
                            Accion              = "Insertar",
                            FechaCreacion       = DateTime.Now,
                            FechaModificacion   = DateTime.Now,
                            UsuarioCreacion     = _CertificadoDeposito.UsuarioCreacion,
                            UsuarioModificacion = _CertificadoDeposito.UsuarioModificacion,
                            UsuarioEjecucion    = _CertificadoDeposito.UsuarioModificacion,
                        });

                        await _context.SaveChangesAsync();


                        foreach (var item in _CertificadoDeposito.RecibosAsociados)
                        {
                            //  GoodsReceivedLine _gr = await _context.GoodsReceivedLine.Where(q => q.GoodsReceivedId == item).FirstOrDefaultAsync();
                            RecibosCertificado _recibocertificado =
                                new RecibosCertificado
                            {
                                IdCD     = _CertificadoDepositoq.IdCD,
                                IdRecibo = item,
                                productocantidadbultos = _CertificadoDeposito.Quantitysum,
                                productorecibolempiras = _CertificadoDeposito.Total,
                                //  WareHouseId = _gr.WareHouseId,
                                // WareHouseName = _gr.WareHouseName,

                                // UnitMeasureId =_CertificadoDeposito.
                            };

                            _context.RecibosCertificado.Add(_recibocertificado);
                        }

                        _CertificadoDeposito.Kardex.DocType           = 0;
                        _CertificadoDeposito.Kardex.DocName           = "CertificadoDeposito/CD";
                        _CertificadoDeposito.Kardex.DocumentDate      = _CertificadoDeposito.FechaCertificado;
                        _CertificadoDeposito.Kardex.FechaCreacion     = DateTime.Now;
                        _CertificadoDeposito.Kardex.FechaModificacion = DateTime.Now;
                        _CertificadoDeposito.Kardex.TypeOperationId   = 1;
                        _CertificadoDeposito.Kardex.TypeOperationName = "Entrada";
                        _CertificadoDeposito.Kardex.KardexDate        = DateTime.Now;
                        _CertificadoDeposito.Kardex.DocumentName      = "CD";

                        _CertificadoDeposito.Kardex.CustomerId          = _CertificadoDeposito.CustomerId;
                        _CertificadoDeposito.Kardex.CustomerName        = _CertificadoDeposito.CustomerName;
                        _CertificadoDeposito.Kardex.CurrencyId          = _CertificadoDeposito.CurrencyId;
                        _CertificadoDeposito.Kardex.CurrencyName        = _CertificadoDeposito.CurrencyName;
                        _CertificadoDeposito.Kardex.DocumentId          = _CertificadoDeposito.IdCD;
                        _CertificadoDeposito.Kardex.UsuarioCreacion     = _CertificadoDeposito.UsuarioCreacion;
                        _CertificadoDeposito.Kardex.UsuarioModificacion = _CertificadoDeposito.UsuarioModificacion;

                        _context.Kardex.Add(_CertificadoDeposito.Kardex);



                        await _context.SaveChangesAsync();



                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw ex;
                    }
                }
                //_CertificadoDepositoq = _CertificadoDeposito;
                //_context.CertificadoDeposito.Add(_CertificadoDepositoq);
                //await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError($"Ocurrio un error: { ex.ToString() }");
                return(BadRequest($"Ocurrio un error:{ex.Message}"));
            }

            return(await Task.Run(() => Ok(_CertificadoDepositoq)));
        }
Beispiel #8
0
        public async Task <ActionResult <GoodsDeliveryAuthorization> > Insert([FromBody] GoodsDeliveryAuthorizationDTO _GoodsDeliveryAuthorization)
        {
            GoodsDeliveryAuthorization _GoodsDeliveryAuthorizationq = new GoodsDeliveryAuthorization();

            try
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        _GoodsDeliveryAuthorizationq = _GoodsDeliveryAuthorization;
                        _context.GoodsDeliveryAuthorization.Add(_GoodsDeliveryAuthorizationq);

                        foreach (var item in _GoodsDeliveryAuthorizationq.GoodsDeliveryAuthorizationLine)
                        {
                            item.GoodsDeliveryAuthorizationId = _GoodsDeliveryAuthorizationq.GoodsDeliveryAuthorizationId;
                            _context.GoodsDeliveryAuthorizationLine.Add(item);

                            Int64 IdCD = await _context.CertificadoDeposito.Where(q => q.NoCD == item.NoCertificadoDeposito).Select(q => q.IdCD).FirstOrDefaultAsync();

                            //Kardex _kardexmax = await (from kdx in _context.Kardex
                            //      .Where(q => q.CustomerId == _GoodsDeliveryAuthorization.CustomerId)
                            //                           from kdxline in _context.KardexLine
                            //                             .Where(q => q.KardexId == kdx.KardexId)
                            //                               .Where(o => o.SubProducId == item.SubProductId)
                            //                               .OrderByDescending(o => o.DocumentDate).Take(1)
                            //                           select kdx).FirstOrDefaultAsync();

                            Kardex _kardexmax = await(from c in _context.Kardex
                                                      .OrderByDescending(q => q.DocumentDate)
                                                      // .Take(1)
                                                      join d in _context.KardexLine on c.KardexId equals d.KardexId
                                                      where c.CustomerId == _GoodsDeliveryAuthorization.CustomerId && d.SubProducId == item.SubProductId &&
                                                      c.DocumentId == IdCD && c.DocumentName == "CD"
                                                      select c
                                                      )
                                                .FirstOrDefaultAsync();

                            if (_kardexmax == null)
                            {
                                _kardexmax = new Kardex();
                            }



                            KardexLine _KardexLine = await _context.KardexLine
                                                     .Where(q => q.KardexId == _kardexmax.KardexId)
                                                     .Where(q => q.SubProducId == item.SubProductId)
                                                     // .Where(q => q.WareHouseId == item.WarehouseId)
                                                     // .Where(q => q.BranchId == _GoodsDeliveryAuthorizationq.BranchId)
                                                     .OrderByDescending(q => q.KardexLineId)
                                                     .Take(1)
                                                     .FirstOrDefaultAsync();

                            SubProduct _subproduct = await(from c in _context.SubProduct
                                                           .Where(q => q.SubproductId == item.SubProductId)
                                                           select c
                                                           ).FirstOrDefaultAsync();

                            if (_KardexLine.TotalCD < item.Quantity)
                            {
                                return(await Task.Run(() => BadRequest($"La cantidad a retirar no puede ser superior al total del ciertificado")));
                            }

                            //  _context.GoodsReceivedLine.Add(item);
                            //item. = item.Quantity + _KardexLine.Total;

                            //Por cada linea de certificado , se agrega un Kardex de salida del tipo CD
                            _GoodsDeliveryAuthorization.Kardex._KardexLine.Add(new KardexLine
                            {
                                DocumentDate = _GoodsDeliveryAuthorization.DocumentDate,
                                // ProducId = _CertificadoDeposito.,
                                // ProductName = _GoodsReceivedq.ProductName,
                                SubProducId       = item.SubProductId,
                                SubProductName    = item.SubProductName,
                                QuantityEntry     = 0,
                                QuantityOut       = item.Quantity,
                                QuantityEntryBags = 0,
                                BranchId          = _GoodsDeliveryAuthorization.BranchId,
                                BranchName        = _GoodsDeliveryAuthorization.BranchName,
                                WareHouseId       = item.WarehouseId,
                                WareHouseName     = item.WarehouseName,
                                UnitOfMeasureId   = item.UnitOfMeasureId,
                                UnitOfMeasureName = item.UnitOfMeasureName,
                                TypeOperationId   = 1,
                                TypeOperationName = "Salida",
                                //Total = item.valorcertificado,
                                //TotalBags = item.QuantitySacos + _KardexLine.TotalBags,
                                //QuantityEntryCD = item.Quantity / (1 + _subproduct.Merma),
                                QuantityOutCD = item.Quantity,
                                TotalCD       = _KardexLine.TotalCD - (item.Quantity),
                            });


                            _GoodsDeliveryAuthorization.Kardex.DocType           = 0;
                            _GoodsDeliveryAuthorization.Kardex.DocName           = "SolicitudAutorizacion/GoodsDeliveryAuthorization";
                            _GoodsDeliveryAuthorization.Kardex.DocumentDate      = _GoodsDeliveryAuthorization.DocumentDate;
                            _GoodsDeliveryAuthorization.Kardex.FechaCreacion     = DateTime.Now;
                            _GoodsDeliveryAuthorization.Kardex.FechaModificacion = DateTime.Now;
                            _GoodsDeliveryAuthorization.Kardex.TypeOperationId   = 1;
                            _GoodsDeliveryAuthorization.Kardex.TypeOperationName = "Salida";
                            _GoodsDeliveryAuthorization.Kardex.KardexDate        = DateTime.Now;
                            _GoodsDeliveryAuthorization.Kardex.DocumentName      = "CD";

                            _GoodsDeliveryAuthorization.Kardex.CustomerId   = _GoodsDeliveryAuthorization.CustomerId;
                            _GoodsDeliveryAuthorization.Kardex.CustomerName = _GoodsDeliveryAuthorization.CustomerName;
                            //_CertificadoDeposito.Kardex.CurrencyId = _CertificadoDeposito.CurrencyId;
                            _GoodsDeliveryAuthorization.Kardex.CurrencyName        = _GoodsDeliveryAuthorization.CurrencyName;
                            _GoodsDeliveryAuthorization.Kardex.DocumentId          = IdCD;
                            _GoodsDeliveryAuthorization.Kardex.UsuarioCreacion     = _GoodsDeliveryAuthorization.UsuarioCreacion;
                            _GoodsDeliveryAuthorization.Kardex.UsuarioModificacion = _GoodsDeliveryAuthorization.UsuarioModificacion;

                            _context.Kardex.Add(_GoodsDeliveryAuthorization.Kardex);
                        }



                        //await _context.SaveChangesAsync();
                        //foreach (var item in _GoodsDeliveryAuthorization.CertificadosAsociados)
                        //{
                        //    CDGoodsDeliveryAuthorization _certificadoauthorization =
                        //        new CDGoodsDeliveryAuthorization
                        //        {
                        //            CD = item,
                        //            GoodsDeliveryAuthorizationId = _GoodsDeliveryAuthorizationq.GoodsDeliveryAuthorizationId,
                        //        };

                        //    _context.CDGoodsDeliveryAuthorization.Add(_certificadoauthorization);
                        //}


                        BitacoraWrite _write = new BitacoraWrite(_context, new Bitacora
                        {
                            IdOperacion  = _GoodsDeliveryAuthorization.GoodsDeliveryAuthorizationId,
                            DocType      = "GoodsDeliveryAuthorization",
                            ClaseInicial =
                                Newtonsoft.Json.JsonConvert.SerializeObject(_GoodsDeliveryAuthorization, new JsonSerializerSettings {
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                            }),
                            ResultadoSerializado = Newtonsoft.Json.JsonConvert.SerializeObject(_GoodsDeliveryAuthorization, new JsonSerializerSettings {
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                            }),
                            Accion              = "Insert",
                            FechaCreacion       = DateTime.Now,
                            FechaModificacion   = DateTime.Now,
                            UsuarioCreacion     = _GoodsDeliveryAuthorization.UsuarioCreacion,
                            UsuarioModificacion = _GoodsDeliveryAuthorization.UsuarioModificacion,
                            UsuarioEjecucion    = _GoodsDeliveryAuthorization.UsuarioModificacion,
                        });

                        await _context.SaveChangesAsync();


                        await _context.SaveChangesAsync();

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw ex;
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Ocurrio un error: { ex.ToString() }");
                return(await Task.Run(() => BadRequest($"Ocurrio un error:{ex.Message}")));
            }

            return(Ok(_GoodsDeliveryAuthorizationq));
        }
        public async Task <ActionResult <GoodsReceived> > Insert([FromBody] GoodsReceivedDTO _GoodsReceived)
        {
            GoodsReceived _GoodsReceivedq = new GoodsReceived();

            try
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        _GoodsReceivedq = _GoodsReceived;

                        BoletaDeSalida _boletadesalida = new BoletaDeSalida
                        {
                            BranchId            = _GoodsReceived.BranchId,
                            BranchName          = _GoodsReceived.BranchName,
                            CustomerId          = _GoodsReceived.CustomerId,
                            CustomerName        = _GoodsReceived.CustomerName,
                            DocumentDate        = _GoodsReceived.DocumentDate,
                            FechaCreacion       = DateTime.Now,
                            FechaModificacion   = DateTime.Now,
                            Marca               = _GoodsReceived.Marca,
                            Placa               = _GoodsReceived.Placa,
                            Motorista           = _GoodsReceived.Name,
                            Quantity            = _GoodsReceived._GoodsReceivedLine.Select(q => q.QuantitySacos).Sum(),
                            SubProductId        = _GoodsReceivedq._GoodsReceivedLine[0].SubProductId,
                            SubProductName      = _GoodsReceivedq._GoodsReceivedLine[0].SubProductName,
                            CargadoId           = 14,
                            Cargadoname         = "Vacío",
                            UsuarioCreacion     = _GoodsReceived.UsuarioCreacion,
                            UsuarioModificacion = _GoodsReceived.UsuarioModificacion,
                            UnitOfMeasureId     = _GoodsReceivedq._GoodsReceivedLine[0].UnitOfMeasureId,
                            UnitOfMeasureName   = _GoodsReceivedq._GoodsReceivedLine[0].UnitOfMeasureName,
                            WeightBallot        = _GoodsReceivedq.WeightBallot,
                            VigilanteId         = _GoodsReceivedq.VigilanteId,
                            Vigilante           = _GoodsReceivedq.VigilanteName,
                        };

                        _context.BoletaDeSalida.Add(_boletadesalida);
                        await _context.SaveChangesAsync();


                        _GoodsReceivedq.ExitTicket = _boletadesalida.BoletaDeSalidaId;

                        _context.GoodsReceived.Add(_GoodsReceivedq);
                        // await _context.SaveChangesAsync();

                        foreach (var item in _GoodsReceivedq._GoodsReceivedLine)
                        {
                            item.GoodsReceivedId = _GoodsReceivedq.GoodsReceivedId;


                            Kardex _kardexmax = await(from c in _context.Kardex
                                                      .OrderByDescending(q => q.DocumentDate)
                                                      // .Take(1)
                                                      join d in _context.KardexLine on c.KardexId equals d.KardexId
                                                      where c.CustomerId == _GoodsReceivedq.CustomerId && d.SubProducId == item.SubProductId &&
                                                      c.DocumentName != "CD" && d.WareHouseId == item.WareHouseId
                                                      select c
                                                      ).FirstOrDefaultAsync();

                            //Kardex _kardexmax = await (from kdx in _context.Kardex
                            //            .Where(q => q.CustomerId == _GoodsReceivedq.CustomerId)
                            //            from kdxline in _context.KardexLine
                            //                .Where(q => q.KardexId == kdx.KardexId)
                            //                .Where(o => o.SubProducId == item.SubProductId)
                            //                //.Where(q=>q.BranchId ==  _GoodsReceivedq.BranchId)
                            //                //.Where(q => q.WareHouseId == _GoodsReceivedq.WarehouseId)
                            //                .OrderByDescending(o => o.DocumentDate).Take(1)
                            //            select kdx).FirstOrDefaultAsync();

                            if (_kardexmax == null)
                            {
                                _kardexmax = new Kardex();
                            }


                            KardexLine _KardexLine = await _context.KardexLine
                                                     .Where(q => q.KardexId == _kardexmax.KardexId)
                                                     .Where(q => q.SubProducId == item.SubProductId)
                                                     .Where(q => q.WareHouseId == item.WareHouseId)
                                                     .Where(q => q.BranchId == _GoodsReceivedq.BranchId)
                                                     .OrderByDescending(q => q.KardexLineId)
                                                     .Take(1)
                                                     .FirstOrDefaultAsync();

                            if (_KardexLine == null)
                            {
                                _KardexLine = new KardexLine();
                            }

                            SubProduct _subproduct = await(from c in _context.SubProduct
                                                           .Where(q => q.SubproductId == item.SubProductId)
                                                           select c
                                                           ).FirstOrDefaultAsync();
                            if (_subproduct.ProductTypeId == 3)
                            {
                                //Alert AlertP = new Alert();
                                Alert Alerta = new Alert();
                                Alerta.DocumentId          = item.SubProductId;
                                Alerta.DocumentName        = "LISTA PROHIBIDA";
                                Alerta.AlertName           = "Productos";
                                Alerta.Code                = "PRODUCT01";
                                Alerta.DescriptionAlert    = "Lista de producto Prohibida";
                                Alerta.FechaCreacion       = Convert.ToDateTime(item.FechaCreacion);
                                Alerta.FechaModificacion   = Convert.ToDateTime(item.FechaModificacion);
                                Alerta.UsuarioCreacion     = item.UsuarioCreacion;
                                Alerta.UsuarioModificacion = item.UsuarioModificacion;
                                // var AlertaP = await InsertAlert(Alerta);
                                _context.Alert.Add(Alerta);
                                //await _context.SaveChangesAsync();

                                BitacoraWrite _writealert = new BitacoraWrite(_context, new Bitacora
                                {
                                    IdOperacion  = Alerta.AlertId,
                                    DocType      = "Alert",
                                    ClaseInicial =
                                        Newtonsoft.Json.JsonConvert.SerializeObject(Alerta, new JsonSerializerSettings {
                                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                                    }),
                                    Accion              = "Insertar",
                                    FechaCreacion       = DateTime.Now,
                                    FechaModificacion   = DateTime.Now,
                                    UsuarioCreacion     = Alerta.UsuarioCreacion,
                                    UsuarioModificacion = Alerta.UsuarioModificacion,
                                    UsuarioEjecucion    = Alerta.UsuarioModificacion,
                                });

                                // await _context.SaveChangesAsync();
                            }

                            _context.GoodsReceivedLine.Add(item);

                            item.Total = item.Quantity + _KardexLine.Total;


                            _GoodsReceived.Kardex._KardexLine.Add(new KardexLine
                            {
                                DocumentDate      = _GoodsReceivedq.DocumentDate,
                                ProducId          = item.ProducId,
                                ProductName       = item.ProductName,
                                SubProducId       = item.SubProductId,
                                SubProductName    = item.SubProductName,
                                QuantityEntry     = item.Quantity,
                                QuantityOut       = 0,
                                QuantityEntryBags = item.QuantitySacos,
                                BranchId          = _GoodsReceivedq.BranchId,
                                BranchName        = _GoodsReceivedq.BranchName,
                                WareHouseId       = item.WareHouseId,
                                WareHouseName     = item.WareHouseName,
                                UnitOfMeasureId   = item.UnitOfMeasureId,
                                UnitOfMeasureName = item.UnitOfMeasureName,
                                TypeOperationId   = 1,
                                TypeOperationName = "Entrada",
                                Total             = item.Total,
                                TotalBags         = item.QuantitySacos + _KardexLine.TotalBags,
                                QuantityEntryCD   = item.Quantity - (item.Quantity * _subproduct.Merma),
                                TotalCD           = _KardexLine.TotalCD + (item.Quantity - (item.Quantity * _subproduct.Merma)),
                            });
                        }//Fin Foreach

                        await _context.SaveChangesAsync();

                        _GoodsReceived.Kardex.DocType           = 0;
                        _GoodsReceived.Kardex.DocName           = "ReciboMercaderia/GoodsReceived";
                        _GoodsReceived.Kardex.DocumentDate      = _GoodsReceivedq.DocumentDate;
                        _GoodsReceived.Kardex.FechaCreacion     = DateTime.Now;
                        _GoodsReceived.Kardex.FechaModificacion = DateTime.Now;
                        _GoodsReceived.Kardex.TypeOperationId   = 1;
                        _GoodsReceived.Kardex.TypeOperationName = "Entrada";
                        _GoodsReceived.Kardex.KardexDate        = DateTime.Now;
                        _GoodsReceived.Kardex.DocumentName      = "RM";

                        _GoodsReceived.Kardex.CustomerId          = _GoodsReceivedq.CustomerId;
                        _GoodsReceived.Kardex.CustomerName        = _GoodsReceivedq.CustomerName;
                        _GoodsReceived.Kardex.CurrencyId          = _GoodsReceivedq.CurrencyId;
                        _GoodsReceived.Kardex.CurrencyName        = _GoodsReceivedq.CurrencyName;
                        _GoodsReceived.Kardex.DocumentId          = _GoodsReceivedq.GoodsReceivedId;
                        _GoodsReceived.Kardex.UsuarioCreacion     = _GoodsReceivedq.UsuarioCreacion;
                        _GoodsReceived.Kardex.UsuarioModificacion = _GoodsReceivedq.UsuarioModificacion;



                        if (_GoodsReceived.ControlId > 0)
                        {
                            _context.Kardex.Add(_GoodsReceived.Kardex);
                        }

                        await _context.SaveChangesAsync();

                        foreach (var item in _GoodsReceivedq._GoodsReceivedLine)
                        {
                            if (item.ControlPalletsId == null)
                            {
                                item.ControlPalletsId = 0;
                            }
                            ControlPallets _ControlPalletsq = await _context.ControlPallets.Where(q => q.ControlPalletsId == item.ControlPalletsId)
                                                              .FirstOrDefaultAsync();

                            if (_ControlPalletsq != null)
                            {
                                _ControlPalletsq.QQPesoBruto = _GoodsReceivedq.PesoBruto;
                                _ControlPalletsq.QQPesoNeto  = _GoodsReceivedq.PesoNeto;
                                _ControlPalletsq.QQPesoFinal = _GoodsReceivedq.PesoNeto2;

                                _context.Entry(_ControlPalletsq).CurrentValues.SetValues((_ControlPalletsq));
                            }
                        }

                        await _context.SaveChangesAsync();

                        BoletaDeSalida _bol = await _context.BoletaDeSalida
                                              .Where(q => q.BoletaDeSalidaId == _boletadesalida.BoletaDeSalidaId).FirstOrDefaultAsync();

                        _bol.GoodsReceivedId = _GoodsReceivedq.GoodsReceivedId;
                        _context.Entry(_bol).CurrentValues.SetValues((_bol));

                        await _context.SaveChangesAsync();

                        JournalEntryConfiguration _journalentryconfiguration = await(_context.JournalEntryConfiguration
                                                                                     .Where(q => q.TransactionId == 1)
                                                                                     .Where(q => q.BranchId == _GoodsReceivedq.BranchId)
                                                                                     .Where(q => q.EstadoName == "Activo")
                                                                                     .Include(q => q.JournalEntryConfigurationLine)
                                                                                     ).FirstOrDefaultAsync();

                        BitacoraWrite _writejec = new BitacoraWrite(_context, new Bitacora
                        {
                            IdOperacion  = _GoodsReceived.CustomerId,
                            DocType      = "JournalEntryConfiguration",
                            ClaseInicial =
                                Newtonsoft.Json.JsonConvert.SerializeObject(_journalentryconfiguration, new JsonSerializerSettings {
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                            }),
                            ResultadoSerializado = Newtonsoft.Json.JsonConvert.SerializeObject(_journalentryconfiguration, new JsonSerializerSettings {
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                            }),
                            Accion              = "InsertGoodsReceived",
                            FechaCreacion       = DateTime.Now,
                            FechaModificacion   = DateTime.Now,
                            UsuarioCreacion     = _GoodsReceived.UsuarioCreacion,
                            UsuarioModificacion = _GoodsReceived.UsuarioModificacion,
                            UsuarioEjecucion    = _GoodsReceived.UsuarioModificacion,
                        });

                        // await _context.SaveChangesAsync();

                        double sumacreditos = 0, sumadebitos = 0;
                        if (_journalentryconfiguration != null)
                        {
                            //Crear el asiento contable configurado
                            //.............................///////
                            JournalEntry _je = new JournalEntry
                            {
                                Date         = _GoodsReceivedq.OrderDate,
                                Memo         = "Bienes Recibidos",
                                DatePosted   = _GoodsReceivedq.OrderDate,
                                ModifiedDate = DateTime.Now,
                                CreatedDate  = DateTime.Now,
                                ModifiedUser = _GoodsReceivedq.UsuarioModificacion,
                                CreatedUser  = _GoodsReceivedq.UsuarioCreacion,
                                DocumentId   = _GoodsReceivedq.GoodsReceivedId,
                            };



                            foreach (var item in _journalentryconfiguration.JournalEntryConfigurationLine)
                            {
                                GoodsReceivedLine _iline = new GoodsReceivedLine();
                                _iline = _GoodsReceivedq._GoodsReceivedLine.Where(q => q.SubProductId == item.SubProductId).FirstOrDefault();
                                if (_iline != null || item.SubProductName.ToUpper().Contains(("Impuesto").ToUpper()))
                                {
                                    if (!item.AccountName.ToUpper().Contains(("Impuestos sobre ventas").ToUpper()) &&
                                        !item.AccountName.ToUpper().Contains(("Sobre Servicios Diversos").ToUpper()))
                                    {
                                        _je.JournalEntryLines.Add(new JournalEntryLine
                                        {
                                            AccountId    = Convert.ToInt32(item.AccountId),
                                            Description  = item.AccountName,
                                            Credit       = item.DebitCredit == "Credito" ? _iline.Total : 0,
                                            Debit        = item.DebitCredit == "Debito" ? _iline.Total : 0,
                                            CreatedDate  = DateTime.Now,
                                            ModifiedDate = DateTime.Now,
                                            CreatedUser  = _GoodsReceivedq.UsuarioCreacion,
                                            ModifiedUser = _GoodsReceivedq.UsuarioModificacion,
                                            Memo         = "",
                                        });

                                        sumacreditos += item.DebitCredit == "Credito" ? _iline.Total : 0;
                                        sumadebitos  += item.DebitCredit == "Debito" ? _iline.Total : 0;
                                    }
                                    else
                                    {
                                        _je.JournalEntryLines.Add(new JournalEntryLine
                                        {
                                            AccountId    = Convert.ToInt32(item.AccountId),
                                            Description  = item.AccountName,
                                            CreatedDate  = DateTime.Now,
                                            ModifiedDate = DateTime.Now,
                                            CreatedUser  = _GoodsReceivedq.UsuarioCreacion,
                                            ModifiedUser = _GoodsReceivedq.UsuarioModificacion,
                                            Memo         = "",
                                        });

                                        //sumacreditos += item.DebitCredit == "Credito" ? _Invoiceq.Tax + _Invoiceq.Tax18 : 0;
                                        //sumadebitos += item.DebitCredit == "Debito" ? _Invoiceq.Tax + _Invoiceq.Tax18 : 0;
                                    }
                                }

                                // _context.JournalEntryLine.Add(_je);
                            }


                            if (sumacreditos != sumadebitos)
                            {
                                transaction.Rollback();
                                _logger.LogError($"Ocurrio un error: No coinciden debitos :{sumadebitos} y creditos{sumacreditos}");
                                return(BadRequest($"Ocurrio un error: No coinciden debitos :{sumadebitos} y creditos{sumacreditos}"));
                            }

                            _je.TotalCredit = sumacreditos;
                            _je.TotalDebit  = sumadebitos;
                            _context.JournalEntry.Add(_je);

                            await _context.SaveChangesAsync();
                        }

                        BitacoraWrite _write = new BitacoraWrite(_context, new Bitacora
                        {
                            IdOperacion  = _GoodsReceivedq.GoodsReceivedId,
                            DocType      = "GoodsReceived",
                            ClaseInicial =
                                Newtonsoft.Json.JsonConvert.SerializeObject(_GoodsReceivedq, new JsonSerializerSettings {
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                            }),
                            ResultadoSerializado = Newtonsoft.Json.JsonConvert.SerializeObject(_GoodsReceivedq, new JsonSerializerSettings {
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                            }),
                            Accion              = "Insert",
                            FechaCreacion       = DateTime.Now,
                            FechaModificacion   = DateTime.Now,
                            UsuarioCreacion     = _GoodsReceivedq.UsuarioCreacion,
                            UsuarioModificacion = _GoodsReceivedq.UsuarioModificacion,
                            UsuarioEjecucion    = _GoodsReceivedq.UsuarioModificacion,
                        });

                        await _context.SaveChangesAsync();


                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw ex;
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Ocurrio un error: { ex.ToString() }");
                return(BadRequest($"Ocurrio un error:{ex.Message}"));
            }

            return(await Task.Run(() => Ok(_GoodsReceivedq)));
        }