2024-07-26 10:30:07 +03:30
|
|
|
|
using Back.Common;
|
|
|
|
|
using Back.Data.Contracts;
|
2024-07-25 17:18:03 +03:30
|
|
|
|
using Back.Data.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Shared.DTOs;
|
|
|
|
|
using Shared.DTOs.Serch;
|
|
|
|
|
|
|
|
|
|
namespace Back.Services
|
|
|
|
|
{
|
|
|
|
|
public class ServOrders
|
|
|
|
|
{
|
2024-07-29 23:32:54 +03:30
|
|
|
|
private readonly IAsyncRepository<PermissionPeriod> _repoPermissionPeriod;
|
2024-07-25 17:18:03 +03:30
|
|
|
|
private readonly IAsyncRepository<Order> _repoOrder;
|
|
|
|
|
private readonly IAsyncRepository<OrderItem> _repoOrderItem;
|
|
|
|
|
|
|
|
|
|
|
2024-07-29 23:32:54 +03:30
|
|
|
|
public ServOrders(IAsyncRepository<Order> repoOrder, IAsyncRepository<OrderItem> repoOrderItem
|
|
|
|
|
, IAsyncRepository<PermissionPeriod> repoPermissionPeriod)
|
2024-07-25 17:18:03 +03:30
|
|
|
|
{
|
|
|
|
|
_repoOrder = repoOrder;
|
|
|
|
|
_repoOrderItem = repoOrderItem;
|
2024-07-29 23:32:54 +03:30
|
|
|
|
_repoPermissionPeriod = repoPermissionPeriod;
|
2024-07-25 17:18:03 +03:30
|
|
|
|
}
|
2024-07-26 10:30:07 +03:30
|
|
|
|
public async Task<PagingDto<OrderDto>> GetOrdersByCompanyID(int CompanyID, ItemSerachOrder itemSerach)
|
2024-07-25 17:18:03 +03:30
|
|
|
|
{
|
|
|
|
|
var request = _repoOrder.Get(w => w.CompanyID == CompanyID);
|
|
|
|
|
|
|
|
|
|
if (itemSerach.Status.HasValue)
|
|
|
|
|
request = request.Where(w => w.Status == itemSerach.Status);
|
|
|
|
|
|
|
|
|
|
if (itemSerach.ID.HasValue)
|
|
|
|
|
request = request.Where(w => w.ID == itemSerach.ID);
|
|
|
|
|
|
|
|
|
|
request = request.Include(inc => inc.OrderItems);
|
|
|
|
|
return await request
|
|
|
|
|
.Select(w => new OrderDto
|
|
|
|
|
{
|
2024-07-27 17:06:32 +03:30
|
|
|
|
ApprovalDate = w.ApprovalDate.ShamciToFormatShamci(),
|
|
|
|
|
DateCreate = w.DateCreate.ShamciToFormatShamci(),
|
2024-07-25 17:18:03 +03:30
|
|
|
|
ID = w.ID,
|
|
|
|
|
PreDiscount = w.PreDiscount,
|
|
|
|
|
Status = w.Status,
|
|
|
|
|
TDiscount = w.TDiscount,
|
|
|
|
|
TPrice = w.TPrice,
|
2024-07-28 22:58:39 +03:30
|
|
|
|
TTax = w.TTax,
|
2024-07-25 17:18:03 +03:30
|
|
|
|
lstDiscount = w.lstDiscount,
|
2024-07-26 10:30:07 +03:30
|
|
|
|
}).Paging(itemSerach.PageIndex,itemSerach.PageSize);
|
2024-07-25 17:18:03 +03:30
|
|
|
|
}
|
|
|
|
|
public async Task<List<OrderItemDto>> GetOrderItems(int OrderID,int CompanyID)
|
|
|
|
|
{
|
2024-07-27 17:06:32 +03:30
|
|
|
|
return await _repoOrderItem.Get(w => w.OrderID == OrderID && w.Order.CompanyID == CompanyID)
|
|
|
|
|
.Include(inc=>inc.Permission)
|
|
|
|
|
.Include(inc=>inc.Promotion)
|
2024-07-25 17:18:03 +03:30
|
|
|
|
.Select(s=>new OrderItemDto
|
|
|
|
|
{
|
|
|
|
|
OrderID = OrderID,
|
|
|
|
|
APrice = s.APrice,
|
|
|
|
|
CreditAmount = s.CreditAmount,
|
|
|
|
|
Discount=s.Discount,
|
|
|
|
|
ID = s.ID,
|
|
|
|
|
Tax = s.Tax,
|
2024-07-28 22:58:39 +03:30
|
|
|
|
IDForType = s.PermissionID.Value ,
|
|
|
|
|
Title = s.Permission.Title ,
|
2024-07-25 17:18:03 +03:30
|
|
|
|
}).ToListAsync();
|
|
|
|
|
}
|
2024-07-27 17:06:32 +03:30
|
|
|
|
public async Task<Order> GetOrder(int OrderID, int CompanyID)
|
|
|
|
|
{
|
2024-07-29 17:50:46 +03:30
|
|
|
|
return await _repoOrder.Get(w => w.ID == OrderID && w.CompanyID == CompanyID)
|
|
|
|
|
.Include(inc=>inc.OrderItems)
|
|
|
|
|
.FirstOrDefaultAsync();
|
2024-07-27 17:06:32 +03:30
|
|
|
|
}
|
|
|
|
|
public async Task<bool> UpdateOrder(Order order)
|
|
|
|
|
{
|
|
|
|
|
return await _repoOrder.UpdateAsync(order);
|
|
|
|
|
}
|
2024-07-28 17:42:40 +03:30
|
|
|
|
public async Task<Order> AddOrder(Order order)
|
2024-07-28 17:31:40 +03:30
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-07-28 17:42:40 +03:30
|
|
|
|
return await _repoOrder.AddAsync(order);
|
|
|
|
|
|
2024-07-28 17:31:40 +03:30
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-07-28 17:42:40 +03:30
|
|
|
|
return null;
|
2024-07-28 17:31:40 +03:30
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-29 23:32:54 +03:30
|
|
|
|
public async Task<bool> SubmitOrder(Order ordermodel,int CompanyID)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
foreach (OrderItem order in ordermodel.OrderItems.ToList())
|
|
|
|
|
{
|
|
|
|
|
var pp = await _repoPermissionPeriod.Get(w => w.PermissionID == order.PermissionID && w.CompanyID == CompanyID && w.CalculationTypeID == 1).FirstOrDefaultAsync();
|
|
|
|
|
if (pp != null)
|
|
|
|
|
{
|
|
|
|
|
pp.RemainingAmount += order.CreditAmount;
|
|
|
|
|
pp.TotalAmount += order.CreditAmount;
|
|
|
|
|
await _repoPermissionPeriod.UpdateAsync(pp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ordermodel.Status = StatusOrder.Paid;
|
|
|
|
|
return await _repoOrder.UpdateAsync(ordermodel);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-07-25 17:18:03 +03:30
|
|
|
|
}
|
|
|
|
|
}
|