public void Insert(string TemplateName,string OfficeIndicator,bool? PublishipOrder,int? CompanyID,int? ConsigneeID,int? NotifyPartyID,int? AgentAtOriginID,int? AgentAtDestinationID,int? PrinterID,int? ClearingAgentID,int? OnCarriageID,int? OrderControllerID,int? OperationsControllerID,int? OriginPortControllerID,int? DestinationPortControllerID,string CustomersRef,int? ContactID,int? OriginPointID,int? PortID,int? DestinationPortID,int? FinalDestinationID,int? CountryID,int? DestinationCountryID,byte[] Ts)
	    {
		    OrderTemplateTable item = new OrderTemplateTable();
		    
            item.TemplateName = TemplateName;
            
            item.OfficeIndicator = OfficeIndicator;
            
            item.PublishipOrder = PublishipOrder;
            
            item.CompanyID = CompanyID;
            
            item.ConsigneeID = ConsigneeID;
            
            item.NotifyPartyID = NotifyPartyID;
            
            item.AgentAtOriginID = AgentAtOriginID;
            
            item.AgentAtDestinationID = AgentAtDestinationID;
            
            item.PrinterID = PrinterID;
            
            item.ClearingAgentID = ClearingAgentID;
            
            item.OnCarriageID = OnCarriageID;
            
            item.OrderControllerID = OrderControllerID;
            
            item.OperationsControllerID = OperationsControllerID;
            
            item.OriginPortControllerID = OriginPortControllerID;
            
            item.DestinationPortControllerID = DestinationPortControllerID;
            
            item.CustomersRef = CustomersRef;
            
            item.ContactID = ContactID;
            
            item.OriginPointID = OriginPointID;
            
            item.PortID = PortID;
            
            item.DestinationPortID = DestinationPortID;
            
            item.FinalDestinationID = FinalDestinationID;
            
            item.CountryID = CountryID;
            
            item.DestinationCountryID = DestinationCountryID;
            
            item.Ts = Ts;
            
	    
		    item.Save(UserName);
	    }
    protected void bind_formview(string mode)
    {
        string[] _key = { "OrderTemplateID" };
        OrderTemplateTableCollection _col = new OrderTemplateTableCollection();

        switch (mode){
            case "Insert":
                {
                    //blank template
                    OrderTemplateTable _template = new OrderTemplateTable();
                    _col.Add(_template);
                    break;
                }
            case "Edit":
                {
                    int _templateid = wwi_func.vint(wwi_security.DecryptString(get_token("pid"), "publiship"));
                    _col.Add(new OrderTemplateTable(_templateid));
                    break;
                }
            case "ReadOnly":
                {
                    //readonly check to see if an orderid has been passed as a param 'src'
                    //if it has populate template with data from selected order
                    int _orderid = wwi_func.vint(wwi_security.DecryptString(get_token("src"), "publiship"));
                    if (_orderid > 0)
                    {
                        //copy from order to template
                        OrderTable _order = new OrderTable(_orderid);
                        OrderTemplateTable _template = new OrderTemplateTable();
                        _template.PublishipOrder = _order.PublishipOrder;
                        _template.OfficeIndicator = _order.OfficeIndicator;
                        _template.CompanyID = _order.CompanyID;
                        _template.ConsigneeID = _order.ConsigneeID;
                        _template.NotifyPartyID = _order.NotifyPartyID;
                        _template.AgentAtOriginID = _order.AgentAtOriginID;
                        _template.AgentAtDestinationID = _order.AgentAtDestinationID;
                        _template.PrinterID = _order.PrinterID;
                        _template.ClearingAgentID = _order.ClearingAgentID;
                        _template.OnCarriageID = _order.OnCarriageID;
                        _template.OrderControllerID = _order.OrderControllerID;
                        _template.OperationsControllerID = _order.OperationsControllerID;
                        _template.OriginPortControllerID = _order.OriginPortControllerID;
                        _template.DestinationPortControllerID = _order.DestinationPortControllerID;
                        _template.CustomersRef = _order.CustomersRef;
                        _template.ContactID = _order.ContactID;
                        _template.OriginPointID = _order.OriginPointID;
                        _template.PortID = _order.PortID;
                        _template.DestinationPortID = _order.DestinationPortID;
                        _template.FinalDestinationID = _order.FinalDestinationID;
                        _template.CountryID = _order.CountryID;
                        _template.DestinationCountryID = _order.DestinationCountryID;
                        //add to collection
                        _col.Add(_template);
                    }
                    else
                    {
                        int _templateid = wwi_func.vint(wwi_security.DecryptString(get_token("pid"), "publiship"));
                        _col.Add(new OrderTemplateTable(_templateid));

                    }
                    break;
                }
            default:
                {
                    break;
                }
        }

        this.fmvTemplate.DataSource = _col;
        this.fmvTemplate.DataKeyNames = _key;
        this.fmvTemplate.DataBind(); 
    }
    /// <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_template(int orderid)
    {
        int _newid = 0;
        string _newname = "";
        try
        {
            //check template name
            ASPxTextBox _txt = (ASPxTextBox)this.fmvTemplate.FindControl("dxtxtTemplateName");
            if (_txt != null) { _newname = _txt.Text; }

            if (!string.IsNullOrEmpty(_newname))
            {
                //copy from order to template
                OrderTable _order = new OrderTable(orderid);
                OrderTemplateTable _template = new OrderTemplateTable();

                _template.TemplateName = _newname;
                //copy details from order table 
                _template.PublishipOrder = _order.PublishipOrder;
                _template.OfficeIndicator = _order.OfficeIndicator;
                _template.CompanyID = _order.CompanyID;
                _template.ConsigneeID = _order.ConsigneeID;
                _template.NotifyPartyID = _order.NotifyPartyID;
                _template.AgentAtOriginID = _order.AgentAtOriginID;
                _template.AgentAtDestinationID = _order.AgentAtDestinationID;
                _template.PrinterID = _order.PrinterID;
                _template.ClearingAgentID = _order.ClearingAgentID;
                _template.OnCarriageID = _order.OnCarriageID;
                _template.OrderControllerID = _order.OrderControllerID;
                _template.OperationsControllerID = _order.OperationsControllerID;
                _template.OriginPortControllerID = _order.OriginPortControllerID;
                _template.DestinationPortControllerID = _order.DestinationPortControllerID;
                _template.CustomersRef = _order.CustomersRef;
                _template.ContactID = _order.ContactID;
                _template.OriginPointID = _order.OriginPointID;
                _template.PortID = _order.PortID;
                _template.DestinationPortID = _order.DestinationPortID;
                _template.FinalDestinationID = _order.FinalDestinationID;
                _template.CountryID = _order.CountryID;
                _template.DestinationCountryID = _order.DestinationCountryID;

                //save values
                _template.Save();

                _newid = (int)_template.GetPrimaryKeyValue();
            }
            else
            {
                string _ex = "You must enter a name for this template";
                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;
    }
    /// <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;
    }
    /// <summary>
    /// insert function for a manually entered template
    /// </summary>
    /// <returns>int key id of new record</returns>
    protected int insert_template()
    {
        int _newid = 0;
        int? _intnull = null; //for nullable values
        string _newname = "";

        try
        {
            ASPxTextBox _txt = (ASPxTextBox)this.fmvTemplate.FindControl("dxtxtTemplateName");
            if (_txt != null) { _newname  = _txt.Text; }

            if (!string.IsNullOrEmpty(_newname))
            {
                OrderTemplateTable _template = new OrderTemplateTable();
                //template name
                _template.TemplateName = _newname; 
                //CustomersRef
                _txt = (ASPxTextBox)this.fmvTemplate.FindControl("dxtxtCustomersRef");
                if (_txt != null) { _template.CustomersRef = _txt.Text.ToString(); }
                //office
                ASPxComboBox _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboOfficeIndicator");
                if (_cbo != null) { _template.OfficeIndicator = _cbo.SelectedItem != null ? _cbo.Text.ToString() : ""; }
                //CompanyId
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboCompanyID");
                if (_cbo != null) { _template.CompanyID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //ConsigneeID
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboConsigneeID");
                if (_cbo != null) { _template.ConsigneeID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //NotifyPartyID
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboNotifyPartyID");
                if (_cbo != null) { _template.NotifyPartyID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //AgentAtOriginID
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboAgentAtOriginID");
                if (_cbo != null) { _template.AgentAtOriginID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //AgentAtDestinationID
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboAgentAtDestinationID");
                if (_cbo != null) { _template.AgentAtDestinationID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //PrinterID
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboPrinterID");
                if (_cbo != null) { _template.PrinterID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //ClearingAgentID
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboClearingAgentID");
                if (_cbo != null) { _template.ClearingAgentID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //OnCarriageID
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboOnCarriageID");
                if (_cbo != null) { _template.OnCarriageID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //OrderControllerID
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboOrderControllerID");
                if (_cbo != null) { _template.OrderControllerID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //OperationsControllerID
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboOperationsControllerID");
                if (_cbo != null) { _template.OperationsControllerID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //OriginPortControllerID
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboOriginPortControllerID");
                if (_cbo != null) { _template.OriginPortControllerID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //DestinationPortControllerID
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboDestinationPortControllerID");
                if (_cbo != null) { _template.DestinationPortControllerID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //ContactID
                //multicolumn combo returns selecteditem as null use value instead
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboContactID");
                if (_cbo != null) { _template.ContactID = _cbo.Value != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //OriginPointID
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboOriginPointID");
                if (_cbo != null) { _template.OriginPointID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //PortID
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboPortID");
                if (_cbo != null) { _template.OriginPointID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //DestinationPortID
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboDestinationPortID");
                if (_cbo != null) { _template.DestinationPortID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //FinalDestinationID
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboFinalDestinationID");
                if (_cbo != null) { _template.FinalDestinationID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //CountryID
                _cbo = (ASPxComboBox)this.fmvTemplate.FindControl("dxcboCountryID");
                if (_cbo != null) { _template.CountryID = _cbo.SelectedItem != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }
                //DestinationCountryID? not on form
                _template.DestinationCountryID = 0;
                //publiship order
                ASPxCheckBox _ckb = (ASPxCheckBox)this.fmvTemplate.FindControl("dxckbPublishipOrder");
                if (_ckb != null) { _template.PublishipOrder = _ckb.Checked; }
                //save values
                _template.Save();

                _newid = (int)_template.GetPrimaryKeyValue();
            }
            else
            {
                string _ex = "You must enter a name for this template";
                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;
    }