OnlineSalesAutoCrop/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/MobileApp/OrderService.cs

168 lines
6.2 KiB
C#
Raw Normal View History

2026-07-15 18:51:09 +06:00
using Ease.NetCore.DataAccess;
using Ease.NetCore.DataAccess.SQL;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Options;
using OnlineSalesAutoCrop.CoreAPI.Models.Global;
using OnlineSalesAutoCrop.CoreAPI.Models.Requests.MobileApp;
using OnlineSalesAutoCrop.CoreAPI.Models.Responses.MobileApp;
using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.MobileApp;
using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Systems;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
namespace OnlineSalesAutoCrop.CoreAPI.Services.Services.MobileApp;
public class OrderService : IOrderService
{
private readonly AppSettings _settings;
private readonly IUserService _userService;
private readonly IMobileMasterDataService _masterService;
public OrderService(IOptions<AppSettings> options, IUserService userService, IMobileMasterDataService masterService)
{
_settings = options.Value;
_userService = userService;
_masterService = masterService;
}
public async Task<SaveOrderResponse> SaveOrderAsync(SaveOrderRequest request)
{
SaveOrderResponse response = new();
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode,true);
GetOrderByUUIDResponse order= await GetOrderByUUIDAsync(tc, request.UUID);
tc.End();
}
catch(Exception)
{
throw;
}
return new SaveOrderResponse()
{
OrderId = 12345,
OrderDate = DateTime.Now,
OrderNo = "Ord-000001-" + DateTime.Now.ToString("HH-mm-ss"),
UUID = request.UUID,
IsNewOrder =true
};
}
private async Task<GetOrderByUUIDResponse> GetOrderByUUIDAsync(TransactionContext tc, string uuid)
{
GetOrderByUUIDResponse response = null;
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@UUID", pType: SqlDbType.UniqueIdentifier, pValue: uuid)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetOrderDetailsByUUID", parameterValues: p))
{
// Result Set 1: Order Header
if (dr.Read())
{
response = new GetOrderByUUIDResponse
{
OrderId = dr.GetInt32(0),
OrderNo = dr.GetString(1),
OrderDate = dr.GetDateTime(2),
ExpectedSalesDate = dr.IsDBNull(3) ? null : dr.GetDateTime(3),
UUID = dr.GetGuid(4),
SalesOrgCode = dr.GetString(5),
CustomerId = dr.GetInt32(6),
CustomerCode = dr.GetString(7),
CustomerName = dr.GetString(8),
EmployeeId = dr.GetInt32(9),
EmployeeCode = dr.GetString(10),
EmployeeName = dr.GetString(11),
PaymentTermCode = dr.GetString(12),
ReferenceTxtNo = dr.IsDBNull(13) ? null : dr.GetString(13),
GrossValue = dr.GetDecimal(14),
DiscountValue = dr.GetDecimal(15),
FOCValue = dr.GetDecimal(16),
NetValue = dr.GetDecimal(17),
OrderStatus = dr.GetString(18),
SAPReferenceOrderNo = dr.IsDBNull(19) ? null : dr.GetString(19)
};
}
if (response != null && dr.NextResult())
{
while (dr.Read())
{
GetOrderItemResponse item = new()
{
ItemId = dr.GetInt32(0),
OrderID = dr.GetInt32(1),
MaterialId = dr.GetInt32(2),
MaterialCode = dr.GetString(3),
MaterialName = dr.GetString(4),
UnitPrice = dr.GetDecimal(5),
SalesUnit = dr.GetString(6),
OrderQuantity = dr.GetDecimal(7),
ValidatedQuantity = dr.GetDecimal(8),
ApprovedQuantity = dr.GetDecimal(9),
DiscountPercentage = dr.GetDecimal(10),
DiscountValue = dr.GetDecimal(11),
IsFOCItem = dr.GetBoolean(12),
OrderFocQuantity = dr.GetDecimal(13),
ValidatedFocQuantity = dr.GetDecimal(14),
ApprovedFocQuantity = dr.GetDecimal(15),
LineTotalValue = dr.GetDecimal(16),
};
response.Items.Add(item);
}
}
// Result Set 3: Promotions (attach to matching item)
if (response != null && dr.NextResult())
{
while (dr.Read())
{
GetOrderPromotionResponse promotion = new()
{
ItemId = dr.GetInt32(0),
OrderID = dr.GetInt32(1),
MaterialId = dr.GetInt32(2),
PromotionCode = dr.GetString(3),
DiscountPercentage = dr.GetDecimal(4),
DiscountValue = dr.GetDecimal(5),
ForQuantity = dr.GetDecimal(6),
OrderFocQuantity = dr.GetDecimal(7),
ValidatedFocQuantity = dr.GetDecimal(8),
ApprovedFocQuantity = dr.GetDecimal(9),
PromotionType = dr.GetInt32(10),
};
response.Promotions.Add(promotion);
}
}
dr.Close();
}
tc.End();
}
catch (Exception)
{
throw;
}
return response;
}
}