public async Task <InvoiceResponseModel> Handle(GetInvoiceQuery query, CancellationToken cancellationToken)
        {
            var invoice = await _invoiceRepository.GetInvoiceAsync(query.Id, cancellationToken);

            var model = new InvoiceResponseModel()
            {
                Id         = invoice.Id,
                Amount     = invoice.Amount,
                AmountPaid = invoice.AmountPaid
            };

            foreach (var invoicePayment in invoice.Payments)
            {
                model.Payments.Add(new PaymentResponseModel()
                {
                    Amount    = invoicePayment.Amount,
                    Reference = invoicePayment.Reference
                });
            }

            return(model);
        }
Example #2
0
        public async Task <InvoiceResponseModel> Create(int CompanyID, Invoices invoice)
        {
            BIWSFEV1 wsfev1;
            List <InvoiceAuthorizationObs> _observations;
            int nroComprobante;
            InvoiceResponseModel     response     = new InvoiceResponseModel();
            List <Taxes>             taxes        = null;
            Certificates             cert         = null;
            DocumentType             documentType = new DocumentType();
            List <(string, decimal)> AmountTaxes  = new List <(string, decimal)>();
            List <TaxesModel>        lstTaxes     = new List <TaxesModel>();
            FinancialMovements       movements;
            decimal CalculatedTaxes    = 0;
            decimal BaseTaxable        = 0;
            decimal CalculatedDiscount = 0;
            double  NetAmount          = 0;

            try
            {
                _observations = new List <InvoiceAuthorizationObs>();
                cert          = await _dbContex.Certificates.FirstOrDefaultAsync(x => x.CompanyID == CompanyID);

                wsfev1 = new BIWSFEV1();
                taxes  = await _dbContex.Taxes.ToListAsync();

                documentType = await _dbContex.DocumentTypes.FirstOrDefaultAsync(x => x.DocumentTypeID == invoice.DocumentTypeID);


                response          = new InvoiceResponseModel();
                invoice.CompanyID = CompanyID;

                invoice.Letter                = documentType.Letter;
                invoice.DocumentTypeCode      = documentType.Code.ToString().PadLeft(3, '0');
                invoice.DocumentTypeShortCode = documentType.ShortName;
                invoice.Created               = DateTime.Now;
                await _dbContex.AddAsync(invoice);

                invoice.Status = Enums.InvoiceStatus.Created;

                movements = new FinancialMovements
                {
                    Ammount     = -(invoice.Total),
                    CompanyID   = invoice.CompanyID,
                    Date        = DateTime.Now,
                    Description = "Factura creada",
                    TypeID      = 3,
                    Invoice     = invoice
                };
                _dbContex.Entry(movements).State = EntityState.Added;

                #region Extraer impuestos y calcular descuentos
                var GroupedTaxes = invoice.Items.GroupBy(x => x.TaxId);
                foreach (var item in GroupedTaxes)
                {
                    int   TaxCode = int.Parse(item.Key.ToString());
                    Taxes Tax     = taxes.FirstOrDefault(x => x.Code == TaxCode);
                    //var TotalDiscount = item.Sum(x => x.dis);
                    CalculatedTaxes = item.Sum(x => x.TaxCalculated);
                    BaseTaxable     = item.Sum(x => x.Price * x.Qtty);
                    lstTaxes.Add(new TaxesModel {
                        CalculatedTax = (double)CalculatedTaxes, TaxBase = (double)BaseTaxable, TaxID = TaxCode
                    });
                    AmountTaxes.Add((Tax.Name, CalculatedTaxes));
                    CalculatedTaxes = 0;
                }
                NetAmount = double.Parse(lstTaxes.Sum(x => x.TaxBase).ToString("N2")); //neto gravado
                if (cert != null)
                {
                    if (await wsfev1.login(cert.Path, cert.Password))
                    {
                        var LastInvoice = await wsfev1.recuperaLastCMPAsync(invoice.PosCode, documentType.Code);

                        nroComprobante = LastInvoice.Body.FECompUltimoAutorizadoResult.CbteNro;

                        nroComprobante++;
                        wsfev1.reset();
                        wsfev1.agregaFactura(int.Parse(invoice.ConceptCode), int.Parse(invoice.IdentityDocumentTypeCode), long.Parse(invoice.IdentityDocumentNumber), nroComprobante, nroComprobante, DateTime.Parse(DateTime.Now.ToShortDateString()), Helpers.Truncate((double)invoice.Total, 2), /*TotalNotTaxed*/ 0, Helpers.Truncate(NetAmount, 2), /*OptionalExemptAmount*/ 0, null, null, null, "PES", 1);

                        if (lstTaxes != null)
                        {
                            if (lstTaxes.Count > 0)
                            {
                                foreach (var tax in lstTaxes)
                                {
                                    wsfev1.agregaIVA(tax.TaxID, Handlers.Helpers.Truncate(tax.TaxBase, 2), Handlers.Helpers.Truncate(tax.CalculatedTax, 2));
                                }
                            }
                        }

                        var Authorizar = await wsfev1.AutorizarAsync(invoice.PosCode, documentType.Code);

                        //wsfev1.autorizarRespuesta(0, ref cae, ref vencimiento, ref resultado);
                        var respuesta = wsfev1.autorizarRespuestaV2(0);
                        if (respuesta.Resultado == "A")
                        {
                            Guid id = Guid.NewGuid();

                            invoice.CAE           = respuesta.CAE;
                            invoice.CAEExpiration = respuesta.VencimientoCae;
                            response.Result       = respuesta.Resultado;

                            /*GENERACION DE CODIGO DE BARRA*/
                            string FileCodeBar = $"{id}" +
                                                 $".bmp";
                            string PathCodeBar = System.IO.Path.Combine($"C:\\Invoices\\Barcode\\{FileCodeBar}");
                            FEAFIPLib.TBarcodeBitmap.generarCodigoBarras(long.Parse(invoice.IdentityDocumentNumber), (byte)int.Parse(invoice.DocumentTypeCode), (byte)invoice.PosCode, respuesta.CAE, respuesta.VencimientoCae, 3, 80, PathCodeBar);

                            BarCode barCode = new BarCode
                            {
                                Created = DateTime.Today,
                                Name    = FileCodeBar
                            };

                            invoice.Status = Enums.InvoiceStatus.Authorized;

                            invoice.BarCode       = barCode;
                            invoice.InvoiceNumber = nroComprobante;
                        }
                        else
                        {
                            if (respuesta.Observaciones.Count > 0)
                            {
                                foreach (var obs in respuesta.Observaciones)
                                {
                                    _observations.Add(new InvoiceAuthorizationObs
                                    {
                                        Code        = obs.Codigo,
                                        Description = obs.Descripcion
                                    });
                                }
                                response.Observations = _observations;
                            }
                        }
                    }
                }

                #endregion
                string fileName = await CreatePDF(invoice, AmountTaxes);

                Download download = new Download
                {
                    File    = fileName,
                    Created = DateTime.Now,
                    Count   = 0,
                };

                invoice.Download = download;

                await _dbContex.SaveChangesAsync();

                response.InvoiceID  = invoice.InvoiceID;
                response.Created    = DateTime.Now;
                response.CAE        = invoice.CAE;
                response.DueDateCae = invoice.CAEExpiration?.ToShortDateString();
                return(response);



                //let calculatedDiscount = 0;
                //let discountPercent = discount / 100;
                //let _subTotal = 0;
                //let _total = this.ToDecimal(0);

                //calculatedDiscount = (price * Qtty) * discountPercent;

                //_subTotal = this.ToDecimal((price * Qtty) - calculatedDiscount);


                //let tax = Tax.taxById(parseInt(taxId));
                //let taxValue = tax.Value;
                //let TaxCalculated = (_subTotal * taxValue) / 100;
                //_total = this.ToDecimal(parseFloat(_subTotal) + parseFloat(TaxCalculated));
            }
            catch (Exception e)
            {
                throw;
            }
        }