219 lines
11 KiB
C#
219 lines
11 KiB
C#
using AutoMapper;
|
|
using Marco.Pms.Model.Dtos.Collection;
|
|
using Marco.Pms.Model.Dtos.PurchaseInvoice;
|
|
using Marco.Pms.Model.Utilities;
|
|
using Marco.Pms.Services.Service.ServiceInterfaces;
|
|
using MarcoBMS.Services.Helpers;
|
|
using Microsoft.AspNetCore.JsonPatch;
|
|
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 IServiceScopeFactory _serviceScopeFactory;
|
|
private readonly Guid tenantId;
|
|
|
|
public PurchaseInvoiceController(UserHelper userHelper, IPurchaseInvoiceService purchaseInvoiceService, ISignalRService signalR, IServiceScopeFactory serviceScopeFactory)
|
|
{
|
|
_userHelper = userHelper;
|
|
_purchaseInvoiceService = purchaseInvoiceService;
|
|
tenantId = _userHelper.GetTenantId();
|
|
_signalR = signalR;
|
|
_serviceScopeFactory = serviceScopeFactory;
|
|
}
|
|
|
|
#region =================================================================== Purchase Invoice Functions ===================================================================
|
|
|
|
/// <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> GetPurchaseInvoiceList([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);
|
|
}
|
|
|
|
[HttpGet("details/{id}")]
|
|
public async Task<IActionResult> GetPurchaseInvoiceDetails(Guid id, CancellationToken cancellationToken)
|
|
{
|
|
// Get the currently logged-in employee
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
|
|
// Retrieve the purchase invoice details using the service
|
|
var response = await _purchaseInvoiceService.GetPurchaseInvoiceDetailsAsync(id, 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);
|
|
}
|
|
|
|
[HttpPatch("edit/{id}")]
|
|
public async Task<IActionResult> EditPurchaseInvoice(Guid id, [FromBody] JsonPatchDocument<PurchaseInvoiceDto> patchDoc, CancellationToken ct)
|
|
{
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var existingPurchaseInvoice = await _purchaseInvoiceService.GetPurchaseInvoiceByIdAsync(id, tenantId, ct);
|
|
if (existingPurchaseInvoice == null)
|
|
return NotFound(ApiResponse<object>.ErrorResponse("Invalid purchase invoice ID", "Invalid purchase invoice ID", 404));
|
|
|
|
using var scope = _serviceScopeFactory.CreateScope();
|
|
var mapper = scope.ServiceProvider.GetRequiredService<IMapper>();
|
|
var modelToPatch = mapper.Map<PurchaseInvoiceDto>(existingPurchaseInvoice);
|
|
|
|
// Apply the JSON Patch document to the DTO and check model state validity
|
|
patchDoc.ApplyTo(modelToPatch, ModelState);
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Validation failed", "Provided patch document values are invalid", 400));
|
|
}
|
|
|
|
var response = await _purchaseInvoiceService.UpdatePurchaseInvoiceAsync(id, existingPurchaseInvoice, modelToPatch, loggedInEmployee, tenantId, ct);
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
|
|
[HttpDelete("delete/{id}")]
|
|
public async Task<IActionResult> DeletePurchaseInvoice(Guid id, CancellationToken ct, [FromQuery] bool isActive = false)
|
|
{
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _purchaseInvoiceService.DeletePurchaseInvoiceAsync(id, isActive, loggedInEmployee, tenantId, ct);
|
|
if (response.Success)
|
|
{
|
|
var notification = new
|
|
{
|
|
LoggedInUserId = loggedInEmployee.Id,
|
|
Keyword = "Purchase_Invoice",
|
|
Response = response.Data
|
|
};
|
|
await _signalR.SendNotificationAsync(notification);
|
|
}
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region =================================================================== Delivery Challan Functions ===================================================================
|
|
|
|
[HttpGet("delivery-challan/list/{purchaseInvoiceId}")]
|
|
public async Task<IActionResult> GetDeliveryChallans(Guid purchaseInvoiceId, CancellationToken cancellationToken)
|
|
{
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _purchaseInvoiceService.GetDeliveryChallansAsync(purchaseInvoiceId, loggedInEmployee, tenantId, cancellationToken);
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a delivery challan.
|
|
/// </summary>
|
|
/// <param name="model">The delivery challan model.</param>
|
|
/// <param name="ct">The cancellation token.</param>
|
|
/// <returns>The HTTP response for adding the delivery challan.</returns>
|
|
[HttpPost("delivery-challan/create")]
|
|
public async Task<IActionResult> AddDeliveryChallan([FromBody] DeliveryChallanDto model, CancellationToken ct)
|
|
{
|
|
// Get the currently logged-in employee
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
|
|
// Add the delivery challan using the purchase invoice service
|
|
var response = await _purchaseInvoiceService.AddDeliveryChallanAsync(model, loggedInEmployee, tenantId, ct);
|
|
|
|
// If the addition is successful, send a notification to the SignalR service
|
|
if (response.Success)
|
|
{
|
|
var notification = new
|
|
{
|
|
LoggedInUserId = loggedInEmployee.Id,
|
|
Keyword = "Delivery_Challan",
|
|
Response = response.Data
|
|
};
|
|
await _signalR.SendNotificationAsync(notification);
|
|
}
|
|
|
|
// Return the HTTP response
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region =================================================================== Purchase Invoice History Functions ===================================================================
|
|
[HttpGet("payment-history/list/{purchaseInvoiceId}")]
|
|
public async Task<IActionResult> GetPurchaseInvoiceHistoryList(Guid purchaseInvoiceId, CancellationToken cancellationToken)
|
|
{
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _purchaseInvoiceService.GetPurchaseInvoiceHistoryListAsync(purchaseInvoiceId, loggedInEmployee, tenantId, cancellationToken);
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
|
|
[HttpPost("add/payment")]
|
|
public async Task<IActionResult> AddPurchaseInvoicePayment([FromBody] ReceivedInvoicePaymentDto model, CancellationToken ct)
|
|
{
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _purchaseInvoiceService.AddPurchaseInvoicePaymentAsync(model, loggedInEmployee, tenantId, ct);
|
|
if (response.Success)
|
|
{
|
|
var notification = new
|
|
{
|
|
LoggedInUserId = loggedInEmployee.Id,
|
|
Keyword = "Purchase_Invoice_Payment",
|
|
Response = response.Data
|
|
};
|
|
await _signalR.SendNotificationAsync(notification);
|
|
}
|
|
|
|
// Return the HTTP response
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|