diff --git a/DoliMiddlewareApi/Controllers/InvoicesController.cs b/DoliMiddlewareApi/Controllers/InvoicesController.cs index 0c0c0ec..20b81c7 100644 --- a/DoliMiddlewareApi/Controllers/InvoicesController.cs +++ b/DoliMiddlewareApi/Controllers/InvoicesController.cs @@ -62,4 +62,16 @@ public class InvoicesController : ControllerBase await _invoiceService.UpdateInvoiceAsync(id, updateInvoiceDto); return NoContent(); } + + [HttpPost("{id:int}/lines")] + [ProducesResponseType(typeof(string), StatusCodes.Status201Created)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status403Forbidden)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] + public async Task> AddInvoiceLine([Range(1, int.MaxValue)] int id, [FromBody] CreateInvoiceLineDto lineDto) + { + var result = await _invoiceService.AddInvoiceLineAsync(id, lineDto); + return CreatedAtAction(nameof(GetInvoice), new { id }, result); + } } \ No newline at end of file diff --git a/DoliMiddlewareApi/Services/InvoiceService.cs b/DoliMiddlewareApi/Services/InvoiceService.cs index 57913fd..f00e2dc 100644 --- a/DoliMiddlewareApi/Services/InvoiceService.cs +++ b/DoliMiddlewareApi/Services/InvoiceService.cs @@ -68,6 +68,22 @@ public class InvoiceService return int.Parse(responseBody); } + public async Task AddInvoiceLineAsync(int invoiceId, CreateInvoiceLineDto lineDto) + { + var invoice = await _apiClient.GetResourceAsync($"invoices/{invoiceId}"); + if (invoice.statut != "0") throw new ForbiddenException("Solo se pueden añadir líneas a facturas en borrador"); + + var requestBody = new + { + desc = lineDto.Description, + qty = lineDto.Quantity.ToString(CultureInfo.InvariantCulture), + subprice = lineDto.UnitPrice.ToString(CultureInfo.InvariantCulture), + tva_tx = lineDto.TaxRate.ToString(CultureInfo.InvariantCulture) + }; + + return await _apiClient.PostAsync($"invoices/{invoiceId}/lines", requestBody); + } + public async Task UpdateInvoiceAsync(int id, UpdateInvoiceDto dto) { // GET el JSON completo @@ -85,4 +101,5 @@ public class InvoiceService await _apiClient.PutAsync($"invoices/{id}", current); } + } \ No newline at end of file