Beispiel #1
0
 /**
  * @before _secure
  */
 public function createinvoice()
 {
     $this->seo(array("title" => "Create Invoice"));
     $view = $this->getActionView();
     $perfs = [];
     $start = RM::get("start");
     $end = RM::get("end");
     $user_id = RM::get("user_id", null);
     $view->set('user_id', $user_id)->set('start', $start)->set('end', $end);
     $query['created'] = Db::dateQuery($start, $end);
     $query['user_id'] = $user_id;
     $view->set("exists", false);
     if ($user_id) {
         $user = \User::first(['type = ?' => 'publisher', 'org_id = ?' => $this->org->_id, 'id = ?' => $user_id]);
         $view->set('affiliate', $user);
         $performances = Performance::all($query, ['clicks', 'impressions', 'conversions', 'created', 'revenue'], 'created', 'desc');
         $perfs = array_values($performances);
         $view->set('performances', $perfs);
         $inv_exist = Invoice::exists($user_id, $start, $end);
         if ($inv_exist) {
             $view->set("message", "Invoice already exist for Date range from " . Framework\StringMethods::only_date($inv_exist->start) . " to " . Framework\StringMethods::only_date($inv_exist->end));
             $view->set("exists", true);
             return;
         }
     } else {
         $affiliates = \User::all(['type = ?' => 'publisher', 'org_id' => $this->org->_id], ['id', 'name']);
         $view->set('affiliates', $affiliates);
     }
     if (RM::post("action") == "cinvoice" && RM::post("amount") > 0) {
         $invoice = new Invoice(["org_id" => $this->org->id, "user_id" => $user->id, "utype" => $user->type, "start" => end($perfs)->created, "end" => $perfs[0]->created, "amount" => RM::post("amount"), "live" => false]);
         $invoice->save();
         Registry::get("session")->set('$flashMessage', 'Payment Saved!!');
         $this->redirect("/billing/affiliates.html");
     }
 }
Beispiel #2
0
 public static function exists($uid, $start = null, $end = null)
 {
     $uid = Db::convertType($uid);
     $dateQuery = Utils::dateQuery($start, $end);
     $inv_exist = self::first(['$or' => [["user_id" => $uid, "start" => ['$lte' => $dateQuery['start'], '$lte' => $dateQuery['end']], "end" => ['$gte' => $dateQuery['start'], '$gte' => $dateQuery['end']]], ["user_id" => $uid, "start" => Db::dateQuery($start, $end), "end" => ['$gte' => $dateQuery['start'], '$gte' => $dateQuery['end']]], ["user_id" => $uid, "start" => ['$lte' => $dateQuery['start'], '$lte' => $dateQuery['end']], "end" => Db::dateQuery($start, $end)], ["user_id" => $uid, "start" => Db::dateQuery($start, $end), "end" => Db::dateQuery($start, $end)]]]);
     return $inv_exist;
 }
Beispiel #3
0
 public static function getStats($query, $dq = [])
 {
     if (count($dq) > 0) {
         $query['created'] = \Shared\Services\Db::dateQuery($dq['start'], $dq['end']);
     }
     $records = self::all($query, ['hits']);
     $total = 0;
     foreach ($records as $r) {
         $total += $r->hits;
     }
     return $total;
 }
Beispiel #4
0
 public static function exists($oid, $opts = [])
 {
     $oid = Db::convertType($oid);
     $dateQuery = Utils::dateQuery($opts);
     $multiple = $opts['multiple'] ?? false;
     if ($multiple) {
         $query = "all";
     } else {
         $query = "first";
     }
     $inv_exist = self::$query(['$or' => [["org_id" => $oid, "start" => ['$lte' => $dateQuery['start'], '$lte' => $dateQuery['end']], "end" => ['$gte' => $dateQuery['start'], '$gte' => $dateQuery['end']]], ["org_id" => $oid, "start" => Db::dateQuery($start, $end), "end" => ['$gte' => $dateQuery['start'], '$gte' => $dateQuery['end']]], ["org_id" => $oid, "start" => ['$lte' => $dateQuery['start'], '$lte' => $dateQuery['end']], "end" => Db::dateQuery($start, $end)], ["org_id" => $oid, "start" => Db::dateQuery($start, $end), "end" => Db::dateQuery($start, $end)]]]);
     return $inv_exist;
 }
Beispiel #5
0
 /**
  * Find the Performance of Affiliates|Advertisers of an organization
  * @param  object $org  \Organization
  * @param  string $type Type of user
  * @return [type]       [description]
  */
 public static function perf($org, $type, $opts = [])
 {
     $start = $opts['start'] ?? RequestMethods::get('start', date('Y-m-d', strtotime("-5 day")));
     $end = $opts['end'] ?? RequestMethods::get('end', date('Y-m-d', strtotime("-1 day")));
     switch ($type) {
         case 'publisher':
             $perfFields = $opts['fields'] ?? ['clicks', 'impressions', 'conversions', 'revenue', 'created'];
             $meta = $opts['meta'] ?? false;
             if ($meta) {
                 $perfFields[] = 'meta';
             }
             $publishers = $opts['publishers'] ?? $org->users($type);
             $pubPerf = Perf::all(['user_id' => ['$in' => $publishers], 'created' => Db::dateQuery($start, $end)], $perfFields, 'created', 'asc');
             $pubPerf = Perf::objectArr($pubPerf, $perfFields);
             return $pubPerf;
         case 'advertiser':
             $advertisers = $opts['advertisers'] ?? $org->users($type);
             $fields = $opts['fields'] ?? ['revenue', 'created'];
             $advertPerf = Perf::all(['user_id' => ['$in' => $advertisers], 'created' => Db::dateQuery($start, $end)], $fields, 'created', 'asc');
             $advertPerf = Perf::objectArr($advertPerf, $fields);
             return $advertPerf;
     }
     return [];
 }
Beispiel #6
0
 public static function livePerf($user, $s = null, $e = null)
 {
     $start = $end = date('Y-m-d');
     $type = $user->type ?? '';
     if ($s) {
         $start = $s;
     }
     if ($e) {
         $end = $e;
     }
     $perf = new \Performance();
     $match = ['is_bot' => false, 'created' => Db::dateQuery($start, $end)];
     switch ($user->type) {
         case 'publisher':
             $match['pid'] = Db::convertType($user->_id);
             break;
         case 'advertiser':
             $ads = \Ad::all(['user_id' => $user->_id], ['_id']);
             $keys = array_keys($ads);
             $match['adid'] = ['$in' => Db::convertType($keys)];
             break;
         default:
             return $perf;
     }
     $results = $commissions = [];
     $records = self::_livePerfQuery($match);
     foreach ($records as $r) {
         $obj = Utils::toArray($r);
         $adid = Utils::getMongoID($obj['_id']);
         $results[$adid] = $obj;
     }
     $comms = Db::query('Commission', ['ad_id' => ['$in' => array_keys($results)]], ['ad_id', 'rate', 'revenue', 'model', 'coverage']);
     $comms = \Click::classify($comms, 'ad_id');
     foreach ($comms as $adid => $value) {
         $value = array_map(function ($v) {
             $v["ad_id"] = Utils::getMongoID($v["ad_id"]);
             unset($v["_id"]);
             return (object) $v;
         }, Utils::toArray($value));
         $commissions[$adid] = \Commission::filter($value);
     }
     foreach ($results as $adid => $obj) {
         $comms = $commissions[$adid];
         foreach ($obj['countries'] as $value) {
             $country = $value['country'];
             $clicks = $value['count'];
             $commission = \Commission::campaignRate($adid, $comms, $country, ['type' => $type, "{$type}" => $user, 'start' => $start, 'end' => $end, 'commFetched' => true]);
             $updateData = [];
             $earning = \Ad::earning($commission, $clicks);
             AM::copy($earning, $updateData);
             $perf->update($updateData);
         }
     }
     return $perf;
 }
Beispiel #7
0
 /**
  * @before _secure
  */
 public function bills()
 {
     $this->seo(array("title" => "Bills"));
     $view = $this->getActionView();
     $start = RM::get("start", date("Y-m-d", strtotime('-7 day')));
     $end = RM::get("end", date("Y-m-d", strtotime('now')));
     $query = ['user_id = ?' => $this->user->_id, 'created = ?' => Db::dateQuery($start, $end)];
     $performances = \Performance::all($query, [], 'created', 'desc');
     $invoices = \Invoice::all(['user_id = ?' => $this->user->_id]);
     $view->set("performances", $performances)->set("invoices", $invoices);
     $view->set("start", $start);
     $view->set("end", $end);
 }
Beispiel #8
0
 /**
  * Gets the Rate based on the type of record i.e 'publisher', or 'advertiser'
  * @param  array  $commissions Array of Commissions to search for ad_id
  * @param  String $type        Advertiser | Publisher
  * @return array
  */
 public static function campaignRate($adid, &$commissions = [], $country = null, $extra = [])
 {
     $commFetched = $extra['commFetched'] ?? false;
     if ($commFetched) {
         $comm = $commissions;
     } else {
         $comm = self::find($commissions, $adid);
     }
     $info = ['campaign' => 'cpc', 'rate' => 0, 'revenue' => 0, 'type' => $extra['type']];
     if (!is_array($comm)) {
         return $info;
     }
     $commission = array_key_exists($country, $comm) ? $comm[$country] : @$comm['ALL'];
     // because commission might not exists if country is null
     if (!is_object($commission)) {
         return $info;
     }
     $query = ['adid' => $adid, 'created' => Db::dateQuery($extra['start'], $extra['end'])];
     switch ($extra['type']) {
         case 'advertiser':
             $info['revenue'] = (double) $commission->revenue;
             break;
         case 'publisher':
             $info['rate'] = self::getPubRate($commission, $extra);
             $query['pid'] = $extra['publisher']->_id ?? null;
             break;
         case 'both':
             $info['revenue'] = (double) $commission->revenue;
             $info['rate'] = self::getPubRate($commission, $extra);
             if (isset($extra['publisher'])) {
                 $query['pid'] = $extra['publisher']->_id;
             }
             break;
     }
     switch (strtolower($commission->model)) {
         case 'cpa':
         case 'cpi':
             $count = \Conversion::count($query);
             $info['conversions'] = $count;
             break;
         case 'cpm':
             $info['impressions'] = \Impression::getStats($query);
             break;
     }
     $info['campaign'] = strtolower($commission->model);
     return $info;
 }
Beispiel #9
0
 /**
  * @before _secure
  * @after _displayData
  */
 public function platforms($id = null)
 {
     $this->seo(["title" => "Platform wise click stats"]);
     $view = $this->getActionView();
     $org = $this->org;
     $clickCol = Registry::get("MongoDB")->clicks;
     // find the platforms
     $platforms = \Platform::all(['user_id' => ['$in' => $org->users('advertisers')]], ['_id', 'url']);
     if (count($platforms) === 0) {
         return $view->set(['platforms' => [], 'publishers' => []]);
     }
     $key = array_rand($platforms);
     $url = RM::get('link', $platforms[$key]->url);
     // find ads having this url
     $ads = \Ad::all(['org_id' => $org->_id], ['_id', 'url']);
     $in = Utils::mongoObjectId(array_keys($ads));
     $matched = [];
     foreach ($ads as $a) {
         $regex = preg_quote($url, '.');
         if (preg_match('#^' . $regex . '#', $a->url)) {
             $matched[] = Utils::mongoObjectId($a->_id);
         }
     }
     if (count($matched) === 0) {
         $query['adid'] = ['$in' => $in];
     } else {
         $query['adid'] = ['$in' => $matched];
     }
     $query['is_bot'] = false;
     $query['created'] = Db::dateQuery($this->start, $this->end);
     $records = $clickCol->aggregate([['$match' => $query], ['$projection' => ['_id' => 1, 'pid' => 1]], ['$group' => ['_id' => '$pid', 'count' => ['$sum' => 1]]], ['$sort' => ['count' => -1]]]);
     $result = [];
     $publishers = [];
     foreach ($records as $r) {
         $obj = (object) $r;
         $id = Utils::getMongoID($obj->_id);
         $user = User::first(['_id' => $id], ['_id', 'name']);
         $result[$id] = (object) ['_id' => $user->_id, 'name' => $user->name, 'clicks' => $obj->count];
     }
     $view->set(['platforms' => $platforms, 'link' => $url, 'publishers' => $result]);
 }
Beispiel #10
0
 public static function total($dq = [], $user = null, $fields = [])
 {
     $name = __CLASS__;
     if (count($fields) === 0) {
         $fields = ['clicks', 'conversions', 'impressions', 'revenue'];
     }
     $project = ['user_id' => 1, '_id' => 0];
     $group = ['_id' => '$user_id'];
     foreach ($fields as $f) {
         $project[$f] = 1;
         $group[$f] = ['$sum' => '$' . $f];
     }
     $match = ['created' => Db::dateQuery($dq['start'], $dq['end'])];
     if (is_array($user)) {
         $keys = ArrayMethods::arrayKeys($user, '_id');
         $match['user_id'] = ['$in' => Db::convertType($keys)];
     } else {
         $match['user_id'] = Db::convertType($user->_id);
     }
     $records = Db::collection($name)->aggregate([['$match' => $match], ['$project' => $project], ['$group' => $group]]);
     $result = [];
     foreach ($records as $r) {
         $obj = Utils::toArray($r);
         $add = [];
         foreach ($fields as $f) {
             $add[$f] = $obj[$f];
         }
         ArrayMethods::add($add, $result);
     }
     return $result;
 }
Beispiel #11
0
 public static function performance($id, $extra = [])
 {
     $stats = [];
     $start = $end = date('Y-m-d');
     $match = ['adid' => Db::convertType($id), 'is_bot' => false];
     $user_id = $extra['pid'] ?? null;
     if ($user_id) {
         $match["pid"] = Db::convertType($user_id);
     }
     if (isset($extra['start']) && isset($extra['end'])) {
         $start = $extra['start'];
         $end = $extra['end'];
     }
     $diff = date_diff(date_create($start), date_create($end));
     $dateWise = $extra['meta'] ?? true;
     // by default datewise query
     if ($dateWise) {
         for ($i = 0; $i <= $diff->format("%a"); $i++) {
             $date = date('Y-m-d', strtotime($start . " +{$i} day"));
             $stats[$date] = ['clicks' => 0, 'meta' => []];
             $match['created'] = Db::dateQuery($date, $date);
             $records = self::_perfQuery($match, $extra);
             self::_getStats($records, $stats, $date);
         }
     } else {
         $match['created'] = Db::dateQuery($start, $end);
         $stats[$start] = ['clicks' => 0, 'meta' => []];
         $records = self::_perfQuery($match, $extra);
         self::_getStats($records, $stats, $start);
     }
     $records = self::earning($stats, $id, $user_id);
     $total = Performance::calTotal($records);
     return ['stats' => $records, 'total' => $total];
 }
Beispiel #12
0
 /**
  * @before _secure
  */
 public function manage()
 {
     $this->seo(['title' => 'Campaign: List', 'description' => 'Manage campaigns']);
     $view = $this->getActionView();
     $campaigns = [];
     $page = RM::get("page", 1);
     $limit = RM::get("limit", 10);
     $property = RM::get("property");
     $value = RM::get("value");
     switch (RM::get("sort")) {
         case 'trending':
             $start = RM::get("start", date("Y-m-d", strtotime('now')));
             $end = RM::get("end", date("Y-m-d", strtotime('now')));
             $q = ['start' => $start, 'end' => $end];
             $view->set($q);
             // Only find the ads for this organizations
             $allAds = \Ad::all(['org_id' => $this->org->_id], ['_id']);
             $in = Db::convertType(array_keys($allAds));
             $clickCol = Db::collection('Click');
             $match = ['created' => Db::dateQuery($start, $end), 'is_bot' => false, 'adid' => ['$in' => $in]];
             $records = $clickCol->aggregate([['$match' => $match], ['$project' => ['adid' => 1, '_id' => 0]], ['$group' => ['_id' => '$adid', 'count' => ['$sum' => 1]]], ['$sort' => ['count' => -1]], ['$limit' => (int) $limit]]);
             foreach ($records as $r) {
                 $arr = Utils::toArray($r);
                 $id = Utils::getMongoID($arr['_id']);
                 $campaigns[] = Ad::first(["id = ?" => $id]);
             }
             $count = $limit;
             break;
         default:
             $query = ["org_id = ?" => $this->org->id];
             if (in_array($property, ["user_id", "live", "id"])) {
                 $query[$property] = $value;
             } else {
                 if (in_array($property, ["url", "title"])) {
                     $query[$property] = Utils::mongoRegex(preg_quote($value));
                 }
             }
             $campaigns = \Ad::all($query, [], 'created', 'desc', $limit, $page);
             $count = \Ad::count($query);
             break;
     }
     $categories = \Category::all(['org_id' => $this->org->_id], ['_id', 'name']);
     $active = \Ad::count(["org_id = ?" => $this->org->id, "live = ?" => 1]);
     $inactive = \Ad::count(["org_id = ?" => $this->org->id, "live = ?" => 0]);
     $view->set("campaigns", $campaigns)->set("count", $count)->set("active", $active)->set("inactive", $inactive)->set("limit", $limit)->set("page", $page)->set("property", $property)->set("value", $value)->set("categories", $categories);
 }
Beispiel #13
0
 /**
  * @before _admin
  */
 public function manage()
 {
     $this->seo(["title" => "List Publisher"]);
     $view = $this->getActionView();
     $page = RM::get("page", 1);
     $limit = RM::get("limit", 10);
     $publishers = [];
     $start = RM::get("start", date('Y-m-d'));
     $end = RM::get("end", date('Y-m-d'));
     $view->set(['start' => $start, 'end' => $end]);
     switch (RM::get("sort")) {
         case 'trending':
             $match = ['pid' => ['$in' => $this->org->users('publisher')], 'is_bot' => false, 'created' => Db::dateQuery($start, $end)];
             $clickCol = Db::collection('Click');
             $records = $clickCol->aggregate([['$match' => $match], ['$project' => ['pid' => 1, '_id' => 0]], ['$group' => ['_id' => '$pid', 'count' => ['$sum' => 1]]], ['$sort' => ['count' => -1]], ['$limit' => (int) $limit]]);
             foreach ($records as $r) {
                 $arr = Utils::toArray($r);
                 $id = Utils::getMongoID($arr['_id']);
                 $publishers[] = User::first(["id = ?" => $id]);
             }
             $count = $limit;
             break;
         default:
             $query = ["type = ?" => "publisher", "org_id = ?" => $this->org->_id];
             $property = RM::get("property");
             $value = RM::get("value");
             if (in_array($property, ["live", "id"])) {
                 $query["{$property} = ?"] = $value;
             } else {
                 if (in_array($property, ["email", "name", "phone"])) {
                     $query["{$property} = ?"] = Db::convertType($value, 'regex');
                 }
             }
             $publishers = \User::all($query, [], 'created', 'desc', $limit, $page);
             $count = \User::count($query);
             break;
     }
     $active = \User::count(["type = ?" => "publisher", "org_id = ?" => $this->org->_id, "live = ?" => 1]);
     $inactive = \User::count(["type = ?" => "publisher", "org_id = ?" => $this->org->_id, "live = ?" => 0]);
     $view->set("publishers", $publishers)->set("property", $property)->set("value", $value)->set("active", $active)->set("inactive", $inactive)->set("count", $count)->set("limit", $limit)->set("page", $page);
 }
Beispiel #14
0
 public static function hourly()
 {
     $today = date('Y-m-d');
     $dq = Db::dateQuery(null, $today);
     // find all the ads whose expiry date is today
     $ads = self::all(['expiry' => $dq]);
     foreach ($ads as $a) {
         $a->live = false;
         // Make them disabled
         $a->save();
     }
 }