예제 #1
0
        public Error InsertOrUpdateCompany(CompanyModel company, UserModel user, string lockGuid)
        {
            var error = validateModel(company);

            if (!error.IsError)
            {
                // Check that the lock is still current
                if (!db.IsLockStillValid(typeof(Company).ToString(), company.Id, lockGuid))
                {
                    error.SetError(EvolutionResources.errRecordChangedByAnotherUser, "Name");
                }
                else
                {
                    Company temp = null;
                    if (company.Id != 0)
                    {
                        temp = db.FindCompany(company.Id);
                    }
                    if (temp == null)
                    {
                        temp = new Company();
                    }

                    var before = new Company();
                    mapToEntity(temp, before);

                    mapToEntity(company, temp);

                    db.InsertOrUpdateCompany(temp);
                    company.Id = temp.Id;

                    logChanges(before, temp, user);

                    if (temp.IsParentCompany)
                    {
                        // Company is the parent company, so switch this setting off on all other companies
                        foreach (var nonParentCompany in db.FindCompanies()
                                 .Where(c => c.Id != company.Id &&
                                        c.IsParentCompany == true)
                                 .ToList())
                        {
                            CompanyModel model = new CompanyModel {
                                Id = nonParentCompany.Id
                            };

                            LockCompany(model);      // Forces other users to refresh as a result of this change

                            nonParentCompany.IsParentCompany = false;
                            db.InsertOrUpdateCompany(nonParentCompany);
                        }
                    }

                    // Create company folders
                    MediaService.MediaService mediaService = new MediaService.MediaService(db);
                    mediaService.CreateCompanyFolders(company.Id);
                }
            }
            return(error);
        }
예제 #2
0
        public string CreateBarCode(string barCode, bool bHtml = false, bool bRegisterForDelete = false)
        {
            string barCodeFile = "",
                                                tempBc = barCode.Trim();

            if (!string.IsNullOrEmpty(tempBc))
            {
                using (var bc = new Barcode()) {
                    try {
                        bc.IncludeLabel = true;
                        var image = bc.Encode(TYPE.CODE128, barCode, 220, 50);

                        var mediaService  = new MediaService.MediaService(db);
                        var barCodeFolder = GetConfigurationSetting("MediaFolder", "") + "\\Barcodes";

                        var error = MediaService.MediaService.CreateDirectory(barCodeFolder);
                        if (!error.IsError)
                        {
                            barCodeFile = $"{tempBc}.png";
                            var temp = $"{barCodeFolder}\\{barCodeFile}";

                            if (bRegisterForDelete)
                            {
                                mediaService.AddFileToLog(temp, 20);                            // So it gets cleaned up later
                            }
                            image.Save(temp, ImageFormat.Png);

                            if (bHtml)
                            {
                                barCodeFolder = GetConfigurationSetting("MediaHttp", "") + "/Barcodes";
                                temp          = $"{barCodeFolder}/{barCodeFile}";
                            }
                            barCodeFile = temp;
                        }
                    } catch { }
                }
            }
            return(barCodeFile);
        }
예제 #3
0
        public Error CreateDocumentPdf(Dictionary <string, string> headerProperties,
                                       List <Dictionary <string, string> > records,
                                       string templateFile,
                                       string outputFile,
                                       int maxItems = Int32.MaxValue)
        {
            var error = new Error();

            try {
                MediaService.MediaService mediaService = new MediaService.MediaService(db);

                string tempFile = MediaService.MediaService.GetTempFile(".html");

                // Perform the substitutions
                TemplateService.TemplateService ts = new TemplateService.TemplateService();

                var templateProperties = new TemplateProperties {
                    DocumentHeaderClass = "DocumentHeader",
                    PageHeaderClass     = "PageHeader",
                    ItemClass           = "ItemSection",
                    PageFooterClass     = "PageFooter",
                    DocumentFooterClass = "DocumentFooter"
                };

                error = ts.LoadTemplateFile(templateFile, templateProperties);

                if (!error.IsError)
                {
                    // Insert the lines
                    int maxItemsPerPage      = 0,
                        maxItemsBeforeFooter = ts.MaxItemsBeforeFooter,
                        pageNo      = 1,
                        itemsOnPage = 0;

                    headerProperties.AddProperty("PAGENO", pageNo);
                    ts.AddContent(templateProperties.DocumentHeaderClass, headerProperties);
                    ts.AddContent(templateProperties.PageHeaderClass, headerProperties);

                    // Add items
                    Dictionary <string, string> dict = new Dictionary <string, string>();

                    int itemCount = 1;
                    foreach (var line in records)
                    {
                        maxItemsPerPage = (pageNo == 1 ? ts.MaxItemsOnFirstPage : ts.MaxItemsOnSecondPage);
                        dict            = line;
                        if (itemsOnPage >= maxItemsPerPage)
                        {
                            headerProperties.AddProperty("PAGENO", pageNo);
                            ts.AddContent(templateProperties.PageFooterClass, headerProperties);      // Includes css page-break

                            pageNo++;
                            headerProperties.AddProperty("PAGENO", pageNo);
                            ts.AddContent(templateProperties.PageHeaderClass, headerProperties);
                            itemsOnPage = 0;
                        }

                        ts.AddContent(templateProperties.ItemClass, dict);
                        itemsOnPage++;
                        itemCount++;
                        if (itemCount > maxItems)
                        {
                            break;
                        }
                    }

                    // End of document
                    if (itemsOnPage > maxItemsBeforeFooter)
                    {
                        // Not enough room left on page, so break
                        ts.AddContent(templateProperties.PageFooterClass, headerProperties);      // Includes css page-break

                        pageNo++;
                        headerProperties.AddProperty("PAGENO", pageNo);
                        ts.AddContent(templateProperties.PageHeaderClass, headerProperties);
                        itemsOnPage = 0;
                    }

                    headerProperties.AddProperty("PAGECOUNT", pageNo);
                    ts.AddContent(templateProperties.DocumentFooterClass, headerProperties, dict);

                    // Write the html file
                    StreamWriter sw = new StreamWriter(tempFile);
                    sw.Write(ts.Render(headerProperties));
                    sw.Close();

                    // Create a folder for it
                    MediaService.MediaService.CreateDirectory(outputFile.FolderName());

                    // Convert it
                    error = convertHtmlFileToPDF(tempFile, outputFile);

                    MediaService.MediaService.DeleteFile(tempFile);
                    if (error.IsError)
                    {
                        MediaService.MediaService.DeleteFile(outputFile);
                    }
                }
            } catch (Exception e1) {
                error.SetError(e1);
            }

            return(error);
        }