//end incremental filtering of printer
    #endregion

    #region crud events
    /// <summary>
    /// on save 
    /// copy order from ordertable to new record in ordertable
    /// </summary>
    protected int insert_clone(int orderid, int neworderno, string officeindicator)
    {
        int _newid = 0;
        
        try
        {
            //get order details we are cloning
            OrderTable _clone = new OrderTable(orderid); 
            
            //new order
            OrderTable _neworder = new OrderTable();
            //values changed from original order
            _neworder.DateOrderCreated = DateTime.Now;
            _neworder.OfficeIndicator = officeindicator;
            _neworder.OrderNumber = neworderno;

            //values derived from original order
            _neworder.PublishipOrder = _clone.PublishipOrder;
            _neworder.CompanyID = _clone.CompanyID;
            _neworder.ConsigneeID = _clone.ConsigneeID;
            _neworder.NotifyPartyID = _clone.NotifyPartyID;
            _neworder.AgentAtOriginID = _clone.AgentAtOriginID;
            _neworder.AgentAtDestinationID = _clone.AgentAtDestinationID;
            _neworder.PrinterID = _clone.PrinterID;
            _neworder.ClearingAgentID = _clone.ClearingAgentID;
            _neworder.OnCarriageID = _clone.OnCarriageID;
            _neworder.OrderControllerID = _clone.OrderControllerID;
            _neworder.OperationsControllerID = _clone.OperationsControllerID;
            _neworder.OriginPortControllerID = _clone.OriginPortControllerID;
            _neworder.DestinationPortControllerID = _clone.DestinationPortControllerID;
            _neworder.CustomersRef = _clone.CustomersRef;
            _neworder.ContactID = _clone.ContactID;
            _neworder.OriginPointID = _clone.OriginPointID;
            _neworder.PortID = _clone.PortID;
            _neworder.DestinationPortID = _clone.DestinationPortID;
            _neworder.FinalDestinationID = _clone.FinalDestinationID;
            _neworder.CountryID = _clone.CountryID;
            _neworder.DestinationCountryID = _clone.DestinationCountryID;

            _neworder.Save();

            _newid = (int)_neworder.GetPrimaryKeyValue();
        }
        catch (Exception ex)
        {
            string _ex = ex.Message.ToString();
            this.dxlblErr.Text = _ex;
            this.dxpnlErr.ClientVisible = true;
        }

        return _newid;
    }
    //end update

    /// <summary>
    /// append to order table
    /// 140114 replacing objectdatasource with code behind
    /// </summary>
    /// <param name="officeid">required so we can get next ordernumber from appropriate table</param>
    /// <returns></returns>
    protected int insert_order(int officeid, int newordernumber, string officeindicator)
    {
        //orderid of saved record
        int _newid = 0;
        
        try
        {
            if (newordernumber > 0)
            {
                OrderTable _t = new OrderTable();

                //defaults
                _t.OrderNumber = newordernumber;
                _t.OfficeIndicator = officeindicator;
                _t.DateOrderCreated = DateTime.Now;
                _t.EWDLastUpdated = DateTime.Now; //exworks last updated

                //dlls
                int? _intnull = null;
                ASPxComboBox _cb = (ASPxComboBox)this.formOrder.FindControl("dxcboController");
                if (_cb != null) { _t.OrderControllerID = _cb.Value != null ? wwi_func.vint(_cb.Value.ToString()) : _intnull; }

                _cb = (ASPxComboBox)this.formOrder.FindControl("dxcboOps");
                if (_cb != null) { _t.OperationsControllerID = _cb.Value != null ? wwi_func.vint(_cb.Value.ToString()) : _intnull; }

                _cb = (ASPxComboBox)this.formOrder.FindControl("dxcboCompany");
                if (_cb != null) { _t.CompanyID = _cb.Value != null ? wwi_func.vint(_cb.Value.ToString()) : _intnull; }

                _cb = (ASPxComboBox)this.formOrder.FindControl("dxcboCountry");
                if (_cb != null) { _t.CountryID = _cb.Value != null ? wwi_func.vint(_cb.Value.ToString()) : _intnull; }

                _cb = (ASPxComboBox)this.formOrder.FindControl("dxcboOrigin");
                if (_cb != null) { _t.OriginPointID = _cb.Value != null ? wwi_func.vint(_cb.Value.ToString()) : _intnull; }

                _cb = (ASPxComboBox)this.formOrder.FindControl("dxcboOriginPort");
                if (_cb != null) { _t.PortID = _cb.Value != null ? wwi_func.vint(_cb.Value.ToString()) : _intnull; }

                _cb = (ASPxComboBox)this.formOrder.FindControl("dxcboDestPort");
                if (_cb != null) { _t.DestinationPortID = _cb.Value != null ? wwi_func.vint(_cb.Value.ToString()) : _intnull; }

                _cb = (ASPxComboBox)this.formOrder.FindControl("dxcboFinal");
                if (_cb != null) { _t.FinalDestinationID = _cb.Value != null ? wwi_func.vint(_cb.Value.ToString()) : _intnull; }

                _cb = (ASPxComboBox)this.formOrder.FindControl("dxcboClientContact");
                if (_cb != null) { _t.ContactID = _cb.Value != null ? wwi_func.vint(_cb.Value.ToString()) : _intnull; }

                _cb = (ASPxComboBox)this.formOrder.FindControl("dxcboPrinter");
                if (_cb != null) { _t.PrinterID = _cb.Value != null ? wwi_func.vint(_cb.Value.ToString()) : _intnull; }

                _cb = (ASPxComboBox)this.formOrder.FindControl("dxcboAgentAtOrigin");
                if (_cb != null) { _t.AgentAtOriginID = _cb.Value != null ? wwi_func.vint(_cb.Value.ToString()) : _intnull; }

                _cb = (ASPxComboBox)this.formOrder.FindControl("dxcboOriginController");
                if (_cb != null) { _t.OriginPortControllerID = _cb.Value != null ? wwi_func.vint(_cb.Value.ToString()) : _intnull; }

                //dates
                DateTime? _dtnull = null; //default for nullable datetimes
                ASPxDateEdit _dt = (ASPxDateEdit)this.formOrder.FindControl("dxdtExWorks");
                if (_dt != null) { _t.ExWorksDate = _dt.Value != null ? wwi_func.vdatetime(_dt.Value.ToString()) : _dtnull; }

                _dt = (ASPxDateEdit)this.formOrder.FindControl("dxdtCargoReady");
                if (_dt != null) { _t.CargoReady = _dt.Value != null ? wwi_func.vdatetime(_dt.Value.ToString()) : _dtnull; }

                _dt = (ASPxDateEdit)this.formOrder.FindControl("dxdtWarehouse");
                if (_dt != null) { _t.WarehouseDate = _dt.Value != null ? wwi_func.vdatetime(_dt.Value.ToString()) : _dtnull; }

                _dt = (ASPxDateEdit)this.formOrder.FindControl("dxdtBookingReceived");
                if (_dt != null) { _t.BookingReceived = _dt.Value != null ? wwi_func.vdatetime(_dt.Value.ToString()) : _dtnull; }

                _dt = (ASPxDateEdit)this.formOrder.FindControl("dxdtDocsApproved");
                if (_dt != null) { _t.DocsApprovedDate = _dt.Value != null ? wwi_func.vdatetime(_dt.Value.ToString()) : _dtnull; }

                //checkboxes
                ASPxCheckBox _ck = (ASPxCheckBox)this.formOrder.FindControl("dxckEditJobPubliship");
                if (_ck != null) { _t.PublishipOrder = _ck.Checked; }

                //job closed not visible on this form
                //_ck = (ASPxCheckBox)this.formOrder.FindControl("dxckJobClosed");
                //if (_ck != null)
                //{
                //    //check if job has been closed in this append
                //    if (_t.JobClosed == false && _ck.Checked == true) { _t.JobClosureDate = DateTime.Now; }
                //    _t.JobClosed = _ck.Checked;
                //}

                _ck = (ASPxCheckBox)this.formOrder.FindControl("dxckEditJobHot");
                if (_ck != null) { _t.HotJob = _ck.Checked; }

                _ck = (ASPxCheckBox)this.formOrder.FindControl("dxckEditPalletised");
                if (_ck != null) { _t.Palletise = _ck.Checked ? -1 : 0; }

                _ck = (ASPxCheckBox)this.formOrder.FindControl("dxckEditDocsAppr");
                if (_ck != null) { _t.DocsRcdAndApproved = _ck.Checked; }

                _ck = (ASPxCheckBox)this.formOrder.FindControl("dxckEditIssueDl");
                if (_ck != null) { _t.ExpressBL = _ck.Checked; }

                _ck = (ASPxCheckBox)this.formOrder.FindControl("dxckEditFumigation");
                if (_ck != null) { _t.FumigationCert = _ck.Checked; }

                _ck = (ASPxCheckBox)this.formOrder.FindControl("dxckEditGSP");
                if (_ck != null) { _t.GSPCert = _ck.Checked; }

                _ck = (ASPxCheckBox)this.formOrder.FindControl("dxckEditPacking");
                if (_ck != null) { _t.PackingDeclaration = _ck.Checked; }

                //memos
                ASPxMemo _mo = (ASPxMemo)this.formOrder.FindControl("dxmemoRemarksToCustomer");
                if (_mo != null) { _t.RemarksToCustomer = _mo.Text.ToString(); }

                _mo = (ASPxMemo)this.formOrder.FindControl("dxmemoRemarksToAgent");
                if (_mo != null) { _t.Remarks = _mo.Text.ToString(); }

                _mo = (ASPxMemo)this.formOrder.FindControl("dxmemoDocs");
                if (_mo != null) { _t.OtherDocsRequired = _mo.Text.ToString(); }

                //text boxes
                ASPxTextBox _tx = (ASPxTextBox)this.formOrder.FindControl("dxtxtCustomersRef");
                if (_tx != null) { _t.CustomersRef = _tx.Text.ToString(); }

                _tx = (ASPxTextBox)this.formOrder.FindControl("dxtxtSellingRate");
                if (_tx != null) { _t.Sellingrate = _tx.Text.ToString(); }

                _tx = (ASPxTextBox)this.formOrder.FindControl("dxtxtSellingAgent");
                if (_tx != null) { _t.SellingrateAgent = _tx.Text.ToString(); }

                //append record
                _t.Save();
                //get new id
                _newid = (int)_t.GetPrimaryKeyValue();
            }
            else
            {
                string _ex = "Not able to find next Order Number";
                this.dxlblErr.Text = string.Format("Order NOT saved. Error: {0}", _ex);
                this.dxpnlErr.ClientVisible = true;
            }
        }
        catch (Exception ex)
        {
            string _ex = ex.Message.ToString();
            this.dxlblErr.Text = string.Format("Order NOT saved. Error: {0}", _ex);
            this.dxpnlErr.ClientVisible = true;
        }

        return _newid;
    }
    //end update

    /// <summary>
    /// we won't need this as charges are in ordertable which must have key information saved before chrge allocations can be entered
    /// </summary>
    protected void insert_charges()
    {
        try
        {
            OrderTable _ot = new OrderTable();

            ASPxComboBox _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboClientsTerms");
            if (_cbo != null) { _ot.ClientsTerms = _cbo.Text.ToString(); }

            _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboOriginTrucking");
            if (_cbo != null) { _ot.OriginTrucking = _cbo.Text.ToString(); }

            _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboOrignTHC");
            if (_cbo != null) { _ot.OrignTHC = _cbo.Text.ToString(); }

            _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboOriginDocs");
            if (_cbo != null) { _ot.OriginDocs = _cbo.Text.ToString(); }

            _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboFreight");
            if (_cbo != null) { _ot.Freight = _cbo.Text.ToString(); }

            _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboDestTHC");
            if (_cbo != null) { _ot.DestTHC = _cbo.Text.ToString(); }

            _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboDestPalletisation");
            if (_cbo != null) { _ot.DestPalletisation = _cbo.Text.ToString(); }

            _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboCustomsClearance");
            if (_cbo != null) { _ot.CustomsClearance = _cbo.Text.ToString(); }

            _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboDeliveryCharges");
            if (_cbo != null) { _ot.DeliveryCharges = _cbo.Text.ToString(); }

            ASPxMemo _mem = (ASPxMemo)this.fmvCharge.FindControl("dxmemCoLoaderComments");
            if (_mem != null) { _ot.CoLoaderComments = _cbo.Text.ToString(); }

            _ot.Save();

            //return new order id
            Int32 _newid = (Int32)_ot.GetPrimaryKeyValue();
                            
            //confirm saved
            //string _msg = "Order " + _newid.ToString() + " has been updated";
            //this.dxlblOrderNo.Text = _newid.ToString(); 
            //this.dxlblInfo.Text = _msg;
            //this.dxpnlMsg.ClientVisible = true;
        }
        catch (Exception ex)
        {
            string _err = ex.Message.ToString();
            this.dxlblErr.Text = _err;
            this.dxpnlErr.ClientVisible = true;
        }
    }
    //end update

    /// <summary>
    /// we won't need this as charges are in ordertable which must have key information saved before chrge allocations can be entered
    /// </summary>
    protected void insert_charges()
    {
        try
        {
            OrderTable _ot = new OrderTable();

            ASPxComboBox _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboClientsTerms");
            if (_cbo != null)
            {
                _ot.ClientsTerms = _cbo.Text.ToString();
            }

            _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboOriginTrucking");
            if (_cbo != null)
            {
                _ot.OriginTrucking = _cbo.Text.ToString();
            }

            _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboOrignTHC");
            if (_cbo != null)
            {
                _ot.OrignTHC = _cbo.Text.ToString();
            }

            _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboOriginDocs");
            if (_cbo != null)
            {
                _ot.OriginDocs = _cbo.Text.ToString();
            }

            _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboFreight");
            if (_cbo != null)
            {
                _ot.Freight = _cbo.Text.ToString();
            }

            _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboDestTHC");
            if (_cbo != null)
            {
                _ot.DestTHC = _cbo.Text.ToString();
            }

            _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboDestPalletisation");
            if (_cbo != null)
            {
                _ot.DestPalletisation = _cbo.Text.ToString();
            }

            _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboCustomsClearance");
            if (_cbo != null)
            {
                _ot.CustomsClearance = _cbo.Text.ToString();
            }

            _cbo = (ASPxComboBox)this.fmvCharge.FindControl("dxcboDeliveryCharges");
            if (_cbo != null)
            {
                _ot.DeliveryCharges = _cbo.Text.ToString();
            }

            ASPxMemo _mem = (ASPxMemo)this.fmvCharge.FindControl("dxmemCoLoaderComments");
            if (_mem != null)
            {
                _ot.CoLoaderComments = _cbo.Text.ToString();
            }

            _ot.Save();

            //return new order id
            Int32 _newid = (Int32)_ot.GetPrimaryKeyValue();

            //confirm saved
            //string _msg = "Order " + _newid.ToString() + " has been updated";
            //this.dxlblOrderNo.Text = _newid.ToString();
            //this.dxlblInfo.Text = _msg;
            //this.dxpnlMsg.ClientVisible = true;
        }
        catch (Exception ex)
        {
            string _err = ex.Message.ToString();
            this.dxlblErr.Text          = _err;
            this.dxpnlErr.ClientVisible = true;
        }
    }
    /// <summary>
    /// insert function for a template generated from a specified order
    /// </summary>
    /// <param name="orderid">int</param>
    /// <returns>int key id of new record</returns>
    protected int insert_order_from_template(int templateid, int neworderno, string newoffice)
    {
        int _newid = 0;
        //return next available orderno but just as a placeholder as it is required for appending the order
        //do not update actual orderno until new order is saved
       
        try
        {
            if (templateid > 0)
            {
                OrderTemplateTable _template = new OrderTemplateTable(templateid);
                //copy from template to order
                OrderTable _order = new OrderTable();

                _order.OrderNumber = neworderno;
                _order.OfficeIndicator = newoffice; //_template.OfficeIndicator;
                
                //copy details to order table 
                _order.PublishipOrder = (bool)_template.PublishipOrder;
                _order.CompanyID = _template.CompanyID;
                _order.ConsigneeID = _template.ConsigneeID;
                _order.NotifyPartyID = _template.NotifyPartyID;
                _order.AgentAtOriginID = _template.AgentAtOriginID;
                _order.AgentAtDestinationID = _template.AgentAtDestinationID;
                _order.PrinterID = _template.PrinterID;
                _order.ClearingAgentID = _template.ClearingAgentID;
                _order.OnCarriageID = _template.OnCarriageID;
                _order.OrderControllerID = _template.OrderControllerID;
                _order.OperationsControllerID = _template.OperationsControllerID;
                _order.OriginPortControllerID = _template.OriginPortControllerID;
                _order.DestinationPortControllerID = _template.DestinationPortControllerID;
                _order.CustomersRef = _template.CustomersRef;
                _order.ContactID = _template.ContactID;
                _order.OriginPointID = _template.OriginPointID;
                _order.PortID = _template.PortID;
                _order.DestinationPortID = _template.DestinationPortID;
                _order.FinalDestinationID = _template.FinalDestinationID;
                _order.CountryID = _template.CountryID;
                _order.DestinationCountryID = _template.DestinationCountryID;

                _order.DateOrderCreated = DateTime.Now;
                //save values
                _order.Save();

                _newid = (int)_order.GetPrimaryKeyValue();
            }
            else
            {
                string _ex = "Not able to create order. Template ID is 0";
                this.dxlblErr.Text = _ex;
                this.dxpnlErr.ClientVisible = true;
            }
        }
        catch (Exception ex)
        {
            string _ex = ex.Message.ToString();
            this.dxlblErr.Text = _ex;
            this.dxpnlErr.ClientVisible = true;
        }

        return _newid;
    }