Exemple #1
0
        //var resolutions = new int[] { 96, 200, 300, 600, 1200 };
        public FileInfo Run2(FileInfo info, StateManager.VoucherItem item, int dpi = 300)
        {
            var images = info.DrawToImage(dpi);

            item.FileInfoList.AddRange(images);
            return(item.FileInfoList.FirstOrDefault());
        }
Exemple #2
0
        public FileInfo Run(FileInfo info, StateManager.VoucherItem item)
        {
            string fullFilePath = null;

            var pdf = new PdfAManager();

            int count = 0;

            foreach (Bitmap bmp in pdf.ExtractImagesFromPDF(info.FullName))
            {
                if (count++ == 0)
                {
                    fullFilePath = Path.ChangeExtension(info.FullName, ".jpg");
                    Global.IgnoreList.Add(fullFilePath);
                    bmp.Save(fullFilePath, ImageFormat.Jpeg);
                }
                else
                {
                    var path = Path.ChangeExtension(info.FullName, ".jpg");
                    path = path.ChangeFilePath((name) => name.Replace(".", string.Concat("_ ", count, ".")));
                    Global.IgnoreList.Add(path);
                    bmp.Crop2().Save(path, ImageFormat.Jpeg);
                    item.FileInfoList.Add(new FileInfo(path)); // Scanned Image
                }
                bmp.DisposeSf();
            }

            info.DeleteSafe();

            return(new FileInfo(fullFilePath));
        }
Exemple #3
0
 private void StateManager_NextItemExpected(object sender, ItemEventArgs e)
 {
     StateManager.VoucherItem vi = e.Item as StateManager.VoucherItem;
     if (vi != null)
     {
         MonitorForm.Message =
             string.Format("Expected voucher: \r\n ISO: {0}\r\nRetailer ID: {1}\r\nVoucher ID: {2}\r\nSite code: {3}",
                           vi.CountryID,
                           vi.RetailerID.ToString().SplitJoinSafe(3, " "),
                           vi.VoucherID.ToString().SplitJoinSafe(3, " "),
                           vi.SiteCode.SplitJoinSafe(3, " "));
     }
 }
Exemple #4
0
        private void ImageIconControl_Click(object sender, EventArgs e)
        {
            ItemControl selectedItemCnt = (ItemControl)sender;

            selectedItemCnt.PlayEffect();

            StateManager.VoucherItem vi = selectedItemCnt.Item as StateManager.VoucherItem;
            if (vi != null)
            {
                MonitorForm.Message =
                    string.Format("Selected voucher: \r\n ISO: {0}\r\nRetailer ID: {1}\r\nVoucher ID: {2}\r\nSite code: {3}",
                                  vi.CountryID,
                                  vi.RetailerID.ToString().SplitJoinSafe(3, " "),
                                  vi.VoucherID.ToString().SplitJoinSafe(3, " "),
                                  vi.SiteCode.SplitJoinSafe(3, " "));
            }
        }
Exemple #5
0
        /// <summary>
        /// EXAMPLE! NOT IN USE NOW
        /// </summary>
        /// <returns></returns>
        private static Action <TaskProcessOrganizer <string> .TaskItem> CreateScanActionORIGINAL()
        {
            const int TRIES = 10;

            return(new Action <TaskProcessOrganizer <string> .TaskItem>((o) =>
            {
                var fullFilePath = o.Item;

                Bitmap bmp = null;
                Bitmap bmpBarcode = null;
                BarcodeData data = null;

                string barcode = null;
                string siteCode = null;

                StateManager.Item item = null;

                try
                {
                    var info = new FileInfo(fullFilePath);

                    if (info.Exists && !info.IsReadOnly(TRIES))
                    {
                        // ".tif"
                        var ext = Path.GetExtension(info.FullName);

#if PROC_TIFF_AS_JPEG
                        if (ext == ".tif")
                        {
                            int c = 0;

                            foreach (var im in info.FullName.TiffGetAllImages())
                            {
                                var name = info.FullName.ChangeFilePath((n) => n.Replace(ext, c++ + ext));
                                im.Save(name);
                                im.DisposeSf();
                            }

                            return;
                        }
#endif
                        bmp = ((Bitmap)Bitmap.FromFile(info.FullName)).Crop2();

                        item = StateManager.Default.ProcessItem_Begin(!MainForm.ms_ImportCoversheet);
                        item.FullFileName = fullFilePath;
                        item.FileInfoList.Add(new FileInfo(fullFilePath)); // Scanned Image

                        if (!MainForm.ms_ImportCoversheet)
                        {
                            StateManager.VoucherItem vitem = (StateManager.VoucherItem)item;
                            FileInfo barcFilePath = null;
                            Rectangle rect = Rectangle.Empty;
                            CommonTools.ParseVoucherImage(ref bmp, ref bmpBarcode, out rect, ref barcode, BarcodeTypeEnum.BT_All);

                            vitem.Barcode = barcode;

                            if (vitem.HasBarcode)
                            {
                                string user = string.Concat("Country: ", Program.currentUser.CountryID, " User: "******"{0:dd-MM-yyyy hh:mm}", DateTime.Now);
                                        gr.DrawString(str, font, Brushes.Black, new PointF(10, 10));
                                        gr.DrawString(u.Cast <string>(), font, Brushes.Black, new PointF(10, 25));
                                    }
                                }, user);

                                var coverArea = StateSaver.Default.Get <Rectangle>(Strings.VOUCHERCOVERREGION);
                                if (!coverArea.IsEmpty)
                                {
                                    var size = StateSaver.Default.Get <int>(Strings.PIXELSIZE, 5);
                                    bmp.Pixellate(coverArea);
                                }

                                List <BarcodeConfig> barcodeLayouts = StateSaver.Default.Get <List <BarcodeConfig> >(Strings.LIST_OF_BARCODECONFIGS);

                                foreach (var cfg in barcodeLayouts)
                                {
                                    if (cfg.ParseBarcode(barcode, ref data))
                                    {
                                        break;
                                    }
                                }

                                if (data == null)
                                {
                                    throw new ApplicationException("Can not find barcode");
                                }

                                item.CountryID = data.CountryID;
                                vitem.RetailerID = data.RetailerID;
                                vitem.VoucherID = data.VoucherID;
                                vitem.Barcode = barcode;

                                var barcodePath = fullFilePath.ChangeFilePath((name) => name.Replace(".", "_barcode."));
                                Global.IgnoreList.Add(barcodePath);

                                bmpBarcode.DrawOnImage((gr, s) =>
                                {
                                    using (var font = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Regular))
                                    {
                                        var str = Convert.ToString(s);
                                        gr.DrawString(str, font, Brushes.Red, new PointF(10, 10));
                                    }
                                }, barcode);

                                bmpBarcode.Save(barcodePath, bmp.RawFormat);

                                barcFilePath = new FileInfo(barcodePath); // Scanned Barcode
                                vitem.FileInfoList.Add(barcFilePath);     // Scanned Barcode Image
                            }
                        }
                        else
                        {
                            MainForm.ms_ImportCoversheet = false;
                            MainForm.Default.InvokeSf(() => MainForm.Default.cbCoversheet.Checked = false);
                        }

                        if (item.CountryID == 0)
                        {
                            item.CountryID = MainForm.ms_DefaultCountryId;
                        }

                        if (!ext.EqualNoCase(".tif"))
                        {
                            bmp.Save(fullFilePath, ImageFormat.Jpeg);
                        }

                        if (item.Thumbnail == null)
                        {
                            item.Thumbnail = bmp.GetThumbnailImage(45, 45, () => false, IntPtr.Zero);
                        }

                        var sec = new CertificateSecurity(X509FindType.FindBySerialNumber, Strings.CERTNUMBER, StoreLocation.LocalMachine);
                        if (sec.Loaded)
                        {
                            var signFilePath = new FileInfo(Path.ChangeExtension(fullFilePath, ".sgn"));
                            item.Signature = sec.SignData(bmp.ToArray());
                            File.WriteAllBytes(signFilePath.FullName, item.Signature);
                            item.FileInfoList.Add(signFilePath);
                        }

                        item.State = StateManager.eState.OK;
                        item.Message = "";
                    }
                }
                catch (Exception ex)
                {
                    if (item != null)
                    {
                        item.State = StateManager.eState.Err;
                        item.Message = ex.Message;
                    }
                    var scex = new ScanException(ex, data)
                    {
                        SiteCode = siteCode,
                        FilePath = fullFilePath
                    };

                    if (Error != null)
                    {
                        Error(null, new ThreadExceptionEventArgs(ex));
                    }
                }
                finally
                {
                    bmp.DisposeSf();
                    bmpBarcode.DisposeSf();

                    MainForm.Default.m_MainContext.Post(MainForm.Default.ShowItemScannedCallback, item);

                    try
                    {
                        StateManager.Default.ProcessItem_End(item);
                    }
                    catch (Exception ex0)
                    {
                        if (item != null)
                        {
                            item.State = StateManager.eState.Err;
                            item.Message = ex0.Message;
                        }

                        var scex = new ScanException(ex0, data)
                        {
                            SiteCode = siteCode,
                            FilePath = fullFilePath
                        };

                        if (Error != null)
                        {
                            Error(null, new ThreadExceptionEventArgs(scex));
                        }
                    }
                }
            }));
        }
        public Action <TaskProcessOrganizer <string> .TaskItem> GetAction()
        {
            return(new Action <TaskProcessOrganizer <string> .TaskItem>((o) =>
            {
                var fullFilePath = o.Item;

                Bitmap bmp = null;
                Bitmap bmpBarcode = null;
                BarcodeData data = null;

                string barcode = null;
                string siteCode = null;

                StateManager.VoucherItem item = (StateManager.VoucherItem)StateManager.Default.ProcessItem_Begin(true);

                try
                {
                    var info = new FileInfo(fullFilePath);

                    if (info.Exists && !info.IsReadOnly(TRIES))
                    {
                        // ".tif"
                        var ext = Path.GetExtension(info.FullName);

                        bmp = ((Bitmap)Bitmap.FromFile(info.FullName)).Crop2();
                        item.FullFileName = fullFilePath;
                        item.FileInfoList.Add(new FileInfo(fullFilePath)); // Scanned Image

                        StateManager.VoucherItem vitem = (StateManager.VoucherItem)item;
                        FileInfo barcFilePath = null;
                        Rectangle rect = Rectangle.Empty;
                        CommonTools.ParseVoucherImage(ref bmp, ref bmpBarcode, out rect, ref barcode, BarcodeTypeEnum.BT_All);
                        vitem.Barcode = barcode;

                        string user = string.Concat("Country: ", Program.currentUser.CountryID, " User: "******"{0:dd-MM-yyyy hh:mm}", DateTime.Now);
                                gr.DrawString(str, font, Brushes.Black, new PointF(10, 10));
                                gr.DrawString(u.Cast <string>(), font, Brushes.Black, new PointF(10, 25));
                            }
                        }, user);

                        var coverArea = StateSaver.Default.Get <Rectangle>(Strings.VOUCHERCOVERREGION);
                        if (!coverArea.IsEmpty)
                        {
                            var size = StateSaver.Default.Get <int>(Strings.PIXELSIZE, 5);
                            bmp.Pixellate(coverArea);
                        }

                        if (vitem.HasBarcode)
                        {
                            List <BarcodeConfig> barcodeLayouts = StateSaver.Default.Get <List <BarcodeConfig> >(Strings.LIST_OF_BARCODECONFIGS);

                            foreach (var cfg in barcodeLayouts)
                            {
                                if (cfg.ParseBarcodeSafe(barcode, ref data))
                                {
                                    break;
                                }
                            }

                            if (data == null)
                            {
                                throw new ApplicationException("Barcode invalid");
                            }

                            vitem.CountryID = data.CountryID;
                            vitem.RetailerID = data.RetailerID;
                            vitem.VoucherID = data.VoucherID;
                            vitem.Barcode = barcode;

                            var barcodePath = fullFilePath.ChangeFilePath((name) => name.Replace(".", "_barcode."));
                            Global.IgnoreList.Add(barcodePath);

                            bmpBarcode.DrawOnImage((gr, s) =>
                            {
                                using (var font = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Regular))
                                {
                                    var str = Convert.ToString(s);
                                    gr.DrawString(str, font, Brushes.Red, new PointF(10, 10));
                                }
                            }, barcode);

                            bmpBarcode.Save(barcodePath, bmp.RawFormat);

                            barcFilePath = new FileInfo(barcodePath); // Scanned Barcode
                            vitem.FileInfoList.Add(barcFilePath);     // Scanned Barcode Image
                        }

                        item = StateManager.Default.AddTransferFileItem(item);

                        if (item.CountryID == 0)
                        {
                            item.CountryID = MainForm.ms_DefaultCountryId;
                        }

                        if (!ext.EqualNoCase(".tif"))
                        {
                            bmp.Save(fullFilePath, ImageFormat.Jpeg);
                        }

                        if (item.Thumbnail == null)
                        {
                            item.Thumbnail = bmp.GetThumbnailImage(45, 45, () => false, IntPtr.Zero);
                        }

                        var sec = new CertificateSecurity(X509FindType.FindBySerialNumber, Strings.CERTNUMBER, StoreLocation.LocalMachine);
                        if (sec.Loaded)
                        {
                            var signFilePath = new FileInfo(Path.ChangeExtension(fullFilePath, ".sgn"));
                            item.Signature = sec.SignData(bmp.ToArray());
                            File.WriteAllBytes(signFilePath.FullName, item.Signature);
                            item.FileInfoList.Add(signFilePath);
                            Global.IgnoreList.Add(signFilePath.FullName);
                        }

                        item.State = StateManager.eState.OK;
                        item.Message = "";
                        StateManager.Default.CompleteItem(item);
                    }
                }
                catch (Exception ex)
                {
                    item.State = StateManager.eState.Err;
                    item.Message = ex.Message;

                    var scex = new ScanException(ex, data)
                    {
                        SiteCode = siteCode,
                        FilePath = fullFilePath
                    };

                    int count = StateManager.Default.SetItemWithErr();
                    DelegateHelper.PostShowItemsWithErrCallback(count);
                    DelegateHelper.FireError(this, ex);
                }
                finally
                {
                    bmp.DisposeSf();
                    bmpBarcode.DisposeSf();
                    DelegateHelper.PostItemScannedCallback(item);
                    StateManager.Default.ShowNextItemExpected();
                }
            }));
        }
Exemple #7
0
        public Action <TaskProcessOrganizer <string> .TaskItem> GetAction()
        {
            return(new Action <TaskProcessOrganizer <string> .TaskItem>((o) =>
            {
                var fullFilePath = o.Item;

                Bitmap bmp = null;
                StateManager.VoucherItem item = (StateManager.VoucherItem)StateManager.Default.ProcessItem_Begin(true);

                try
                {
                    var info = new FileInfo(fullFilePath);

                    if (info.Exists && !info.IsReadOnly(TRIES))
                    {
                        Global.IgnoreList.Add(fullFilePath);
                        // ".tif"
                        var ext = Path.GetExtension(info.FullName);
                        if (ext.EqualNoCase(".pdf"))
                        {
                            var helper = new PDFFileHelper();
                            var finfo = helper.Run(info, item);
                            fullFilePath = (finfo != null) ? finfo.FullName : "";
                        }

                        bmp = ((Bitmap)Bitmap.FromFile(fullFilePath));

                        var useCrop = StateSaver.Default.Get <bool>(Strings.CROPIMAGE);
                        if (useCrop)
                        {
                            bmp = bmp.Crop2();
                        }

                        item.FileInfoList.Add(new FileInfo(fullFilePath)); // Scanned Image

                        item.FullFileName = fullFilePath;

                        StateManager.VoucherItem vitem = (StateManager.VoucherItem)item;

#if DRAW_ON_VOUCHER
                        bmp.DrawOnImage((gr) =>
                        {
                            var str = string.Format("{0:dd-MM-yyyy hh:mm}", DateTime.Now);
                            ///string user = Program.currentUser.ToString();
                            var an = StateSaver.Default.Get <AssemblyName>(Strings.VERSION);

                            using (var font = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Regular))
                            {
                                gr.DrawString(str, font, Brushes.Black, new PointF(10, 10));
                                gr.DrawString(an.Name, font, Brushes.Black, new PointF(10, 25));
                                gr.DrawString(an.Version.ToString(), font, Brushes.Black, new PointF(10, 40));
                            }
                        });
#endif

#if COVER_CCD
                        var coverArea = StateSaver.Default.Get <Rectangle>(Strings.VOUCHERCOVERREGION);
                        if (!coverArea.IsEmpty)
                        {
                            var size = StateSaver.Default.Get <int>(Strings.PIXELSIZE, 5);
                            bmp.Pixellate(coverArea);
                        }
#endif
                        string site;
                        int location;
                        if (!VPrinting.Common.CommonTools.ParseSiteCode(info.Name, out site, out location))
                        {
                            throw new Exception("Wrong sitecode");
                        }

                        var vinfo = ServiceDataAccess.Instance.FindVoucherTRSBySiteCode(site, location);
                        if (vinfo == null || !vinfo.IsValid)
                        {
                            vinfo = ServiceDataAccess.Instance.FindVoucherPRBySiteCode(site, location);
                            if (!vinfo.IsValid)
                            {
                                throw new Exception("Cannot find voucher by sitecode");
                            }
                        }

                        item.CountryID = vinfo.IsoId;
                        vitem.RetailerID = vinfo.RetailerId;
                        vitem.VoucherID = vinfo.VoucherId;
                        vitem.Barcode = "";
                        vitem.SiteCode = string.Concat(site, location);

                        if (item.CountryID == 0)
                        {
                            item.CountryID = MainForm.ms_DefaultCountryId;
                        }
#if SAVE_VOUCHER
                        if (!ext.EqualNoCase(".tif"))
                        {
                            bmp.Save(fullFilePath, ImageFormat.Jpeg);
                        }
#endif

                        if (item.Thumbnail == null)
                        {
                            item.Thumbnail = bmp.GetThumbnailImage(45, 45, () => false, IntPtr.Zero);
                        }

                        if (StateSaver.Default.Get <bool>(Strings.USE_VCOVER))
                        {
                            using (WaitObject obj = new WaitObject(item.FileInfoList))
                            {
                                var ptr = StateSaver.Default.Get <IntPtr>(Strings.VCOVER_FUNC);
                                var time = StateSaver.Default.Get <TimeSpan>(Strings.VCOVER_TIMEOUT, TimeSpan.FromMinutes(10));
                                var dlg = ptr.GetDelegate <CallVCoverService_ReadDataDelegate>();
                                dlg.DynamicInvoke(obj);
                                if (!obj.WaitOne(time))
                                {
                                    throw new ApplicationException("VCover timeout");
                                }
                                if (obj.Err != null)
                                {
                                    throw obj.Err;
                                }
                            }
                        }

                        var certificateSigning = StateSaver.Default.Get <bool>(Strings.CERTIFICATE_SIGNING_AVAILABLE);
                        if (certificateSigning)
                        {
                            var crInfo = new PdfCreationInfo()
                            {
                                Title = string.Concat("Voucher ", vitem.VoucherID),
                                Subject = string.Concat("Retailer ", vitem.RetailerID),
                                Author = string.Concat("PTF ", StateSaver.Default.Get <string>(Strings.Certigicate_COUNTRY)),
                                Creator = string.Concat("PTF ", StateSaver.Default.Get <string>(Strings.Certigicate_LOCATION)),
                            };

                            var signInfo = new PdfSignInfo()
                            {
                                pfxFilePath = StateSaver.Default.Get <string>(Strings.COUNTRY_CERTIFICATE_PATH),
                                pfxKeyPass = StateSaver.Default.Get <string>(Strings.COUNTRY_CERTIFICATE_PASS),
                                DocPass = null,
                                SignImagePath = StateSaver.Default.Get <string>(Strings.PTFLogoFileFullPath),
                                ReasonForSigning = string.Concat("Signing electronic copy of voucher ", vitem.Barcode),
                                Location = StateSaver.Default.Get <string>(Strings.Certigicate_LOCATION)
                            };

                            var prtGetMetaData = StateSaver.Default.Get <IntPtr>(Strings.Certigicate_METADATA_FUNC);
                            if (prtGetMetaData != IntPtr.Zero)
                            {
                                crInfo.MetaData = signInfo.MetaData = prtGetMetaData.GetDelegate <GetMetaDataDelegate>().DynamicInvoke(this, vinfo.IsoId, 0, vinfo.RetailerId).cast <ArrayList>();
                                var str = string.Format("{0:dd-MM-yyyy hh:mm}", DateTime.Now);
                                var an = StateSaver.Default.Get <AssemblyName>(Strings.VERSION);
                                crInfo.MetaData.Insert(0, new Tuple <string, string>("", ""));
                                crInfo.MetaData.Insert(0, new Tuple <string, string>("Voucher number", vinfo.VoucherId.ToString()));

                                crInfo.MetaData.Add(new Tuple <string, string>("", ""));
                                crInfo.MetaData.Add(new Tuple <string, string>("Operator", Program.currentUser.Username));
                                crInfo.MetaData.Add(new Tuple <string, string>("App.name", an.Name));
                                crInfo.MetaData.Add(new Tuple <string, string>("App.version", an.Version.ToString()));
                                crInfo.MetaData.Add(new Tuple <string, string>("Create at", str));
                            }

                            var pdfFileName = pdfFileAccess.Instance.CreateSignPdf(bmp, vitem.Barcode, vitem.RetailerID, vitem.VoucherID, crInfo, signInfo);
                            item.FileInfoList.Add(new FileInfo(pdfFileName)); // Signed Image
                        }

                        item.State = StateManager.eState.OK;
                        item.Message = "";
                        StateManager.Default.CompleteItem(item);
                    }
                }
                catch (Exception ex)
                {
                    item.State = StateManager.eState.Err;
                    item.Message = ex.Message;

                    int count = StateManager.Default.SetItemWithErr();
                    DelegateHelper.PostShowItemsWithErrCallback(count);
                    DelegateHelper.FireError(this, ex);
                }
                finally
                {
                    bmp.DisposeSf();

                    DelegateHelper.PostItemScannedCallback(item);

                    try
                    {
                        if (!item.IsSetup)
                        {
                            using (var mngr = new AsyncFormManager <RetailerForm>("Enter voucher details"))
                            {
                                mngr.Result = item;
                                mngr.RunWait();
                                if (!item.IsSetup)
                                {
                                    throw new ApplicationException("Cannot find barcode.");
                                }
                                item.State = StateManager.eState.OK;
                            }
                        }

                        StateManager.Default.AddNewItem(item);
                    }
                    catch (Exception ex0)
                    {
                        item.State = StateManager.eState.Err;
                        item.Message = ex0.Message;
                        DelegateHelper.FireError(this, ex0);
                    }
                }
            }));
        }