DELIMITER $$
DROP PROCEDURE IF EXISTS `wp_expenseszyx987_getCustomerList`$$
CREATE PROCEDURE `wp_expenseszyx987_getCustomerList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),
   IN searchwords varchar(200),
   IN customertypeid int, 
   IN customerstatus int,
   OUT customertotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default '';
   declare orderbyclause varchar(10000) default '';

   set @searchwords = CONCAT('%', searchwords, '%');
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   set @customertypeid = customertypeid;

   set @customerstatus = 0;
   set @customerstatus2 = 1;
   if customerstatus = -1 then set @customerstatus = 0; set @customerstatus2 = 1; end if;
   if customerstatus = 0 then set @customerstatus = 0; set @customerstatus2 = 0; end if;
   if customerstatus = 1 then set @customerstatus = 1; set @customerstatus2 = 1; end if;

    if customertypeid <= 0 && searchwords = '' then SET @statement = concat( 'Select count(*) into @customertotalquantity from ', ' wp_expenseszyx987_customer WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @customerstatus, @customerstatus2;
       DEALLOCATE PREPARE stmt;

    elseif customertypeid <= 0 && searchwords != '' then SET @statement = concat( 'Select count(*) into @customertotalquantity from ', ' wp_expenseszyx987_customer WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @customerstatus, @customerstatus2;
       DEALLOCATE PREPARE stmt;
       
    elseif customertypeid > 0 && searchwords = '' then SET @statement = concat( 'Select count(*) into @customertotalquantity from ', ' wp_expenseszyx987_customer WHERE customertypeid = ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @customertypeid, @customerstatus, @customerstatus2;
       DEALLOCATE PREPARE stmt;

    elseif customertypeid > 0 && searchwords != '' then SET @statement = concat( 'Select count(*) into @customertotalquantity from ', ' wp_expenseszyx987_customer WHERE customertypeid = ? AND name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @customertypeid, @searchwords, @customerstatus, @customerstatus2;
       DEALLOCATE PREPARE stmt; 
    end if;    

    SET customertotalquantity = @customertotalquantity;

--  ==========================================================================================================================

    if myorder = 'asc' && mysortby = 'id' && customertypeid <= 0 && searchwords = '' then SET @sqlv = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.status IN (?,?) order by c.id ASC LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && customertypeid <= 0 && searchwords != '' then SET @statement = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.name like ? AND c.status IN (?,?) order by c.id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customertypeid > 0 && searchwords = '' then SET @sqlv = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.customertypeid = ? AND c.status IN (?,?) order by c.id ASC LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customertypeid, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && customertypeid > 0 && searchwords != '' then SET @statement = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.customertypeid = ? AND c.name like ? AND c.status IN (?,?) order by c.id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @customertypeid, @searchwords, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'name' && customertypeid <= 0 && searchwords = '' then SET @sqlv = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.status IN (?,?) order by c.name ASC,c.id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'name' && customertypeid <= 0 && searchwords != '' then SET @statement = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.name like ? AND c.status IN (?,?) order by c.name ASC,c.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'name'&& customertypeid > 0  && searchwords = '' then SET @sqlv = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.customertypeid = ? AND c.status IN (?,?) order by c.name ASC,c.id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customertypeid, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'name'&& customertypeid > 0  && searchwords != '' then SET @statement = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.customertypeid = ? AND c.name like ? AND c.status IN (?,?) order by c.name ASC,c.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @customertypeid, @searchwords, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && customertypeid <= 0 && searchwords = '' then SET @sqlv = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.status IN (?,?) order by c.createtimeutc ASC,c.id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && customertypeid <= 0 && searchwords != '' then SET @statement = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.name like ? AND c.status IN (?,?) order by c.createtimeutc ASC,c.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && customertypeid > 0 && searchwords = '' then SET @sqlv = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.customertypeid = ? AND c.status IN (?,?) order by c.createtimeutc ASC,c.id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customertypeid, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && customertypeid > 0 && searchwords != '' then SET @statement = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.customertypeid = ? AND c.name like ? AND c.status IN (?,?) order by c.createtimeutc ASC,c.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @customertypeid, @searchwords, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && customertypeid <= 0 && searchwords = '' then SET @sqlv = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.status IN (?,?) order by c.id DESC LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && customertypeid <= 0 && searchwords != '' then SET @statement = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.name like ? AND c.status IN (?,?) order by c.id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customertypeid > 0 && searchwords = '' then SET @sqlv = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.customertypeid = ? AND c.status IN (?,?) order by c.id DESC LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customertypeid, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && customertypeid > 0 && searchwords != '' then SET @statement = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.customertypeid = ? AND c.name like ? AND c.status IN (?,?) order by c.id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @customertypeid, @searchwords, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'name' && customertypeid <= 0 && searchwords = '' then SET @sqlv = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.status IN (?,?) order by c.name DESC,c.id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'name' && customertypeid <= 0 && searchwords != '' then SET @statement = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.name like ? AND c.status IN (?,?) order by c.name DESC,c.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'name' && customertypeid > 0 && searchwords = '' then SET @sqlv = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.customertypeid = ? AND c.status IN (?,?) order by c.name DESC,c.id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customertypeid, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'name' && customertypeid > 0 && searchwords != '' then SET @statement = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.customertypeid = ? AND c.name like ? AND c.status IN (?,?) order by c.name DESC,c.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @customertypeid, @searchwords, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && customertypeid <= 0 && searchwords = '' then SET @sqlv = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.status IN (?,?) order by c.createtimeutc DESC,c.id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && customertypeid <= 0 && searchwords != '' then SET @statement = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.name like ? AND c.status IN (?,?) order by c.createtimeutc DESC,c.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
       
    elseif myorder = 'desc' && mysortby = 'createtimeutc' && customertypeid > 0 && searchwords = '' then SET @sqlv = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.customertypeid = ? AND c.status IN (?,?) order by c.createtimeutc DESC,c.id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customertypeid, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && customertypeid > 0 && searchwords != '' then SET @statement = 'SELECT c.*,t.id as customertypeid, t.name as customertypename from wp_expenseszyx987_customer c LEFT JOIN wp_expenseszyx987_customertype t ON c.customertypeid = t.id WHERE c.customertypeid = ? AND c.name like ? AND c.status IN (?,?) order by c.createtimeutc DESC,c.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @customertypeid, @searchwords, @customerstatus, @customerstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;       
       
    end if;

END  $$
DROP PROCEDURE IF EXISTS `wp_expenseszyx987_getRecurringpaymentfrequencyList`$$
CREATE PROCEDURE `wp_expenseszyx987_getRecurringpaymentfrequencyList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),
   IN searchwords varchar(200),
   IN recurringpaymentfrequencystatus int,
   OUT recurringpaymentfrequencytotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default '';
   declare orderbyclause varchar(10000) default '';

   set @searchwords = CONCAT('%', searchwords, '%');
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;

   set @recurringpaymentfrequencystatus = 0;
   set @recurringpaymentfrequencystatus2 = 1;
   if recurringpaymentfrequencystatus = -1 then set @recurringpaymentfrequencystatus = 0; set @recurringpaymentfrequencystatus2 = 1; end if;
   if recurringpaymentfrequencystatus = 0 then set @recurringpaymentfrequencystatus = 0; set @recurringpaymentfrequencystatus2 = 0; end if;
   if recurringpaymentfrequencystatus = 1 then set @recurringpaymentfrequencystatus = 1; set @recurringpaymentfrequencystatus2 = 1; end if;

    if searchwords = '' then SET @statement = concat( 'Select count(*) into @recurringpaymentfrequencytotalquantity from ', ' wp_expenseszyx987_recurringpaymentfrequency WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @recurringpaymentfrequencystatus, @recurringpaymentfrequencystatus2;
       DEALLOCATE PREPARE stmt;

    elseif searchwords != '' then SET @statement = concat( 'Select count(*) into @recurringpaymentfrequencytotalquantity from ', ' wp_expenseszyx987_recurringpaymentfrequency WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @recurringpaymentfrequencystatus, @recurringpaymentfrequencystatus2;
       DEALLOCATE PREPARE stmt;
    end if;

    SET recurringpaymentfrequencytotalquantity = @recurringpaymentfrequencytotalquantity;

--  ==========================================================================================================================

    if myorder = 'asc' && mysortby = 'id' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_recurringpaymentfrequency WHERE status IN (?,?) order by id ASC LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @recurringpaymentfrequencystatus, @recurringpaymentfrequencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_recurringpaymentfrequency WHERE name like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @recurringpaymentfrequencystatus, @recurringpaymentfrequencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'name' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_recurringpaymentfrequency WHERE status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @recurringpaymentfrequencystatus, @recurringpaymentfrequencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_recurringpaymentfrequency WHERE name like ? AND status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @recurringpaymentfrequencystatus, @recurringpaymentfrequencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_recurringpaymentfrequency WHERE status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @recurringpaymentfrequencystatus, @recurringpaymentfrequencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_recurringpaymentfrequency WHERE name like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @recurringpaymentfrequencystatus, @recurringpaymentfrequencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_recurringpaymentfrequency WHERE status IN (?,?) order by id DESC LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @recurringpaymentfrequencystatus, @recurringpaymentfrequencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_recurringpaymentfrequency WHERE name like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @recurringpaymentfrequencystatus, @recurringpaymentfrequencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'name' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_recurringpaymentfrequency WHERE status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @recurringpaymentfrequencystatus, @recurringpaymentfrequencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_recurringpaymentfrequency WHERE name like ? AND status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @recurringpaymentfrequencystatus, @recurringpaymentfrequencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_recurringpaymentfrequency WHERE status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @recurringpaymentfrequencystatus, @recurringpaymentfrequencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_recurringpaymentfrequency WHERE name like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @recurringpaymentfrequencystatus, @recurringpaymentfrequencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;

END  $$
DROP PROCEDURE IF EXISTS `wp_expenseszyx987_getCustomertypeList`$$
CREATE PROCEDURE `wp_expenseszyx987_getCustomertypeList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),
   IN searchwords varchar(200),
   IN customertypestatus int,
   OUT customertypetotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default '';
   declare orderbyclause varchar(10000) default '';

   set @searchwords = CONCAT('%', searchwords, '%');
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;

   set @customertypestatus = 0;
   set @customertypestatus2 = 1;
   if customertypestatus = -1 then set @customertypestatus = 0; set @customertypestatus2 = 1; end if;
   if customertypestatus = 0 then set @customertypestatus = 0; set @customertypestatus2 = 0; end if;
   if customertypestatus = 1 then set @customertypestatus = 1; set @customertypestatus2 = 1; end if;

    if searchwords = '' then SET @statement = concat( 'Select count(*) into @customertypetotalquantity from ', ' wp_expenseszyx987_customertype WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @customertypestatus, @customertypestatus2;
       DEALLOCATE PREPARE stmt;

    elseif searchwords != '' then SET @statement = concat( 'Select count(*) into @customertypetotalquantity from ', ' wp_expenseszyx987_customertype WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @customertypestatus, @customertypestatus2;
       DEALLOCATE PREPARE stmt;
    end if;

    SET customertypetotalquantity = @customertypetotalquantity;

--  ==========================================================================================================================

    if myorder = 'asc' && mysortby = 'id' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_customertype WHERE status IN (?,?) order by id ASC LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customertypestatus, @customertypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_customertype WHERE name like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @customertypestatus, @customertypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'name' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_customertype WHERE status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customertypestatus, @customertypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_customertype WHERE name like ? AND status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @customertypestatus, @customertypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_customertype WHERE status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customertypestatus, @customertypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_customertype WHERE name like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @customertypestatus, @customertypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_customertype WHERE status IN (?,?) order by id DESC LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customertypestatus, @customertypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_customertype WHERE name like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @customertypestatus, @customertypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'name' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_customertype WHERE status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customertypestatus, @customertypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_customertype WHERE name like ? AND status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @customertypestatus, @customertypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_customertype WHERE status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @customertypestatus, @customertypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_customertype WHERE name like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @customertypestatus, @customertypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;

END  $$
DROP PROCEDURE IF EXISTS `wp_expenseszyx987_getDepartmentList`$$
CREATE PROCEDURE `wp_expenseszyx987_getDepartmentList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),
   IN searchwords varchar(200),
   IN departmentstatus int,
   OUT departmenttotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default '';
   declare orderbyclause varchar(10000) default '';

   set @searchwords = CONCAT('%', searchwords, '%');
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;

   set @departmentstatus = 0;
   set @departmentstatus2 = 1;
   if departmentstatus = -1 then set @departmentstatus = 0; set @departmentstatus2 = 1; end if;
   if departmentstatus = 0 then set @departmentstatus = 0; set @departmentstatus2 = 0; end if;
   if departmentstatus = 1 then set @departmentstatus = 1; set @departmentstatus2 = 1; end if;

    if searchwords = '' then SET @statement = concat( 'Select count(*) into @departmenttotalquantity from ', ' wp_expenseszyx987_department WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @departmentstatus, @departmentstatus2;
       DEALLOCATE PREPARE stmt;

    elseif searchwords != '' then SET @statement = concat( 'Select count(*) into @departmenttotalquantity from ', ' wp_expenseszyx987_department WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @departmentstatus, @departmentstatus2;
       DEALLOCATE PREPARE stmt;
    end if;

    SET departmenttotalquantity = @departmenttotalquantity;

--  ==========================================================================================================================

    if myorder = 'asc' && mysortby = 'id' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_department WHERE status IN (?,?) order by id ASC LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_department WHERE name like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'name' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_department WHERE status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_department WHERE name like ? AND status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_department WHERE status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_department WHERE name like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_department WHERE status IN (?,?) order by id DESC LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_department WHERE name like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'name' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_department WHERE status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_department WHERE name like ? AND status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = 'SELECT * from wp_expenseszyx987_department WHERE status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?';
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_department WHERE name like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;

END  $$
DROP PROCEDURE IF EXISTS `wp_expenseszyx987_getLogList`$$
CREATE PROCEDURE `wp_expenseszyx987_getLogList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int,
   IN mysortby varchar(200),
   IN myorder varchar(200),
   IN searchwords varchar(200),
   IN searchtype varchar(200),
   IN processname varchar(200),
   IN userid int,
   IN logstatus int,
   OUT logtotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';

   set @searchwords = CONCAT('%', searchwords, '%');
   set @searchtype = searchtype;
   set @processname = processname;
   set @userid = userid;   
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;

   set @logstatus = 0;
   set @logstatus2 = 1;
   if logstatus = -1 then set @logstatus = 0; set @logstatus2 = 1; end if;
   if logstatus = 0 then set @logstatus = 0; set @logstatus2 = 0; end if;
   if logstatus = 1 then set @logstatus = 1; set @logstatus2 = 1; end if;

    if searchwords = '' && searchtype = '' && processname = '' && userid <= 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_expenseszyx987_log WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;

    elseif searchwords != '' && searchtype = '' && processname = '' && userid <= 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_expenseszyx987_log WHERE description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;


    --  -------------------------
    elseif searchwords = '' && searchtype != '' && processname = '' && userid <= 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_expenseszyx987_log WHERE `type` = ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchtype, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;

    elseif searchwords != '' && searchtype != '' && processname = '' && userid <= 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_expenseszyx987_log WHERE `type` = ? AND description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchtype, @searchwords, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;


    --   ####################################################################
 
    elseif searchwords = '' && searchtype = '' && processname != '' && userid <= 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_expenseszyx987_log WHERE processname = ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;

    elseif searchwords != '' && searchtype = '' && processname != '' && userid <= 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_expenseszyx987_log WHERE processname = ? AND description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchwords, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;


    --  -------------------------
    elseif searchwords = '' && searchtype != '' && processname != '' && userid <= 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_expenseszyx987_log WHERE processname = ? AND `type` = ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchtype, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;

    elseif searchwords != '' && searchtype != '' && processname != '' && userid <= 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_expenseszyx987_log WHERE processname = ? AND `type` = ? AND description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchtype, @searchwords, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;


    -- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    -- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&   
    -- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    -- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    elseif searchwords = '' && searchtype = '' && processname = '' && userid > 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_expenseszyx987_log WHERE userid = ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;

    elseif searchwords != '' && searchtype = '' && processname = '' && userid > 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_expenseszyx987_log WHERE userid = ? AND description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchwords, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;


    --  -------------------------
    elseif searchwords = '' && searchtype != '' && processname = '' && userid > 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_expenseszyx987_log WHERE userid = ? AND `type` = ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchtype, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;

    elseif searchwords != '' && searchtype != '' && processname = '' && userid > 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_expenseszyx987_log WHERE userid = ? AND `type` = ? AND description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchtype, @searchwords, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;


    --   ####################################################################

    elseif searchwords = '' && searchtype = '' && processname != '' && userid > 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;

    elseif searchwords != '' && searchtype = '' && processname != '' && userid > 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchwords, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;


    --  -------------------------
    elseif searchwords = '' && searchtype != '' && processname != '' && userid > 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchtype, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;

    elseif searchwords != '' && searchtype != '' && processname != '' && userid > 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchtype, @searchwords, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;
    end if;

   SET logtotalquantity = @logtotalquantity;

--  ==========================================================================================================================
--  ==========================================================================================================================

    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchtype = '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchtype = '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE description like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'type' && searchwords = '' && searchtype = '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'type' && searchwords != '' && searchtype = '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE description like ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype = '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype = '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE description like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchtype = '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchtype = '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE description like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'type' && searchwords = '' && searchtype = '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'type' && searchwords != '' && searchtype = '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE description like ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype = '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype = '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    elseif myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchtype != '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE `type` = ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchtype != '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE `type` = ? AND description like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'type' && searchwords = '' && searchtype != '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE `type` = ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'type' && searchwords != '' && searchtype != '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE `type` = ? AND description like ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype != '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE `type` = ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype != '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE `type` = ? AND description like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchtype != '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE `type` = ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchtype != '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE `type` = ? AND description like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'type' && searchwords = '' && searchtype != '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE `type` = ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'type' && searchwords != '' && searchtype != '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE `type` = ? AND description like ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype != '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE `type` = ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype != '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE `type` = ? AND description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    -- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2

    elseif myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchtype = '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE processname = ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @processname, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchtype = '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE processname = ? AND description like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'type' && searchwords = '' && searchtype = '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE processname = ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'type' && searchwords != '' && searchtype = '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE processname = ? AND description like ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype = '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE processname = ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype = '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE processname = ? AND description like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchtype = '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE processname = ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @processname, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchtype = '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE processname = ? AND description like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'type' && searchwords = '' && searchtype = '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE processname = ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'type' && searchwords != '' && searchtype = '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE processname = ? AND description like ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype = '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE processname = ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype = '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE processname = ? AND description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    elseif myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchtype != '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE processname = ? AND `type` = ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchtype != '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'type' && searchwords = '' && searchtype != '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE processname = ? AND `type` = ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'type' && searchwords != '' && searchtype != '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype != '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE processname = ? AND `type` = ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype != '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchtype != '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE processname = ? AND `type` = ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchtype != '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'type' && searchwords = '' && searchtype != '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE processname = ? AND `type` = ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'type' && searchwords != '' && searchtype != '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype != '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE processname = ? AND `type` = ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype != '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    --    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    --    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    --    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    elseif myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchtype = '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchtype = '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND description like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'type' && searchwords = '' && searchtype = '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'type' && searchwords != '' && searchtype = '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND description like ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype = '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype = '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND description like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchtype = '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchtype = '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND description like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'type' && searchwords = '' && searchtype = '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'type' && searchwords != '' && searchtype = '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND description like ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype = '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype = '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    elseif myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchtype != '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND `type` = ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchtype != '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND `type` = ? AND description like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'type' && searchwords = '' && searchtype != '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND `type` = ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'type' && searchwords != '' && searchtype != '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND `type` = ? AND description like ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype != '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND `type` = ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype != '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND `type` = ? AND description like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchtype != '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND `type` = ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchtype != '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND `type` = ? AND description like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'type' && searchwords = '' && searchtype != '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND `type` = ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'type' && searchwords != '' && searchtype != '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND `type` = ? AND description like ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype != '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND `type` = ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype != '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND `type` = ? AND description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    -- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2    

    elseif myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchtype = '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchtype = '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND description like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'type' && searchwords = '' && searchtype = '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'type' && searchwords != '' && searchtype = '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND description like ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype = '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype = '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND description like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchtype = '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchtype = '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND description like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'type' && searchwords = '' && searchtype = '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'type' && searchwords != '' && searchtype = '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND description like ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype = '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype = '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    elseif myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchtype != '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchtype != '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'type' && searchwords = '' && searchtype != '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'type' && searchwords != '' && searchtype != '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype != '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype != '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchtype != '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchtype != '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'type' && searchwords = '' && searchtype != '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'type' && searchwords != '' && searchtype != '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype != '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userid, @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype != '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_expenseszyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;

END  $$
DROP PROCEDURE IF EXISTS `wp_expenseszyx987_getRoleList`$$
CREATE PROCEDURE `wp_expenseszyx987_getRoleList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int,
   IN mysortby varchar(200),
   IN myorder varchar(200),
   IN searchwords varchar(200),
   IN rolestatus int,
   OUT roletotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default '';
   declare orderbyclause varchar(10000) default '';

   set @searchwords = CONCAT('%', searchwords, '%');
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;

   set @rolestatus = 0;
   set @rolestatus2 = 1;
   if rolestatus = -1 then set @rolestatus = 0; set @rolestatus2 = 1; end if;
   if rolestatus = 0 then set @rolestatus = 0; set @rolestatus2 = 0; end if;
   if rolestatus = 1 then set @rolestatus = 1; set @rolestatus2 = 1; end if;

    if searchwords = '' then SET @statement = concat( 'Select count(*) into @roletotalquantity from ', ' wp_expenseszyx987_role WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @rolestatus, @rolestatus2;
       DEALLOCATE PREPARE stmt;

    elseif searchwords != '' then SET @statement = concat( 'Select count(*) into @roletotalquantity from ', ' wp_expenseszyx987_role WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @rolestatus, @rolestatus2;
       DEALLOCATE PREPARE stmt;
    end if;

   SET roletotalquantity = @roletotalquantity;

--  ==========================================================================================================================

    if myorder = 'asc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_role WHERE status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @rolestatus, @rolestatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_role WHERE name like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @rolestatus, @rolestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_role WHERE status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @rolestatus, @rolestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_role WHERE name like ? AND status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @rolestatus, @rolestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_role WHERE status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @rolestatus, @rolestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_role WHERE name like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @rolestatus, @rolestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_role WHERE status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @rolestatus, @rolestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_role WHERE name like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @rolestatus, @rolestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_role WHERE status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @rolestatus, @rolestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_role WHERE name like ? AND status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @rolestatus, @rolestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_role WHERE status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @rolestatus, @rolestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_role WHERE name like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @rolestatus, @rolestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;

END  $$
DROP PROCEDURE IF EXISTS `wp_expenseszyx987_getExpenseList`$$
CREATE PROCEDURE `wp_expenseszyx987_getExpenseList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int,
   IN mysortby varchar(200),
   IN myorder varchar(200),
   IN wpprefix varchar(200),
   IN userid int,
   IN expensetypeid int,
   IN searchreferencenumber varchar(200),
   IN searchexpensenotes varchar(200),
   IN fromdate date,
   IN todate date,
   IN minexpenseamount DECIMAL(15,2),
   IN maxexpenseamount DECIMAL(15,2),
   IN customerid int,
   IN isrecurring int,
   IN expensestatus int,
   OUT expensetotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default '';
   declare orderbyclause varchar(10000) default '';

   set @userid = userid;
   set @expensetypeid = expensetypeid;

   set @searchreferencenumber = CONCAT('%', searchreferencenumber, '%');
   set @searchexpensenotes = CONCAT('%', searchexpensenotes, '%');

   set @pagenumber= pagenumber;
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;

   set @minexpenseamount = 0;  -- minexpenseamount;
   set @maxexpenseamount = 9999999999999;  -- maxexpenseamount;
   if minexpenseamount > 0 then set @minexpenseamount = minexpenseamount; end if;
   if maxexpenseamount > 0 then set @maxexpenseamount = maxexpenseamount; end if;

   set @fromdate = '1000-01-01';
   set @todate = '9999-12-31';
   if fromdate > '1000-01-01' then set @fromdate = fromdate; end if;
   if todate > '1000-01-01' && todate < '9999-12-31' then set @todate = todate; end if;

   set @isrecurring = 0;
   set @isrecurring2 = 1;
   if isrecurring = -1 then set @isrecurring = 0; set @isrecurring2 = 1; end if;
   if isrecurring = 0 then set @isrecurring = 0; set @isrecurring2 = 0; end if;
   if isrecurring = 1 then set @isrecurring = 1; set @isrecurring2 = 1; end if;

   set @customerid = customerid;

   set @expensestatus = 0;
   set @expensestatus2 = 1;
   if expensestatus = -1 then set @expensestatus = 0; set @expensestatus2 = 1; end if;
   if expensestatus = 0 then set @expensestatus = 0; set @expensestatus2 = 0; end if;
   if expensestatus = 1 then set @expensestatus = 1; set @expensestatus2 = 1; end if;

   set @wp_prefix = wpprefix;

   set @querypart_count1 =  concat('SELECT count(*) into @expensetotalquantity  '
                    ,     ' FROM wp_expenseszyx987_expense AS a '
                    ,     ' left join wp_expenseszyx987_users AS c '
                    ,     ' ON a.userid = c.userid '
                    ,     ' left join '
                    ,     @wp_prefix
                    ,     'usermeta e '
                    ,     " ON a.userid = e.user_id AND e.meta_key = 'first_name' "
                    ,     ' left join '
                    ,     @wp_prefix
                    ,     'usermeta e2 '
                    ,     " ON a.userid = e2.user_id AND e2.meta_key = 'last_name' "
                    ,     ' left join wp_expenseszyx987_type AS y '
                    ,     ' on a.expensetypeid = y.id '
                    ,     ' where c.status = 1 AND a.status IN (?,?)  '
                    );

    set @querypart_result1 =  concat("SELECT a.*, y.name as expensetypename, m.name as customername, cu.name as expensecurrencyname, q.name as recurringpaymentfrequencyname, x.rate as taxrate, CASE WHEN y.id IS NULL THEN 0 ELSE FORMAT(time_to_sec(timediff(concat(a.expensedate, ' ' ,a.recurringpaymentenddate), concat(a.expensedate, ' ' ,a.recurringpaymentstartdate) )) / 3600, 2) END as 'hours' "
                    ,     " , concat(IFNULL(e.meta_value,''),' ',IFNULL(e2.meta_value,'')) as username, concat(IFNULL(c.firstname,''),' ',IFNULL(c.lastname,'')) as username2  "
                    ,     ' FROM wp_expenseszyx987_expense AS a '
                    ,     ' left join wp_expenseszyx987_users AS c '
                    ,     ' ON a.userid = c.userid '
                    ,     ' left join '
                    ,     @wp_prefix
                    ,     'usermeta e '
                    ,     " ON a.userid = e.user_id AND e.meta_key = 'first_name' "
                    ,     ' left join '
                    ,     @wp_prefix
                    ,     'usermeta e2 '
                    ,     " ON a.userid = e2.user_id AND e2.meta_key = 'last_name' "
                    ,     ' left join wp_expenseszyx987_type AS y '
                    ,     ' on a.expensetypeid = y.id '
                    ,     ' left join wp_expenseszyx987_currency AS cu '
                    ,     ' on a.expensecurrencyid = cu.id '
                    ,     ' left join wp_expenseszyx987_customer AS m '
                    ,     ' on a.customerid = m.id '
                    ,     ' left join wp_expenseszyx987_recurringpaymentfrequency AS q '
                    ,     ' on a.recurringpaymentfrequencyid = q.id '
                    ,     ' left join wp_expenseszyx987_tax AS x '
                    ,     ' on a.taxrateid = x.id '
                    ,     ' where c.status = 1 AND a.status IN (?,?) '
                    );

    set  @querypart2 = '';
    if searchreferencenumber != '' then set @querypart2 = ' AND a.expensereferencenumber LIKE ? '; end if;

    set  @querypart3 = '';
    if searchexpensenotes != '' then set @querypart3 = ' AND a.expensenotes LIKE ? '; end if;

    set  @querypart4 = '';

    set  @querypart5 = '';
    if expensetypeid > 0 then set @querypart5 = ' AND a.expensetypeid = ? '; end if;

    set  @querypart6 = '';
    if userid > 0 then set @querypart6 = ' AND a.userid = ? '; end if;

    set  @querypart7 =  ' AND a.expensedate >= ? AND a.expensedate <= ? ';
    set  @querypart8 = ' AND a.expenseamount >= ? AND a.expenseamount <= ? ';
    set  @querypart9 = ' AND a.isrecurring IN (?,?) ';

    set  @querypart10 = '';
    if customerid > 0 then set @querypart10 = ' AND a.customerid = ? '; end if;

    if customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2;
       DEALLOCATE PREPARE stmt;

    elseif customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2;
       DEALLOCATE PREPARE stmt;

    elseif customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2;
       DEALLOCATE PREPARE stmt;

    elseif customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2;
       DEALLOCATE PREPARE stmt;


    elseif customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2;
       DEALLOCATE PREPARE stmt;

    elseif customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2;
       DEALLOCATE PREPARE stmt;

    elseif customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2;
       DEALLOCATE PREPARE stmt;

    elseif customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2;
       DEALLOCATE PREPARE stmt;


    elseif customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2;
       DEALLOCATE PREPARE stmt;

    elseif customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2;
       DEALLOCATE PREPARE stmt;

    elseif customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2;
       DEALLOCATE PREPARE stmt;

    elseif customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2;
       DEALLOCATE PREPARE stmt;


    elseif customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2;
       DEALLOCATE PREPARE stmt;

    elseif customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2;
       DEALLOCATE PREPARE stmt;

    elseif customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2;
       DEALLOCATE PREPARE stmt;

    elseif customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2;
       DEALLOCATE PREPARE stmt;


    --      ///////////////////////////////////////////////////////

    elseif customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid;
       DEALLOCATE PREPARE stmt;

    elseif customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid;
       DEALLOCATE PREPARE stmt;

    elseif customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid;
       DEALLOCATE PREPARE stmt;

    elseif customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid;
       DEALLOCATE PREPARE stmt;


    elseif customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid;
       DEALLOCATE PREPARE stmt;

    elseif customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid;
       DEALLOCATE PREPARE stmt;

    elseif customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid;
       DEALLOCATE PREPARE stmt;

    elseif customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid;
       DEALLOCATE PREPARE stmt;


    elseif customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid;
       DEALLOCATE PREPARE stmt;

    elseif customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid;
       DEALLOCATE PREPARE stmt;

    elseif customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid;
       DEALLOCATE PREPARE stmt;

    elseif customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid;
       DEALLOCATE PREPARE stmt;


    elseif customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid;
       DEALLOCATE PREPARE stmt;

    elseif customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid;
       DEALLOCATE PREPARE stmt;

    elseif customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid;
       DEALLOCATE PREPARE stmt;

    elseif customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid;
       DEALLOCATE PREPARE stmt;
    end if;

    SET expensetotalquantity = @expensetotalquantity;

    if expensetotalquantity > 0 && amountperpage > 0 &&  pagenumber > CEIL(expensetotalquantity/amountperpage) then set @myoffset = (CEIL(expensetotalquantity/amountperpage) - 1) * @amountperpage;  end if;
--  ==========================================================================================================================
--  ==========================================================================================================================

    if myorder = 'asc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

    elseif myorder = 'asc' && mysortby = 'id' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'id' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'id' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'id' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'id' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    --   ---------------------------------------------
    --   ---------------------------------------------
    
    elseif myorder = 'asc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

    elseif myorder = 'asc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
   
    elseif myorder = 'asc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------
    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------
    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------
    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'asc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber ASC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    elseif myorder = 'desc' && mysortby = 'id' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'id' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'id' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'id' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'id' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.id DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------
    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    elseif myorder = 'desc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'userid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by a.userid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------
    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensedate' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensedate DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------
    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensetypeid' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensetypeid DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------
    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    
    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid <= 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
--    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @expensestatus, @expensestatus2, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then  
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then  
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber = '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes = '' && expensetypeid > 0 && userid > 0 then
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid <= 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid <= 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;

    elseif myorder = 'desc' && mysortby = 'expensereferencenumber' && customerid > 0 && searchreferencenumber != '' && searchexpensenotes != '' && expensetypeid > 0 && userid > 0 then 
       SET @querypart11 = ' order by expensereferencenumber DESC LIMIT ? OFFSET ? ';
       SET @statement = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5, @querypart6, @querypart7, @querypart8, @querypart9, @querypart10, @querypart11);
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @expensestatus, @expensestatus2, @searchreferencenumber, @searchexpensenotes, @expensetypeid, @userid, @fromdate, @todate, @minexpenseamount, @maxexpenseamount, @isrecurring, @isrecurring2, @customerid, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;
END  $$
DROP PROCEDURE IF EXISTS `wp_expenseszyx987_getTypecategoryList`$$
CREATE PROCEDURE `wp_expenseszyx987_getTypecategoryList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),   
   IN typecategorystatus int,
   OUT typecategorytotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';

   set @searchwords = CONCAT('%', searchwords, '%');
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;

   set @typecategorystatus = 0;
   set @typecategorystatus2 = 1;
   if typecategorystatus = -1 then set @typecategorystatus = 0; set @typecategorystatus2 = 1; end if;
   if typecategorystatus = 0 then set @typecategorystatus = 0; set @typecategorystatus2 = 0; end if;
   if typecategorystatus = 1 then set @typecategorystatus = 1; set @typecategorystatus2 = 1; end if;

    if searchwords = '' then SET @statement = concat( 'Select count(*) into @typecategorytotalquantity from ', ' wp_expenseszyx987_typecategory WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @typecategorystatus, @typecategorystatus2;
       DEALLOCATE PREPARE stmt;    

    elseif searchwords != '' then SET @statement = concat( 'Select count(*) into @typecategorytotalquantity from ', ' wp_expenseszyx987_typecategory WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @typecategorystatus, @typecategorystatus2;
       DEALLOCATE PREPARE stmt;
    end if;

   SET typecategorytotalquantity = @typecategorytotalquantity;

--  ==========================================================================================================================

    if myorder = 'asc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_typecategory WHERE status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @typecategorystatus, @typecategorystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_typecategory WHERE name like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @typecategorystatus, @typecategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_typecategory WHERE status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @typecategorystatus, @typecategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_typecategory WHERE name like ? AND status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @typecategorystatus, @typecategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_typecategory WHERE status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @typecategorystatus, @typecategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_typecategory WHERE name like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @typecategorystatus, @typecategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_typecategory WHERE status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @typecategorystatus, @typecategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_typecategory WHERE name like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @typecategorystatus, @typecategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_typecategory WHERE status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @typecategorystatus, @typecategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_typecategory WHERE name like ? AND status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @typecategorystatus, @typecategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_typecategory WHERE status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @typecategorystatus, @typecategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_typecategory WHERE name like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @typecategorystatus, @typecategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;

END  $$
DROP PROCEDURE IF EXISTS `wp_expenseszyx987_getTypeList`$$
CREATE PROCEDURE `wp_expenseszyx987_getTypeList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int,
   IN mysortby varchar(200),
   IN myorder varchar(200),
   IN searchwords varchar(200),
   IN typestatus int,
   OUT typetotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';

   set @searchwords = CONCAT('%', searchwords, '%');
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;

   set @typestatus = 0;
   set @typestatus2 = 1;
   if typestatus = -1 then set @typestatus = 0; set @typestatus2 = 1; end if;
   if typestatus = 0 then set @typestatus = 0; set @typestatus2 = 0; end if;
   if typestatus = 1 then set @typestatus = 1; set @typestatus2 = 1; end if;

    if searchwords = '' then SET @statement = concat( 'Select count(*) into @typetotalquantity from ', ' wp_expenseszyx987_type WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @typestatus, @typestatus2;
       DEALLOCATE PREPARE stmt;

    elseif searchwords != '' then SET @statement = concat( 'Select count(*) into @typetotalquantity from ', ' wp_expenseszyx987_type WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @typestatus, @typestatus2;
       DEALLOCATE PREPARE stmt;
    end if;

   SET typetotalquantity = @typetotalquantity;

--  ==========================================================================================================================

    if myorder = 'asc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT t.*,tc.name 'typecategoryname' from wp_expenseszyx987_type t left join wp_expenseszyx987_typecategory tc ON t.typecategoryid = tc.id  WHERE t.status IN (?,?) order by t.id ASC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @typestatus, @typestatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && searchwords != '' then SET @statement = "SELECT t.*,tc.name 'typecategoryname' from wp_expenseszyx987_type t left join wp_expenseszyx987_typecategory tc ON t.typecategoryid = tc.id WHERE t.name like ? AND t.status IN (?,?) order by t.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @typestatus, @typestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT t.*,tc.name 'typecategoryname' from wp_expenseszyx987_type t left join wp_expenseszyx987_typecategory tc ON t.typecategoryid = tc.id WHERE t.status IN (?,?) order by t.name ASC,t.id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @typestatus, @typestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'name' && searchwords != '' then SET @statement = "SELECT t.*,tc.name 'typecategoryname' from wp_expenseszyx987_type t left join wp_expenseszyx987_typecategory tc ON t.typecategoryid = tc.id WHERE t.name like ? AND t.status IN (?,?) order by t.name ASC,t.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @typestatus, @typestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT t.*,tc.name 'typecategoryname' from wp_expenseszyx987_type t left join wp_expenseszyx987_typecategory tc ON t.typecategoryid = tc.id WHERE t.status IN (?,?) order by t.createtimeutc ASC,t.id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @typestatus, @typestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = "SELECT t.*,tc.name 'typecategoryname' from wp_expenseszyx987_type t left join wp_expenseszyx987_typecategory tc ON t.typecategoryid = tc.id WHERE t.name like ? AND t.status IN (?,?) order by t.createtimeutc ASC,t.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @typestatus, @typestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT t.*,tc.name 'typecategoryname' from wp_expenseszyx987_type t left join wp_expenseszyx987_typecategory tc ON t.typecategoryid = tc.id WHERE t.status IN (?,?) order by t.id DESC LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @typestatus, @typestatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && searchwords != '' then SET @statement = "SELECT t.*,tc.name 'typecategoryname' from wp_expenseszyx987_type t left join wp_expenseszyx987_typecategory tc ON t.typecategoryid = tc.id WHERE t.name like ? AND t.status IN (?,?) order by t.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @typestatus, @typestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT t.*,tc.name 'typecategoryname' from wp_expenseszyx987_type t left join wp_expenseszyx987_typecategory tc ON t.typecategoryid = tc.id WHERE t.status IN (?,?) order by t.name DESC,t.id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @typestatus, @typestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'name' && searchwords != '' then SET @statement = "SELECT t.*,tc.name 'typecategoryname' from wp_expenseszyx987_type t left join wp_expenseszyx987_typecategory tc ON t.typecategoryid = tc.id WHERE t.name like ? AND t.status IN (?,?) order by t.name DESC,t.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @typestatus, @typestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT t.*,tc.name 'typecategoryname' from wp_expenseszyx987_type t left join wp_expenseszyx987_typecategory tc ON t.typecategoryid = tc.id WHERE t.status IN (?,?) order by t.createtimeutc DESC,t.id LIMIT ? OFFSET ?";
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @typestatus, @typestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = "SELECT t.*,tc.name 'typecategoryname' from wp_expenseszyx987_type t left join wp_expenseszyx987_typecategory tc ON t.typecategoryid = tc.id WHERE t.name like ? AND t.status IN (?,?) order by t.createtimeutc DESC,t.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @typestatus, @typestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;
END  $$
DROP PROCEDURE IF EXISTS `wp_expenseszyx987_getUserList`$$
CREATE PROCEDURE `wp_expenseszyx987_getUserList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),
   IN userid int,
   IN searchuserfullname varchar(200),
   IN searchemail varchar(200),
   IN reportto int,
   IN canreadinadminpage int,
   IN cancreateinadminpage int,
   IN caneditinadminpage int,
   IN canactivateinadminpage int,
   IN candeactivateinadminpage int,
   IN roleid int,
   IN departmentid int,
   IN userstatus int,
   OUT usertotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default '';
   declare orderbyclause varchar(10000) default '';

   set @userid = userid;
   set @searchuserfullname = CONCAT('%', searchuserfullname, '%');
   set @searchemail = CONCAT('%', searchemail, '%');
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;

   set @reportto = reportto;
   set @canreadinadminpage = canreadinadminpage;
   set @cancreateinadminpage = cancreateinadminpage;
   set @caneditinadminpage = caneditinadminpage;
   set @canactivateinadminpage = canactivateinadminpage;
   set @candeactivateinadminpage = candeactivateinadminpage;
   set @roleid = roleid;
   set @departmentid = departmentid;

   set @canreadinadminpage = 0;
   set @canreadinadminpage2 = 1;
   if canreadinadminpage = -1 then set @canreadinadminpage = 0; set @canreadinadminpage2 = 1; end if;
   if canreadinadminpage = 0 then set @canreadinadminpage = 0; set @canreadinadminpage2 = 0; end if;
   if canreadinadminpage = 1 then set @canreadinadminpage = 1; set @canreadinadminpage2 = 1; end if;

   set @cancreateinadminpage = 0;
   set @cancreateinadminpage2 = 1;
   if cancreateinadminpage = -1 then set @cancreateinadminpage = 0; set @cancreateinadminpage2 = 1; end if;
   if cancreateinadminpage = 0 then set @cancreateinadminpage = 0; set @cancreateinadminpage2 = 0; end if;
   if cancreateinadminpage = 1 then set @cancreateinadminpage = 1; set @cancreateinadminpage2 = 1; end if;   

   set @caneditinadminpage = 0;
   set @caneditinadminpage2 = 1;
   if caneditinadminpage = -1 then set @caneditinadminpage = 0; set @caneditinadminpage2 = 1; end if;
   if caneditinadminpage = 0 then set @caneditinadminpage = 0; set @caneditinadminpage2 = 0; end if;
   if caneditinadminpage = 1 then set @caneditinadminpage = 1; set @caneditinadminpage2 = 1; end if; 

   set @canactivateinadminpage = 0;
   set @canactivateinadminpage2 = 1;
   if canactivateinadminpage = -1 then set @canactivateinadminpage = 0; set @canactivateinadminpage2 = 1; end if;
   if canactivateinadminpage = 0 then set @canactivateinadminpage = 0; set @canactivateinadminpage2 = 0; end if;
   if canactivateinadminpage = 1 then set @canactivateinadminpage = 1; set @canactivateinadminpage2 = 1; end if;   

   set @candeactivateinadminpage = 0;
   set @candeactivateinadminpage2 = 1;
   if candeactivateinadminpage = -1 then set @candeactivateinadminpage = 0; set @candeactivateinadminpage2 = 1; end if;
   if candeactivateinadminpage = 0 then set @candeactivateinadminpage = 0; set @candeactivateinadminpage2 = 0; end if;
   if candeactivateinadminpage = 1 then set @candeactivateinadminpage = 1; set @candeactivateinadminpage2 = 1; end if;

   set @userstatus = 0;
   set @userstatus2 = 1;
   if userstatus = -1 then set @userstatus = 0; set @userstatus2 = 1; end if;
   if userstatus = 0 then set @userstatus = 0; set @userstatus2 = 0; end if;
   if userstatus = 1 then set @userstatus = 1; set @userstatus2 = 1; end if;

   set @querypart_count1 =  concat("Select count(*) into @usertotalquantity from  wp_expenseszyx987_users a WHERE a.status IN (?,?) "
                         , " AND a.canreadinadminpage IN (?,?)  "
                         , " AND a.cancreateinadminpage IN (?,?)  "
                         , " AND a.caneditinadminpage IN (?,?)  "
                         , " AND a.canactivateinadminpage IN (?,?)  "
                         , " AND a.candeactivateinadminpage IN (?,?)  "
   );

    set  @querypart1 = "";
    if userid > 0 then set @querypart1 = " AND userid = ?  "; end if;

    set @querypart_count1 =  concat(@querypart_count1, @querypart1);


    set @querypart_result1 = concat("Select a.*,r.name 'rolename',d.name 'departmentname' from  wp_expenseszyx987_users a "
                      , " LEFT JOIN  wp_expenseszyx987_role r "
                      , " ON a.roleid = r.id "
                      , " LEFT JOIN  wp_expenseszyx987_department d "
                      , " ON a.departmentid = d.id "
                      , " WHERE a.status IN (?,?)  "                      
                      , " AND a.canreadinadminpage IN (?,?)  "
                      , " AND a.cancreateinadminpage IN (?,?)  "
                      , " AND a.caneditinadminpage IN (?,?)  "
                      , " AND a.canactivateinadminpage IN (?,?)  "
                      , " AND a.candeactivateinadminpage IN (?,?)  "

    );
    set @querypart_result1 =  concat(@querypart_result1, @querypart1);

    set  @querypart2 = "";
    if searchuserfullname != '' then set @querypart2 = " AND concat(a.firstname, ' ', a.lastname) like ?  "; end if;

     set  @querypart3 = "";
    if searchemail != '' then set @querypart3 = " AND a.email like ?  "; end if;    

    set  @querypart4 = "";
    if reportto > 0 then set @querypart4 = " AND a.reportto = ?  "; end if;  

    set  @querypart5 = "";
    if roleid > 0 then set @querypart10 = " AND a.roleid = ?  "; end if;

    set  @querypart6 = "";
    if departmentid > 0 then set @querypart11 = " AND a.departmentid = ?  "; end if;

    if reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail;
       DEALLOCATE PREPARE stmt;

 --   ------------------------------------   
    elseif reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail;
       DEALLOCATE PREPARE stmt;



    --    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&  2  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    elseif reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @departmentid;
       DEALLOCATE PREPARE stmt;

 --   ------------------------------------   
    elseif reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @departmentid;
       DEALLOCATE PREPARE stmt;


    --    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&   3   &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&   

    elseif reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @roleid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @roleid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @roleid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @roleid;
       DEALLOCATE PREPARE stmt;

 --   ------------------------------------   
    elseif reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @roleid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @roleid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @roleid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @roleid;
       DEALLOCATE PREPARE stmt;


    --    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&   4    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    elseif reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @roleid, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @roleid, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @roleid, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @roleid, @departmentid;
       DEALLOCATE PREPARE stmt;

 --   ------------------------------------   
    elseif reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @roleid, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @roleid, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @roleid, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @roleid, @departmentid;
       DEALLOCATE PREPARE stmt;


    --    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&    5    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&   

    elseif reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto;
       DEALLOCATE PREPARE stmt;

 --   ------------------------------------   
    elseif reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto;
       DEALLOCATE PREPARE stmt;


    --    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&   6    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    elseif reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @departmentid;
       DEALLOCATE PREPARE stmt;

 --   ------------------------------------   
    elseif reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @departmentid;
       DEALLOCATE PREPARE stmt;


    --    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&    7   &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&  

    elseif reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @roleid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @roleid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @roleid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @roleid;
       DEALLOCATE PREPARE stmt;

 --   ------------------------------------   
    elseif reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @roleid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @roleid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @roleid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @roleid;
       DEALLOCATE PREPARE stmt;


    --    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&   8    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    elseif reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @roleid, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @roleid, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @roleid, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @roleid, @departmentid;
       DEALLOCATE PREPARE stmt;

 --   ------------------------------------   
    elseif reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @roleid, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @roleid, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @roleid, @departmentid;
       DEALLOCATE PREPARE stmt;

    elseif reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then SET @statement = concat(@querypart_count1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @roleid, @departmentid;
       DEALLOCATE PREPARE stmt;
    end if;

    --    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&    

