public static async Task <Response <Payroll> > MakePayment(Guid orderId, Guid clientId) { using (var unitOfWork = new DataLayerUnitOfWork()) { var orderRepository = unitOfWork.TrackingRepository <OrderRepository>(); var payrollRepository = unitOfWork.TrackingRepository <PayrollRepository>(); var order = await orderRepository.GetAsync(orderId, new[] { nameof(Order.OrderItems) }).ConfigureAwait(false); if (order == null) { return(ResponseFactory <Payroll> .CreateResponse(false, ResponseCode.ErrorInvalidInput)); } var payroll = new DataLayer.Payroll { ClientId = clientId, OrderId = orderId, Date = DateTime.Now, Value = order.OrderItems.Sum(orderItem => orderItem.Price * orderItem.Quantity) }; payroll = await payrollRepository.CreateAsync(payroll, true).ConfigureAwait(false); if (payroll == null) { return(ResponseFactory <Payroll> .CreateResponse(false, ResponseCode.Error)); } order.Status = OrderStatus.Finalized.ToInt(); var updatedOrder = await orderRepository.UpdateAsync(order, true).ConfigureAwait(false); if (updatedOrder == null) { return(ResponseFactory <Payroll> .CreateResponse(false, ResponseCode.Error)); } return(ResponseFactory <Payroll> .CreateResponse(true, ResponseCode.Success, payroll.CopyTo <Payroll>())); } }
public static async Task <Response <OrderItem> > UpdateAsync(OrderItem orderItem) { using (var unitOfWork = new DataLayerUnitOfWork()) { var order = await unitOfWork.TrackingRepository <OrderRepository>().GetAsync(orderItem.OrderId).ConfigureAwait(false); if (!SalesValidator.ValidateOrderItem(orderItem, (SaleType)order.SaleType)) { return(ResponseFactory <OrderItem> .CreateResponse(false, ResponseCode.ErrorInvalidInput)); } var dbModel = orderItem.CopyTo <DataLayer.OrderItem>(); var result = await unitOfWork.TrackingRepository <OrderItemRepository>().UpdateAsync(dbModel).ConfigureAwait(false); if (result == null) { return(ResponseFactory <OrderItem> .CreateResponse(false, ResponseCode.Error)); } return(ResponseFactory <OrderItem> .CreateResponse(true, ResponseCode.Success, result.CopyTo <OrderItem>())); } }
private static async Task <Response <Order> > CreatePayroll(Order order, DataLayerUnitOfWork unitOfWork, OrderRepository orderRepository) { var orderWithItems = await orderRepository.GetAsync(order.Id, new[] { nameof(Order.OrderItems) }).ConfigureAwait(false); var payroll = new Payroll { ClientId = order.ClientId, Date = DateTime.Now, OrderId = order.Id, Value = orderWithItems.OrderItems.Sum(orderItem => orderItem.Price * orderItem.Quantity) }; payroll = await unitOfWork.TrackingRepository <PayrollRepository>().CreateAsync(payroll).ConfigureAwait(false); if (payroll == null) { return(ResponseFactory <Order> .CreateResponse(false, ResponseCode.Error)); } return(ResponseFactory <Order> .CreateResponse(true, ResponseCode.Success, order)); }
public static async Task <Response <Order> > Finalize(Order order) { if (!SalesValidator.ValidateOrder(order, false)) { return(ResponseFactory <Order> .CreateResponse(false, ResponseCode.ErrorInvalidInput)); } order.Status = OrderStatus.Finalized.ToInt(); using (var unitOfWork = new DataLayerUnitOfWork()) { var orderRepository = unitOfWork.TrackingRepository <OrderRepository>(); var dbModel = order.CopyTo <DataLayer.Order>(); var updatedOrder = await orderRepository.UpdateAsync(dbModel, true).ConfigureAwait(false); if (updatedOrder == null) { return(ResponseFactory <Order> .CreateResponse(false, ResponseCode.Error)); } return(await CreatePayroll(order, unitOfWork, orderRepository)); } }