/** * @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]]); }
/** * Returns data of clicks, impressions, payouts for publishers with custom date range * @before _secure */ public function performance() { $this->JSONview(); $view = $this->getActionView(); $start = RM::get("start", date("Y-m-d", strtotime('-7 day'))); $end = RM::get("end", date("Y-m-d", strtotime('now'))); $dateQuery = Utils::dateQuery(['start' => $start, 'end' => $end]); $find = Performance::overall($dateQuery, $this->user); $view->set($find); }
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]); } } }