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; }
public function testDateQuery() { $start = date('Y-m-d', strtotime('-3 day')); $end = date('Y-m-d'); $dateQuery = Utils::dateQuery($start, $end); $startObj = $dateQuery['start']; $endObj = $dateQuery['end']; $sec = $startObj->toDateTime()->getTimestamp(); $this->assertEquals($start, date('Y-m-d', $sec), 'Start Date doesnot match'); $sec = $endObj->toDateTime()->getTimestamp(); $this->assertEquals($end, date('Y-m-d', $sec), 'End Date doesnot match'); }
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; }
/** * @before _secure * @after _displayData */ public function links() { $this->seo(array("title" => "Link Logs")); $view = $this->getActionView(); $prop = RM::get("property"); $val = RM::get("value"); $sort = RM::get("sort", "desc"); $sign = RM::get("sign", "equal"); $orderBy = RM::get("order", 'created'); $fields = (new \Link())->getColumns(); $searching = $query = []; $query['user_id'] = ['$in' => $this->org->users('publisher')]; foreach ($fields as $key => $value) { $search = RM::get($key); if (!$search) { continue; } $searching[$key] = $search; // Only allow full object ID's and rest regex searching if (in_array($key, ['user_id', 'ad_id', '_id'])) { $query[$key] = RM::get($key); } else { $query[$key] = Utils::mongoRegex($search); } } $dateQuery = Utils::dateQuery(['start' => $this->start, 'end' => $this->end]); $query['created'] = ['$gte' => $dateQuery['start'], '$lte' => $dateQuery['end']]; $records = \Link::all($query, [], $orderBy, $sort, $this->limit, $this->page); $count = \Link::count($query); $view->set(['links' => $records, 'fields' => $fields, 'property' => $prop, 'value' => $val, 'sign' => $sign, 'sort' => $sort, 'order' => $orderBy, 'count' => $count, 'query' => $searching]); }
/** * @before _secure */ public function billing() { $this->seo(array("title" => "Billing")); $view = $this->getActionView(); $bills = Bill::all(["org_id = ?" => $this->org->id], [], "created", "desc"); $invoice = RM::get("invoice", "current"); $imp_cost = 0; $click_cost = 0; switch ($invoice) { case 'current': $start = RM::get('start', date('Y-m-01')); $end = RM::get('end', date('Y-m-d')); $dateQuery = Utils::dateQuery(['start' => $start, 'end' => $end]); // find advertiser performances to get clicks and impressions $performances = \Performance::overall($dateQuery, User::all(['org_id' => $this->org->_id, 'type' => 'advertiser'], ['_id'])); $clicks = $performances['total_clicks']; $impressions = $performances['total_impressions']; break; default: $bill = Bill::first(["org_id = ?" => $this->org->id, "id = ?" => $invoice]); $start = $bill->start; $end = $bill->end; $clicks = $bill->clicks; $impressions = $bill->impressions; break; } if ($clicks > 1000) { $click_cost = 0.001 * $clicks * $this->org->meta["bill"]["tcc"]; } if ($impressions > 1000000) { $imp_cost = 0.001 * 0.001 * $impressions * $this->org->meta["bill"]["mic"]; } $view->set(['bills' => $bills, 'clicks' => ['total' => $clicks, 'cost' => $click_cost], 'start' => $start, 'end' => $end, 'invoice' => $invoice, 'impressions' => ['total' => $impressions, 'cost' => $imp_cost]]); }
public static function dateQuery($start = null, $end = null) { $changed = false; if ($start && $end) { if (self::isType($start, 'date') && self::isType($end, 'date')) { $dq = ['start' => $start, 'end' => $end]; $changed = true; } } if (!$changed) { $dq = \Shared\Utils::dateQuery(['start' => $start, 'end' => $end]); } $result = []; if ($start) { $result['$gte'] = $dq['start']; } if ($end) { $result['$lte'] = $dq['end']; } return $result; }
public function generateBills() { $orgs = Organization::all(["live = ?" => true]); foreach ($orgs as $org) { $imp_cost = 0; $click_cost = 0; $month_ini = new DateTime("first day of last month"); $month_end = new DateTime("last day of last month"); $start = $month_ini->format('Y-m-d'); $end = $month_end->format('Y-m-d'); $dateQuery = Utils::dateQuery(['start' => $start, 'end' => $end]); // find advertiser performances to get clicks and impressions $performances = \Performance::overall($dateQuery, User::all(['org_id' => $org->_id, 'type' => 'advertiser'], ['_id'])); $clicks = $performances['total_clicks']; if ($clicks > 1000) { $click_cost = 0.001 * $clicks * $org->meta["bill"]["tcc"]; } $impressions = $performances['total_impressions']; if ($impressions > 100000) { $imp_cost = 0.001 * 0.001 * $impressions * $org->meta["bill"]["mic"]; } $total = $click_cost + $imp_cost; $bill = new Bill(["org_id" => $org->id, "impressions" => $impressions, "clicks" => $clicks, "mic" => $org->meta["bill"]["mic"], "tcc" => $org->meta["bill"]["tcc"], "start" => $start, "end" => $end, "amount" => $total, "live" => false, "created" => Db::time('-1 day')]); if ($total > 1) { $bill->save(); $user = User::first(["org_id = ?" => $org->id, "type = ?" => "admin"]); Mail::send(['user' => $user, 'bill' => $bill, 'template' => 'adminBilling', 'subject' => 'Billing at vNative', 'click_cost' => $click_cost, 'imp_cost' => $imp_cost, 'org' => $org]); } } }