private double Calculate_Die_Price(bool silent = false)
        {
            if (Loaded)
            {
                this.Height = panel1.Visible ? 512 : 412;
                TFLP.Size   = new Size(this.Width - 2, this.Height - 2);


                if (!silent)
                {
                    Grey_Out();
                    Application.DoEvents();

                    if (secondThreadFormHandle == IntPtr.Zero)
                    {
                        Loading_Form form2 = new Loading_Form(parent, this.Location, this.Size, "CALCULATING", "PRICE")
                        {
                        };
                        form2.HandleCreated   += SecondFormHandleCreated;
                        form2.HandleDestroyed += SecondFormHandleDestroyed;
                        form2.RunInNewThread(false);
                    }
                }

                bool   Error  = false;
                bool   Rework = false;
                double price  = 0;
                double weight = 0;

                try
                {
                    price             = Ref_BaseCharge.Price;
                    current_cav_price = 0;

                    // If base price is 0, error
                    if (price == 0)
                    {
                        Error = true;
                    }

                    Console.WriteLine(Ref_BaseCharge.ToString());

                    // Get hole charge
                    if (cavities.Text != "1" && ((refHoleCharges != null && refHoleCharges.holeQty != Convert.ToInt32(cavities.Text)) || refHoleCharges == null))
                    {
                        refHoleCharges    = parent.holeCharge_List.FirstOrDefault(x => (x.holeQty == Convert.ToInt32(cavities.Text)) && x.Diameter == Ref_BaseCharge.Diameter && x.Thickness == Ref_BaseCharge.Thickness && x.Prefix == parent.Get_Die_Prefix(dt));
                        price            += refHoleCharges.Price * refHoleCharges.holeQty;
                        current_cav_price = refHoleCharges.Price * refHoleCharges.holeQty;
                    }
                    else if (cavities.Text != "1")
                    {
                        price            += refHoleCharges.Price * refHoleCharges.holeQty;
                        current_cav_price = refHoleCharges.Price * refHoleCharges.holeQty;
                    }

                    // Get special charges
                    additionalCharges.Text = "";
                    string passText = "";
                    foreach (DieCharge DC in DieCharge_List)
                    {
                        if (DC.Name.ToLower().Contains("rework") && DC.Quantity > 0)
                        {
                            Rework    = true;
                            price    += DC.GetAdditionalCost(Ref_BaseCharge.Price + current_cav_price); // Only allow one quantity of rework
                            passText += ", " + DC.Name + " (" + DC.Quantity + ")";
                        }
                        else if (DC.Quantity > 0)
                        {
                            price    += DC.GetAdditionalCost(Ref_BaseCharge.Price + current_cav_price) * DC.Quantity;
                            passText += ", " + DC.Name + " (" + DC.Quantity + ")";
                        }
                    }
                    if (passText.Length > 2)
                    {
                        additionalCharges.Text = passText.Substring(2);
                    }
                    if (passText.Length > 150)
                    {
                        additionalCharges.ScrollBars = ScrollBars.Vertical;
                    }

                    Console.WriteLine("Base Price = " + price);

                    // Get surcharge price
                    GetSurchargeWeight(0, dt, Ref_BaseCharge.Diameter, Ref_BaseCharge.Thickness, 1, out weight);
                    price += parent.surchargeRate * weight;

                    // Get Nitride cost if checked

                    if (nitride.Checked)
                    {
                        price += parent.GetFreightWeight("", dt, Convert.ToDouble(dia.Text) / MetricFactor, Convert.ToDouble(thk.Text) / MetricFactor, 0) * parent.nitrideRate;
                    }

                    // Nitride cost off actual size instead of odd size
                    //if (nitride.Checked)
                    //    price += parent.GetFreightWeight("",dt, Ref_BaseCharge.Diameter, Ref_BaseCharge.Thickness, 0) * parent.nitrideRate;

                    if (plate.Checked)
                    {
                        price += 50;
                    }

                    surchargeLabel.Text = "Steel Surcharge: $" + String.Format("{0:0.00}", parent.surchargeRate * weight) + " (" + Math.Round(weight, 2) + "lbs @ $" + String.Format("{0:0.00}", parent.surchargeRate) + "/lb)";
                    totalLabel.Text     = "Total: $" + String.Format("{0:0.00}", price);
                }
                catch
                {
                    Error = true;
                }

                if (!silent)
                {
                    Grey_In();

                    if (secondThreadFormHandle != IntPtr.Zero)
                    {
                        PostMessage(secondThreadFormHandle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
                    }
                }

                if (Error || (!Rework && (price - parent.surchargeRate * weight) == 0) || (plate.Checked || mandrel.Checked || backer.Checked))
                {
                    surchargeLabel.Text = "";
                    totalLabel.Text     = "Please call in for quote";
                    return(0);
                }
                else
                {
                    Console.WriteLine("Price = " + price);
                    return(price);
                }
            }
            return(0);
        }
        // Panel paint
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            //unit of measure
            string unit = parent.isMetric ? " mm" : " in";

            // allow scroll transformation
            e.Graphics.TranslateTransform(bufferedPanel3.AutoScrollPosition.X, bufferedPanel3.AutoScrollPosition.Y);

            var panel           = bufferedPanel3;
            int data_height     = 16;
            int row_count       = 0;
            int height_offset   = 1;
            int start_height    = 0;
            int start_margin    = 15;           // Item
            int item_charge_col = itemchg.Left + 5;
            int extn_price_col  = extn.Left;

            int main_margin = start_margin + 25;

            Color DrawForeColor  = Color.White;
            Color BackColor      = Color.FromArgb(64, 64, 64);
            Color HighlightColor = Color.FromArgb(76, 76, 76);

            SolidBrush WritingBrush = new SolidBrush(DrawForeColor);
            SolidBrush GreyBrush    = new SolidBrush(Color.FromArgb(88, 88, 88));
            SolidBrush RedBrush     = new SolidBrush(Color.FromArgb(150, 150, 150));
            SolidBrush GreenBrush   = new SolidBrush(Color.LightGreen);
            Pen        p            = new Pen(WritingBrush, 1);
            Pen        Grey_Pen     = new Pen(GreyBrush, 1);

            Font f_minor   = new Font("MS Reference Sans Serif", 8.5F, FontStyle.Regular);
            Font f         = new Font("MS Reference Sans Serif", 9.5F, FontStyle.Regular);
            Font f_strike  = new Font("MS Reference Sans Serif", 9, FontStyle.Strikeout);
            Font f_total   = new Font("MS Reference Sans Serif", 8, FontStyle.Bold);
            Font f_header  = new Font("MS Reference Sans Serif", 10, FontStyle.Underline);
            Font f_italic  = new Font("MS Reference Sans Serif", 9, FontStyle.Italic);
            Font f_9_bold  = new Font("MS Reference Sans Serif", 9, FontStyle.Bold);
            Font f_10_bold = new Font("MS Reference Sans Serif", 10, FontStyle.Bold);
            Font f_10      = new Font("MS Reference Sans Serif", 10, FontStyle.Regular);
            Font f_9       = new Font("MS Reference Sans Serif", 9, FontStyle.Regular);
            Font f_14_bold = new Font("MS Reference Sans Serif", 14, FontStyle.Bold);
            Font f_12_bold = new Font("MS Reference Sans Serif", 11, FontStyle.Bold);

            double surcharge_total = 0;

            List <Solid>   Solid_List   = externalID.Length > 0 ? parent.Solid_List_History.Where(x => x.OrderID == externalID).ToList() : parent.Solid_List;
            List <Hollow>  Hollow_List  = externalID.Length > 0 ? parent.Hollow_List_History.Where(x => x.OrderID == externalID).ToList() : parent.Hollow_List;
            List <Support> Support_List = externalID.Length > 0 ? parent.Support_List_History.Where(x => x.OrderID == externalID).ToList() : parent.Support_List;

            if (externalID.Length > 0)
            {
                Ref_Order = parent.Order_List.First(x => x.ID == externalID);
            }

            #region Solid Dies
            foreach (Solid s in Solid_List)
            {
                baseCharges Ref_Base = getDiaThk(s.OddDiameter, s.OddThickness, s.DieType, true);

                // If returns null, invalid dimensions
                needsCallQuote = Ref_Base == null;

                if (needsCallQuote)
                {
                    needsCallQuote2 = true;
                }

                // Get surcharge price
                double weight = 0;
                if (!needsCallQuote)
                {
                    GetSurchargeWeight(0, s.DieType, Ref_Base.Diameter, Ref_Base.Thickness, 1, out weight);
                }
                surcharge_total += s.SurchargePrice;

                e.Graphics.DrawString(s.DieType + " Die (" + Math.Round(s.Diameter * (parent.isMetric ? 25.4 : 1), 2) + unit + " X " + Math.Round(s.Thickness * (parent.isMetric ? 25.4 : 1), 2) + unit + ") - " + s.HoleCount + " Cavit" + (s.HoleCount > 1 ? "ies" : "y"), f, WritingBrush, start_margin, start_height + height_offset + (row_count * data_height));
                // Base price
                e.Graphics.DrawString(needsCallQuote ? CFQ : "$" + String.Format("{0:0.00}", Ref_Base.Price + s.CavityPrice), f_minor, WritingBrush, item_charge_col, start_height + height_offset + (row_count * data_height));
                // Extension price
                e.Graphics.DrawString(needsCallQuote ? CFQ : "$" + String.Format("{0:0.00}", s.BasePrice), f_minor, WritingBrush, extn_price_col, start_height + height_offset + (row_count * data_height));
                row_count++;

                //height_offset += 5;
                e.Graphics.DrawString("Part #: " + s.DieNo.Replace("--", "-"), f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                row_count++;
                if (s.Location.Length > 0)
                {
                    e.Graphics.DrawString("Location: " + s.Location, f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    row_count++;
                }
                if (s.Nitride)
                {
                    double nitrideprice = 0;
                    // Nitride cost
                    if (!needsCallQuote)
                    {
                        nitrideprice = parent.GetFreightWeight("", s.DieType, s.Diameter, s.Thickness, 0) * parent.nitrideRate;
                    }

                    e.Graphics.DrawString("Nitriding", f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    e.Graphics.DrawString(needsCallQuote ? CFQ : "$" + String.Format("{0:0.00}", nitrideprice), f_minor, WritingBrush, item_charge_col, start_height + height_offset + (row_count * data_height));
                    row_count++;
                }
                if (s.Pocket)
                {
                    // Nitride cost
                    e.Graphics.DrawString("Pocketing", f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    e.Graphics.DrawString("$" + String.Format("{0:0.00}", 50), f_minor, WritingBrush, item_charge_col, start_height + height_offset + (row_count * data_height));
                    row_count++;
                }
                if (s.FE_Backer_No.Length > 0)
                {
                    e.Graphics.DrawString("For existing Backer #: " + s.FE_Backer_No + " at location: " + s.FE_Backer_Loc, f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    row_count++;
                }
                if (s.FE_Feeder_No.Length > 0)
                {
                    e.Graphics.DrawString("For existing Feeder #:" + s.FE_Feeder_No + " at location: " + s.FE_Feeder_Loc, f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    row_count++;
                }
                if (s.FE_Bolster_No.Length > 0)
                {
                    e.Graphics.DrawString("For existing Bolster #:" + s.FE_Bolster_No + " at location: " + s.FE_Bolster_Loc, f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    row_count++;
                }
                if (s.FE_SubBol_Loc.Length > 0)
                {
                    e.Graphics.DrawString("For existing Sub Bolster #:" + s.FE_SubBol_No + " at location: " + s.FE_SubBol_Loc, f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    row_count++;
                }
                if (s.Notes.Length > 0)
                {
                    List <string> note_line = s.Notes.Split(new string[] { "```" }, StringSplitOptions.None).ToList();
                    e.Graphics.DrawString("Notes: " + note_line[0], f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    row_count++;
                    foreach (string line in note_line.GetRange(1, note_line.Count - 1))
                    {
                        e.Graphics.DrawString("            " + line, f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                        row_count++;
                    }
                }
                foreach (DieCharge DC in s.Charge_List)
                {
                    if (DC.Quantity > 0)
                    {
                        e.Graphics.DrawString(DC.Name + " (" + DC.Quantity + ")" + (DC.Notes.Length > 0 ? ": " + DC.Notes : ""), f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                        e.Graphics.DrawString(needsCallQuote ? CFQ : "$" + String.Format("{0:0.00}", DC.Quantity * DC.GetAdditionalCost(Ref_Base.Price + s.CavityPrice)), f_minor, WritingBrush, item_charge_col, start_height + height_offset + (row_count * data_height));
                        row_count++;
                    }
                }

                e.Graphics.DrawString("Steel Surcharge", f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                e.Graphics.DrawString(needsCallQuote ? CFQ : "$" + String.Format("{0:0.00}", s.SurchargePrice), f_minor, WritingBrush, item_charge_col, start_height + height_offset + (row_count * data_height));

                row_count += 3;
            }
            #endregion

            #region Hollow Dies
            foreach (Hollow s in Hollow_List)
            {
                if (s.Plate_Only || s.Mandrel_Only || s.No_Backer)
                {
                    needsCallQuote  = true;
                    needsCallQuote2 = true;
                }

                baseCharges Ref_Base = getDiaThk(s.OddDiameter, s.OddThickness, s.DieType, true);

                // If returns null, invalid dimensions
                needsCallQuote = Ref_Base == null;

                if (needsCallQuote)
                {
                    needsCallQuote2 = true;
                }

                // Get surcharge price
                double weight = 0;
                if (!needsCallQuote)
                {
                    GetSurchargeWeight(0, s.DieType, Ref_Base.Diameter, Ref_Base.Thickness, 1, out weight);
                }
                surcharge_total += s.SurchargePrice;

                e.Graphics.DrawString(s.DieType + " Die (" + Math.Round(s.Diameter * (parent.isMetric ? 25.4 : 1), 2) + unit + " X " + Math.Round(s.Thickness * (parent.isMetric ? 25.4 : 1), 2) + unit + ") - " + s.HoleCount + " Cavit" + (s.HoleCount > 1 ? "ies" : "y"), f, WritingBrush, start_margin, start_height + height_offset + (row_count * data_height));
                // Base price
                e.Graphics.DrawString(needsCallQuote ? CFQ : "$" + String.Format("{0:0.00}", Ref_Base.Price + s.CavityPrice), f_minor, WritingBrush, item_charge_col, start_height + height_offset + (row_count * data_height));
                // Extension price
                e.Graphics.DrawString(needsCallQuote ? CFQ : "$" + String.Format("{0:0.00}", s.BasePrice), f_minor, WritingBrush, extn_price_col, start_height + height_offset + (row_count * data_height));
                row_count++;

                //height_offset += 5;
                e.Graphics.DrawString("Part #: " + s.DieNo.Replace("--", "-"), f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                row_count++;
                if (s.Location.Length > 0)
                {
                    e.Graphics.DrawString("Location: " + s.Location, f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    row_count++;
                }
                if (s.Nitride)
                {
                    double nitrideprice = 0;
                    // Ni   tride cost
                    if (!needsCallQuote)
                    {
                        nitrideprice = parent.GetFreightWeight("", s.DieType, s.Diameter, s.Thickness, 0) * parent.nitrideRate;
                    }

                    e.Graphics.DrawString("Nitriding", f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    e.Graphics.DrawString(needsCallQuote ? CFQ : "$" + String.Format("{0:0.00}", nitrideprice), f_minor, WritingBrush, item_charge_col, start_height + height_offset + (row_count * data_height));
                    row_count++;
                }
                if (s.FE_Backer_No.Length > 0)
                {
                    e.Graphics.DrawString("For existing Backer #: " + s.FE_Backer_No + " at location: " + s.FE_Backer_Loc, f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    row_count++;
                }
                if (s.FE_Feeder_No.Length > 0)
                {
                    e.Graphics.DrawString("For existing Feeder #:" + s.FE_Feeder_No + " at location: " + s.FE_Feeder_Loc, f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    row_count++;
                }
                if (s.FE_Bolster_No.Length > 0)
                {
                    e.Graphics.DrawString("For existing Bolster #:" + s.FE_Bolster_No + " at location: " + s.FE_Bolster_Loc, f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    row_count++;
                }
                if (s.FE_SubBol_Loc.Length > 0)
                {
                    e.Graphics.DrawString("For existing Sub Bolster #:" + s.FE_SubBol_No + " at location: " + s.FE_SubBol_Loc, f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    row_count++;
                }
                if (s.Notes.Length > 0)
                {
                    List <string> note_line = s.Notes.Split(new string[] { "```" }, StringSplitOptions.None).ToList();
                    e.Graphics.DrawString("Notes: " + note_line[0], f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    row_count++;
                    foreach (string line in note_line.GetRange(1, note_line.Count - 1))
                    {
                        e.Graphics.DrawString("      " + line, f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                        row_count++;
                    }
                }
                foreach (DieCharge DC in s.Charge_List)
                {
                    if (DC.Quantity > 0)
                    {
                        e.Graphics.DrawString(DC.Name + " (" + DC.Quantity + ")" + (DC.Notes.Length > 0 ? ": " + DC.Notes : ""), f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                        e.Graphics.DrawString(needsCallQuote ? CFQ : "$" + String.Format("{0:0.00}", DC.Quantity * DC.GetAdditionalCost(Ref_Base.Price + s.CavityPrice)), f_minor, WritingBrush, item_charge_col, start_height + height_offset + (row_count * data_height));
                        row_count++;
                    }
                }

                e.Graphics.DrawString("Steel Surcharge", f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                e.Graphics.DrawString(needsCallQuote ? CFQ : "$" + String.Format("{0:0.00}", s.SurchargePrice), f_minor, WritingBrush, item_charge_col, start_height + height_offset + (row_count * data_height));

                row_count += 3;
            }
            #endregion

            #region Support Dies
            foreach (Support s in Support_List)
            {
                baseCharges Ref_Base;

                if (s.DieType == DieType.Ring)
                {
                    Ref_Base = getDiaThk(s.OddDiameter, s.OddThickness, s.InnerDiameter, s.DieType, true);
                }
                else
                {
                    Ref_Base = getDiaThk(s.OddDiameter, s.OddThickness, s.DieType, true);
                }

                // If returns null, invalid dimensions
                needsCallQuote = Ref_Base == null;

                if (needsCallQuote)
                {
                    needsCallQuote2 = true;
                }

                // Get surcharge price
                double weight = 0;
                if (!needsCallQuote)
                {
                    GetSurchargeWeight(0, s.DieType, Ref_Base.Diameter, Ref_Base.Thickness, 1, out weight);
                }
                surcharge_total += s.SurchargePrice;

                e.Graphics.DrawString(s.DieType + " (" + Math.Round(s.Diameter * (parent.isMetric ? 25.4 : 1), 2) + unit + " X " + Math.Round(s.Thickness * (parent.isMetric ? 25.4 : 1), 2) + unit + ") - " + s.HoleCount + " Cavit" + (s.HoleCount > 1 ? "ies" : "y"), f, WritingBrush, start_margin, start_height + height_offset + (row_count * data_height));
                // Base price
                e.Graphics.DrawString(needsCallQuote ? CFQ : "$" + String.Format("{0:0.00}", Ref_Base.Price + s.CavityPrice), f_minor, WritingBrush, item_charge_col, start_height + height_offset + (row_count * data_height));
                // Extension price
                e.Graphics.DrawString(needsCallQuote ? CFQ : "$" + String.Format("{0:0.00}", s.BasePrice), f_minor, WritingBrush, extn_price_col, start_height + height_offset + (row_count * data_height));
                row_count++;

                if (s.InnerDiameter > 0)
                {
                    e.Graphics.DrawString("Ring Inner Diameter: " + Math.Round(s.InnerDiameter * (parent.isMetric ? 25.4 : 1), 2) + unit, f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    row_count++;
                }

                //height_offset += 5;
                e.Graphics.DrawString("Part #: " + s.DieNo.Replace("--", "-"), f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                row_count++;
                if (s.Location.Length > 0)
                {
                    e.Graphics.DrawString("Location: " + s.Location, f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    row_count++;
                }
                if (s.Notes.Length > 0)
                {
                    List <string> note_line = s.Notes.Split(new string[] { "```" }, StringSplitOptions.None).ToList();
                    e.Graphics.DrawString("Notes: " + note_line[0], f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                    row_count++;
                    foreach (string line in note_line.GetRange(1, note_line.Count - 1))
                    {
                        e.Graphics.DrawString("      " + line, f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                        row_count++;
                    }
                }
                foreach (DieCharge DC in s.Charge_List)
                {
                    if (DC.Quantity > 0)
                    {
                        e.Graphics.DrawString(DC.Name + " (" + DC.Quantity + ")" + (DC.Notes.Length > 0 ? ": " + DC.Notes : ""), f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                        e.Graphics.DrawString(needsCallQuote ? CFQ : "$" + String.Format("{0:0.00}", DC.Quantity * DC.GetAdditionalCost(Ref_Base.Price)), f_minor, WritingBrush, item_charge_col, start_height + height_offset + (row_count * data_height));
                        row_count++;
                    }
                }

                e.Graphics.DrawString("Steel Surcharge", f_minor, WritingBrush, main_margin, start_height + height_offset + (row_count * data_height));
                e.Graphics.DrawString(needsCallQuote ? CFQ : "$" + String.Format("{0:0.00}", s.SurchargePrice), f_minor, WritingBrush, item_charge_col, start_height + height_offset + (row_count * data_height));

                row_count += 3;
            }
            #endregion

            height_offset -= 3;
            // Draw sub lines header line
            e.Graphics.DrawLine(p, itemchg.Left, start_height + height_offset + (row_count * data_height), itemchg.Right, start_height + height_offset + (row_count * data_height));
            e.Graphics.DrawLine(p, extn.Left, start_height + height_offset + (row_count * data_height), extn.Right, start_height + height_offset + (row_count * data_height));

            // Subtotal
            height_offset += 3;
            e.Graphics.DrawString("Subtotal (including steel surcharge)", f, WritingBrush, start_margin, start_height + height_offset + (row_count * data_height));
            e.Graphics.DrawString(needsCallQuote2 ? CFQ : "$" + String.Format("{0:0.00}", (Ref_Order != null ? (Ref_Order.Total - Ref_Order.FreightCost) : parent.Current_Order_Total - parent.currentFreightCost)), f_minor, WritingBrush, extn_price_col, start_height + height_offset + (row_count * data_height));
            row_count++;
            height_offset += 15;
            e.Graphics.DrawString("Freight", f, WritingBrush, start_margin, start_height + height_offset + (row_count * data_height));
            e.Graphics.DrawString(needsCallQuote ? CFQ : "$" + String.Format("{0:0.00}", (Ref_Order != null ? Ref_Order.FreightCost : parent.currentFreightCost)), f_minor, WritingBrush, extn_price_col, start_height + height_offset + (row_count * data_height));
            row_count++;
            row_count++;
            row_count++;

            // Resize panel
            bufferedPanel1.AutoScrollMinSize = new Size(bufferedPanel3.Width, start_height + height_offset + row_count * data_height);

            // Force resize only if too big
            if (start_height + height_offset + row_count * data_height > bufferedPanel1.Height)
            {
                bufferedPanel3.Height = new Size(bufferedPanel1.Width, start_height + height_offset + row_count * data_height).Height;
            }


            // Adjust surcharge value
            surchargeText.Text = "Total steel surcharge included: " + "$" + String.Format("{0:0.00}", surcharge_total);

            // Dispose all objects
            p.Dispose();
            Grey_Pen.Dispose();
            GreenBrush.Dispose();
            RedBrush.Dispose();
            GreyBrush.Dispose();
            WritingBrush.Dispose();
            f_minor.Dispose();
            f.Dispose();
            f_strike.Dispose();
            f_total.Dispose();
            f_header.Dispose();
            f_italic.Dispose();
            f_10_bold.Dispose();
            f_9_bold.Dispose();
            f_10.Dispose();
            f_9.Dispose();
            f_12_bold.Dispose();
            f_14_bold.Dispose();

            Invalidate();
        }
        public PDFGenerator(Main parent_, string OrderID, bool GenerateFromHistory = true, string print_name = "")
        {
            parent = parent_;

            if (parent.isMetric)
            {
                MetricFactor = 25.4;
            }

            int data_height = 15;

            Ref_Order = parent.Order_List.FirstOrDefault(x => x.ID == OrderID);

            if (Ref_Order == null)
            {
                Ref_Order = new Order()
                {
                    PO            = parent.PO,
                    Authorizer    = parent.Auth,
                    Requisitioner = parent.Req,
                    Due           = parent.Due,
                    DieQty        = parent.Solid_List.Count + parent.Hollow_List.Count + parent.Support_List.Count,
                    Date          = DateTime.Now,
                    ID            = parent.randomID,
                    FreightCost   = parent.currentFreightCost,
                    Total         = parent.Current_Order_Total
                };
            }

            string fileName = "EXCO_Purchase_Order" + " (QTY-" + Ref_Order.DieQty + " @ " + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "_" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second + DateTime.Now.Millisecond + ")";

            if (print_name.Length > 0)
            {
                fileName = print_name;
            }

            document.Info.Author  = "EXCO";
            document.Info.Subject = "Quote-Generating Program";
            document.Info.Title   = "Exco Pricing Tool Order";

            page = document.AddPage();
            XGraphics      gfx          = XGraphics.FromPdfPage(page);
            XFont          font         = new XFont("MS Reference Sans Serif", 10, XFontStyle.Bold);
            XFont          fontreg      = new XFont("MS Reference Sans Serif", 10, XFontStyle.Regular);
            XFont          smallfont    = new XFont("MS Reference Sans Serif", 7, XFontStyle.Bold);
            XFont          smallfontreg = new XFont("MS Reference Sans Serif", 7, XFontStyle.Regular);
            XFont          bigfontbold  = new XFont("MS Reference Sans Serif", 25, XFontStyle.Bold);
            XTextFormatter tf           = new XTextFormatter(gfx);

            DrawHeader(gfx, page);

            List <Solid>   Solid_List   = !GenerateFromHistory ? parent.Solid_List : parent.Solid_List_History;
            List <Hollow>  Hollow_List  = !GenerateFromHistory ? parent.Hollow_List : parent.Hollow_List_History;
            List <Support> Support_List = !GenerateFromHistory ? parent.Support_List : parent.Support_List_History;

            double surcharge_total = 0;

            //unit of measure
            string unit = parent.isMetric ? " mm" : " in";

            #region Solid
            foreach (Solid s in Solid_List.Where(x => x.OrderID == OrderID).ToList())
            {
                baseCharges Ref_Base = getDiaThk(s.OddDiameter, s.OddThickness, s.DieType);

                // If returns null, invalid dimensions
                needsCallQuote = Ref_Base == null;

                if (needsCallQuote)
                {
                    needsCallQuote2 = true;
                }

                // Get surcharge price
                double weight = 0;
                if (!needsCallQuote)
                {
                    GetSurchargeWeight(0, s.DieType, Ref_Base.Diameter, Ref_Base.Thickness, 1, ref weight);
                }
                surcharge_total += s.SurchargePrice;

                DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), s.DieType + " Die (" + Math.Round(s.Diameter * (parent.isMetric ? 25.4 : 1), 2) + unit + " X " + Math.Round(s.Thickness * (parent.isMetric ? 25.4 : 1), 2) + unit + ") - " + s.HoleCount + " Cavit" + (s.HoleCount > 1 ? "ies" : "y"), needsCallQuote ? CFQ : (Ref_Base.Price + s.CavityPrice).ToString(), needsCallQuote ? CFQ : (s.BasePrice).ToString(), false, !needsCallQuote);

                DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "Part #: " + s.DieNo.Replace("--", "-"), "", "", true, false);

                if (s.Location.Length > 0)
                {
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "Location: " + s.Location, "", "", true, false);
                }

                if (s.Nitride)
                {
                    double nitrideprice = parent.GetFreightWeight("", s.DieType, s.Diameter, s.Thickness, 0) * parent.nitrideRate;
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "Nitriding", needsCallQuote ? CFQ : nitrideprice.ToString(), "", true, !needsCallQuote);
                }

                if (s.Pocket)
                {
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "Pocketing", needsCallQuote ? CFQ : "50", "", true, !needsCallQuote);
                }

                if (s.FE_Backer_No.Length > 0)
                {
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "For existing Backer #: " + s.FE_Backer_No + " at location: " + s.FE_Backer_Loc, "", "", true, false);
                }

                if (s.FE_Feeder_No.Length > 0)
                {
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "For existing Feeder #:" + s.FE_Feeder_No + " at location: " + s.FE_Feeder_Loc, "", "", true, false);
                }

                if (s.FE_Bolster_No.Length > 0)
                {
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "For existing Bolster #:" + s.FE_Bolster_No + " at location: " + s.FE_Bolster_Loc, "", "", true, false);
                }

                if (s.FE_SubBol_Loc.Length > 0)
                {
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "For existing Sub Bolster #:" + s.FE_SubBol_No + " at location: " + s.FE_SubBol_Loc, "", "", true, false);
                }

                if (s.Notes.Length > 0)
                {
                    List <string> note_line = s.Notes.Split(new string[] { "```" }, StringSplitOptions.None).ToList();
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "Notes: " + note_line[0], "", "", true, false);
                    height_offset -= 2;
                    foreach (string line in note_line.GetRange(1, note_line.Count - 1))
                    {
                        DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "           " + line, "", "", true, false);
                        height_offset -= 2;
                    }
                }

                foreach (DieCharge DC in s.Charge_List)
                {
                    if (DC.Quantity > 0)
                    {
                        DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), DC.Name + " (" + DC.Quantity + ")" + (DC.Notes.Length > 0 ? ": " + DC.Notes : ""), needsCallQuote ? CFQ : (DC.Quantity * DC.GetAdditionalCost(Ref_Base.Price + s.CavityPrice)).ToString(), "", true, !needsCallQuote);
                    }
                }

                DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "Steel Surcharge", needsCallQuote ? CFQ : (s.SurchargePrice).ToString(), "", true, !needsCallQuote);

                if (ifNewPage(start_height + height_offset + (row_count++ *data_height)))
                {
                    row_count     = 0;
                    height_offset = 0;
                    NextPage(ref gfx, document);
                }
            }
            #endregion

            #region Hollow
            foreach (Hollow s in Hollow_List.Where(x => x.OrderID == OrderID).ToList())
            {
                if (s.Plate_Only || s.Mandrel_Only || s.No_Backer)
                {
                    needsCallQuote  = true;
                    needsCallQuote2 = true;
                }

                baseCharges Ref_Base = getDiaThk(s.OddDiameter, s.OddThickness, s.DieType);

                // If returns null, invalid dimensions
                needsCallQuote = Ref_Base == null;

                if (needsCallQuote)
                {
                    needsCallQuote2 = true;
                }

                // Get surcharge price
                double weight = 0;
                if (!needsCallQuote)
                {
                    GetSurchargeWeight(0, s.DieType, Ref_Base.Diameter, Ref_Base.Thickness, 1, ref weight);
                }
                surcharge_total += s.SurchargePrice;

                DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), s.DieType + " Die (" + Math.Round(s.Diameter * (parent.isMetric ? 25.4 : 1), 2) + unit + " X " + Math.Round(s.Thickness * (parent.isMetric ? 25.4 : 1), 2) + unit + ") - " + s.HoleCount + " Cavit" + (s.HoleCount > 1 ? "ies" : "y"), needsCallQuote ? CFQ : (Ref_Base.Price + s.CavityPrice).ToString(), needsCallQuote ? CFQ : (s.BasePrice).ToString(), false, !needsCallQuote);

                DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "Part #: " + s.DieNo.Replace("--", "-"), "", "", true, false);

                if (s.Location.Length > 0)
                {
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "Location: " + s.Location, "", "", true, false);
                }

                if (s.Plate_Only || s.Mandrel_Only || s.No_Backer)
                {
                    string str = s.Plate_Only ? "Plate Only" : (s.Mandrel_Only ? "Mandrel Only" : "No Backer");
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), str, needsCallQuote ? CFQ : "50", "", true, !needsCallQuote);
                }

                if (s.Nitride)
                {
                    double nitrideprice = parent.GetFreightWeight("", s.DieType, s.Diameter, s.Thickness, 0) * parent.nitrideRate;
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "Nitriding", needsCallQuote ? CFQ : nitrideprice.ToString(), "", true, !needsCallQuote);
                }

                if (s.FE_Backer_No.Length > 0)
                {
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "For existing Backer #: " + s.FE_Backer_No + " at location: " + s.FE_Backer_Loc, "", "", true, false);
                }

                if (s.FE_Feeder_No.Length > 0)
                {
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "For existing Feeder #:" + s.FE_Feeder_No + " at location: " + s.FE_Feeder_Loc, "", "", true, false);
                }

                if (s.FE_Bolster_No.Length > 0)
                {
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "For existing Bolster #:" + s.FE_Bolster_No + " at location: " + s.FE_Bolster_Loc, "", "", true, false);
                }

                if (s.FE_SubBol_Loc.Length > 0)
                {
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "For existing Sub Bolster #:" + s.FE_SubBol_No + " at location: " + s.FE_SubBol_Loc, "", "", true, false);
                }

                if (s.Notes.Length > 0)
                {
                    List <string> note_line = s.Notes.Split(new string[] { "```" }, StringSplitOptions.None).ToList();
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "Notes: " + note_line[0], "", "", true, false);
                    height_offset -= 2;
                    foreach (string line in note_line.GetRange(1, note_line.Count - 1))
                    {
                        DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "            " + line, "", "", true, false);
                        height_offset -= 2;
                    }
                }

                foreach (DieCharge DC in s.Charge_List)
                {
                    if (DC.Quantity > 0)
                    {
                        DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), DC.Name + " (" + DC.Quantity + ")" + (DC.Notes.Length > 0 ? ": " + DC.Notes : ""), needsCallQuote ? CFQ : (DC.Quantity * DC.GetAdditionalCost(Ref_Base.Price + s.CavityPrice)).ToString(), "", true, !needsCallQuote);
                    }
                }

                DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "Steel Surcharge", needsCallQuote ? CFQ : (s.SurchargePrice).ToString(), "", true, !needsCallQuote);

                if (ifNewPage(start_height + height_offset + (row_count++ *data_height)))
                {
                    row_count     = 0;
                    height_offset = 0;
                    NextPage(ref gfx, document);
                }
            }
            #endregion

            #region Support
            foreach (Support s in Support_List.Where(x => x.OrderID == OrderID).ToList())
            {
                baseCharges Ref_Base;

                if (s.DieType == DieType.Ring)
                {
                    Ref_Base = getDiaThk(s.OddDiameter, s.OddThickness, s.InnerDiameter, s.DieType);
                }
                else
                {
                    Ref_Base = getDiaThk(s.OddDiameter, s.OddThickness, s.DieType);
                }

                // If returns null, invalid dimensions
                needsCallQuote = Ref_Base == null;

                if (needsCallQuote)
                {
                    needsCallQuote2 = true;
                }

                // Get surcharge price
                double weight = 0;
                if (!needsCallQuote)
                {
                    GetSurchargeWeight(0, s.DieType, Ref_Base.Diameter, Ref_Base.Thickness, 1, ref weight);
                }
                surcharge_total += s.SurchargePrice;

                DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), s.DieType + " Die (" + Math.Round(s.Diameter * (parent.isMetric ? 25.4 : 1), 2) + unit + " X " + Math.Round(s.Thickness * (parent.isMetric ? 25.4 : 1), 2) + unit + ") - " + s.HoleCount + " Cavit" + (s.HoleCount > 1 ? "ies" : "y"), needsCallQuote ? CFQ : (Ref_Base.Price + s.CavityPrice).ToString(), needsCallQuote ? CFQ : (s.BasePrice).ToString(), false, !needsCallQuote);

                if (s.InnerDiameter > 0)
                {
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "Ring Inner Diameter: " + Math.Round(s.InnerDiameter * (parent.isMetric ? 25.4 : 1), 2) + unit, "", "", true, false);
                }

                DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "Part #: " + s.DieNo.Replace("--", "-"), "", "", true, false);

                if (s.Location.Length > 0)
                {
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "Location: " + s.Location, "", "", true, false);
                }

                if (s.Notes.Length > 0)
                {
                    List <string> note_line = s.Notes.Split(new string[] { "```" }, StringSplitOptions.None).ToList();
                    DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "Notes: " + note_line[0], "", "", true, false);
                    height_offset -= 2;
                    foreach (string line in note_line.GetRange(1, note_line.Count - 1))
                    {
                        DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "            " + line, "", "", true, false);
                        height_offset -= 2;
                    }
                }

                foreach (DieCharge DC in s.Charge_List)
                {
                    if (DC.Quantity > 0)
                    {
                        DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), DC.Name + " (" + DC.Quantity + ")" + (DC.Notes.Length > 0 ? ": " + DC.Notes : ""), needsCallQuote ? CFQ : (DC.Quantity * DC.GetAdditionalCost(Ref_Base.Price + s.CavityPrice)).ToString(), "", true, !needsCallQuote);
                    }
                }

                DrawRow(ref gfx, page, start_height + height_offset + (row_count++ *data_height), "Steel Surcharge", needsCallQuote ? CFQ : (s.SurchargePrice).ToString(), "", true, !needsCallQuote);

                if (ifNewPage(start_height + height_offset + (row_count++ *data_height)))
                {
                    row_count     = 0;
                    height_offset = 0;
                    NextPage(ref gfx, document);
                }
            }
            #endregion

            #region Summation line
            row_count++;
            DrawTotal(ref gfx, page, start_height + height_offset + (row_count++ *data_height), Ref_Order, surcharge_total);
            #endregion

            document.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), fileName + ".pdf"));

            // Open only if not printing
            if (print_name.Length == 0)
            {
                Process.Start(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), fileName + ".pdf"));
            }
        }
        private double Calculate_Die_Price(bool silent = false)
        {
            if (Loaded)
            {
                this.Height = panel1.Visible ? 512 : 412;
                TFLP.Size   = new Size(this.Width - 2, this.Height - 2);

                if (!silent)
                {
                    Grey_Out();
                    Application.DoEvents();

                    if (secondThreadFormHandle == IntPtr.Zero)
                    {
                        Loading_Form form2 = new Loading_Form(parent, this.Location, this.Size, "CALCULATING", "PRICE")
                        {
                        };
                        form2.HandleCreated   += SecondFormHandleCreated;
                        form2.HandleDestroyed += SecondFormHandleDestroyed;
                        form2.RunInNewThread(false);
                    }
                }

                bool   Error  = false;
                bool   Rework = false;
                double price  = 0;
                double weight = 0;

                try
                {
                    price             = Ref_BaseCharge.Price;
                    current_cav_price = 0;

                    // If base price is 0, error
                    if (price == 0 || Ref_BaseCharge.InnerDiameter - 1 > Ref_BaseCharge.Diameter)
                    {
                        Error = true;
                    }

                    Console.WriteLine(Ref_BaseCharge.ToString());

                    // Get special charges
                    additionalCharges.Text = "";
                    string passText = "";
                    foreach (DieCharge DC in DieCharge_List)
                    {
                        if (DC.Name.ToLower().Contains("rework") && DC.Quantity > 0)
                        {
                            Rework    = true;
                            price    += DC.GetAdditionalCost(Ref_BaseCharge.Price + current_cav_price); // Only allow one quantity of rework
                            passText += ", " + DC.Name + " (" + DC.Quantity + ")";
                        }
                        else if (DC.Quantity > 0)
                        {
                            price    += DC.GetAdditionalCost(Ref_BaseCharge.Price + current_cav_price) * DC.Quantity;
                            passText += ", " + DC.Name + " (" + DC.Quantity + ")";
                        }
                    }
                    if (passText.Length > 2)
                    {
                        additionalCharges.Text = passText.Substring(2);
                    }
                    if (passText.Length > 150)
                    {
                        additionalCharges.ScrollBars = ScrollBars.Vertical;
                    }

                    Console.WriteLine("Base Price = " + price);

                    // Get surcharge price
                    GetSurchargeWeight(0, dt, Ref_BaseCharge.Diameter, Ref_BaseCharge.Thickness, 1, out weight);
                    price += parent.surchargeRate * weight;

                    surchargeLabel.Text = "Steel Surcharge: $" + String.Format("{0:0.00}", parent.surchargeRate * weight) + " (" + Math.Round(weight, 2) + "lbs @ $" + String.Format("{0:0.00}", parent.surchargeRate) + "/lb)";
                    totalLabel.Text     = "Total: $" + String.Format("{0:0.00}", price);
                }
                catch
                {
                    Error = true;
                }

                if (!silent)
                {
                    Grey_In();

                    if (secondThreadFormHandle != IntPtr.Zero)
                    {
                        PostMessage(secondThreadFormHandle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
                    }
                }

                if (Error || (!Rework && (price - parent.surchargeRate * weight) == 0))
                {
                    surchargeLabel.Text = "";
                    totalLabel.Text     = "Please call in for quote";
                    return(0);
                }
                else
                {
                    Console.WriteLine("Price = " + price);
                    return(price);
                }
            }
            return(0);
        }