feat : Agrego endpoint Post para Payments
This commit is contained in:
parent
26ad842256
commit
0b098376c5
|
|
@ -16,9 +16,9 @@ public class InvoicesController(InvoiceService invoiceService) : ControllerBase
|
|||
[HttpGet]
|
||||
public async Task<ActionResult<List<InvoiceDto>>> GetInvoices(
|
||||
[FromQuery] int limit = 50,
|
||||
[FromQuery][Range(1, int.MaxValue)] int page = 1,
|
||||
[FromQuery] [Range(1, int.MaxValue)] int page = 1,
|
||||
[FromQuery] string? status = null,
|
||||
[FromQuery][StringLength(100)] string? search = null)
|
||||
[FromQuery] [StringLength(100)] string? search = null)
|
||||
{
|
||||
var invoices = await invoiceService.GetInvoicesAsync(limit, page, status, search);
|
||||
return Ok(invoices);
|
||||
|
|
@ -39,14 +39,16 @@ public class InvoicesController(InvoiceService invoiceService) : ControllerBase
|
|||
}
|
||||
|
||||
[HttpPut("{id:int}")]
|
||||
public async Task<IActionResult> UpdateInvoice([Range(1, int.MaxValue)] int id, [FromBody] UpdateInvoiceDto updateInvoiceDto)
|
||||
public async Task<IActionResult> UpdateInvoice([Range(1, int.MaxValue)] int id,
|
||||
[FromBody] UpdateInvoiceDto updateInvoiceDto)
|
||||
{
|
||||
await invoiceService.UpdateInvoiceAsync(id, updateInvoiceDto);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPost("{id:int}/lines")]
|
||||
public async Task<ActionResult<string>> AddInvoiceLine([Range(1, int.MaxValue)] int id, [FromBody] CreateInvoiceLineDto lineDto)
|
||||
public async Task<ActionResult<string>> AddInvoiceLine([Range(1, int.MaxValue)] int id,
|
||||
[FromBody] CreateInvoiceLineDto lineDto)
|
||||
{
|
||||
var result = await invoiceService.AddInvoiceLineAsync(id, lineDto);
|
||||
return CreatedAtAction(nameof(GetInvoice), new { id }, result);
|
||||
|
|
@ -90,4 +92,13 @@ public class InvoicesController(InvoiceService invoiceService) : ControllerBase
|
|||
var payments = await invoiceService.GetInvoicePaymentsAsync(id);
|
||||
return Ok(payments);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("{id:int}/payments")]
|
||||
public async Task<ActionResult<long>> AddPayment(
|
||||
[Range(1, int.MaxValue)] int id,
|
||||
[FromBody] CreateInvoicePaymentDto dto)
|
||||
{
|
||||
var paymentId = await invoiceService.AddInvoicePaymentAsync(id, dto);
|
||||
return Ok(paymentId);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace DoliMiddlewareApi.Dtos.command;
|
||||
|
||||
public class CreateInvoicePaymentDto
|
||||
{
|
||||
// Si se especifica, es un pago parcial. Si es null, paga todo lo pendiente.
|
||||
[Range(0.01, double.MaxValue, ErrorMessage = "Amount must be greater than 0")]
|
||||
public decimal? Amount { get; set; }
|
||||
|
||||
[Required] public DateTime PaymentDate { get; set; }
|
||||
|
||||
// Acepta ambos nombres: paymentMethodId (del frontend) o paymentModeId (fallback)
|
||||
[JsonPropertyName("paymentMethodId")]
|
||||
public int? PaymentMethodId { get; set; }
|
||||
|
||||
[JsonPropertyName("paymentModeId")]
|
||||
public int PaymentModeId { get; set; }
|
||||
|
||||
[Required] [RegularExpression("yes|no", ErrorMessage = "Must be 'yes' or 'no'")]
|
||||
public string ClosePaidInvoices { get; set; } = "yes";
|
||||
|
||||
public int AccountId { get; set; } = 1;
|
||||
|
||||
[JsonPropertyName("numPayment")]
|
||||
public string? PaymentNumber { get; set; }
|
||||
}
|
||||
|
|
@ -171,4 +171,45 @@ public class InvoiceService(IDolibarrApiClient apiClient)
|
|||
|
||||
return payments;
|
||||
}
|
||||
public async Task<int> AddInvoicePaymentAsync(int invoiceId, CreateInvoicePaymentDto dto)
|
||||
{
|
||||
// Usar paymentMethodId si viene, si no usar paymentModeId
|
||||
var paymentModeId = dto.PaymentMethodId ?? dto.PaymentModeId;
|
||||
|
||||
if (dto.Amount.HasValue)
|
||||
{
|
||||
// Pago parcial: usar /invoices/paymentsdistributed
|
||||
var requestBody = new
|
||||
{
|
||||
arrayofamounts = new Dictionary<string, object>
|
||||
{
|
||||
{
|
||||
invoiceId.ToString(),
|
||||
new { amount = dto.Amount.Value.ToString(CultureInfo.InvariantCulture) }
|
||||
}
|
||||
},
|
||||
datepaye = dto.PaymentDate.ToString("yyyy-MM-dd"),
|
||||
paymentid = paymentModeId,
|
||||
closepaidinvoices = dto.ClosePaidInvoices,
|
||||
accountid = dto.AccountId,
|
||||
num_payment = dto.PaymentNumber,
|
||||
};
|
||||
var responseBody = await apiClient.PostAsync("invoices/paymentsdistributed", requestBody);
|
||||
return int.Parse(responseBody);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Pagar completa pendiente: usar /invoices/{id}/payments
|
||||
var requestBody = new
|
||||
{
|
||||
datepaye = dto.PaymentDate.ToString("yyyy-MM-dd"),
|
||||
paymentid = paymentModeId,
|
||||
closepaidinvoices = dto.ClosePaidInvoices,
|
||||
accountid = dto.AccountId,
|
||||
num_payment = dto.PaymentNumber,
|
||||
};
|
||||
var responseBody = await apiClient.PostAsync($"invoices/{invoiceId}/payments", requestBody);
|
||||
return int.Parse(responseBody);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue