public InventoryOperation ManageOrderItemBalance(OrderItemBalance orderItemBalance, int userId) { using (var dbContext = new InventoryDbContext()) { using (var transaction = dbContext.Database.BeginTransaction(System.Data.IsolationLevel.ReadCommitted)) { //Finding relevant Receipt Item var receiptReferenceNumber = orderItemBalance.FuelReportDetailId.ToString(); var receiptReference = findInventoryOperationReference( dbContext, InventoryOperationType.Receipt, FUEL_REPORT_DETAIL_RECEIVE, receiptReferenceNumber); var receiptReferenceTransactionItem = dbContext.TransactionItems.Single( tip => tip.TransactionId == (int)receiptReference.OperationId && tip.GoodId == (int)orderItemBalance.FuelReportDetail.Good.SharedGoodId); var receiptPriceReferenceNumber = generateOrderItemBalancePricingReferenceNumber(orderItemBalance); var receiptRegisteredPriceReference = findInventoryOperationReference(dbContext, InventoryOperationType.Pricing, FUEL_REPORT_DETAIL_RECEIPT_INVOICE, receiptPriceReferenceNumber); //There is no mechanism for editing the price of priced receipts portions. var transactionItemPrice = new TransactionItemPrice() { TransactionItemId = receiptReferenceTransactionItem.Id, QuantityUnitId = getMeasurementUnitId(dbContext, orderItemBalance.UnitCode), QuantityAmount = orderItemBalance.QuantityAmountInMainUnit, PriceUnitId = getCurrencyId(dbContext, orderItemBalance.InvoiceItem.Invoice.Currency.Abbreviation), Fee = orderItemBalance.InvoiceItem.Fee, RegistrationDate = DateTime.Now, Description = "Received Good Pricing > " + orderItemBalance.FuelReportDetail.Good.Code, UserCreatorId = userId }; string pricingMessage; priceTransactionItemManually(dbContext, transactionItemPrice, userId, out pricingMessage, FUEL_REPORT_DETAIL_RECEIPT_INVOICE, receiptPriceReferenceNumber); var pricingOperationReference = findInventoryOperationReference(dbContext, InventoryOperationType.Pricing, FUEL_REPORT_DETAIL_RECEIPT_INVOICE, receiptPriceReferenceNumber); var result = new InventoryOperation( pricingOperationReference.OperationId, actionNumber: string.Format("{0}/{1}", (InventoryOperationType)pricingOperationReference.OperationType, pricingOperationReference.OperationId), actionDate: DateTime.Now, actionType: InventoryActionType.Pricing, fuelReportDetailId: null, charterId: null); transaction.Commit(); return result; } } }
public InventoryOperation NotifySubmittingOrderItemBalance(OrderItemBalance orderItemBalance) { try { return this.inventoryOperationManager.ManageOrderItemBalance(orderItemBalance, //TODO: Fake ActorId 1101); } catch (Exception) { throw; } }
private List<OrderItemBalance> balanceInvoiceItemInMainUnit(InvoiceItem invoiceItem, IEnumerable<OrderItem> orderItems) { List<OrderItemBalance> generatedOrderItemBalances = new List<OrderItemBalance>(); var fuelReportDetialListFetchStrategy = new ListFetchStrategy<FuelReportDetail>() .OrderBy(frd => frd.FuelReport.EventDate); var orderIds = orderItems.Select(oi => oi.OrderId).ToList(); var fuelReportDetailsOfOrderItems = fuelReportDetailRepository.Find( frd => frd.GoodId == invoiceItem.GoodId && frd.ReceiveReference.ReferenceType == ReferenceType.Order && orderIds.Contains(frd.ReceiveReference.ReferenceId.Value), fuelReportDetialListFetchStrategy); var unassignedInvoiceItemQuantity = goodUnitConvertorDomainService.ConvertUnitValueToMainUnitValue(invoiceItem.MeasuringUnit, invoiceItem.Quantity); for (int index = 0; index < fuelReportDetailsOfOrderItems.Count; index++) { var checkingFuelReportDetail = fuelReportDetailsOfOrderItems[index]; var fuelReportDetailInvoicedQuantityInMainUnit = balanceRepository.Find(b => b.FuelReportDetailId == checkingFuelReportDetail.Id).Sum(found => found.QuantityAmountInMainUnit); var fuelReportDetailReceivedInMainUnit = goodUnitConvertorDomainService.ConvertUnitValueToMainUnitValue(checkingFuelReportDetail.MeasuringUnit, (decimal)checkingFuelReportDetail.Receive.Value); var fuelReportDetailNotInvoicedQuantity = fuelReportDetailReceivedInMainUnit - fuelReportDetailInvoicedQuantityInMainUnit; if (fuelReportDetailNotInvoicedQuantity <= 0) continue; var availableQuantityForBalancing = Math.Min(fuelReportDetailNotInvoicedQuantity, unassignedInvoiceItemQuantity); var relevantOrderItem = orderItems.Single( oi => oi.OrderId == checkingFuelReportDetail.ReceiveReference.ReferenceId.Value && oi.GoodId == checkingFuelReportDetail.GoodId); var orderItemBalanceToAdd = new OrderItemBalance( relevantOrderItem, invoiceItem, checkingFuelReportDetail, availableQuantityForBalancing, checkingFuelReportDetail.Good.SharedGood.MainUnit.Abbreviation); balanceRepository.Add(orderItemBalanceToAdd); generatedOrderItemBalances.Add(orderItemBalanceToAdd); relevantOrderItem.UpdateInvoiced(availableQuantityForBalancing); unassignedInvoiceItemQuantity -= availableQuantityForBalancing; if (unassignedInvoiceItemQuantity <= 0) break; } if (unassignedInvoiceItemQuantity != 0) { throw new BusinessRuleException("", string.Format("Invoiced Quantity for Good '{0}' has deficiencies with selected Orders.", invoiceItem.Good.Name)); } return generatedOrderItemBalances; }