/// /// Copyright (c) 2021-2022 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using log4net; using NHibernate; using SharedDatabase; using SharedDatabase.Entities; using OrderManagement.Resources; namespace OrderManagement { public partial class EditOrderDlg : Form { private static readonly ILog log = LogManager.GetLogger(typeof(EditOrderDlg)); ISessionFactory sessionFactory; OrderInfo oriOrder; IList listOfOrders; bool isEnablePredefinedOrders; bool editMode; EditParametersCtrl editParametersCtrl; public EditOrderDlg(ISessionFactory sessionFactory, OrderInfo order, IList listOfOrders, bool isEnablePredefinedOrders = false, bool editMode = false) { InitializeComponent(); this.sessionFactory = sessionFactory; this.oriOrder = order; this.listOfOrders = listOfOrders; this.isEnablePredefinedOrders = isEnablePredefinedOrders; this.editMode = editMode; editParametersCtrl = new EditParametersCtrl(oriOrder.Clone()); editParametersCtrl.Dock = DockStyle.Fill; editParametersCtrl.Unlock(); splitContainer1.Panel1.Controls.Add(editParametersCtrl); Localize(); } void Localize() { Text = Strings.Producton_order_properties; okButton.Text = Strings.OK; cancelButton.Text = Strings.Cancel; } private void okButton_Click(object sender, EventArgs e) { OrderInfo modifiedOrder = editParametersCtrl.ParamsProvider as OrderInfo; long modifiedOrderNr; if (!long.TryParse(modifiedOrder.POName, out modifiedOrderNr) || modifiedOrderNr <= 0 || (!isEnablePredefinedOrders && modifiedOrderNr <= OrderManagementDlg.MaxPredefinedOrderNr)) { MessageBox.Show(string.Format(Strings.Order_number_must_be_larger_then_0, OrderManagementDlg.MaxPredefinedOrderNr)); return; } OrderInfo orderWithTheSameName = listOfOrders.FirstOrDefault(x => (x.POName == modifiedOrder.POName)); if (orderWithTheSameName != null && (!editMode || modifiedOrder.POName != oriOrder.POName)) { MessageBox.Show(Strings.Order_with_the_same_ID_already_exists); return; } if (IsConflict(modifiedOrder, listOfOrders, editMode ? oriOrder : null)) { return; } Cursor.Current = Cursors.WaitCursor; ITransaction transaction = null; bool oriOrdermodified = false; bool saved = false; try { ISession session = sessionFactory.OpenSession(); transaction = session.BeginTransaction(); var list = (oriOrder.Id != 0) ? session.QueryOver().Where(x => x.Id == oriOrder.Id).List() : new List { oriOrder }; if (list.Count != 1) { transaction.Rollback(); } else if (list[0].Equals(oriOrder, true)) { modifiedOrder.CopyContentTo(list[0], true); session.SaveOrUpdate(list[0]); transaction.Commit(); session.Flush(); list[0].CopyContentTo(oriOrder, false); saved = true; } else { list[0].CopyContentTo(oriOrder, false); transaction.Rollback(); oriOrdermodified = true; } } catch (Exception exc) { if (transaction != null) transaction.Rollback(); log.ErrorFormat("Saving an order failed: {1}", exc.Message); } Cursor.Current = Cursors.Default; if (saved) { DialogResult = DialogResult.OK; } else if (oriOrdermodified) { MessageBox.Show("Original order was modified by someone else,\r\ntry to adit this order once again"); DialogResult = DialogResult.OK; } else { MessageBox.Show("Saving the modified order to the database failed"); DialogResult = DialogResult.Cancel; } Close(); } private void cancelButton_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } /// /// Check for conflicts of serial numbers or radio addresses /// /// New or modified order to be checked /// List of all orders /// Order to be ignered when doing check (original order in the edit mode) /// false = OK, true = conflict (MessageBox appears before returning true) bool IsConflict(OrderInfo order, IList listOfOrders, OrderInfo orderToIgnore) { if (order.POName.Length == 7 && order.POName[0] == '3') { if (MessageBox.Show(string.Format("{0}\r\n{1}", Strings.It_is_not_recommended_that_orders_start_with_3, Strings.Abort_creating_an_order_QM), Strings.Warning, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes) { return true; } } int snLength = CodeLength(order); Int64 snOffset; string snStdPrefix = StandardPrefix(order, out snOffset); string snStdSuffix = StandardSuffix(order); Int64 snFrst = First(order, snOffset); Int64 snLast = Last(order, snOffset); int raLength = CodeLength(order, true); Int64 raOffset; string raStdPrefix = StandardPrefix(order, out raOffset, true); string raStdSuffix = StandardSuffix(order, true); Int64 raFrst = First(order, raOffset, true); Int64 raLast = Last(order, raOffset, true); foreach (var another in listOfOrders) { if (orderToIgnore != null && orderToIgnore == another) continue; Int64 offset; string anotherSnStdPrefix = StandardPrefix(another, out offset); Int64 first2 = First(another, offset); Int64 last2 = Last(another, offset); if (snLength == CodeLength(another) && snStdPrefix == anotherSnStdPrefix && snStdSuffix == StandardSuffix(another) && ((first2 <= snFrst && snFrst <= last2 ) || (first2 <= snLast && snLast <= last2 ) || (snFrst <= first2 && first2 <= snLast) || (snFrst <= last2 && last2 <= snLast))) { if (MessageBox.Show(string.Format("{0}\r\n{1}: s/n {2} .. {3}\r\n{4}: s/n {5} .. {6}\r\n{7}", Strings.There_is_a_conflict, order.POName, order.SNPrefix + order.SNFirst.ToString(string.Format("D{0}", order.SNDigitsCount)) + snStdSuffix, order.SNPrefix + (order.SNFirst + order.PiecesCount - 1).ToString(string.Format("D{0}", order.SNDigitsCount)) + snStdSuffix, another.POName, another.SNPrefix + another.SNFirst.ToString(string.Format("D{0}", another.SNDigitsCount)) + StandardSuffix(another), another.SNPrefix + (another.SNFirst + another.PiecesCount - 1).ToString(string.Format("D{0}", another.SNDigitsCount)) + StandardSuffix(another), Strings.Abort_creating_an_order_QM), Strings.Warning, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes) { return true; } } string anotherRaStdPrefix = StandardPrefix(another, out offset, true); first2 = First(another, offset, true); last2 = Last(another, offset, true); if (!string.IsNullOrEmpty(order.RAPrefix) && !string.IsNullOrEmpty(another.RAPrefix) && raLength == CodeLength(another, true) && raStdPrefix == anotherRaStdPrefix && raStdSuffix == StandardSuffix(another, true) && ((first2 <= raFrst && raFrst <= last2 ) || (first2 <= raLast && raLast <= last2 ) || (raFrst <= first2 && first2 <= raLast) || (raFrst <= last2 && last2 <= raLast))) { if (MessageBox.Show(string.Format("{0}\r\n{1}: radio {2} .. {3}\r\n{4}: radio {5} .. {6}\r\n{7}", Strings.There_is_a_conflict, order.POName, order.RAPrefix + order.RAFirst.ToString(Const.RAFormat) + snStdSuffix, order.RAPrefix + (order.RAFirst + order.PiecesCount).ToString(Const.RAFormat) + snStdSuffix, another.POName, another.RAPrefix + another.RAFirst.ToString(Const.RAFormat) + StandardSuffix(another, true), another.RAPrefix + (another.RAFirst + another.PiecesCount).ToString(Const.RAFormat) + StandardSuffix(another, true), Strings.Abort_creating_an_order_QM), Strings.Warning, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes) { return true; } } } return false; } /// /// Prefix lenght + digits count + Suffix length /// /// Order /// true = Radio address /// Complete S/N or RA length int CodeLength(OrderInfo order, bool isRadio = false) { string prefix = isRadio ? order.RAPrefix : order.SNPrefix; string suffix = isRadio ? order.RASuffix : order.SNSuffix; int digitsCount = isRadio ? 9 : order.SNDigitsCount; return (string.IsNullOrEmpty(prefix) ? 0 : prefix.Length) + digitsCount + (string.IsNullOrEmpty(suffix) ? 0 : suffix.Length); } /// /// Standar prefix does not have digits at the end /// /// Order /// Offset represented by digits at the end of prefix /// true = Radio address /// string StandardPrefix(OrderInfo order, out Int64 offset, bool isRadio = false) { string prefix = isRadio ? order.RAPrefix : order.SNPrefix; int digitsCount = isRadio ? 9 : order.SNDigitsCount; if (string.IsNullOrEmpty(prefix)) { offset = 0; return string.Empty; } for (int i = prefix.Length - 1; i >= 0; i--) { if (!Char.IsDigit(prefix[i])) { offset = (i == prefix.Length - 1) ? 0 : Int64.Parse(prefix.Substring(i + 1)) * Convert.ToInt64(Math.Pow(10, digitsCount)); return prefix.Substring(0, i + 1); } } offset = Int64.Parse(prefix) * Convert.ToInt64(Math.Pow(10, digitsCount)); return string.Empty; } /// /// Standar suffix is never null, null suffix is converted to string.Empty /// /// Order /// true = Radio address /// string StandardSuffix(OrderInfo order, bool isRadio = false) { string suffix = isRadio ? order.RASuffix : order.SNSuffix; return string.IsNullOrEmpty(suffix) ? string.Empty : suffix; } /// /// First S/N or RA of an order extended with digits at the end of prefix /// /// Order /// Offset represented by digits at the end of prefix /// true = Radio address /// First extended S/N or RA in this order Int64 First(OrderInfo order, Int64 offset, bool isRadio = false) { return offset + (isRadio ? order.RAFirst : order.SNFirst); } /// /// Last S/N or RA of an order extended with digits at the end of prefix /// /// Order /// Offset represented by digits at the end of prefix /// true = Radio address /// Last extended S/N or RA in this order Int64 Last(OrderInfo order, Int64 offset, bool isRadio = false) { return First(order, offset, isRadio) + order.PiecesCount - 1; } } }