/**
     * @service pay_affiliate read
     * @param $data
     */
    public function load(Gpf_Rpc_Params $params) {
        $data = new Gpf_Rpc_Data($params);

        $select = new Gpf_SqlBuilder_SelectBuilder();
        $select->select->add("au.username","userName");
        $select->select->add("au.firstname","firstName");
        $select->select->add("au.lastname","lastname");
        $select->select->add("SUM(t.".Pap_Db_Table_Transactions::COMMISSION.")", "payout");
        $select->select->add("pu.".Pap_Db_Table_Users::MINIMUM_PAYOUT, "minimumPayout");
        $select->select->add("IFNULL(po.".Gpf_Db_Table_FieldGroups::NAME.", 'undefined')", "payoutMethod");
        $select->from->add(Pap_Db_Table_Transactions::getName(), "t");
        $select->from->addInnerJoin(Pap_Db_Table_Users::getName(), "pu", "t.userid = pu.userid");
        $select->from->addInnerJoin(Gpf_Db_Table_Users::getName(), "gu", "pu.accountuserid = gu.accountuserid");
        $select->from->addInnerJoin(Gpf_Db_Table_AuthUsers::getName(), "au", "gu.authid = au.authid");
        $select->groupBy->add("t.".Pap_Db_Table_Transactions::USER_ID);
        $select->from->addLeftJoin(Gpf_Db_Table_FieldGroups::getName(), "po", "pu.payoutoptionid = po.fieldgroupid");
        $select->where->add("t.".Pap_Db_Table_Transactions::USER_ID, "=", $params->get("id"));
        $row = $select->getOneRow();

        $data->setValue("userName", $row->get("userName"));
        $data->setValue("firstName", $row->get("firstName"));
        $data->setValue("lastname", $row->get("lastname"));
        $data->setValue("payout", $row->get("payout"));
        $data->setValue("minimumPayout", $row->get("minimumPayout"));
        $data->setValue("payoutMethod", $row->get("payoutMethod"));

        return $data;
    }
    public function run() {
    	$time1 = microtime();
    	Pap3Compatibility_Migration_OutputWriter::logOnce("Deleting migrated data from existing PAP4 tables<br/>");

    	try {
    		$this->deleteTable(Pap_Db_Table_Campaigns::getName());
    		$this->deleteTable(Pap_Db_Table_CommissionGroups::getName());
    		$this->deleteTable(Pap_Db_Table_CommissionTypes::getName());
    		$this->deleteTable(Pap_Db_Table_Commissions::getName());
    		$this->deleteTable(Pap_Db_Table_UserInCommissionGroup::getName());
    		
    		$this->deleteTable(Gpf_Db_Table_FormFields::getName());
    		$this->deleteTable(Gpf_Db_Table_FieldGroups::getName());
    		
    		$this->deleteTable(Pap_Db_Table_Transactions::getName());
    		$this->deleteTable(Pap_Db_Table_Clicks::getName());
    		$this->deleteTable(Pap_Db_Table_RawClicks::getName());
    		$this->deleteTable(Pap_Db_Table_Impressions::getName());

    		$this->deleteTable(Pap_Db_Table_Banners::getName());
    		
    		$this->deleteTable(Gpf_Db_Table_FieldGroups::getName());
    		$this->deleteTable(Pap_Db_Table_PayoutsHistory::getName());
    		$this->deleteTable(Pap_Db_Table_Payouts::getName());
    		
    		$this->deleteTable(Gpf_Db_Table_Currencies::getName());
    		$this->deleteTable(Gpf_Db_Table_MailAccounts::getName());
    		
    	} catch(Exception $e) {
    		Pap3Compatibility_Migration_OutputWriter::log("&nbsp;&nbsp;Errror: ".$e->getMessage()."<br/>");
    	}

    	$time2 = microtime();
		Pap3Compatibility_Migration_OutputWriter::logDone($time1, $time2);
    }
 public static function getInstance()
 {
     if (self::$instance === null) {
         self::$instance = new self();
     }
     return self::$instance;
 }
 function buildFrom() {       
     $this->_selectBuilder->from->add(Pap_Db_Table_Payouts::getName(), "p"); 
     $this->_selectBuilder->from->addInnerJoin(Pap_Db_Table_Users::getName(), "pu", "p.userid = pu.userid");
     $this->_selectBuilder->from->addInnerJoin(Gpf_Db_Table_Users::getName(), "gu", "pu.accountuserid = gu.accountuserid");
     $this->_selectBuilder->from->addInnerJoin(Gpf_Db_Table_AuthUsers::getName(), "au", "gu.authid = au.authid");
     $this->_selectBuilder->from->addLeftJoin(Gpf_Db_Table_FieldGroups::getName(), "po", "pu.payoutoptionid = po.fieldgroupid");
     $this->_selectBuilder->from->addLeftJoin(Gpf_Db_Table_Currencies::getName(), "c", "p.currencyid = c.currencyid");
 }
    public static function getAllPayoutOptionNames() {
    	$result = new Gpf_Data_RecordSet('id');

    	$selectBuilder = new Gpf_SqlBuilder_SelectBuilder();
    	$selectBuilder->select->add(Gpf_Db_Table_FieldGroups::ID, 'id');
    	$selectBuilder->select->add(Gpf_Db_Table_FieldGroups::NAME, 'name');
    	$selectBuilder->from->add(Gpf_Db_Table_FieldGroups::getName());
    	$selectBuilder->where->add(Gpf_Db_Table_FieldGroups::TYPE, '=', Pap_Common_Constants::FIELDGROUP_TYPE_PAYOUTOPTION);

    	$result->load($selectBuilder);
    	
    	$resultArray = array();
    	foreach($result as $record) {
    		$resultArray[$record->get('id')] = Gpf_Lang::_localizeRuntime($record->get('name'));
    	}
    	return $resultArray;
    }
	protected function buildFrom() {
		$this->_selectBuilder->from->add(Gpf_Db_Table_FieldGroups::getName());
	}
    protected function buildFrom() {
        if ($this->filters->isFilter('orderid')) {
            $transSelect = new Gpf_SqlBuilder_SelectBuilder();
            $transSelect->select->setDistinct();
            $transSelect->select->add(Pap_Db_Table_Transactions::USER_ID);
            $transSelect->from->add(Pap_Db_Table_Transactions::getName());
            $this->addOrderIdFilterToSelect($transSelect, $this->filters);
            $this->_selectBuilder->from->addSubselect($transSelect, 't');
            $this->_selectBuilder->from->addInnerJoin(Pap_Db_Table_Users::getName(),
            'u', 'u.userid=t.userid');  
        } else {
            $this->_selectBuilder->from->add(Pap_Db_Table_Users::getName(), 'u');
        }

        $this->_selectBuilder->from->addInnerJoin(Gpf_Db_Table_Users::getName(),
            'gu', 'u.accountuserid=gu.accountuserid');
        $this->_selectBuilder->from->addInnerJoin(Gpf_Db_Table_AuthUsers::getName(),
            'au', 'au.authid=gu.authid');

        $this->_selectBuilder->from->addLeftJoin(Pap_Db_Table_Users::getName(),
            'pu', 'u.parentuserid=pu.userid');
        $this->_selectBuilder->from->addLeftJoin(Gpf_Db_Table_Users::getName(),
            'pgu', 'pu.accountuserid=pgu.accountuserid');
        $this->_selectBuilder->from->addLeftJoin(Gpf_Db_Table_AuthUsers::getName(),
            'pau', 'pau.authid=pgu.authid');

        $this->_selectBuilder->from->addLeftJoin(Gpf_Db_Table_FieldGroups::getName(),
            'pay', 'pay.fieldgroupid=u.payoutoptionid AND pay.rtype=\'P\' AND pay.rstatus=\'' .
        Gpf_Db_FieldGroup::ENABLED . '\'');

        $this->buildStatsFrom();
    }
    protected function buildFrom() {
        $this->_selectBuilder->from->add(Pap_Db_Table_Users::getName(), 'u');
        $this->_selectBuilder->from->addInnerJoin(Gpf_Db_Table_Users::getName(),
            'gu', 'u.accountuserid=gu.accountuserid AND u.' . Pap_Db_Table_Users::DELETED . ' = \'' . Gpf::NO . '\''.
        ' AND u.' .Pap_Db_Table_Users::TYPE . ' = \'' . Pap_Application::ROLETYPE_AFFILIATE . '\'');
        $this->_selectBuilder->from->addInnerJoin(Gpf_Db_Table_AuthUsers::getName(),
            'au', 'au.authid=gu.authid');

        $this->_selectBuilder->from->addLeftJoin(Pap_Db_Table_Users::getName(),
            'pu', 'u.parentuserid=pu.userid');
        $this->_selectBuilder->from->addLeftJoin(Gpf_Db_Table_Users::getName(),
            'pgu', 'pu.accountuserid=pgu.accountuserid');
        $this->_selectBuilder->from->addLeftJoin(Gpf_Db_Table_AuthUsers::getName(),
            'pau', 'pau.authid=pgu.authid');

        $this->_selectBuilder->from->addLeftJoin(Gpf_Db_Table_FieldGroups::getName(),
            'pay', 'pay.fieldgroupid=u.payoutoptionid AND pay.rtype=\'P\' AND pay.rstatus=\'' .
        Gpf_Db_FieldGroup::ENABLED . '\'');


        if ($this->areColumnsRequiredOrInFilter(array('lastlogin', 'loginscount', 'loginsnr'))) {
            $select = new Gpf_SqlBuilder_SelectBuilder();
            $select->select->add('MAX(login)', 'lastlogin');
            $select->select->add('accountuserid');
            $select->select->add('COUNT(*)', 'loginsnr');
            $select->from->add(Gpf_Db_Table_LoginsHistory::getName());
            $select->groupBy->add(Gpf_Db_Table_LoginsHistory::ACCOUNTUSERID);

            $this->_selectBuilder->from->addLeftJoin('('.$select->toString().')',
                    'lo', 'u.' . Gpf_Db_Table_LoginsHistory::ACCOUNTUSERID . '=lo.'.Gpf_Db_Table_LoginsHistory::ACCOUNTUSERID);
        }
        $this->buildStatsFrom();
    }
 function init()
 {
     $this->setTable(Gpf_Db_Table_FieldGroups::getInstance());
     parent::init();
 }
	function buildFrom() {
	    $this->subselect = new Gpf_SqlBuilder_SelectBuilder();
	    $this->subselect->select->add(Pap_Db_Table_Transactions::USER_ID);
	    $this->subselect->select->add(Pap_Db_Table_Transactions::DATE_INSERTED);
	    $this->subselect->select->add(Pap_Db_Table_Transactions::CAMPAIGN_ID);
	    $this->subselect->select->add("SUM(IF(".Pap_Db_Table_Transactions::R_STATUS." = '".Pap_Common_Constants::STATUS_APPROVED."', ".Pap_Db_Table_Transactions::COMMISSION.", 0))", Pap_Db_Table_Transactions::COMMISSION);
	    $this->subselect->select->add("SUM(IF(".Pap_Db_Table_Transactions::R_STATUS." = '".Pap_Common_Constants::STATUS_PENDING."', ".Pap_Db_Table_Transactions::COMMISSION.", 0))", "pendingAmount");
        $this->subselect->select->add("SUM(IF(".Pap_Db_Table_Transactions::R_STATUS." = '".Pap_Common_Constants::STATUS_DECLINED."', ".Pap_Db_Table_Transactions::COMMISSION.", 0))", "declinedAmount");
	    $this->subselect->from->add(Pap_Db_Table_Transactions::getName(), self::SUBSELECT_TANSACTIONS_TABLE_ALIAS);
	    $this->subselect->where->add(Pap_Db_Table_Transactions::PAYOUT_STATUS, '=', Pap_Common_Transaction::PAYOUT_UNPAID);
		$this->subselect->groupBy->add(Pap_Db_Table_Transactions::USER_ID);

		$this->_selectBuilder->from->addSubselect($this->subselect, self::TANSACTIONS_TABLE_ALIAS);
		$this->_selectBuilder->from->addInnerJoin(Pap_Db_Table_Users::getName(), "pu", self::TANSACTIONS_TABLE_ALIAS.".userid = pu.userid");
		$this->_selectBuilder->from->addInnerJoin(Gpf_Db_Table_Users::getName(), "gu", "pu.accountuserid = gu.accountuserid");
		$this->_selectBuilder->from->addInnerJoin(Gpf_Db_Table_AuthUsers::getName(), "au", "gu.authid = au.authid");
		$this->_selectBuilder->from->addLeftJoin(Gpf_Db_Table_FieldGroups::getName(), "po", "pu.payoutoptionid = po.fieldgroupid");
		$this->buildStatsFrom();
	}
 protected function buildFrom() {
     $this->_selectBuilder->from->add(Pap_Db_Table_Transactions::getName(), 't');
     $this->_selectBuilder->from->addInnerJoin(Pap_Db_Table_Users::getName(), 'pu', 't.userid = pu.userid');
     $this->_selectBuilder->from->addInnerJoin(Gpf_Db_Table_Users::getName(), 'gu', 'pu.accountuserid = gu.accountuserid');
     $this->_selectBuilder->from->addInnerJoin(Gpf_Db_Table_FieldGroups::getName(), 'po', 'pu.payoutoptionid = po.fieldgroupid');
 }