protected override void GenerateItems(IMaterialInventory forInventory, int year, int month, IInvoiceFormGenerationContext context, IReleasingFormsGenerationTask task, Action <DateTime, IMaterialBatch, Amount, DirectSaleDescriptor> itemCallback) { if (!forInventory.CanBeConnectedToTag) { context.Info($"Pro sklad \"{forInventory.Name}\" se výdejky typu \"Přímý prodej\" negenerují - přeskakuji"); return; } if (forInventory.AllowedUnitId == null) { throw new InvalidOperationException($"Pro sklad \"{forInventory.Name}\" musi byt nastavena dovolena merna jednotka"); } var unit = m_unitRepository.GetUnit(forInventory.AllowedUnitId.Value); DateUtil.GetMonthDt(year, month, out var fromDt, out var toDt); var events = m_saleEventRepository.GetEvents(fromDt, toDt); foreach (var evt in events) { foreach (var allocation in evt.Allocations.Where(e => e.ReturnDt != null)) { var soldAmount = new Amount(allocation.AllocatedQuantity - allocation.ReturnedQuantity ?? 0m, allocation.Unit); itemCallback(evt.EventDt, allocation.Batch, soldAmount, new DirectSaleDescriptor() { Event = evt, Allocation = allocation }); } } }
protected override void GenerateItems(IMaterialInventory forInventory, int year, int month, IInvoiceFormGenerationContext context, IReleasingFormsGenerationTask task, Action <DateTime, IMaterialBatch, Amount, ReturnedOrderDescriptor> itemCallback) { if (!forInventory.CanBeConnectedToTag) { context.Info($"Pro sklad \"{forInventory.Name}\" se výdejky typu \"VRACENE OBJEDNAVKY\" negenerují - přeskakuji"); return; } var returns = m_orderRepository.GetReturns(month, year).ToList(); DateUtil.GetMonthDt(year, month, out var dtFrom, out var dtTo); foreach (var order in returns) { var allOrderItems = new List <IOrderItem>(); foreach (var orderItem in order.Items) { if (orderItem.AssignedBatches.Any()) { allOrderItems.Add(orderItem); } foreach (var kitChild in orderItem.KitChildren) { if (kitChild.AssignedBatches.Any()) { allOrderItems.Add(kitChild); } } } foreach (var item in allOrderItems) { foreach (var assignment in item.AssignedBatches) { //todo: may be neccessary to filter by batch inventory in the future var batchMaterial = m_materialRepository.GetMaterialById(assignment.MaterialBatch.MaterialId); itemCallback(order.ReturnDt ?? order.BuyDate, assignment.MaterialBatch, m_amountProcessor.Neg(new Amount(assignment.Quantity, batchMaterial.NominalUnit)), new ReturnedOrderDescriptor { OrderNumber = order.OrderNumber }); } } foreach (var connectedEvent in m_stockEventRepository.GetEvents(dtFrom, dtTo, order.Id)) { itemCallback(order.ReturnDt ?? order.BuyDate, connectedEvent.Batch, m_amountProcessor.Neg(new Amount(connectedEvent.Delta, connectedEvent.Unit)), new ReturnedOrderDescriptor { OrderNumber = order.OrderNumber }); } } }
protected override void GenerateItems(IMaterialInventory forInventory, int year, int month, IInvoiceFormGenerationContext context, IReleasingFormsGenerationTask task, Action <DateTime, IMaterialBatch, Amount, EshopOrderDescriptor> itemCallback) { if (!forInventory.CanBeConnectedToTag) { context.Info($"Pro sklad \"{forInventory.Name}\" se výdejky typu \"PRODEJ e-shop\" negenerují - přeskakuji"); return; } if (forInventory.AllowedUnitId == null) { throw new InvalidOperationException($"Pro sklad \"{forInventory.Name}\" musi byt nastavena dovolena merna jednotka"); } var unit = m_unitRepository.GetUnit(forInventory.AllowedUnitId.Value); DateUtil.GetMonthDt(year, month, out var fromDt, out var toDt); m_orderRepository.PreloadOrders(fromDt, toDt); var allOrders = m_orderRepository.GetOrders(q => q.Where(o => o.BuyDate >= fromDt && o.BuyDate < toDt)) .ToList(); foreach (var order in allOrders) { if (!OrderStatus.IsSent(order.OrderStatusId)) { continue; } var orderText = order.OrderNumber; foreach (var item in order.Items) { foreach (var itemBatch in item.AssignedBatches) { itemCallback(order.BuyDate, itemBatch.MaterialBatch, new Amount(itemBatch.Quantity, unit), new EshopOrderDescriptor() { OrderIdentifierText = orderText, OrderItemBatchAssignmentId = itemBatch.Id, OrderInvoiceVarSymbol = order.VarSymbol }); } foreach (var kitChild in item.KitChildren) { foreach (var kitBatchBridge in kitChild.AssignedBatches) { itemCallback(order.BuyDate, kitBatchBridge.MaterialBatch, new Amount(kitBatchBridge.Quantity, unit), new EshopOrderDescriptor() { OrderIdentifierText = orderText, OrderItemBatchAssignmentId = kitBatchBridge.Id, OrderInvoiceVarSymbol = order.VarSymbol }); } } } } }
protected override IEnumerable <IMaterialBatch> FindSourceBatches(IMaterialInventory forInventory, int year, int month, IMaterialBatchFacade facade, IInvoiceFormGenerationContext context) { var startDate = new DateTime(year, month, 1).Date; var endDate = startDate.AddMonths(1); context.Info($"Hledám šarže na skladu {forInventory.Name} od {startDate} do {endDate}"); return(facade.FindNotClosedBatches(forInventory.Id, startDate, endDate).ToList()); }
private void DoGeneration(IMaterialInventory inventory, IInvoiceFormGenerationContext context, int year, int month, IReleasingFormsGenerationTask task) { var generator = m_generatorFactory.Get(task == null ? inventory.ReceivingInvoiceFormGeneratorName : task.GeneratorName); if (task == null) { context.Info($"Zacinam generovat: {generator.GetGenerationName(inventory, year, month)}"); } generator.Generate(inventory, year, month, context, task); }
protected override void GenerateItems(IMaterialInventory forInventory, int year, int month, IInvoiceFormGenerationContext context, IReleasingFormsGenerationTask task, Action <DateTime, IMaterialBatch, Amount, StockEventDescriptor> itemCallback) { DateUtil.GetMonthDt(year, month, out var dtFrom, out var dtTo); var events = m_stockEventRepository.GetEvents(dtFrom, dtTo, forInventory.Id).Where(e => e.Type.IsSubtracting).ToList(); foreach (var evt in events) { if (evt.Batch.IsHiddenForAccounting == true) { continue; } itemCallback(evt.EventDt.Date, evt.Batch, new Amount(evt.Delta, evt.Unit), new StockEventDescriptor() { StockEventId = evt.Id, StockEventTypeName = evt.Type.Name, Event = evt, StockEventNote = evt.Note }); } }
private void CreateItemsByBatchComposition(IMaterialInventory forInventory, int year, int month, Action <DateTime, IMaterialBatch, Amount, ManufacturingReleseEventDescriptor> itemCallback) { var allBatchesWithComponentsFromThisInventory = m_batchRepository.GetBatchesByComponentInventory(forInventory.Id, year, month).Where(b => b.Batch.IsHiddenForAccounting != true).ToList(); foreach (var composition in allBatchesWithComponentsFromThisInventory.Select(c => c.Batch)) { foreach (var componentRecord in composition.Components) { var componentBatch = m_batchRepository.GetBatchById(componentRecord.ComponentId).Batch; if (componentBatch.Material.InventoryId != forInventory.Id) { continue; } itemCallback(composition.Created, componentBatch, new Amount(componentRecord.Volume, componentRecord.Unit), new ManufacturingReleseEventDescriptor() { BatchCompositionRecordId = componentRecord.Id, CompositionBatchText = composition.GetTextInfo() }); } } }
public void Generate(IMaterialInventory forInventory, int year, int month, IInvoiceFormGenerationContext context, IReleasingFormsGenerationTask task = null) { var index = new Dictionary <string, List <ItemReleaseModel> >(); GenerateItems(forInventory, year, month, context, task, (time, batch, amount, descriptor) => { if (batch.IsHiddenForAccounting == true) { return; } var item = new ItemReleaseModel(time, batch, amount, descriptor); var key = GetGroupingKey(item); if (!index.TryGetValue(key, out var items)) { items = new List <ItemReleaseModel>(); index.Add(key, items); } item.Price = GetPrice(item, context); if (item.Price.Item2.HasWarning) { context.Warning($"Výpocet ceny šarže \"{item.TookFromBatch.GetTextInfo()}\" není konečný: {item.Price.Item2.Text}"); } items.Add(item); }); foreach (var key in index.Keys) { var list = index[key]; var totalPriceModel = BatchPrice.Combine(list.Select(i => i.Price.Item2)); var form = context.NewInvoiceForm(f => { f.InvoiceFormNumber = $"NESCHVALENO_{Guid.NewGuid():N}"; f.IssueDate = list[0].Date.Date; f.MaterialInventoryId = forInventory.Id; f.FormTypeId = FormType.Id; f.Text = task?.FormText ?? "?"; f.PriceCalculationLog = totalPriceModel.Text; f.PriceHasWarning = totalPriceModel.HasWarning; f.SourceTaskId = task?.Id; f.Explanation = GetExplanation(list, f); CustomizeFormCreation(list, f); }); foreach (var item in list) { var material = item.TookFromBatch.Material ?? m_materialRepository.GetMaterialById(item.TookFromBatch.MaterialId).Adaptee; var formItem = context.NewFormItem(form, item.TookFromBatch, i => { i.MaterialName = material.Name; i.Quantity = item.TookAmount.Value; i.UnitId = item.TookAmount.Unit.Id; i.PrimaryCurrencyPrice = item.Price.Item1; CustomizeFormItemCreation(item, i); }); OnAfterItemSaved(form, formItem, item); } } }
public string GetGenerationName(IMaterialInventory forInventory, int year, int month) { return($"Generator vydejeky ze skladu '{forInventory.Name}' typu '{GetType().Name}' pro {month}/{year}"); }
protected abstract void GenerateItems(IMaterialInventory forInventory, int year, int month, IInvoiceFormGenerationContext context, IReleasingFormsGenerationTask task, Action <DateTime, IMaterialBatch, Amount, TItemDescriptor> itemCallback);
protected override void GenerateItems(IMaterialInventory forInventory, int year, int month, IInvoiceFormGenerationContext context, IReleasingFormsGenerationTask task, Action <DateTime, IMaterialBatch, Amount, ManufacturingReleseEventDescriptor> itemCallback) { CreateItemsByBatchComposition(forInventory, year, month, itemCallback); }
public void Generate(IMaterialInventory forInventory, int year, int month, IInvoiceFormGenerationContext context, IReleasingFormsGenerationTask task = null) { if (task != null) { throw new InvalidOperationException("Illegal usage of generator"); } if (!m_groupingSetup) { SetupGrouping(m_batchesGrouping); m_groupingSetup = true; } var formType = m_invoiceFormsRepository.GetInvoiceFormTypes().FirstOrDefault(t => t.GeneratorName == "ReceivingInvoice"); if (formType == null) { throw new InvalidOperationException("No InvoiceFormType found by GeneratorName == ReceivingInvoice"); } var sourceBatches = FindSourceBatches(forInventory, year, month, m_batchFacade, context).Where(b => b.IsHiddenForAccounting != true).ToList(); context.Info($"Nalezeno {sourceBatches.Count}. Začínám indexování"); var groups = m_batchesGrouping.GroupBatches(sourceBatches, context).ToList(); context.Info($"Sestaveno {sourceBatches.Count} skupin"); foreach (var group in groups) { var referenceBatch = group.FirstOrDefault(); if (referenceBatch == null) { continue; } var explanation = GetFormExplanation(group).Limit(1000); var priceIndex = group.ToDictionary(b => b.Id, b => m_batchFacade.GetBatchPrice(b, context)); var totalPrice = BatchPrice.Combine(priceIndex.Values); var form = context.NewInvoiceForm(f => { f.InvoiceFormNumber = $"NESCHVALENO_{Guid.NewGuid():N}"; f.InvoiceNumber = referenceBatch.InvoiceNr; f.InvoiceVarSymbol = referenceBatch.InvoiceVarSymbol; f.IssueDate = m_batchFacade.GetBatchAccountingDate(referenceBatch).AccountingDate; f.MaterialInventoryId = referenceBatch.Material.InventoryId; f.SupplierId = referenceBatch.SupplierId; f.FormTypeId = formType.Id; f.PriceCalculationLog = totalPrice.Text; f.PriceHasWarning = totalPrice.HasWarning; f.Explanation = explanation; }); CustomizeFormMapping(referenceBatch, form, context); foreach (var batch in group) { var existingCollection = m_invoiceFormsRepository.GetCollectionByMaterialBatchId(batch.Id, formType.Id); if (existingCollection != null) { context.Error($"Šarže \"{batch.GetTextInfo()}\" je již zahrnuta v soupisce příjemek \"{existingCollection.Name}\", novou soupisku není možné vygenerovat"); } m_invoiceFormsRepository.NewItem(form, batch.Id, item => { item.MaterialName = batch.Material.Name; item.Quantity = batch.Volume; item.UnitId = batch.UnitId; item.Note = GetFormItemNote(batch); var price = priceIndex[batch.Id]; item.PrimaryCurrencyPrice = price.TotalPriceInPrimaryCurrency; if (batch.PriceConversionId != null) { item.SourceCurrencyPrice = batch.PriceConversion.SourceValue; item.SourceCurrencyId = batch.PriceConversion.SourceCurrencyId; item.ConversionId = batch.PriceConversion.Id; } CustomizeItemMapping(form, item, batch, context); }); } } }
public string GetGenerationName(IMaterialInventory forInventory, int year, int month) { return($"Příjemky na sklad {forInventory.Name} {month.ToString().PadLeft(2, '0')}/{year}"); }
protected abstract IEnumerable <IMaterialBatch> FindSourceBatches(IMaterialInventory forInventory, int year, int month, IMaterialBatchFacade facade, IInvoiceFormGenerationContext context);