using System.Collections.Generic; using System.Drawing.Printing; using System.Net; using System.Reflection; using BlubbFish.Utils; using BlubbFish.Applications.Barcodes.Inventree.Helpers; using BlubbFish.Applications.Barcodes.Inventree.Models; using LitJson; namespace BlubbFish.Applications.Barcodes.Inventree { public partial class Form1 : Form { private Ecia part; private Helpers.WebRequest req; private readonly Queue printList = new(); public Form1() { this.InitializeComponent(); InIReader.SetSearchPath(["/etc/invbarcodegen", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\blubb\\invbarcodegen"]); if(!InIReader.ConfigExist("settings")) { _ = MessageBox.Show("settings.ini nicht gefunden.\nBitte settings.ini anlegen.", "Fehler"); return; } this.req = new Helpers.WebRequest(); this.PrepairForm(); } #region Methods private void PrepairForm() { this._statusLabel.Text = "Nicht Verbunden"; if(this.CheckServerConfig()) { this.CheckPrinterConfig(); } this.GetPOList(); } private void GetPOList() { try { String json = this.req.RequestString(this._textBox_ServerAddress.Text + "/api/order/po/"); JsonData j = JsonMapper.ToObject(json); this.comboBox1.Items.Clear(); _ = this.comboBox1.Items.Add(" - Auswahl -"); foreach(JsonData item in j) { _ = this.comboBox1.Items.Add(new POList { Pk = Int32.Parse(item["pk"].ToString()), Description = item["description"].ToString(), Reference = item["reference"].ToString(), SupplierName = item["supplier_name"].ToString(), SupplierReference = item["supplier_reference"].ToString(), }); } this.comboBox1.SelectedIndex = 0; } catch { } } private void CheckPrinterConfig() { String[] printers = Printer.ListPrinters(); if(printers.Length == 0) { _ = MessageBox.Show("Keine Drucker gefunden!", "Fehler"); return; } String printer = InIReader.GetInstance("settings").GetValue("Printer", "printer"); String paperSize = InIReader.GetInstance("settings").GetValue("Printer", "papersize"); String paperSource = InIReader.GetInstance("settings").GetValue("Printer", "papersource"); String printerResolution = InIReader.GetInstance("settings").GetValue("Printer", "printerresolution"); String landscape = InIReader.GetInstance("settings").GetValue("Printer", "landscape"); this.ListPrinters(printers, printer); this.ListPaperSizes(Printer.ListPaperSizes(this.printDocument1.PrinterSettings.PaperSizes), paperSize); this.ListPaperSources(Printer.ListPaperSources(this.printDocument1.PrinterSettings.PaperSources), paperSource); this.ListPrinterResolutions(Printer.ListPrinterResolutions(this.printDocument1.PrinterSettings.PrinterResolutions), printerResolution); this.ListLandscape(landscape); this._listBox_Printer.SelectedIndexChanged += this.ListBoxPrinter_SelectedIndexChanged; this._listBox_PaperSize.SelectedIndexChanged += this.ListBoxPaperSize_SelectedIndexChanged; this._listBox_PaperSources.SelectedIndexChanged += this.ListBoxPaperSources_SelectedIndexChanged; this._listBox_PrinterResolution.SelectedIndexChanged += this.ListBoxPrinterResolution_SelectedIndexChanged; this._checkBox_Landscape.CheckedChanged += this.CheckBoxLandscape_CheckedChanged; this.DrawPreview(); } private Boolean CheckServerConfig() { String server = InIReader.GetInstance("settings").GetValue("Inventree", "server"); String auth = InIReader.GetInstance("settings").GetValue("Inventree", "authtoken"); if(server is null || auth is null) { this._mainTabBox.SelectedIndex = 1; this._statusLabel.Text = "No Server/Authtoken is Set!"; return false; } else { this._textBox_ServerAddress.Text = server; this._textBox_AuthToken.Text = auth; this._textServerName.Text = server; this.req.SetAuthToken(auth); } try { String text = this.req.RequestString(server + "/api/version/"); JsonData json = JsonMapper.ToObject(text); if(json.ContainsKey("version") && json["version"].IsObject) { this._statusLabel.Text = "Verbunden"; } } catch { this._statusLabel.Text = "Fehler beim Verbinden!"; this._mainTabBox.SelectedIndex = 1; return false; } return true; } private void DrawPreview() { this.pictureBox1.Size = this.printDocument1.DefaultPageSettings.Landscape ? new Size((this.printDocument1.DefaultPageSettings.PaperSize.Height * 2) + 2, (this.printDocument1.DefaultPageSettings.PaperSize.Width * 2) + 2) : new Size((this.printDocument1.DefaultPageSettings.PaperSize.Width * 2) + 2, (this.printDocument1.DefaultPageSettings.PaperSize.Height * 2) + 2); Bitmap bmp = new(this.printDocument1.DefaultPageSettings.PaperSize.Width - 2, this.printDocument1.DefaultPageSettings.PaperSize.Height - 2); Graphics g = Graphics.FromImage(bmp); Drawing.GetLabel(g, this.part); Bitmap bigger_bmp = new(bmp, new Size(bmp.Width * 2, bmp.Height * 2)); Bitmap scaled_prev = new(this.pictureBox1.Size.Width, this.pictureBox1.Size.Height); Graphics.FromImage(scaled_prev).DrawImage(bigger_bmp, 1, 1); Graphics.FromImage(scaled_prev).DrawRectangle(new Pen(Brushes.Red), new Rectangle(0, 0, scaled_prev.Width - 1, scaled_prev.Height - 1)); this.pictureBox1.Image = scaled_prev; } #endregion #region Get Printer Settings private void ListPrinters(String[] printers, String printer) { this._listBox_Printer.Items.Clear(); this._listBox_Printer.Items.AddRange(printers); if(printer is null) { this.SetPrinter(printers[0]); this._listBox_Printer.SelectedIndex = 0; } else { Int32 printer_index = Array.IndexOf(printers, printer); if(printer_index != -1) { this.SetPrinter(printers[printer_index]); this._listBox_Printer.SelectedIndex = printer_index; } else { this.SetPrinter(printers[0]); this._listBox_Printer.SelectedIndex = 0; } } } private void ListPaperSizes(String[] paperSizes, String papersize) { this._listBox_PaperSize.Items.Clear(); if(paperSizes.Length > 0) { this._listBox_PaperSize.Items.AddRange(paperSizes); if(papersize is null) { this.SetPaperSize(0); this._listBox_PaperSize.SelectedIndex = 0; } else { Int32 papersize_index = Array.IndexOf(paperSizes, papersize); if(papersize_index != -1) { this.SetPaperSize(papersize_index); this._listBox_PaperSize.SelectedIndex = papersize_index; } else { this.SetPaperSize(0); this._listBox_PaperSize.SelectedIndex = 0; } } } } private void ListPaperSources(String[] paperSources, String paperSource) { this._listBox_PaperSources.Items.Clear(); if(paperSources.Length > 0) { this._listBox_PaperSources.Items.AddRange(paperSources); if(paperSource is null) { this.SetPaperSource(0); this._listBox_PaperSources.SelectedIndex = 0; } else { Int32 papersource_index = Array.IndexOf(paperSources, paperSource); if(papersource_index != -1) { this.SetPaperSource(papersource_index); this._listBox_PaperSources.SelectedIndex = papersource_index; } else { this.SetPaperSource(0); this._listBox_PaperSources.SelectedIndex = 0; } } } } private void ListPrinterResolutions(String[] printerResolutions, String printerResolution) { this._listBox_PrinterResolution.Items.Clear(); if(printerResolutions.Length > 0) { this._listBox_PrinterResolution.Items.AddRange(printerResolutions); if(printerResolution is null) { this.SetPrinterResolution(0); this._listBox_PrinterResolution.SelectedIndex = 0; } else { Int32 printerresolution_index = Array.IndexOf(printerResolutions, printerResolution); if(printerresolution_index != -1) { this.SetPrinterResolution(printerresolution_index); this._listBox_PrinterResolution.SelectedIndex = printerresolution_index; } else { this.SetPrinterResolution(0); this._listBox_PrinterResolution.SelectedIndex = 0; } } } } private void ListLandscape(String landscape) { this._checkBox_Landscape.Checked = this.printDocument1.DefaultPageSettings.Landscape; if(landscape is not null) { if(Boolean.TryParse(landscape, out Boolean ls)) { this.printDocument1.DefaultPageSettings.Landscape = ls; this._checkBox_Landscape.Checked = ls; } } } #endregion #region Set Printer Settings private void SetPrinter(String printer) { this.printDocument1.PrinterSettings.PrinterName = printer; this._textPrinterName.Text = printer; } private void SetPaperSize(Int32 papersize) => this.printDocument1.DefaultPageSettings.PaperSize = this.printDocument1.PrinterSettings.PaperSizes[papersize]; private void SetPaperSource(Int32 papersource) => this.printDocument1.DefaultPageSettings.PaperSource = this.printDocument1.PrinterSettings.PaperSources[papersource]; private void SetPrinterResolution(Int32 printerresolution) => this.printDocument1.DefaultPageSettings.PrinterResolution = this.printDocument1.PrinterSettings.PrinterResolutions[printerresolution]; #endregion private void PrintDocument1_PrintPage(Object sender, PrintPageEventArgs e) { if(this.printList.TryDequeue(out Ecia item)) { Drawing.GetLabel(e.Graphics, item); e.HasMorePages = this.printList.Count > 0; } } private void ButtonPrintPreview_Click(Object sender, EventArgs e) { this.PreparePintList(); _ = this.printPreviewDialog1.ShowDialog(); } private void PreparePintList() { this.printList.Clear(); foreach(DataGridViewRow item in this.dataGridView1.Rows) { if(item.Cells[1].Value is Boolean print) { if(print && item.Cells[2].Value is Boolean bag) { if(item.Cells[3].Value is LineItem l) { if(Double.TryParse(item.Cells[5].Value.ToString(), out Double q)) { for(Int32 i = 0; i < Convert.ToInt32(bag ? 1 : q); i++) { Ecia t = l.EciaFormat; t.Quantity = q; this.printList.Enqueue(t); } } } } } } } private void ButtonPrint_Click(Object sender, EventArgs e) { this.PreparePintList(); this.printDocument1.Print(); } private void ButtonQueryPo_Click(Object sender, EventArgs e) => this.GetPOList(); private void ButtonSaveSettings_Click(Object sender, EventArgs e) { InIReader.GetInstance("settings").SetValue("Inventree", "server", this._textBox_ServerAddress.Text); InIReader.GetInstance("settings").SetValue("Inventree", "authtoken", this._textBox_AuthToken.Text); InIReader.GetInstance("settings").SetValue("Printer", "printer", this._listBox_Printer.Items[this._listBox_Printer.SelectedIndex].ToString()); InIReader.GetInstance("settings").SetValue("Printer", "papersize", this._listBox_PaperSize.Items[this._listBox_PaperSize.SelectedIndex].ToString()); InIReader.GetInstance("settings").SetValue("Printer", "papersource", this._listBox_PaperSources.Items[this._listBox_PaperSources.SelectedIndex].ToString()); InIReader.GetInstance("settings").SetValue("Printer", "printerresolution", this._listBox_PrinterResolution.Items[this._listBox_PrinterResolution.SelectedIndex].ToString()); InIReader.GetInstance("settings").SetValue("Printer", "landscape", this._checkBox_Landscape.Checked.ToString()); this._mainTabBox.SelectedIndex = 0; this.PrepairForm(); } private void ListBoxPrinter_SelectedIndexChanged(Object sender, EventArgs e) { if(sender is ListBox and not null) { ListBox a = sender as ListBox; this.SetPrinter(a.SelectedItem.ToString()); this._listBox_PaperSize.SelectedIndexChanged -= this.ListBoxPaperSize_SelectedIndexChanged; this._listBox_PaperSources.SelectedIndexChanged -= this.ListBoxPaperSources_SelectedIndexChanged; this._listBox_PrinterResolution.SelectedIndexChanged -= this.ListBoxPrinterResolution_SelectedIndexChanged; this._checkBox_Landscape.CheckedChanged -= this.CheckBoxLandscape_CheckedChanged; this.ListPaperSizes(Printer.ListPaperSizes(this.printDocument1.PrinterSettings.PaperSizes), null); this.ListPaperSources(Printer.ListPaperSources(this.printDocument1.PrinterSettings.PaperSources), null); this.ListPrinterResolutions(Printer.ListPrinterResolutions(this.printDocument1.PrinterSettings.PrinterResolutions), null); this._listBox_PaperSize.SelectedIndexChanged += this.ListBoxPaperSize_SelectedIndexChanged; this._listBox_PaperSources.SelectedIndexChanged += this.ListBoxPaperSources_SelectedIndexChanged; this._listBox_PrinterResolution.SelectedIndexChanged += this.ListBoxPrinterResolution_SelectedIndexChanged; this._checkBox_Landscape.CheckedChanged += this.CheckBoxLandscape_CheckedChanged; this.DrawPreview(); } } private void ListBoxPaperSize_SelectedIndexChanged(Object sender, EventArgs e) { if(sender is ListBox and not null) { ListBox a = sender as ListBox; this.SetPaperSize(a.SelectedIndex); this.DrawPreview(); } } private void ListBoxPaperSources_SelectedIndexChanged(Object sender, EventArgs e) { if(sender is ListBox and not null) { ListBox a = sender as ListBox; this.SetPaperSource(a.SelectedIndex); this.DrawPreview(); } } private void ListBoxPrinterResolution_SelectedIndexChanged(Object sender, EventArgs e) { if(sender is ListBox and not null) { ListBox a = sender as ListBox; this.SetPrinterResolution(a.SelectedIndex); this.DrawPreview(); } } private void CheckBoxLandscape_CheckedChanged(Object sender, EventArgs e) { if(sender is CheckBox and not null) { CheckBox c = sender as CheckBox; this.printDocument1.DefaultPageSettings.Landscape = c.Checked; this.DrawPreview(); } } private void comboBox1_SelectedIndexChanged(Object sender, EventArgs e) { if(sender is ComboBox c and not null) { if(c.Items[c.SelectedIndex] is String s) { if(s == " - Auswahl -") { return; } } else if(c.Items[c.SelectedIndex] is POList po) { try { String json = this.req.RequestString(this._textBox_ServerAddress.Text + "/api/order/po-line/?limit=1000&order=" + po.Pk); JsonData j = JsonMapper.ToObject(json); this.dataGridView1.Rows.Clear(); Int32 i = 1; foreach(JsonData item in j["results"]) { DataGridViewRow r = new(); r.CreateCells(this.dataGridView1); Double recieved = Double.Parse(item["received"].ToString()); Double quantity = Double.Parse(item["quantity"].ToString()); r.Cells[0].Value = i; r.Cells[1].Value = quantity > recieved; r.Cells[2].Value = false; r.Cells[3].Value = new LineItem { Name = item["internal_part_name"].ToString(), SKU = item["sku"].ToString(), MPN = item["mpn"].ToString(), LineNr = i, Quantity = quantity, EciaFormat = new Ecia { CustomerPO = po.Reference, SupplierOrderNumber = po.SupplierReference, ShipDate = DateTime.Now.ToString("yyyyMMdd"), SKU = item["sku"].ToString(), MPN = item["mpn"].ToString(), CustomerPOLine = i++.ToString(), Quantity = quantity, DateCode = DateTime.Now.ToString("yy01"), LotCode = "1", County = "DE" } }; r.Cells[4].Value = item["sku"].ToString(); r.Cells[5].Value = quantity >= recieved ? quantity - recieved : 0; r.DefaultCellStyle.BackColor = quantity <= recieved ? Color.Green : recieved != 0? Color.Yellow: Color.White; _ = this.dataGridView1.Rows.Add(r); } this.CalcLabelCount(); } catch { } } } } private void DataGridView1_CellContentClick(Object sender, DataGridViewCellEventArgs e) => this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); private void DataGridView1_CellValueChanged(Object sender, DataGridViewCellEventArgs e) => this.CalcLabelCount(); private void CalcLabelCount() { Int32 i = 0; foreach(DataGridViewRow item in this.dataGridView1.Rows) { if(item.Cells[1].Value is Boolean print) { if(print && Double.TryParse(item.Cells[5].Value.ToString(), out Double c)) { if(item.Cells[2].Value is Boolean bag) { i += Convert.ToInt32(bag ? 1 : c); } } } } this.labelCount.Text = "Anzahl zu druckender Labels: " + i; } private void DataGridView1_SelectionChanged(Object sender, EventArgs e) { if(sender is DataGridView g) { Int32 i = g.CurrentRow.Index; if(this.dataGridView1.Rows.Count >= i) { if(this.dataGridView1.Rows[i].Cells[3].Value is LineItem l) { this.part = l.EciaFormat; if(Double.TryParse(this.dataGridView1.Rows[i].Cells[5].Value.ToString(), out Double c)) { this.part.Quantity = c; } this.DrawPreview(); } } } } private void ButtonAll_Click(Object sender, EventArgs e) { foreach(DataGridViewRow item in this.dataGridView1.Rows) { item.Cells[1].Value = true; } } private void ButtonNone_Click(Object sender, EventArgs e) { foreach(DataGridViewRow item in this.dataGridView1.Rows) { item.Cells[1].Value = false; } } } }