Example #1
0
        public BatchPrice GetBatchPrice(IMaterialBatch batch, IBatchPriceBulkProvider provider)
        {
            var compos = provider.GetBatchPriceComponents(batch.Id);

            var bp = new BatchPrice(batch);

            foreach (var c in compos)
            {
                bp.AddComponent(c.IsWarning, c.SourceBatchId, c.Text, c.RawValue);
            }

            return(bp);
        }
Example #2
0
        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);
                }
            }
        }
Example #3
0
        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);
                    });
                }
            }
        }