コード例 #1
0
        public void ProcessItem_End(Item item)
        {
            if (item == null)
            {
                return;
            }

            lock (this)
            {
                try
                {
                    var vitem = item as VoucherItem;
                    if (vitem == null)
                    {
                        m_ItemCollection.Add(item);
                        m_CurrentItem = item;
                        FireNewItemAdded(item);
                    }
                    else
                    {
                        #region Voucher

                        switch (Mode)
                        {
                        /// No document. No order.
                        /// Everything is going in.
                        /// Must be a barcode.
                        case eMode.Barcode:
                        {
                            #region

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

                            m_CurrentItem       = new VoucherItem();
                            m_CurrentItem.JobID = 1;

                            if (VoucherMustExist)
                            {
                                var da = ServiceDataAccess.Instance;
                                vitem.SiteCode = da.FindVoucher(vitem.CountryID, vitem.VoucherID, vitem.VoucherIDCD);

                                if (string.IsNullOrWhiteSpace(vitem.SiteCode))
                                {
                                    throw new FileInfoApplicationException("Cannot find sitecode.")
                                          {
                                              Info = vitem.FileInfoList.FirstOrDefault(fi => !fi.Name.Contains("barcode"))
                                          };
                                }
                            }
                            m_ItemCollection.Add(m_CurrentItem);
                            FireNewItemAdded(m_CurrentItem);

                            ((VoucherItem)m_CurrentItem).CopyFromWithBarcode(vitem);

                            break;
                            #endregion
                        }

                        /// There is document.
                        /// They scan. Multi scan TIFF document.
                        /// If there is a barcode the barcode has been validated
                        /// Otherwise it has been inserted by using expected barcode details.
                        case eMode.TransferFile:
                        {
                            #region

                            if (vitem.IsSetup)
                            {
                                var itm = m_ItemCollection.FindFirstOrDefault((ii) => (ii.State == eState.NA && ii.Forsed) || ((ii.State == eState.NA) && ((VoucherItem)ii).Equals(vitem)));
                                if (itm == null)
                                {
                                    throw new FileInfoApplicationException(
                                              string.Concat("Cannot match voucher.\r\nCountry: ", vitem.CountryID, "\r\nRetailer: ",
                                                            vitem.RetailerID, "\r\nVoucher: ", vitem.VoucherID, "\r\nState: NA"))
                                          {
                                              Info = vitem.FileInfoList.FirstOrDefault(fi => !fi.Name.Contains("barcode"))
                                          };
                                }

                                m_CurrentItem = itm;
                                ((VoucherItem)m_CurrentItem).CopyFromWithBarcode(vitem);
                            }
                            else
                            {
                                var itm = m_ItemCollection.FindFirstOrDefault((ii) => (ii.State == eState.NA && !ii.Ignored));
                                if (itm == null)
                                {
                                    throw new ApplicationException("No more vouchers are expected.");
                                }
                                m_CurrentItem = itm;
                                ((VoucherItem)m_CurrentItem).CopyFromNoBarcode(vitem);
                            }
                            break;
                            #endregion
                        }

                        /// No order.
                        /// Barcode is mandatory
                        /// Must be sitecode
                        /// System calls TRS to resolve barcode
                        case eMode.Sitecode:
                        {
                            #region
                            m_CurrentItem       = new VoucherItem();
                            m_CurrentItem.JobID = 1;
                            m_ItemCollection.Add(m_CurrentItem);
                            FireNewItemAdded(m_CurrentItem);
                            ((VoucherItem)m_CurrentItem).CopyFromNoBarcode(vitem);
                            break;
                            #endregion
                        }

                        default:
                            throw new NotImplementedException();
                        }

                        #endregion
                    }
                }
                finally
                {
                    if (m_CurrentItem != null)
                    {
                        m_CurrentItem.State = eState.OK;

                        FireCurrentItemCompleted(m_CurrentItem);

                        m_CurrentItem.FireUpdated();

                        var nextItem = m_ItemCollection.FindFirstOrDefault((ii) => ii.State == eState.NA && !ii.Ignored);

                        FireNextItemExpected(nextItem);
                    }
                }
            }
        }
コード例 #2
0
 public InputForm(AsyncFormManager <InputForm> manager)
 {
     InitializeComponent();
     Target = manager;
 }
コード例 #3
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);
                    }
                }
            }));
        }
コード例 #4
0
        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);

                        if (!vitem.HasBarcode)
                        {
                            using (var mngr = new AsyncFormManager <RetailerForm>("Enter voucher details"))
                            {
                                mngr.Result = item;
                                mngr.RunWait();
                                if (!item.IsSetup)
                                {
                                    throw new ApplicationException("Cannot find barcode.");
                                }
                                vitem.State = StateManager.eState.OK;
                            }
                        }
                        else
                        {
                            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("Barcode invalid");
                            }

                            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
                        }

                        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 = "";
                        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.AddNewItem(item);
                }
            }));
        }