481 lines
19 KiB
C#
481 lines
19 KiB
C#
///
|
|
/// Copyright (c) 2021-2023 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Windows.Forms;
|
|
using log4net;
|
|
using DataMatrix4Net;
|
|
using SharedDatabase.Entities;
|
|
using LabelPrinting.Resources;
|
|
using TBF.Rig.Output.Printers.Label;
|
|
using NHibernate;
|
|
using Common;
|
|
using Results.Entities;
|
|
|
|
namespace LabelPrinting
|
|
{
|
|
public partial class MainWnd : Form
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(MainWnd));
|
|
|
|
private readonly Factory factory;
|
|
private readonly Printer printer;
|
|
|
|
string programNameAndVer;
|
|
string prefix; /// S/N prefix after parsing/verification by the UI
|
|
string separator; /// Separator after parsing/verification by the UI
|
|
int currentSNTrail; /// Current S/N trai (1st S/N trail after parsing/verification by the UI)
|
|
int totalCount; /// Total S/N count after parsing/verification by the UI
|
|
int remainingCount; /// Remaining S/N-s count
|
|
IList<OrderInfo> orderInfos; /// null in Standalone mode
|
|
Timer timer;
|
|
bool interrupt;
|
|
|
|
public MainWnd()
|
|
{
|
|
InitializeComponent();
|
|
Text = programNameAndVer = string.Format("{0} v.{1}", Program.ProgramName, Program.Version); ;
|
|
factory = new Factory();
|
|
}
|
|
|
|
public MainWnd(IList<OrderInfo> orderInfos)
|
|
: this()
|
|
{
|
|
this.orderInfos = orderInfos;
|
|
|
|
if (Program.LocalSettings.Mode == Mode.TbfLabelPrinter)
|
|
{
|
|
try
|
|
{
|
|
var cmpntEntity = Config.Entities.Component.CreateFromCfg("Printer", factory.ClassName, string.Empty, 1,
|
|
DebugMode.Normal, LogLevel.Off,
|
|
Program.LocalSettings.LabelPrinterCfg);
|
|
printer = factory.GetComponent(factory.CmpntCfgFromCmpntEntity(cmpntEntity), null) as Printer;
|
|
printer.Initialize();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
MessageBox.Show("Cannot create or initialize a printer",
|
|
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
string culture = Program.LocalSettings.Language.Replace('_', '-');
|
|
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
MessageBox.Show("Selected language is not supported.\nUsing English.",
|
|
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
|
|
System.Threading.Thread.CurrentThread.CurrentUICulture =
|
|
new System.Globalization.CultureInfo("en");
|
|
}
|
|
|
|
timer = new Timer();
|
|
timer.Interval = Program.LocalSettings.DelayBetweenLabels; /// [ms] default = 1000
|
|
timer.Tick += new EventHandler(timer_Tick);
|
|
}
|
|
|
|
void Localize()
|
|
{
|
|
startButton.Text = Strings.Start_printing;
|
|
interruptButton.Text = Strings.Interrupt;
|
|
settingsToolStripMenuItem.Text = Strings.Settings;
|
|
|
|
if (Program.LocalSettings.Mode == Mode.TbfLabelPrinter)
|
|
{
|
|
sapNumberLabel.Text = Strings.Order;
|
|
separatorLabel.Text = string.Empty;
|
|
separatorComboBox.Visible = false;
|
|
snTrailLabel.Text = Strings.Serial_number;
|
|
}
|
|
else
|
|
{
|
|
sapNumberLabel.Text = Strings.SAP_number_8_digits;
|
|
separatorLabel.Text = Strings.Separator_3_characters;
|
|
snTrailLabel.Text = Strings.First_SN_trail_9_digits;
|
|
}
|
|
|
|
countLabel.Text = Strings.Labels_count;
|
|
}
|
|
|
|
private void MainWnd_Load(object sender, EventArgs e)
|
|
{
|
|
Localize();
|
|
|
|
LocalSettings.PrepareCombo(Program.LocalSettings.SapNumberHistory, sapNumberComboBox);
|
|
LocalSettings.PrepareCombo(Program.LocalSettings.SeparatorHistory, separatorComboBox);
|
|
|
|
if (Program.LocalSettings.Mode == Mode.TbfLabelPrinter)
|
|
{
|
|
snTrailTextBox.Text = string.Empty;
|
|
countTextBox.Text = Math.Max(Program.LocalSettings.RemainingSNsCount, 1).ToString();
|
|
|
|
snTrailLabel.Top -= 26;
|
|
snTrailTextBox.Top -= 26;
|
|
countLabel.Top -= 26;
|
|
countTextBox.Top -= 26;
|
|
|
|
this.ActiveControl = snTrailTextBox;
|
|
}
|
|
else if (Program.LocalSettings.Mode == Mode.Standalone)
|
|
{
|
|
snTrailTextBox.Text = Program.LocalSettings.CurrentSNTrail.ToString("D9");
|
|
countTextBox.Text = Math.Max(Program.LocalSettings.RemainingSNsCount, 1).ToString();
|
|
}
|
|
else
|
|
{
|
|
if (orderInfos == null) throw new Exception("Missing OrderInfo-s from production tracing database");
|
|
|
|
currentSNTrail = 0;
|
|
foreach (var oi in orderInfos)
|
|
{
|
|
if (oi.BaseNr1 + oi.PiecesCount + Cnst.ExtraPieces > currentSNTrail)
|
|
{
|
|
currentSNTrail = oi.BaseNr1 + oi.PiecesCount + Cnst.ExtraPieces;
|
|
}
|
|
}
|
|
|
|
snTrailTextBox.Text = currentSNTrail.ToString("D9");
|
|
snTrailTextBox.Enabled = false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Start printing labels
|
|
/// </summary>
|
|
private void startButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (!int.TryParse(countTextBox.Text, out totalCount) || totalCount <= 0)
|
|
{
|
|
MessageBox.Show(Strings.Invalid_SNs_count);
|
|
return;
|
|
}
|
|
remainingCount = totalCount;
|
|
|
|
if (Program.LocalSettings.Mode == Mode.TbfLabelPrinter)
|
|
{
|
|
var wm = new Results.Entities.WaterMeter();
|
|
wm.PurchaseOrder = string.IsNullOrEmpty(sapNumberComboBox.Text) ? string.Empty : sapNumberComboBox.Text;
|
|
wm.SerialNr = string.IsNullOrEmpty(snTrailTextBox.Text) ? string.Empty : snTrailTextBox.Text;
|
|
|
|
LocalSettings.UpdateHistory(sapNumberComboBox.Text, ref Program.LocalSettings.SapNumberHistory);
|
|
//Program.LocalSettings.CurrentSNTrail = snTrailTextBox.Text;
|
|
Program.LocalSettings.RemainingSNsCount = remainingCount;
|
|
Program.LocalSettings.Save();
|
|
|
|
startButton.Enabled = false;
|
|
interruptButton.Enabled = true;
|
|
interrupt = false;
|
|
|
|
printer.PrintResults(wm, "document name");
|
|
|
|
remainingCount--;
|
|
IncrementSN(snTrailTextBox);
|
|
}
|
|
else
|
|
{
|
|
/// Program.LocalSettings.Mode == Mode.Standalone || Program.LocalSettings.Mode == Mode.WithDatabase
|
|
|
|
int sapNumber;
|
|
if (sapNumberComboBox.Text.Length != 8 || !int.TryParse(sapNumberComboBox.Text, out sapNumber) || sapNumber <= 0 || sapNumber > 99999999)
|
|
{
|
|
MessageBox.Show(Strings.Invalid_SAP_number);
|
|
return;
|
|
}
|
|
|
|
if (separatorComboBox.Text.Length != 3)
|
|
{
|
|
MessageBox.Show(Strings.Invalid_separator);
|
|
return;
|
|
}
|
|
|
|
if (snTrailTextBox.Text.Length != 9 || !int.TryParse(snTrailTextBox.Text, out currentSNTrail))
|
|
{
|
|
MessageBox.Show(Strings.Invalid_first_SN_trail);
|
|
return;
|
|
}
|
|
else if (Program.LocalSettings.Mode == Mode.Standalone && currentSNTrail < Program.LocalSettings.CurrentSNTrail)
|
|
{
|
|
MessageBox.Show(string.Format("{0}, {1} {2:D9}", Strings.Invalid_first_SN_trail, Strings.min, Program.LocalSettings.CurrentSNTrail));
|
|
return;
|
|
}
|
|
|
|
if (Program.LocalSettings.Mode == Mode.Standalone && currentSNTrail > Program.LocalSettings.CurrentSNTrail)
|
|
{
|
|
if (MessageBox.Show(string.Format("{0} {1:D9}{2}{3}{4}",
|
|
Strings.You_are_skipping,
|
|
Program.LocalSettings.CurrentSNTrail,
|
|
(currentSNTrail == Program.LocalSettings.CurrentSNTrail + 1) ? "" : string.Format(" ... {0:D9}", currentSNTrail - 1),
|
|
Environment.NewLine,
|
|
Strings.Are_you_sure),
|
|
string.Empty,
|
|
MessageBoxButtons.YesNo,
|
|
MessageBoxIcon.Question) == DialogResult.No)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (Program.LocalSettings.Mode == Mode.WithDatabase)
|
|
{
|
|
SaveNewOrderInfo(ref currentSNTrail, totalCount);
|
|
}
|
|
|
|
prefix = sapNumberComboBox.Text;
|
|
separator = separatorComboBox.Text;
|
|
|
|
sapNumberComboBox.Enabled = false;
|
|
separatorComboBox.Enabled = false;
|
|
snTrailTextBox.Enabled = false;
|
|
countTextBox.Enabled = false;
|
|
|
|
LocalSettings.UpdateHistory(sapNumberComboBox.Text, ref Program.LocalSettings.SapNumberHistory);
|
|
LocalSettings.UpdateHistory(separatorComboBox.Text, ref Program.LocalSettings.SeparatorHistory);
|
|
|
|
Program.LocalSettings.CurrentSNTrail = currentSNTrail;
|
|
Program.LocalSettings.RemainingSNsCount = remainingCount;
|
|
Program.LocalSettings.Save();
|
|
|
|
startButton.Enabled = false;
|
|
interruptButton.Enabled = true;
|
|
interrupt = false;
|
|
|
|
PrintOneLabel();
|
|
}
|
|
|
|
if (remainingCount > 0)
|
|
{
|
|
timer.Start();
|
|
}
|
|
else
|
|
{
|
|
Text = string.Format("{0} ... {1}", programNameAndVer, Strings.completed);
|
|
MessageBox.Show(string.Format(Strings.x_labels_printed, totalCount));
|
|
SaveSettingsUpdateAndEnableUI();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Save a record tp OrderInfo table that coresponds to printed labels
|
|
/// </summary>
|
|
/// <param name="currentSNTrail">Start S/N trail number</param>
|
|
/// <param name="totalCount">Printed labels count</param>
|
|
/// <returns>true when successful</returns>
|
|
bool SaveNewOrderInfo(ref int currentSNTrail, int totalCount)
|
|
{
|
|
ISession session = SharedDatabase.TracingDB.CreateSession(Program.LocalSettings.TracingDBConnString);
|
|
ITransaction transaction = session.BeginTransaction();
|
|
OrderInfo oi = new OrderInfo();
|
|
bool successfullySaved = false;
|
|
try
|
|
{
|
|
var orders = session.QueryOver<OrderInfo>().List();
|
|
|
|
/// Determine current S/N trail once again
|
|
int snTrail = 0;
|
|
foreach (var o in orders)
|
|
{
|
|
if (o.BaseNr1 + o.PiecesCount + Cnst.ExtraPieces > snTrail)
|
|
{
|
|
snTrail = o.BaseNr1 + o.PiecesCount + Cnst.ExtraPieces;
|
|
}
|
|
}
|
|
|
|
/// Compare current S/N trail with the newly obtained one, update if necessary
|
|
if (snTrail > currentSNTrail) currentSNTrail = snTrail;
|
|
|
|
oi.POName = "0000000";
|
|
oi.PiecesCount = totalCount;
|
|
oi.TestProcedure = string.Empty;
|
|
oi.BaseNr1 = currentSNTrail;
|
|
oi.BaseNr2 = 0;
|
|
oi.BaseNr3 = 0;
|
|
oi.BaseNr4 = 0;
|
|
oi.BaseNr5 = 0;
|
|
oi.Remark = "Manual S/N printing";
|
|
oi.Workflow = null;
|
|
orders.Add(oi);
|
|
session.SaveOrUpdate(oi);
|
|
transaction.Commit();
|
|
successfullySaved = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
log.ErrorFormat("MySQL database transaction rolled back, data not comitted: {0}", exc.Message);
|
|
transaction.Rollback();
|
|
}
|
|
|
|
return successfullySaved;
|
|
}
|
|
|
|
|
|
void timer_Tick(object sender, EventArgs args)
|
|
{
|
|
timer.Stop();
|
|
|
|
if (interrupt)
|
|
{
|
|
Text = string.Format("{0} ... {1}", programNameAndVer, Strings.interrupted);
|
|
MessageBox.Show(string.Format(Strings.x_labels_printed, totalCount - remainingCount));
|
|
SaveSettingsUpdateAndEnableUI();
|
|
}
|
|
else if (Program.LocalSettings.Mode == Mode.TbfLabelPrinter)
|
|
{
|
|
var wm = new Results.Entities.WaterMeter();
|
|
wm.PurchaseOrder = string.IsNullOrEmpty(sapNumberComboBox.Text) ? string.Empty : sapNumberComboBox.Text;
|
|
wm.SerialNr = string.IsNullOrEmpty(snTrailTextBox.Text) ? string.Empty : snTrailTextBox.Text;
|
|
|
|
printer.PrintResults(wm, "document name");
|
|
|
|
remainingCount--;
|
|
IncrementSN(snTrailTextBox);
|
|
|
|
if (remainingCount > 0)
|
|
{
|
|
timer.Enabled = true;
|
|
}
|
|
else
|
|
{
|
|
Text = string.Format("{0} ... {1}", programNameAndVer, Strings.completed);
|
|
MessageBox.Show(string.Format(Strings.x_labels_printed, totalCount));
|
|
SaveSettingsUpdateAndEnableUI();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/// Program.LocalSettings.Mode == Mode.Standalone || Program.LocalSettings.Mode == Mode.WithDatabase
|
|
|
|
PrintOneLabel();
|
|
|
|
if (remainingCount > 0)
|
|
{
|
|
timer.Enabled = true;
|
|
}
|
|
else
|
|
{
|
|
Text = string.Format("{0} ... {1}", programNameAndVer, Strings.completed);
|
|
MessageBox.Show(string.Format(Strings.x_labels_printed, totalCount));
|
|
SaveSettingsUpdateAndEnableUI();
|
|
}
|
|
}
|
|
}
|
|
|
|
char[] digits = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
|
|
///
|
|
void IncrementSN(TextBox tb)
|
|
{
|
|
if (tb != null)
|
|
{
|
|
string currentSN = tb.Text;
|
|
|
|
int currentSnNr;
|
|
int startIx = currentSN.IndexOfAny(digits);
|
|
if (startIx >= 0)
|
|
{
|
|
int lastDigitPosPlus1 = startIx + 1;
|
|
var listOfDigits = new List<char>(digits);
|
|
while (lastDigitPosPlus1 < currentSN.Length && listOfDigits.Contains(currentSN[lastDigitPosPlus1]))
|
|
{
|
|
lastDigitPosPlus1++;
|
|
}
|
|
int digitsCount = lastDigitPosPlus1 - startIx;
|
|
|
|
if (int.TryParse(currentSN.Substring(startIx, digitsCount), out currentSnNr) && currentSnNr >= 0)
|
|
{
|
|
currentSnNr++;
|
|
string newSN = currentSnNr.ToString();
|
|
int len = newSN.Length;
|
|
if (len <= digitsCount)
|
|
{
|
|
tb.Text = currentSN.Substring(0, startIx + digitsCount - len) + newSN + currentSN.Substring(startIx + digitsCount);
|
|
}
|
|
else
|
|
{
|
|
tb.Text = currentSN.Substring(0, startIx) + newSN + currentSN.Substring(startIx + digitsCount); ;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
void PrintOneLabel()
|
|
{
|
|
if (remainingCount > 0)
|
|
{
|
|
string code = string.Format("{0}{1}{2:D9}", prefix, separator, currentSNTrail);
|
|
Text = string.Format("{0} ... {1} ... {2} / {3}", programNameAndVer, code, totalCount - remainingCount + 1, totalCount);
|
|
DataMatrix matrix = new DataMatrix(code, SymbolSize.SquareAuto);
|
|
new Printers.DataMatrixAndTextPrintDoc(matrix.Matrix, code.Replace(separator.Substring(0, 2), "\n")).Print();
|
|
currentSNTrail++;
|
|
remainingCount--;
|
|
}
|
|
}
|
|
|
|
void SaveSettingsUpdateAndEnableUI()
|
|
{
|
|
if (Program.LocalSettings.Mode != Mode.TbfLabelPrinter) snTrailTextBox.Text = currentSNTrail.ToString("D9");
|
|
countTextBox.Text = remainingCount.ToString();
|
|
|
|
Program.LocalSettings.CurrentSNTrail = currentSNTrail;
|
|
Program.LocalSettings.RemainingSNsCount = remainingCount;
|
|
Program.LocalSettings.Save();
|
|
|
|
sapNumberComboBox.Enabled = true;
|
|
separatorComboBox.Enabled = true;
|
|
snTrailTextBox.Enabled = (Program.LocalSettings.Mode == Mode.TbfLabelPrinter || Program.LocalSettings.Mode == Mode.Standalone);
|
|
countTextBox.Enabled = true;
|
|
|
|
startButton.Enabled = true;
|
|
interruptButton.Enabled = false;
|
|
}
|
|
|
|
private void interruptButton_Click(object sender, EventArgs e)
|
|
{
|
|
interrupt = true;
|
|
}
|
|
|
|
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
/// Connect to the database of users and log in a user
|
|
try
|
|
{
|
|
SharedDatabase.UsersDB.ConnectionString = Program.LocalSettings.UsersDBConnString;
|
|
SharedDatabase.UsersDB.DbType = Common.DBType.MySql;
|
|
|
|
DialogResult dr = new SharedDatabase.Forms.LoginDlg().ShowDialog();
|
|
if (dr != DialogResult.OK) return;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string msg = string.Format("Failed to connect to the database of users:{0}{1}", Environment.NewLine, exc.Message);
|
|
log.Error(msg);
|
|
MessageBox.Show(msg);
|
|
return;
|
|
}
|
|
|
|
/// Modify program settings
|
|
try
|
|
{
|
|
DialogResult dr = new SettingsDlg(Program.LocalSettings).ShowDialog();
|
|
if (dr == DialogResult.OK)
|
|
{
|
|
Program.LocalSettings.Save();
|
|
MessageBox.Show(Strings.Program_restart_is_required);
|
|
Close();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string msg = string.Format("Error occurred, settings were not modified:{0}{1}", Environment.NewLine, exc.Message);
|
|
log.Error(msg);
|
|
MessageBox.Show(msg);
|
|
}
|
|
}
|
|
}
|
|
}
|