<commit_before><?php

/**
 *  ocs-webserver
 *
 *  Copyright 2016 by pling GmbH.
 *
 *    This file is part of ocs-webserver.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as
 *    published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * */
class Backend_MemberPayoutCliController extends Local_Controller_Action_CliAbstract {

    public static $ACTION_PAYOUT = "payout";
    public static $CONTEXT_ALL = "all";
    
    public static $PAYOUT_STATUS_NEW = 0;
    public static $PAYOUT_STATUS_REQUESTED = 1;
    public static $PAYOUT_STATUS_PROCESSED = 10;
    public static $PAYOUT_STATUS_COMPLETED = 100;
    public static $PAYOUT_STATUS_DENIED = 30;
    public static $PAYOUT_STATUS_ERROR = 99;
    
    
    /** @var Zend_Config */
    protected $_config;
    
    /** @var \Zend_Log */
    protected $_logger;

    /**
     * Run php code as cronjob.
     * I.e.:
     * /usr/bin/php /var/www/pling.it/pling/scripts/cron.php -a /backend/member-payout-cli/run/action/payout/context/all >> /var/www/ocs-www/logs/masspay.log $
     *
     * @see Local_Controller_Action_CliInterface::runAction()
     */
    public function runAction() {
        $this->initVars();
        
        echo "Start runAction\n";
        echo "AppEnv: " . APPLICATION_ENV . "\n";
        echo "SandboxActive: " . $this->_config->third_party->paypal->sandbox->active . "\n";
        echo "Endpoint: " . $this->_config->third_party->paypal->masspay->endpoint . "\n";
        echo "Test: " . $this->_config->third_party->paypal->test . "\n";

        $action = $this->getParam('action');
        $context = $this->getParam('context');

        echo "action: " . $action . "\n";
        echo "context: " . $context . "\n";
        if (isset($action) && $action == $this::$ACTION_PAYOUT && isset($context) && $context == $this::$CONTEXT_ALL) {
            $this->payoutMembers();
        }
    }

    public function initVars() {
        //init
        $this->_config = Zend_Registry::get('config');
        $this->_logger = Zend_Registry::get('logger');
    }

    private function payoutMembers() {
        echo "payoutMembers()\n";
        
        //Select all members for payout and write them in the payout table, ignore allways inserted members
        $this->prepareMasspaymentTable();
        
        //get payouts
        $allPayouts = $this->getPayouts();
        $payouts = array();
        
        //loop over all payouts, send max 250 per loop
        $i = 0;
        foreach ($allPayouts as $payout) {
            if($i < 250) {
                $payouts[] = $payout;
            } else {
                //start payout with Masspay
                $this->startMassPay($payouts);
                
                //reset counter and array for restart
                $i = 0;
                $payouts = array();
            }
            $i++;
        }
        
        if($i>0) {
            //send request for < 250 payouts
            $this->startMassPay($payouts);
        }
        
    }
    
    private function prepareMasspaymentTable() {
        echo "prepareMasspaymentTable()\n";
        $db = Zend_Db_Table::getDefaultAdapter();

        $sql = "SELECT * FROM stat_dl_payment_last_month s";
        
        //if(!$this->_config->third_party->paypal->sandbox->active) {
        //    $sql.=" WHERE s.num_downloads > 100";
        //}

        $stmt = $db->query($sql);
        $payouts = $stmt->fetchAll();
        
        echo "Select ".count($payouts)." payouts. Sql: ".$sql."\n";
        
        //Insert/Update users in table project_rating
        foreach ($payouts as $payout) {
            //Insert item in payment table
            //INSERT IGNORE INTO `pling`.`payout` (`yearmonth`, `member_id`, `amount`) VALUES ('201612', '223978', '181.0500');<commit_msg>masspay<commit_after><?php

/**
 *  ocs-webserver
 *
 *  Copyright 2016 by pling GmbH.
 *
 *    This file is part of ocs-webserver.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as
 *    published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * */
class Backend_MemberPayoutCliController extends Local_Controller_Action_CliAbstract {

    public static $ACTION_PAYOUT = "payout";
    public static $CONTEXT_ALL = "all";
    
    public static $PAYOUT_STATUS_NEW = 0;
    public static $PAYOUT_STATUS_REQUESTED = 1;
    public static $PAYOUT_STATUS_PROCESSED = 10;
    public static $PAYOUT_STATUS_COMPLETED = 100;
    public static $PAYOUT_STATUS_DENIED = 30;
    public static $PAYOUT_STATUS_ERROR = 99;
    
    
    /** @var Zend_Config */
    protected $_config;
    
    /** @var \Zend_Log */
    protected $_logger;

    /**
     * Run php code as cronjob.
     * I.e.:
     * /usr/bin/php /var/www/pling.it/pling/scripts/cron.php -a /backend/member-payout-cli/run/action/payout/context/all >> /var/www/ocs-www/logs/masspay.log $
     *
     * @see Local_Controller_Action_CliInterface::runAction()
     */
    public function runAction() {
        $this->initVars();
        
        echo "Start runAction\n";
        echo "AppEnv: " . APPLICATION_ENV . "\n";
        echo "SandboxActive: " . $this->_config->third_party->paypal->sandbox->active . "\n";
        echo "Endpoint: " . $this->_config->third_party->paypal->masspay->endpoint . "\n";
        echo "Test: " . $this->_config->third_party->paypal->test . "\n";

        $action = $this->getParam('action');
        $context = $this->getParam('context');

        echo "action: " . $action . "\n";
        echo "context: " . $context . "\n";
        if (isset($action) && $action == $this::$ACTION_PAYOUT && isset($context) && $context == $this::$CONTEXT_ALL) {
            $this->payoutMembers();
        }
    }

    public function initVars() {
        //init
        $this->_config = Zend_Registry::get('config');
        $this->_logger = Zend_Registry::get('logger');
    }

    private function payoutMembers() {
        echo "payoutMembers()\n";
        
        //Select all members for payout and write them in the payout table, ignore allways inserted members
        $this->prepareMasspaymentTable();
        
        //get payouts
        $allPayouts = $this->getPayouts();
        $payouts = array();
        
        //loop over all payouts, send max 250 per loop
        $i = 0;
        foreach ($allPayouts as $payout) {
            if($i < 250) {
                $payouts[] = $payout;
            } else {
                //start payout with Masspay
                $this->startMassPay($payouts);
                
                //reset counter and array for restart
                $i = 0;
                $payouts = array();
            }
            $i++;
        }
        
        if($i>0) {
            //send request for < 250 payouts
            $this->startMassPay($payouts);
        }
        
    }
    
    private function prepareMasspaymentTable() {
        echo "prepareMasspaymentTable()\n";
        $db = Zend_Db_Table::getDefaultAdapter();

        $sql = "SELECT * FROM stat_dl_payment_last_month s WHERE s.amount >= 1";
        
        $stmt = $db->query($sql);
        $payouts = $stmt->fetchAll();
        
        echo "Select ".count($payouts)." payouts. Sql: ".$sql."\n";
        
        //Insert/Update users in table project_rating
        foreach ($payouts as $payout) {
            //Insert item in payment table
            //INSERT IGNORE INTO `pling`.`payout` (`yearmonth`, `member_id`, `amount`) VALUES ('201612', '223978', '181.0500');
{'fraction_non_alphanumeric': 0.12745439758336238, 'fraction_numerical': 0.01138608109678169, 'mean_word_length': 2.114327062228654, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3957278', 'n_tokens_mistral': 2667, 'n_tokens_neox': 2532, 'n_words': 689}