using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using log4net; using NHibernate; using SharedDatabase.Entities; namespace Workplace.CheckItems { public partial class SNGeneratorPrinter : UserControl, ICheckItem { static readonly ILog log = LogManager.GetLogger(typeof(SNGeneratorPrinter)); public const int FirstAssignedBaseSN = 200100000; public const int NrGeneratorIx = 0; public override string ToString() { return string.Format("S/N-generator: {0} {1}", IxPolozky, Part.Name); } WorkplaceDlg parent; public Part Part { get; set; } public int IxPolozky { get; set; } public IList BlackListedCodes { get; set; } SNGeneratorPrinter() { InitializeComponent(); BlackListedCodes = new List(); lastAuftrag = null; ResetOrderInfoEtc(); } public SNGeneratorPrinter(WorkplaceDlg parent, SharedDatabase.Entities.Part part) : this() { this.parent = parent; this.Part = part; kodMaterialuLabel.Text = part.Name; descriptionMaterialuLabel.Text = part.Description; } public string ScannedCode { get { return scannedCodeTextBox.Text; } set { scannedCodeTextBox.Text = value; } } public void ClearCode() { try { var session = parent.SessionFactory.OpenSession(); string nextCode; NegotiateSerialNr(session, parent.CurrentOrder, parent.CurrentWorkflow, parent.CurrentWorkstep, out nextCode); scannedCodeTextBox.Text = nextCode; session.Close(); } catch (Exception exc) { log.ErrorFormat("Failed to negotiate s/n in ClearCode() : {0}", exc.Message); } } public void SubmitCode(string code) { scannedCodeTextBox.Text = code; scannedCodeTextBox_KeyPress(null, new KeyPressEventArgs('\r')); } public void ResetFocus() { setFocusButton.Text = ""; } public void SetFocus() { 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; parent.OnBarcodeReceived(this, new BarcodeReceivedEventArgs(ScannedCode, IxPolozky, Part.CodeLocation)); return; } /// /// Unique serial code generation and printing as data matrices /// public OrderProgress OrderInfoState; Auftrag lastAuftrag; /// OrderInfo orderInfo; bool thisIsThe1stWorkplace; bool thisIsThe2ndWorkplace; bool thisIsThe3rdWorkplace; /// void ResetOrderInfoEtc() { orderInfo = null; thisIsThe1stWorkplace = false; thisIsThe2ndWorkplace = false; thisIsThe3rdWorkplace = false; } /// /// Negotiate the serial number and the next candidate serial number. /// /// MySQL database session /// Order info from VT_AUFTRAG_PD from Oracle database /// Production tracing workflow /// Production tracing workstep /// Negotiated S/N (in most cases it is the candidate S/N) /// Next candidate S/N /// Candidate S/N, if -1 the negotiated S/N will not be marked as saved /// NoOrderInfoYet, OrderInProgress or OrderCompleted public OrderProgress NegotiateSerialNr(ISession session, OrderInfo order, Process workflow, Workstep workstep, out string nextCode, bool commitOrderInfoChanges = false) { nextCode = "ERROR"; if (order == null) return OrderProgress.Error; OrderProgress retv = OrderProgress.Error; try { OrderInfo updatedOrder; /// Order to be used when negotiating if (commitOrderInfoChanges) { IList list = session.QueryOver() .Where(x => x.Id == order.Id) .List(); if (list.Count != 1) { log.ErrorFormat("Order {0} wasn't found in the database", order.POName); return OrderProgress.Error; } updatedOrder = list[0]; } else { updatedOrder = order; } string generatedSNFormat = Part.Name + "7VQ{0:D9}"; int baseNr, currentNr; switch (NrGeneratorIx) { default: case 0: baseNr = updatedOrder.BaseNr1; currentNr = updatedOrder.CurrentNr1; break; case 1: baseNr = updatedOrder.BaseNr2; currentNr = updatedOrder.CurrentNr2; break; case 2: baseNr = updatedOrder.BaseNr3; currentNr = updatedOrder.CurrentNr3; break; case 3: baseNr = updatedOrder.BaseNr4; currentNr = updatedOrder.CurrentNr4; break; case 4: baseNr = updatedOrder.BaseNr5; currentNr = updatedOrder.CurrentNr5; break; } if (baseNr == 0 || currentNr < baseNr) { if (baseNr == 0) { /// There are no pieces saved for this order, determine BaseNr1 baseNr = parent.GetNextBaseNr(parent.AllOrders, NrGeneratorIx); } currentNr = baseNr; nextCode = string.Format(generatedSNFormat, currentNr); retv = commitOrderInfoChanges ? OrderProgress.InProgress : OrderProgress.NotStarted; } else if (currentNr < baseNr + updatedOrder.PiecesCount - 1) { currentNr++; nextCode = string.Format(generatedSNFormat, currentNr); retv = OrderProgress.InProgress; } else if (currentNr < baseNr + updatedOrder.PiecesCount + Cnst.ExtraPieces - 1) { currentNr++; nextCode = string.Format(generatedSNFormat, currentNr); retv = OrderProgress.Overproduction; } else { nextCode = "END"; retv = OrderProgress.Completed; } if (commitOrderInfoChanges) { switch (NrGeneratorIx) { default: case 0: updatedOrder.BaseNr1 = baseNr; updatedOrder.CurrentNr1 = currentNr; break; case 1: updatedOrder.BaseNr2 = baseNr; updatedOrder.CurrentNr2 = currentNr; break; case 2: updatedOrder.BaseNr3 = baseNr; updatedOrder.CurrentNr3 = currentNr; break; case 3: updatedOrder.BaseNr4 = baseNr; updatedOrder.CurrentNr4 = currentNr; break; case 4: updatedOrder.BaseNr5 = baseNr; updatedOrder.CurrentNr5 = currentNr; break; } session.SaveOrUpdate(updatedOrder); updatedOrder.CopyContentTo(order); } } catch (Exception exc) { log.ErrorFormat("Error reading or updating order {0}: {1}", order.POName, exc.Message); if (exc.InnerException != null) log.ErrorFormat("Inner exception: {0}", exc.InnerException.Message); return OrderProgress.Error; } return retv; } } }