// GET: Commission/Create
        public ActionResult Create()
        {
            CommissionMVCCreateModel commission = new CommissionMVCCreateModel();


            List <CompanyMVCModel> companies = SQLiteDataAccess.LoadCompanies();

            commission.Companies = companies.Select(x => new SelectListItem {
                Text = x.CompanyName, Value = x.Id.ToString()
            }).ToList();

            List <ClientMVCModel> clients = SQLiteDataAccess.LoadMVCClients();

            commission.Clients = clients.Select(x => new SelectListItem {
                Text = x.FullName, Value = x.Id.ToString()
            }).ToList();

            List <CreatorMVCModel> creators = SQLiteDataAccess.LoadCreators();

            commission.Creators = creators.Select(x => new SelectListItem {
                Text = x.FullName, Value = x.Id.ToString()
            }).ToList();


            List <ItemMVCModel> products = SQLiteDataAccess.LoadProducts();

            commission.Products = products.Select(x => new SelectListItem {
                Text = $"{ x.ItemName }, { x.Cost }zł / {x.ItemUnit} ", Value = x.Id.ToString()
            }).ToList();

            products.ForEach(x => commission.ProductQuantities.Add(1));
            products.ForEach(x => commission.ProductPrices.Add(x.Cost));

            return(View(commission));
        }
        public ActionResult Create(CommissionMVCCreateModel commissionModel)
        {
            try
            {
                string zipFolderPath  = $"{ Server.MapPath("~") }GeneratedZips";
                string docsFolderPath = $"{ Server.MapPath("~") }GeneratedDocuments";

                string documentsStorageFolder = Server.MapPath("~") + "GeneratedDocuments\\";
                string zipPath = $"{ Server.MapPath("~") }GeneratedZips\\result.zip";

                string uploadsFolderPath    = $"{ Server.MapPath("~") }Uploads";
                string uploadsStorageFolder = $"{uploadsFolderPath}\\";

                if (!Directory.Exists(uploadsFolderPath))
                {
                    Directory.CreateDirectory(uploadsFolderPath);
                }



                if (!Directory.Exists(docsFolderPath))
                {
                    Directory.CreateDirectory(docsFolderPath);
                }
                if (!Directory.Exists(zipFolderPath))
                {
                    Directory.CreateDirectory(zipFolderPath);
                }

                // Get Company information
                CompanyMVCModel documentCompany = GetCompanyInformation(commissionModel.CreationCompany, commissionModel.SelectedCompany);

                //Get Clients information
                List <ClientMVCModel> documentClients = GetClientsInformation(commissionModel.CreationClient, commissionModel.SelectedClients);


                //Get Products information
                List <ItemMVCModel> documentProducts = GetProductsInformation(commissionModel.SelectedProducts, commissionModel.ProductPrices, commissionModel.ProductQuantities);


                //Get Creator information
                CreatorMVCModel documentCreator = GetCreatorInformation(commissionModel.CommissionCreator, commissionModel.SelectedCreator);

                if (documentCompany == null || documentClients.Count == 0 || documentProducts.Count == 0 || documentCreator == null)
                {
                    return(RedirectToAction("Create", "Commission"));
                }


                string fileName;
                // Saving uploaded file
                if (commissionModel.PostedFile != null)
                {
                    fileName = Path.GetFileName(commissionModel.PostedFile.FileName);
                    commissionModel.PostedFile.SaveAs(Server.MapPath("~") + "Uploads\\" + fileName);
                }

                if (documentClients.Count > 1)
                {
                    foreach (var item in documentClients)
                    {
                        PersonalData personalData = new PersonalData(documentCompany.ConvertToCompanyModel(), item.ConvertToClientModel(), documentCreator.ConvertToCommissionCreatorModel());

                        string path = $"{documentsStorageFolder}{ documentCompany.CompanyName }_{ item.FullName.Replace("\"", "") }_{ DateTime.Today.ToShortDateString() }.docx";
                        path.Trim();
                        if (commissionModel.PostedFile != null)
                        {
                            DocumentHelper.GenerateDocumentFromTemplate($"{Server.MapPath("~")}\\Uploads\\{Path.GetFileName(commissionModel.PostedFile.FileName)}",
                                                                        path, personalData, documentProducts.ConvertToItemModel(), true);
                        }
                        else
                        {
                            DocumentHelper.GenerateNewDocument(path,
                                                               personalData, documentProducts.ConvertToItemModel());
                        }
                    }

                    ZipFile.CreateFromDirectory(docsFolderPath, zipPath);

                    // Append headers
                    Response.AppendHeader("content-disposition", $"attachment; filename={ documentCompany.CompanyName }_{ DateTime.Today.ToShortDateString() }.zip");
                    // Open/Save dialog
                    Response.ContentType = "application/octet-stream";
                    // Push it!
                    Response.TransmitFile(zipPath);
                    Response.End();
                }
                else
                {
                    PersonalData personalData = new PersonalData(documentCompany.ConvertToCompanyModel(), documentClients.First().ConvertToClientModel(), documentCreator.ConvertToCommissionCreatorModel());
                    string       path         = $"{documentsStorageFolder}{ documentCompany.CompanyName }_{ documentClients.First().FullName.Replace("\"", "") }_{ DateTime.Today.ToShortDateString() }.docx";

                    path.Trim();
                    if (commissionModel.PostedFile != null)
                    {
                        DocumentHelper.GenerateDocumentFromTemplate(uploadsStorageFolder + Path.GetFileName(commissionModel.PostedFile.FileName),
                                                                    path, personalData, documentProducts.ConvertToItemModel(), true);
                    }
                    else
                    {
                        DocumentHelper.GenerateNewDocument(path,
                                                           personalData, documentProducts.ConvertToItemModel());
                    }
                    // Append headers
                    Response.AppendHeader("content-disposition", $"attachment; filename={ documentCompany.CompanyName }_{ documentClients.First().FullName.Replace("\"", "") }_{ DateTime.Today.ToShortDateString() }.docx");
                    // Open/Save dialog
                    Response.ContentType = "application/octet-stream";
                    // Push it!
                    Response.TransmitFile(path);
                    Response.End();
                }



                DeleteFiles();



                return(RedirectToAction("Index", "Home"));
            }
            catch (Exception e)
            {
                DeleteFiles();
                string msg = e.Message;

                return(RedirectToAction("Create", "Commission"));
            }
        }