public IInvoiceFormItem NewFormItem(IInvoiceForm form, IMaterialBatch batch, Action <IInvoiceFormItem> setup) { return(m_invoiceFormsRepository.NewItem(form, batch.Id, setup)); }
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); }); } } }