2024-04-21 19:17:40 +04:00
|
|
|
|
using AccountingWarehouseProductsContracts.BindingModels;
|
|
|
|
|
using AccountingWarehouseProductsContracts.ViewModels;
|
2024-05-01 22:21:11 +04:00
|
|
|
|
using AccountingWarehouseProductsDataModels.Enums;
|
2024-04-21 19:17:40 +04:00
|
|
|
|
using AccountingWarehouseProductsDataModels.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2024-05-01 22:21:11 +04:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2024-04-21 19:17:40 +04:00
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AccountingWarehouseProductsDatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Order : IOrderModel
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
|
2024-05-01 22:21:11 +04:00
|
|
|
|
public int ProductId { get; set; }
|
|
|
|
|
|
|
|
|
|
public int SupplierId { get; set; }
|
|
|
|
|
|
|
|
|
|
public string ProductName { get; set; }
|
|
|
|
|
|
2024-05-07 16:15:07 +04:00
|
|
|
|
public string SupplierName { get; set; }
|
|
|
|
|
|
2024-05-01 22:21:11 +04:00
|
|
|
|
public int Count { get; set; }
|
|
|
|
|
|
|
|
|
|
public double Sum { get; set; }
|
|
|
|
|
|
|
|
|
|
public virtual Product Product { get; set; }
|
|
|
|
|
|
|
|
|
|
public DateTime? DateofOrder { get; set; } = DateTime.Now;
|
|
|
|
|
public DateTime? DateImplement { get; private set; }
|
2024-04-21 19:17:40 +04:00
|
|
|
|
|
|
|
|
|
public AccountingWarehouseProductsDataModels.Enums.OrderStatus Status { get; set; }
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, (IProductModel, int)>? _orderProduct = null;
|
|
|
|
|
|
|
|
|
|
[NotMapped]
|
2024-05-01 22:21:11 +04:00
|
|
|
|
public Dictionary<int, (IProductModel, int)> OrderProduct
|
2024-04-21 19:17:40 +04:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_orderProduct == null)
|
|
|
|
|
{
|
|
|
|
|
_orderProduct = Products.ToDictionary(recOP => recOP.ProductId, recOP => (recOP.Product as IProductModel, recOP.Count));
|
|
|
|
|
}
|
|
|
|
|
return _orderProduct;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ForeignKey("OrderId")]
|
|
|
|
|
public virtual List<OrderProduct> Products { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
public static Order Create(AccountingWarehouseProductsDatabase context, OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Order()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
2024-05-01 22:21:11 +04:00
|
|
|
|
ProductId = model.ProductId,
|
|
|
|
|
SupplierId = model.SupplierId,
|
|
|
|
|
ProductName = model.ProductName,
|
2024-05-07 16:15:07 +04:00
|
|
|
|
SupplierName = model.SupplierName,
|
2024-05-01 22:21:11 +04:00
|
|
|
|
Count = model.Count,
|
|
|
|
|
Sum = model.Sum,
|
2024-04-21 19:17:40 +04:00
|
|
|
|
Status = model.Status,
|
2024-05-01 22:21:11 +04:00
|
|
|
|
DateofOrder = model.DateofOrder,
|
|
|
|
|
DateImplement = model.DateImplement,
|
|
|
|
|
Products = model.OrderProduct.Select(x => new OrderProduct
|
2024-04-21 19:17:40 +04:00
|
|
|
|
{
|
|
|
|
|
Product = context.Products.First(y => y.Id == x.Key),
|
|
|
|
|
Count = x.Value.Item2
|
|
|
|
|
}).ToList(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(OrderBindingModel model)
|
|
|
|
|
{
|
2024-05-01 22:21:11 +04:00
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-21 19:17:40 +04:00
|
|
|
|
Status = model.Status;
|
2024-05-01 22:21:11 +04:00
|
|
|
|
if (model.DateofOrder != null)
|
|
|
|
|
{
|
|
|
|
|
DateofOrder = model.DateofOrder;
|
|
|
|
|
}
|
|
|
|
|
DateImplement = model.DateImplement;
|
2024-04-21 19:17:40 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrderViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
2024-05-01 22:21:11 +04:00
|
|
|
|
ProductId = ProductId,
|
|
|
|
|
SupplierId = SupplierId,
|
|
|
|
|
ProductName = ProductName,
|
2024-05-07 16:15:07 +04:00
|
|
|
|
SupplierName = SupplierName,
|
2024-05-01 22:21:11 +04:00
|
|
|
|
Count = Count,
|
|
|
|
|
Sum = Sum,
|
2024-04-21 19:17:40 +04:00
|
|
|
|
Status = Status,
|
2024-05-01 22:21:11 +04:00
|
|
|
|
DateofOrder = DateofOrder,
|
|
|
|
|
DateImplement = DateImplement,
|
|
|
|
|
OrderProduct = OrderProduct
|
2024-04-21 19:17:40 +04:00
|
|
|
|
};
|
|
|
|
|
|
2024-05-01 22:21:11 +04:00
|
|
|
|
|
|
|
|
|
|
2024-04-21 19:17:40 +04:00
|
|
|
|
public void UpdateProducts(AccountingWarehouseProductsDatabase context, OrderBindingModel model)
|
|
|
|
|
{
|
2024-05-01 22:21:11 +04:00
|
|
|
|
var orderProduct = context.OrderProduct.Where(rec => rec.OrderId == model.Id).ToList();
|
|
|
|
|
if (orderProduct != null && orderProduct.Count > 0)
|
2024-04-21 19:17:40 +04:00
|
|
|
|
{
|
2024-05-01 22:21:11 +04:00
|
|
|
|
context.OrderProduct.RemoveRange(orderProduct.Where(rec => !model.OrderProduct.ContainsKey(rec.ProductId)));
|
2024-04-21 19:17:40 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
2024-05-01 22:21:11 +04:00
|
|
|
|
foreach (var updateProduct in orderProduct)
|
2024-04-21 19:17:40 +04:00
|
|
|
|
{
|
2024-05-01 22:21:11 +04:00
|
|
|
|
updateProduct.Count = model.OrderProduct[updateProduct.ProductId].Item2;
|
|
|
|
|
model.OrderProduct.Remove(updateProduct.ProductId);
|
2024-04-21 19:17:40 +04:00
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var order = context.Orders.First(x => x.Id == Id);
|
2024-05-01 22:21:11 +04:00
|
|
|
|
foreach (var rc in model.OrderProduct)
|
2024-04-21 19:17:40 +04:00
|
|
|
|
{
|
2024-05-01 22:21:11 +04:00
|
|
|
|
context.OrderProduct.Add(new OrderProduct
|
2024-04-21 19:17:40 +04:00
|
|
|
|
{
|
|
|
|
|
Order = order,
|
|
|
|
|
Product = context.Products.First(x => x.Id == rc.Key),
|
|
|
|
|
Count = rc.Value.Item2
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_orderProduct = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|