/// /// Copyright (c) 2016-2018 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Windows.Forms; using SharedDatabase; using SharedDatabase.Entities; namespace Workplace.CheckItems { public partial class Barcode : UserControl, ICheckItem { public override string ToString() { return string.Format("Barcode: {0} {1}", IxPolozky, Part.Name); } WorkplaceDlg parent; public Part Part { get; set; } public int IxPolozky { get; set; } public IList BlackListedCodes { get { return blackListedCodes; } set { blackListedCodes = value; blackListLabel.Visible = (blackListedCodes != null) && (blackListedCodes.Count > 0); } } IList blackListedCodes; public string ScannedCode { get { return scannedCodeTextBox.Text; } set { scannedCodeTextBox.Text = value; } } Barcode() { InitializeComponent(); blackListedCodes = new List(); } public Barcode(WorkplaceDlg parent, Part part) : this() { this.parent = parent; this.Part = part; scannedCodeTextBox.Text = (part.DefaultCode != null) ? part.DefaultCode : string.Empty; kodMaterialuLabel.Text = part.Name; descriptionMaterialuLabel.Text = part.Description; if (part.CodeForm == (sbyte)CodeForm.Barcode) { try { Stream imageStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Workplace.Barcode.png"); pictureBox1.Image = new Bitmap(imageStream); } catch {} } } public void ClearCode() { if (Part.CodeType != (sbyte)CodeType.Silicon) { scannedCodeTextBox.Text = (Part.DefaultCode != null) ? Part.DefaultCode : string.Empty; } } public void SubmitCode(string code) { scannedCodeTextBox.Text = code; scannedCodeTextBox_KeyPress(null, new KeyPressEventArgs('\r')); } public void ResetFocus() { setFocusButton.Text = ""; } public void SetFocus() { ClearCode(); setFocusButton.Text = ">"; scannedCodeTextBox.Focus(); } private void setFocusButton_Click(object sender, EventArgs e) { setFocusButton.Text = ">"; parent.OnFocusPressed(this, new FocusPressedEventArgs(IxPolozky, Part.CodeLocation)); } private void scannedCodeTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != '\r') return; if (blackListedCodes != null) { if (null != blackListedCodes.FirstOrDefault(x => x.Equals(scannedCodeTextBox.Text))) { parent.OnBarcodeReceived(this, new BarcodeReceivedEventArgs(scannedCodeTextBox.Text, IxPolozky, Part.CodeLocation, PartError.BlacklistedPart)); return; } } PartError error = PartError.None; if (string.IsNullOrEmpty(scannedCodeTextBox.Text)) { error = PartError.FieldEmpty; } else { switch ((CodeType)Part.CodeType) { case CodeType.FlowtubeNr: if ((scannedCodeTextBox.Text.Length < 3) || !scannedCodeTextBox.Text.Substring(0, 3).Equals(Part.Name.Substring(Part.Name.Length - 3))) { error = PartError.WrongPart; } break; case CodeType.FlowtubeNrLU: if ((scannedCodeTextBox.Text.Length < Part.Name.Length || !scannedCodeTextBox.Text.Substring(0, Part.Name.Length).Equals(Part.Name))) { error = PartError.WrongPart; } break; case CodeType.BatchNrContainingPartName: if (!scannedCodeTextBox.Text.Contains(Part.Name)) { error = PartError.WrongPart; } break; case CodeType.DateMMYY: if (Part.LifetimeManagement == LifetimeManagement.OneYear) { int mmyy; if (!int.TryParse(scannedCodeTextBox.Text, out mmyy)) { error = PartError.WrongPart; } else { int batteryMonthsSince2000 = 12 * (mmyy % 100) + (mmyy / 100); int monthsSince2000 = 12 * (DateTime.Now.Year - 2000) + DateTime.Now.Month; int age = monthsSince2000 - batteryMonthsSince2000; if (age < 0 || age > 12 + parent.ExtraBatteryLifetime) { error = PartError.WrongPart; } } } break; } } parent.OnBarcodeReceived(this, new BarcodeReceivedEventArgs(scannedCodeTextBox.Text, IxPolozky, Part.CodeLocation, error)); } private void Polozka_MouseClick(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Right) return; LocalSettingsFormForPolozka dlg = new LocalSettingsFormForPolozka(); if (dlg.ShowDialog() != DialogResult.OK) return; if (string.IsNullOrEmpty(dlg.BlackListFileName)) { if (this.IxPolozky == Program.LocalSettings.BlackListIx1) { Program.LocalSettings.BlackListIx1 = 0; Program.LocalSettings.BlackListFile1 = string.Empty; } if (this.IxPolozky == Program.LocalSettings.BlackListIx2) { Program.LocalSettings.BlackListIx2 = 0; Program.LocalSettings.BlackListFile2 = string.Empty; } if (this.IxPolozky == Program.LocalSettings.BlackListIx3) { Program.LocalSettings.BlackListIx3 = 0; Program.LocalSettings.BlackListFile3 = string.Empty; } } else if (Program.LocalSettings.BlackListIx1 == this.IxPolozky) { Program.LocalSettings.BlackListIx1 = this.IxPolozky; Program.LocalSettings.BlackListFile1 = dlg.BlackListFileName; } else if (Program.LocalSettings.BlackListIx2 == this.IxPolozky) { Program.LocalSettings.BlackListIx2 = this.IxPolozky; Program.LocalSettings.BlackListFile2 = dlg.BlackListFileName; } else if (Program.LocalSettings.BlackListIx3 == this.IxPolozky) { Program.LocalSettings.BlackListIx3 = this.IxPolozky; Program.LocalSettings.BlackListFile3 = dlg.BlackListFileName; } else if (Program.LocalSettings.BlackListIx1 == 0) { Program.LocalSettings.BlackListIx1 = this.IxPolozky; Program.LocalSettings.BlackListFile1 = dlg.BlackListFileName; } else if (Program.LocalSettings.BlackListIx2 == 0) { Program.LocalSettings.BlackListIx2 = this.IxPolozky; Program.LocalSettings.BlackListFile2 = dlg.BlackListFileName; } else if (Program.LocalSettings.BlackListIx3 == 0) { Program.LocalSettings.BlackListIx3 = this.IxPolozky; Program.LocalSettings.BlackListFile3 = dlg.BlackListFileName; } else { MessageBox.Show("Too many black lists, the black list was not saved !"); return; } BlackListedCodes = !string.IsNullOrEmpty(dlg.BlackListFileName) ? Workplace.BlackList.FromFile(dlg.BlackListFileName).Codes : null; Program.LocalSettings.Save(); } } }