<?php

class Stats
{
    public function __construct(){}

    public static function chartDailyPayout($last = "7", $id)
    {
        if (is_array($last)) {
            $time = (int) $last['from'];
            $date = [];
            for($i=0; $i <= (int) ($last['to']-$last['from'])/86400; $i++) {
                $date[] = date('Y-m-d', (int) $time );
                $time = $time + 86400;
            }
        } else {
            $time = strtotime($last." days ago");
            $date = [];
            for($i=0; $i <= $last; $i++) {
                $date[] = date('Y-m-d', $time );
                $time = $time + 86400;
            }
        }

        $orders = [];
        foreach ($date as $v) {
                // echo $v;
    //            $where = array(
    //                'where' => array(
    //                    'trx_date' => $v
    //                )
    //            );
    //            if($order == "in") { $where['where']['trx_type'] = 'in'; };
    //            if($order == "out") { $where['where']['trx_type'] = 'out'; };
            $orders[] = self::getIdPayoutByDate($v, $id);
        }

    //        print_r($orders);

        return implode(',', $orders);
    }

    public static function getIdPayoutByDate($date, $id)
    {

        $q = Db::result("SELECT SUM(`payout`) AS `payout`
            FROM `conversions`
            WHERE `date` LIKE '%{$date}%'
            AND `subid` = '{$id}' ");

        if (Db::$num_rows > 0) {
            return number_format($q[0]->payout, 2);
        } else {
            return 0;
        }
    }

    public static function chartDate($last = "7")
    {
        if (is_array($last)) {
            $time = (int) $last['from'];
            $date = [];
            for($i=0; $i <= (int) ($last['to']-$last['from'])/86400; $i++) {
                $date[] = "'".date('d/m', (int) $time )."'";
                $time = $time + 86400;
            }
        } else {
            $time = strtotime($last." days ago");
            $date = [];
            for($i=0; $i <= $last; $i++) {
                $date[] = "'".date('d/m', $time )."'";
                $time = $time + 86400;
            }
        }



        return implode(',', $date);
    }
}