marco.pms.api/Marco.Pms.Services/Controllers/PurchaseInvoiceController.cs

82 lines
3.9 KiB
C#

using Marco.Pms.Model.Dtos.PurchaseInvoice;
using Marco.Pms.Services.Service.ServiceInterfaces;
using MarcoBMS.Services.Helpers;
using Microsoft.AspNetCore.Mvc;
namespace Marco.Pms.Services.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PurchaseInvoiceController : ControllerBase
{
private readonly UserHelper _userHelper;
private readonly IPurchaseInvoiceService _purchaseInvoiceService;
private readonly ISignalRService _signalR;
private readonly Guid tenantId;
public PurchaseInvoiceController(UserHelper userHelper, IPurchaseInvoiceService purchaseInvoiceService, ISignalRService signalR)
{
_userHelper = userHelper;
_purchaseInvoiceService = purchaseInvoiceService;
tenantId = _userHelper.GetTenantId();
_signalR = signalR;
}
/// <summary>
/// Retrieves a list of purchase invoices based on search string, filter, activity status, page size, and page number.
/// </summary>
/// <param name="searchString">Optional search string to filter invoices by.</param>
/// <param name="filter">Optional filter to apply to the invoices.</param>
/// <param name="isActive">Optional flag to filter invoices by activity status.</param>
/// <param name="pageSize">The number of invoices to display per page.</param>
/// <param name="pageNumber">The requested page number (1-based).</param>
/// <param name="cancellationToken">Token to propagate notification that operations should be canceled.</param>
/// <returns>A HTTP 200 OK response with a list of purchase invoices or an appropriate HTTP error code.</returns>
[HttpGet("list")]
public async Task<IActionResult> GetPurchaseInvoiceListAsync([FromQuery] string? searchString, [FromQuery] string? filter, CancellationToken cancellationToken, [FromQuery] bool isActive = true,
[FromQuery] int pageSize = 20, [FromQuery] int pageNumber = 1)
{
// Get the currently logged-in employee
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
// Retrieve the purchase invoice list using the service
var response = await _purchaseInvoiceService.GetPurchaseInvoiceListAsync(searchString, filter, isActive, pageSize, pageNumber, loggedInEmployee, tenantId, cancellationToken);
// Return the response with the appropriate HTTP status code
return StatusCode(response.StatusCode, response);
}
/// <summary>
/// Creates a purchase invoice.
/// </summary>
/// <param name="model">The purchase invoice model.</param>
/// <param name="ct">The cancellation token.</param>
/// <returns>The HTTP response for the creation of the purchase invoice.</returns>
[HttpPost("create")]
public async Task<IActionResult> CreatePurchaseInvoice([FromBody] PurchaseInvoiceDto model, CancellationToken ct)
{
// Get the currently logged-in employee
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
// Create a purchase invoice using the purchase invoice service
var response = await _purchaseInvoiceService.CreatePurchaseInvoiceAsync(model, loggedInEmployee, tenantId, ct);
// If the creation is successful, send a notification to the SignalR service
if (response.Success)
{
var notification = new
{
LoggedInUserId = loggedInEmployee.Id,
Keyword = "Purchase_Invoice",
Response = response.Data
};
await _signalR.SendNotificationAsync(notification);
}
// Return the HTTP response
return StatusCode(response.StatusCode, response);
}
}
}