示例#1
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);
                    }
                }
            }));
        }
        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);
                        if (ext.EqualNoCase(".pdf"))
                        {
                            fullFilePath = new PDFFileHelper().Run(info, item).FullName;
                        }

                        bmp = ((Bitmap)Bitmap.FromFile(fullFilePath)).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)
                            {
                                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();
                }
            }));
        }