private IInvoiceFormGenerationContext PrepareContext(int formTypeId, int year, int month)
        {
            var existingCollection = m_invoiceFormsRepository.FindCollection(formTypeId, year, month);

            if (existingCollection?.ApproveUserId != null)
            {
                throw new InvalidOperationException("J*z bylo vygenerovano");
            }

            var preapprovedMessages = new HashSet <string>();

            if (existingCollection != null)
            {
                foreach (var m in existingCollection.Log.Where(l =>
                                                               l.IsWarning && (l.ApproveUserId == m_session.User.Id)))
                {
                    preapprovedMessages.Add(m.Message);
                }

                m_invoiceFormsRepository.DeleteCollection(existingCollection.Id);
            }

            var context = m_invoiceFormsRepository.StartGeneration($"{month.ToString().PadLeft(2, '0')}/{year}",
                                                                   year,
                                                                   month,
                                                                   formTypeId);

            context.AutoApproveWarnings(preapprovedMessages);
            return(context);
        }
        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 InvoiceFormsCollection <T> Load <T>(int invoiceFormTypeId, int year, int month,
                                                   Func <IInvoiceForm, T> itemMapper) where T : InvoiceFormModelBase
        {
            var coll = m_invoiceFormsRepository.FindCollection(invoiceFormTypeId, year, month);

            if (coll == null)
            {
                return(new InvoiceFormsCollection <T>()
                {
                    CanApprove = false,
                    CanGenerate = true,
                    InvoiceFormTypeId = invoiceFormTypeId,
                    IsGenerated = false,
                    Month = month,
                    Year = year
                });
            }

            var homeUrl = m_session.Project.HomeUrl ?? "Project.HomeUrl";

            var collection = new InvoiceFormsCollection <T>()
            {
                Id = coll.Id
            };

            foreach (var form in coll.Forms.OrderBy(f => f.IssueDate))
            {
                var itemModel = itemMapper(form);
                itemModel.InvoiceFormId     = form.Id;
                itemModel.IssueDate         = StringUtil.FormatDate(form.IssueDate);
                itemModel.InvoiceFormNumber = form.InvoiceFormNumber;
                itemModel.PrimaryCurrencyPriceWithoutVat = form.Items.Sum(i => i.PrimaryCurrencyPrice);

                itemModel.FormattedPrimaryCurrencyPriceWithoutVat = itemModel.PrimaryCurrencyPriceWithoutVat.ToString("F");

                itemModel.PriceCalculationLog =
                    PriceCalculationLog.Get(form.PriceCalculationLog, form.PriceHasWarning ?? false);

                itemModel.CancelReason  = form.CancelDt == null ? string.Empty : form.CancelReason ?? "STORNO";
                itemModel.InventoryName = form.MaterialInventory?.Name;
                itemModel.DownloadUrl   = $"{StringUtil.JoinUrlSegments(homeUrl, "/invoiceforms/LoadFormPdf")}?id={form.Id}";
                itemModel.DetailUrl     = $"/invoiceforms/GetFormHtml?id={form.Id}";
                itemModel.Explanation   = form.Explanation;

                ManageSourceCurrency(itemModel, form);

                collection.Forms.Add(itemModel);
            }

            collection.Year                = coll.Year;
            collection.Month               = coll.Month;
            collection.InvoiceFormTypeId   = coll.InvoiceFormTypeId;
            collection.Title               = coll.Name ?? "InvoiceFormType.CollectionName not set";
            collection.TotalPriceFormatted =
                StringUtil.FormatDecimal(collection.Forms.Sum(i => i.PrimaryCurrencyPriceWithoutVat));

            collection.CanApprove  = (coll.ApproveDt == null) && IsLogClear(coll.Log);
            collection.CanGenerate = (coll.ApproveDt == null);
            collection.IsGenerated = true;
            collection.IsApproved  = (coll.ApproveDt != null);
            collection.CanDelete   = !collection.IsApproved;

            GroupLogEntries(coll.Log, collection.Log);

            collection.HasWarnings    = collection.Log.Any(l => l.IsWarning);
            collection.HasErrors      = collection.Log.Any(l => l.IsError);
            collection.NeedsAttention = (!collection.IsApproved) && (collection.HasWarnings || collection.HasErrors);

            return(collection);
        }