Пример #1
0
 /**
  * @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);
 }
Пример #2
0
 /**
  * @before _secure
  * @after setDate
  */
 public function advertisers()
 {
     $this->seo(["title" => "Advertiser Stats"]);
     $view = $this->getActionView();
     $org = $this->org;
     if ($this->user_id) {
         $advertiser = User::first(['_id' => $this->user_id, 'org_id' => $org->_id, 'type' => 'advertiser']);
         if (!$advertiser) {
             $this->_404();
         }
         $in = [$advertiser->_id];
     } else {
         $in = $org->users('advertiser');
     }
     $fields = ['clicks', 'impressions', 'conversions', 'revenue', 'created'];
     $advertPerf = Perf::perf($org, 'advertiser', ['advertisers' => $in, 'start' => $this->start, 'end' => $this->end, 'fields' => $fields]);
     $perf = [];
     Perf::revenue($advertPerf, $perf);
     $data = ['stats' => $perf, 'total' => Perf::calTotal($perf)];
     $view->set($data);
 }
Пример #3
0
 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');
 }