/** * @before _secure */ public function campaign($id) { $ad = \Ad::first(['_id' => $id, 'org_id' => $this->org->_id]); if (!$ad) { $this->_404(); } $this->seo(["title" => $ad->title]); $view = $this->getActionView(); if (RM::post("action")) { // action value already checked in _postback func $this->_postback('add', ['ad' => $ad]); } if (RM::type() === 'DELETE') { $this->_postback('delete'); } $start = RM::get("start", date('Y-m-d', strtotime("-7 day"))); $end = RM::get("end", date('Y-m-d')); $limit = RM::get("limit", 10); $page = RM::get("page", 1); $query = ['adid' => Db::convertType($id), 'created' => Db::dateQuery($start, $end)]; $clicks = \Click::all($query, [], 'created', 'desc', $limit, $page); $count = \Click::count($query); $cf = Utils::getConfig("cf", "cloudflare"); $view->set("domain", $cf->api->domain)->set("clicks", $clicks)->set("count", $count)->set('advertiser', $this->user); $comms = Commission::all(["ad_id = ?" => $id], ['model', 'coverage', 'revenue', 'description']); $models = ArrayMethods::arrayKeys($comms, 'model'); $advertiser = User::first(["id = ?" => $ad->user_id], ['name']); $categories = \Category::all(["org_id = ?" => $this->org->_id], ['name', '_id']); $this->_postback('show', ['ad' => $ad]); $view->set("ad", $ad)->set("comms", $comms)->set("categories", $categories)->set("advertiser", $advertiser)->set('models', $models)->set("start", $start)->set("end", $end); }
public function main($id) { if (is_null(Agent::find($id))) { return Redirect::to('admin'); } $commissions = Commission::where('agent_id', $id)->orderBy('created_at', 'desc')->get(); $agent = Agent::find($id); View::make('admin.commission.list', compact('commissions', 'agent')); }
public function update(Commission $commission){ $query = $this->_db->prepare(' UPDATE t_commission SET titre=:titre, commissionnaire=:commissionnaire, montant=:montant, etat=:etat, updated=:updated, updatedBy=:updatedBy WHERE id=:id') or die (print_r($this->_db->errorInfo())); $query->bindValue(':id', $commission->id()); $query->bindValue(':titre', $commission->titre()); $query->bindValue(':commissionnaire', $commission->commissionnaire()); $query->bindValue(':montant', $commission->montant()); $query->bindValue(':etat', $commission->etat()); $query->bindValue(':updated', $commission->updated()); $query->bindValue(':updatedBy', $commission->updatedBy()); $query->execute(); $query->closeCursor(); }
public static function earning($stats = [], $adid, $user_id = null) { $records = []; foreach ($stats as $date => $r) { $commissions = []; $meta = $r['meta'] ?? []; if (array_key_exists("country", $meta)) { $countryWise = $meta["country"] ?? []; foreach ($countryWise as $country => $clicks) { $extra = ['type' => 'both', 'start' => $date, 'end' => $date]; if ($user_id) { $extra['publisher'] = \User::first(['_id' => $user_id]); } $comm = \Commission::campaignRate($adid, $commissions, $country, $extra); $earning = \Ad::earning($comm, $clicks); ArrayMethods::add($earning, $r); } } $records[$date] = $r; } return $records; }
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; }
public function give_commission_post($id) { if (is_null(Agent::find($id))) { return Redirect::to('admin'); } $agent = Agent::find($id); $action = Input::get('action'); if ($action == 'deduct') { $rules = array('action' => 'required', 'remarks' => 'required', 'amount' => 'required|numeric'); $validator = Validator::make(Input::all(), $rules); if ($validator->passes()) { $commissions = new Commission(); $commissions->user_id = Input::get('user_id'); $commissions->property_id = Input::get('property_id'); $commissions->agent_id = $agent->id; $commissions->action = 'deduct'; $commissions->remarks = Input::get('remarks'); $commissions->amount = Input::get('amount'); $commissions->save(); // AGENT COMMISSION if ($agent->earnings > 0) { $agent->earnings = $agent->earnings - Input::get('amount'); $agent->save(); } return Redirect::to('admin/agents/commissions/' . $id)->with('success', 'Commission has been added.'); } else { return Redirect::to('admin/give_commission/' . $id)->withErrors($validator)->withInput(); } } elseif ($action == 'add') { $rules = array('user_id' => 'required|numeric|exists:users,id', 'property_id' => 'required|numeric|exists:properties,id', 'action' => 'required', 'remarks' => 'required', 'amount' => 'required|numeric'); $validator = Validator::make(Input::all(), $rules); if ($validator->passes()) { $commissions = new Commission(); $commissions->user_id = Input::get('user_id'); $commissions->property_id = Input::get('property_id'); $commissions->agent_id = $agent->id; $commissions->action = 'add'; $commissions->remarks = Input::get('remarks'); $commissions->amount = Input::get('amount'); $commissions->save(); // AGENT COMMISSION $agent->earnings = $agent->earnings + Input::get('amount'); $agent->save(); return Redirect::to('admin/agents/commissions/' . $id)->with('success', 'Commission has been added.'); } else { return Redirect::to('admin/give_commission/' . $id)->withErrors($validator)->withInput(); } } }
public function delete() { $deleteList = ['Link', 'Platform', 'Ad', 'Performance', 'Invoice', 'Adaccess']; $query = ['user_id' => $this->_id]; $delete = false; switch ($this->_type) { case 'publisher': $clicks = Click::count(['pid' => $this->_id]); if ($clicks !== 0) { return false; } $delete = true; $this->removeFields(); break; case 'advertiser': $ads = Ad::all(['user_id' => $this->_id], ['_id']); if (count($ads) === 0) { $delete = true; } else { $in = array_keys($ads); $in = Db::convertType($in, 'id'); $clickCount = Click::count(['adid' => ['$in' => $in]]); if ($clickCount === 0) { Commission::deleteAll(['ad_id' => ['$in' => $in]]); $delete = true; } } break; } if ($delete) { parent::delete(); foreach ($deleteList as $table) { $table::deleteAll($query); } } return $delete; }
/** * @before _secure */ public function settings() { $this->seo(array("title" => "Campaign: Settings")); $view = $this->getActionView(); $user = $this->user; $org = $this->org; if (RM::type() === 'DELETE') { switch (RM::get("action")) { case 'commDel': $comm = Commission::first(['_id' => RM::get("comm_id"), "org_id" => $this->org->_id]); if ($comm) { $comm->delete(); $view->set('message', 'Commission removed!!'); } else { $view->set('message', 'Invalid request!!'); } return; } } if (RM::type() == 'POST') { $action = RM::post('action', ''); switch ($action) { case 'commadd': case 'commedit': $fields = ['model' => RM::post('model'), 'rate' => RM::post('rate'), 'coverage' => RM::post('coverage') ?? ['ALL']]; $comm_id = RM::post('comm_id'); if ($comm_id) { $comm = Commission::first(['_id' => $comm_id, 'org_id' => $this->org->_id]); if (!$comm) { $view->set('Invalid Request!!'); break; } } else { $comm = new Commission(['org_id' => $this->org->_id]); } foreach ($fields as $key => $value) { $comm->{$key} = $value; } $comm->save(); //echo "<pre>", print_r($_POST), "</pre>";die(); $view->set('message', 'Commission saved successfully!!'); break; case 'domains': $message = $org->updateDomains(); $this->setOrg($org); $view->set('message', $message); break; case 'categories': $success = Category::updateNow($this->org); if ($success) { $msg = 'Categories updated Successfully!!'; } else { $msg = 'Failed to delete some categories because in use by campaigns!!'; } $view->set('message', $msg); break; } $this->setUser($user); } $commissions = Commission::all(['org_id' => $this->org->_id]); $categories = \Category::all(['org_id' => $this->org->_id]); $view->set('categories', $categories)->set('hideRevenue', true)->set('commissions', $commissions); }
/** * @before _admin */ public function info($id = null) { $this->seo(array("title" => "Publisher Edit")); $view = $this->getActionView(); $publisher = User::first(["_id = ?" => $id, "type = ?" => "publisher", "org_id = ?" => $this->org->id]); if (!$publisher) { $this->_404(); } $platforms = Platform::all(["user_id = ?" => $publisher->id]); $view->set("platforms", $platforms); $view->set("errors", []); if (RM::type() == 'POST') { $action = RM::post('action', ''); switch ($action) { case 'account': $fields = ['name', 'email', 'phone', 'country', 'currency', 'username']; foreach ($fields as $f) { $publisher->{$f} = RM::post($f); } $publisher->save(); $view->set('message', 'Account Info updated!!'); break; case 'password': $old = RM::post('password'); $new = RM::post('npassword'); $view->set($publisher->updatePassword($old, $new)); break; case 'campaign': $publisher->getMeta()['campaign'] = ['model' => RM::post('model'), 'rate' => $this->currency(RM::post('rate'))]; $publisher->save(); $view->set('message', 'Payout Info Updated!!'); break; case 'trackingDomain': $tdomain = (array) RM::post('tdomain', ''); if ($tdomain && ArrayMethods::inArray($this->org->tdomains, $tdomain)) { $publisher->getMeta()['tdomain'] = $tdomain; $publisher->save(); $view->set('message', 'Added Tracking Domain for publisher'); } else { $view->set('message', 'Invalid Request!!'); } case 'commadd': case 'commedit': $comm_id = RM::post('comm_id'); if ($comm_id) { $comm = Commission::first(['_id' => $comm_id, 'user_id' => $publisher->_id]); } else { $comm = new Commission(['user_id' => $publisher->_id]); } $comm->model = RM::post('model'); $comm->description = RM::post('description'); $comm->rate = $this->currency(RM::post('rate')); $comm->coverage = RM::post('coverage', ['ALL']); $comm->save(); $view->set('message', "Multi Country Payout Saved!!"); break; } } if (RM::type() === 'DELETE') { $action = RM::get("action"); switch ($action) { case 'payoutdel': unset($publisher->getMeta()['campaign']); $publisher->save(); $view->set('message', 'Payout Deleted!!'); break; case 'commDel': $comm = Commission::first(['_id' => RM::get("comm_id"), 'user_id' => $publisher->_id]); if ($comm) { $comm->delete(); $view->set('message', 'Payout Deleted!!'); } else { $view->set('message', 'Invalid Request!!'); } break; case 'afields': $meta = $publisher->meta; $publisher->removeFields(); unset($meta['afields']); Db::updateRaw('users', ['_id' => Db::convertType($publisher->_id, 'id')], ['$set' => ['meta' => $meta]]); $view->set('message', 'Data Removed!!'); break; case 'defaultDomain': unset($publisher->getMeta()['tdomain']); $publisher->save(); $view->set('message', 'Removed tracking domain!!'); break; } } $afields = Meta::search('customField', $this->org); $view->set('afields', $afields)->set("publisher", $publisher)->set("commissions", Commission::all(["user_id = ?" => $publisher->id]))->set("start", strftime("%Y-%m-%d", strtotime('-7 day')))->set("end", strftime("%Y-%m-%d", strtotime('now')))->set("d", Performance::total(['start' => $start ?? $publisher->created->format('Y-m-d'), 'end' => $end ?? date('Y-m-d')], $publisher)); }
/** * @before _secure * @after _cleanUp */ public function campaign($id = null) { $view = $this->getActionView(); $org = $this->_org; $active = RequestMethods::get('active', 1); $fields = ['_id', 'title', 'description', 'image', 'url', 'device', 'expiry', 'created']; $commFields = ['model', 'rate', 'revenue', 'coverage']; if ($id) { $campaign = Ad::first(['_id' => $id, 'org_id' => $org->_id], $fields); } else { $campaign = null; } if ($id && !$campaign) { return $this->failure('30'); } $type = RequestMethods::type(); switch ($type) { case 'GET': if (!$id) { // display list of campaigns $ads = Ad::all(['org_id' => $org->_id, 'live' => $active], $fields); $ads = Ad::objectArr($ads, $fields); $results = []; foreach ($ads as $id => $a) { $arr = Utils::toArray($a); $comms = Commission::all(['ad_id' => $a->_id], $commFields); $arr['commissions'] = Ad::objectArr($comms, $commFields); $results[$id] = (object) $arr; } $data = ['campaigns' => $results]; $view->set('data', $data); } else { $ads = Ad::objectArr($campaign, $fields); $campaign = array_shift($ads); $comm = Commission::all(['ad_id' => $campaign->_id], $commFields); $data = ['campaign' => $campaign, 'commissions' => Commission::objectArr($comm, $commFields)]; $view->set('data', $data); } break; case 'POST': if ($id) { // edit a particular campaign } else { // create a new campaign $fields = ['title', 'description', 'url', 'expiry', 'category', 'device', 'user_id']; $img = RequestMethods::post('image'); // contains image url $campaign = new Ad(['org_id' => $org->_id, 'type' => 'article', 'image' => Utils::media($img, 'download')]); foreach ($fields as $f) { $campaign->{$f} = RequestMethods::post($f); } $view->set('success', false); $visibility = RequestMethods::post('visibility', 'public'); if ($visibility === 'private') { $campaign->getMeta()['private'] = true; } $opts = ['devices' => array_keys(Shared\Markup::devices()), 'advertisers' => $org->users('advertiser', false)]; if (true) { // $campaign->save(); $arr = ArrayMethods::reArray($_POST['commissions']); var_dump($arr); var_dump($_POST['commissions']); $view->set('success', true); } else { $data = ['errors' => $campaign->errors]; $view->set('data', $data); } } break; case 'DELETE': $message = $campaign->delete(); $view->set($message); break; } }
/** * Overrides the parent delete method to check for clicks on the * ad before deleting it */ public function delete() { $id = Utils::mongoObjectId($this->_id); $count = \Click::count(['adid' => $id]); if ($count !== 0) { return ['message' => 'Can not delete!! Campaign contain clicks', 'success' => false]; } $this->_removeMedia(); parent::delete(); \Commission::deleteAll(['ad_id' => $id]); \Link::deleteAll(['ad_id' => $id]); return ['message' => 'Campaign removed successfully!!', 'success' => true]; }
protected function perf($clicks, $arr, $dq = []) { $perf = new Performance(); $commissions = []; $classify = \Click::classify($clicks, 'adid'); foreach ($classify as $key => $value) { $countryWise = \Click::classify($value, 'country'); foreach ($countryWise as $country => $records) { $adClicks = count($records); $updateData = []; $extra = array_merge($arr, $dq); $info = \Commission::campaignRate($key, $commissions, $country, $extra); $earning = \Ad::earning($info, $adClicks); \Framework\ArrayMethods::copy($earning, $updateData); $perf->update($updateData); } } return $perf; }
/** * Process the Meta table for campaign urls and create the * campaign from the corresponding URL */ protected function importCampaigns() { $metas = \Meta::all(['prop = ?' => 'campImport']); $users = []; $orgs = []; foreach ($metas as $m) { $user = Usr::find($users, $m->propid, ['_id', 'org_id', 'email', 'meta']); $org = \Organization::find($orgs, $user->org_id); $categories = \Category::all(['org_id' => $org->_id], ['_id', 'name']); $categories = array_values($categories); $category = $categories[array_rand($categories)]; // fetch ad info foreach URL $advert_id = $m->value['advert_id']; $comm = $m->value['campaign'] ?? $org->meta; if (!isset($comm['model']) || !isset($comm['rate'])) { continue; } $urls = $m->value['urls']; foreach ($urls as $url) { $ad = \Ad::first(['org_id' => $org->_id, 'url' => $url]); if ($ad) { continue; } // already crawled URL may be due to failed cron earlier $data = Utils::fetchCampaign($url); $image = Utils::downloadImage($data['image']); if (!$image) { $image = ''; } $ad = new \Ad(['user_id' => $advert_id, 'org_id' => $org->_id, 'title' => $data['title'], 'description' => $data['description'], 'url' => $url, 'image' => $image, 'category' => [$category->_id], 'type' => 'article', 'live' => false, 'device' => ['ALL']]); if ($ad->validate()) { $ad->save(); $rev = $comm['revenue'] ?? 1.25 * (double) $comm['rate']; $commission = new \Commission(['ad_id' => $ad->_id, 'model' => $comm['model'], 'rate' => $comm['rate'], 'revenue' => round($rev, 6), 'coverage' => ['ALL']]); $commission->save(); } else { var_dump($ad->getErrors()); } } $msg = 'Campaigns imported for the user: ' . $user->email; $this->log($msg); $m->delete(); } }
private function createCommission($analyticsActivity) { if ($analyticsActivity) { $yiidActivity = MongoManager::getDM()->getRepository("Documents\\YiidActivity")->find($analyticsActivity->getYiidActivityId()); $commissionValue = $this->getBillingType() == DealTable::BILLING_TYPE_LIKE ? $this->commission_per_unit : $this->commission_per_unit * $analyticsActivity->getMediaPenetration(); if ($this->commission_pot < $commissionValue) { $commissionValue = $this->commission_pot; } if ($commissionValue > 0) { $commission = new Commission(); $commission->setPrice($commissionValue); $commission->setDomainProfileId($yiidActivity->getIId()); $commission->setDealId($this->getId()); $commission->setYaId($yiidActivity->getId()); $commission->save(); $this->commission_pot -= $commissionValue; } } }