Пример #1
0
        public FileResult GetAllForms(int year, int month)
        {
            var formTypes = m_invoiceFormsRepository.GetInvoiceFormTypes().ToList();

            var tempDir = Path.Combine($"C:\\Elsa\\Temp\\{Guid.NewGuid()}\\{month.ToString().PadLeft(2, '0')}-{year}");

            Directory.CreateDirectory(tempDir);

            foreach (var formType in formTypes)
            {
                var ftDir = Path.Combine(tempDir, formType.Name);
                Directory.CreateDirectory(ftDir);

                var collection = m_invoiceFormsRepository.FindCollection(formType.Id, year, month);
                if (collection == null)
                {
                    continue;
                }

                foreach (var form in collection.Forms)
                {
                    var renderer = m_formRendererFactory.GetRenderer(form);
                    var path     = Path.Combine(ftDir, $"{form.InvoiceFormNumber}.pdf");
                    File.WriteAllBytes(path, renderer.GetPdf());
                }
            }

            return(null);
        }
        public IInvoiceFormGenerationContext RunTasks(int year, int month)
        {
            m_fixedCostRepository.CalculateFixedCostComponents(year, month);

            var invoiceFormType = m_invoiceFormsRepository.GetInvoiceFormTypes()
                                  .FirstOrDefault(t => t.GeneratorName == "ReleasingForm");

            if (invoiceFormType == null)
            {
                throw new InvalidOperationException("InvoiceFormType was not found by GeneratorName 'ReleasingForm'");
            }

            m_log.Info($"Called for month={month}, year={year}");

            //using (var tx = m_database.OpenTransaction())
            //{
            var context            = PrepareContext(invoiceFormType.Id, year, month);
            var allGenerationTasks = m_invoiceFormsRepository.GetReleasingFormsTasks();

            foreach (var task in allGenerationTasks)
            {
                var inventories = task.Inventories?.Select(i => i.MaterialInventoryId).ToList();
                if (inventories?.Any() != true)
                {
                    inventories = m_materialRepository.GetMaterialInventories().Select(i => i.Id).ToList();
                }

                foreach (var inventoryId in inventories)
                {
                    var materialInventory = m_materialRepository.GetMaterialInventories()
                                            .FirstOrDefault(i => i.Id == inventoryId);

                    if (materialInventory == null)
                    {
                        throw new InvalidOperationException("Unknown inventory Id");
                    }

                    try
                    {
                        context.Info($"Začínám generování výdejek typu \"{task.FormText}\" ze skladu {materialInventory.Name}");
                        DoGeneration(materialInventory, context, year, month, task);
                        context.Info($"Dokončeno generování výdejek typu \"{task.FormText}\" ze skladu {materialInventory.Name}");
                    }
                    catch (Exception ex)
                    {
                        context.Error($"Generovani vydejek typu \"{task.FormText}\" pro sklad {materialInventory.Name} selhalo: {ex.Message}", ex);
                    }
                }
            }

            if (context.CountForms() == 0)
            {
                context.Error($"Nebyla vygenerována žádná výdejka");
            }

            //tx.Commit();

            return(context);
            //}
        }
Пример #3
0
        protected ReleaseFormsGeneratorBase(IMaterialBatchFacade batchFacade,
                                            IInvoiceFormsRepository invoiceFormsRepository, IMaterialRepository materialRepository)
        {
            m_batchFacade        = batchFacade;
            m_materialRepository = materialRepository;

            FormType = invoiceFormsRepository.GetInvoiceFormTypes().FirstOrDefault(t =>
                                                                                   t.GeneratorName.Equals("ReleasingForm", StringComparison.InvariantCultureIgnoreCase));

            if (FormType == null)
            {
                throw new InvalidOperationException("No InvoiceFormType found by 'ReleasingForm'");
            }
        }
Пример #4
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);
                    });
                }
            }
        }