add stock checking in save order endpoint
This commit is contained in:
parent
3aea7dee2c
commit
c4ad3e53c7
|
|
@ -7,6 +7,7 @@ using OnlineSalesAutoCrop.CoreAPI.Models.Global;
|
|||
using OnlineSalesAutoCrop.CoreAPI.Models.Requests.MobileApp;
|
||||
using OnlineSalesAutoCrop.CoreAPI.Models.Responses;
|
||||
using OnlineSalesAutoCrop.CoreAPI.Models.Responses.MobileApp;
|
||||
using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Integrations;
|
||||
using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.MobileApp;
|
||||
using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Systems;
|
||||
using System;
|
||||
|
|
@ -23,12 +24,16 @@ public class OrderService : IOrderService
|
|||
private readonly AppSettings _settings;
|
||||
private readonly IUserService _userService;
|
||||
private readonly IMobileMasterDataService _masterService;
|
||||
private readonly IIntegrationHttpService _httpService;
|
||||
|
||||
public OrderService(IOptions<AppSettings> options, IUserService userService, IMobileMasterDataService masterService)
|
||||
|
||||
public OrderService(IOptions<AppSettings> options, IUserService userService, IMobileMasterDataService masterService,
|
||||
IIntegrationHttpService httpService)
|
||||
{
|
||||
_settings = options.Value;
|
||||
_userService = userService;
|
||||
_masterService = masterService;
|
||||
_httpService = httpService;
|
||||
}
|
||||
|
||||
public async Task<SaveOrderResponse> SaveOrderAsync(SaveOrderRequest request, int userId)
|
||||
|
|
@ -69,6 +74,23 @@ public class OrderService : IOrderService
|
|||
item.OrderId = request.OrderId;
|
||||
}
|
||||
|
||||
var materialStock = await _httpService.GetMaterialStockAsync(request.PlantCode);
|
||||
if(materialStock is null || materialStock?.Count==0)
|
||||
{
|
||||
throw new Exception($"Material Stocks not found against this Plant Code-{request.PlantCode}");
|
||||
}
|
||||
|
||||
|
||||
foreach (var item in request.Items)
|
||||
{
|
||||
decimal currentStock = materialStock.FirstOrDefault(x => x.MaterialCode == item.MaterialCode)?.AvailableStock ?? 0;
|
||||
|
||||
if(currentStock<item.ApprovedQuantity)
|
||||
{
|
||||
throw new Exception($"Insufficient stock is available for Material Code '{item.MaterialCode}' in Plant Code '{request.PlantCode}'.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 2. Insert vs update decision
|
||||
bool isUpdate = existingOrder != null && existingOrder.OrderId > 0 ;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user