/** * @before _secure * @after _cleanUp */ public function earning($id = null) { $view = $this->getActionView(); if (!$id) { return $this->failure('20'); } $org = $this->_org; $perfFields = ['clicks', 'revenue', 'impressions', 'conversions', 'created']; $publisher = User::first(['_id' => $id, 'org_id' => $org->_id]); if (!$publisher) { return $this->failure('30'); } $publisher = User::objectArr($publisher, Usr::fields()); $publisher = array_shift($publisher); $start = RequestMethods::get("start", date('Y-m-d', strtotime("-5 day"))); $end = RequestMethods::get("end", date('Y-m-d', strtotime('-1 day'))); $pubPerf = Perf::perf($org, 'publisher', ['meta' => true, 'publishers' => [$publisher->_id], 'start' => $start, 'end' => $end]); $perf = []; Perf::payout($pubPerf, $perf); $data = ['user' => $publisher, 'stats' => $perf, 'total' => Perf::calTotal($perf)]; $view->set('data', $data); }
/** * @before _secure * @after setDate */ public function publishers() { $this->seo(["title" => "Publisher Stats"]); $view = $this->getActionView(); $org = $this->org; if ($this->user_id) { $publisher = User::first(['_id' => $this->user_id, 'org_id' => $org->_id, 'type' => 'publisher']); if (!$publisher) { $this->_404(); } $in = [$publisher->_id]; } else { $in = $org->users('publisher'); } $pubPerf = Perf::perf($org, 'publisher', ['meta' => true, 'publishers' => $in, 'start' => $this->start, 'end' => $this->end]); $perf = []; Perf::payout($pubPerf, $perf); $data = ['stats' => $perf, 'total' => Perf::calTotal($perf)]; $view->set($data); }
public function _invoice() { $orgs = Organization::all(['live' => true]); $today = date('Y-m-d'); $this->log('Started Invoices'); foreach ($orgs as $o) { $start = date('Y-m-d', strtotime("-1 day")); // check auto invoice $aff = $o->billing['aff'] ?? []; if (!isset($aff['auto']) || !$aff['auto']) { continue; } $this->log('Checking Invoices for Org: ' . $o->name); // make invoice for all publishers b/w $start and $today $pubs = User::all(['org_id' => $o->_id, 'live' => true, 'type' => 'publisher']); foreach ($pubs as $p) { $invoice = Invoice::first(['org_id' => $o->_id, 'user_id' => $p->_id], ['created'], 'created', 'desc'); if ($invoice) { // check no of days $lastCreated = $invoice->created->format('Y-m-d'); $diff = date_diff(date_create($lastCreated), date_create($today)); if ($diff->d !== (int) $aff['freq']) { continue; } $start = date('Y-m-d', strtotime("-" . $aff['freq'] . " day")); } else { $start = $o->created->format('Y-m-d'); } // check if invoice exists for the date range $inv = Invoice::exists($p->_id, $start, $today); if ($inv) { continue; } $pubPerf = Perf::perf($o, 'publisher', ['publishers' => [$p->_id], 'fields' => ['revenue', 'created'], 'start' => $start, 'end' => $today]); $perf = []; Perf::payout($pubPerf, $perf); $total = Perf::calTotal($perf); $payout = $total['payout'] ?? 0; $keys = array_keys($perf); if ($payout < $aff['minpay']) { continue; } // create a new invoice $inv = new Invoice(['org_id' => $o->_id, 'user_id' => $p->_id, 'utype' => $p->type, 'amount' => $payout, 'start' => $keys[0], 'end' => end($keys), 'live' => false]); $inv->save(); $this->log('Invoice saved for User: '******' email: ' . $p->email); } } $this->log('End Invoices'); }