Ejemplo n.º 1
0
 public function get_daily_stat($start, $end, $filters, $order, $seq, $page, $page_size)
 {
     $quote_service = new QuoteStat();
     $quote = $quote_service->get_ad_income($start, $end);
     $income = $quote_service->get_all_ad_income($start, $end);
     $rmb_change_service = new RmbChangeLog();
     $rmb_change = $rmb_change_service->get_all_ad_rmb_change($start, $end);
     $cut_service = new ADCut();
     $cut = $cut_service->get_cut_rmb($start, $end);
     $cut_rmb = $cut_service->get_all_cut_rmb($start, $end);
     $activity_service = new Activity();
     $activity = $activity_service->get_activity_rmb($start, $end);
     $activities = $activity_service->get_ads_activity_rmb($start, $end);
     $stat = $this->total_stat($start, $end);
     $ad_service = new AD();
     $ads = $ad_service->get_all_ad_info($filters);
     $ratio = ($stat['total']['rmb_out'] + $stat['total']['task_out'] + $stat['total']['transfer_cost'] + $stat['total']['happy_lock_total_cost'] - $stat['total']['happy_lock_rmb']) / ($stat['total']['rmb_out'] + $stat['total']['task_out']);
     $android_average_rmb = $stat['total']['android_transfer'] ? $stat['total']['android_rmb'] / $stat['total']['android_transfer'] : 0;
     $ios_average_rmb = $stat['total']['ios_transfer'] ? $stat['total']['ios_rmb'] / $stat['total']['ios_transfer'] : 0;
     if ($page_size) {
         $total['all']['amount'] = array('count' => count(array_unique(array_merge(array_keys($stat['ads']), array_keys($quote)))), 'transfer' => $stat['total']['transfer'], 'cpa' => $income['nums'], 'ratio' => $stat['total']['transfer'] ? round($income['nums'] / $stat['total']['transfer'] * 100, 2) : 0, 'out' => $stat['total']['rmb_out'] + $stat['total']['task_out'], 'operation_cost' => $stat['total']['happy_lock_total_cost'] - $stat['total']['happy_lock_rmb'] + $stat['total']['transfer_cost'] + $activity['outcome'], 'total_cost' => $stat['total']['rmb_out'] + $stat['total']['task_out'] + $stat['total']['transfer_cost'] + $stat['total']['happy_lock_total_cost'] - $stat['total']['happy_lock_rmb'] + $activity['outcome'], 'income' => ($income['income'] - $cut_rmb) / 100 + $activity['income'], 'average_rmb' => $stat['total']['transfer'] ? round(($stat['total']['rmb_out'] + $stat['total']['task_out'] + $stat['total']['transfer_cost'] + $stat['total']['happy_lock_total_cost'] - $stat['total']['happy_lock_rmb'] + $activity['outcome']) / $stat['total']['transfer'], 2) : 0, 'profit' => round($income['income'] / 100 * TAX_RATIO - ($stat['total']['rmb_out'] + $stat['total']['task_out'] + $stat['total']['transfer_cost'] + $stat['total']['happy_lock_total_cost'] - $stat['total']['happy_lock_rmb']), 2), 'profit_ratio' => $income['income'] ? round((TAX_RATIO - ($stat['total']['rmb_out'] + $stat['total']['task_out'] + $stat['total']['transfer_cost'] + $stat['total']['happy_lock_total_cost'] - $stat['total']['happy_lock_rmb']) / ($income['income'] / 100)) * 100, 2) : 0);
         $total['all']['happy_lock'] = array('transfer' => $stat['total']['happy_lock_transfer'], 'out' => $stat['total']['happy_lock_rmb'], 'operation_cost' => $stat['total']['happy_lock_total_cost'] - $stat['total']['happy_lock_rmb'], 'total_cost' => $stat['total']['happy_lock_total_cost'], 'average_rmb' => $stat['total']['happy_lock_transfer'] ? round($stat['total']['happy_lock_total_cost'] / $stat['total']['happy_lock_transfer'], 2) : 0);
         $total['all']['without_happy_lock'] = array('transfer' => $stat['total']['transfer'] - $stat['total']['happy_lock_transfer'], 'out' => $stat['total']['rmb_out'] + $stat['total']['task_out'] - $stat['total']['happy_lock_rmb'], 'operation_cost' => $stat['total']['transfer_cost'] + $activity['outcome'], 'total_cost' => $stat['total']['rmb_out'] + $stat['total']['task_out'] - $stat['total']['happy_lock_rmb'] + $stat['total']['transfer_cost'] + $activity['outcome'], 'average_rmb' => $stat['total']['transfer'] - $stat['total']['happy_lock_transfer'] ? round(($stat['total']['rmb_out'] + $stat['total']['task_out'] - $stat['total']['happy_lock_rmb'] + $stat['total']['transfer_cost'] + $activity['outcome']) / ($stat['total']['transfer'] - $stat['total']['happy_lock_transfer']), 2) : 0);
         $all_ad_ids = array_intersect(array_keys($stat['ads']), array_keys($quote));
         foreach ($all_ad_ids as $id) {
             $total['all']['happy_lock']['income'] += $stat['ads'][$id]['transfer'] ? round($quote[$id]['income'] * $stat['ads'][$id]['happy_lock_transfer'] / $stat['ads'][$id]['transfer'], 2) : 0;
         }
         $total['all']['without_happy_lock']['income'] = $total['all']['amount']['income'] - $total['all']['happy_lock']['income'];
     }
     $ids = array_unique(array_intersect(array_keys($ads), array_unique(array_merge(array_keys($quote), array_keys($stat['ads'])))));
     $total['show']['amount']['count'] = count($ids);
     if (!$order && $page_size) {
         $ids = array_slice($ids, $page * $page_size, $page_size);
     }
     $list = array();
     foreach ($ids as $id) {
         $real = $stat['ads'][$id]['transfer'] ? round($quote[$id]['income'] / $stat['ads'][$id]['transfer'], 2) : 0;
         $rmb_change_key = $ads[$id]['pack_name'] . $ads[$id]['ad_app_type'] . $ads[$id]['ad_sdk_type'] . $ads[$id]['cpc_cpa'];
         $ad = array_merge($ads[$id], array('id' => $id, 'step_rmb' => $rmb_change[$rmb_change_key] ? $rmb_change[$rmb_change_key]['min'] . '~' . $rmb_change[$rmb_change_key]['max'] : $ads[$id]['step_rmb'], 'quote_rmb' => isset($quote[$id]) ? $quote[$id]['max'] != $quote[$id]['min'] ? $quote[$id]['min'] / 100 . '~' . $quote[$id]['max'] / 100 : $quote[$id]['min'] / 100 : $ads[$id]['quote_rmb'] / 100, 'real' => $real, 'ratio' => $stat['ads'][$id]['transfer'] ? round($quote[$id]['nums'] / $stat['ads'][$id]['transfer'] * 100) : 0, 'cpa' => (int) $quote[$id]['nums'], 'income' => round($quote[$id]['income'] - $cut[$id] / 100 + $activities[$id]['income'], 2), 'profit' => round($quote[$id]['income'] * TAX_RATIO - ($stat['ads'][$id]['rmb_out'] + $stat['ads'][$id]['task_out']) * $ratio, 2), 'profit_ratio' => $quote[$id]['income'] ? round((TAX_RATIO - ($stat['ads'][$id]['rmb_out'] + $stat['ads'][$id]['task_out']) * $ratio / $quote[$id]['income']) * 100, 2) : 0, 'beprice' => $ads[$id]['ad_app_type'] == ADModel::ANDROID ? round($real * TAX_RATIO - $android_average_rmb * $ratio, 2) : round($real * TAX_RATIO - $ios_average_rmb * $ratio, 2), 'rmb_out' => (double) $stat['ads'][$id]['rmb_out'], 'task_out' => (double) $stat['ads'][$id]['task_out'], 'transfer' => (int) $stat['ads'][$id]['transfer'], 'happy_lock_rmb' => (double) $stat['ads'][$id]['happy_lock_rmb'], 'happy_lock_transfer' => (int) $stat['ads'][$id]['happy_lock_transfer'], 'happy_lock_income' => $stat['ads'][$id]['transfer'] ? round($quote[$id]['income'] * $stat['ads'][$id]['happy_lock_transfer'] / $stat['ads'][$id]['transfer'], 2) : 0, 'other_income' => round($quote[$id]['income'], 2) - ($stat['ads'][$id]['transfer'] ? round($quote[$id]['income'] * $stat['ads'][$id]['happy_lock_transfer'] / $stat['ads'][$id]['transfer'], 2) : 0), 'happy_lock_cut' => $stat['ads'][$id]['rmb_out'] ? $cut[$id] * $stat['ads'][$id]['happy_lock_rmb'] / $stat['ads'][$id]['rmb_out'] / 100 : 0, 'other_cut' => $stat['ads'][$id]['rmb_out'] ? $cut[$id] * (1 - $stat['ads'][$id]['happy_lock_rmb'] / $stat['ads'][$id]['rmb_out']) / 100 : 0, 'activity_out' => (double) $activities[$id]['outcome']));
         $list[] = $ad;
     }
     if ($order) {
         if ($seq == 'desc') {
             function build_sorter($order)
             {
                 return function ($a, $b) use($order) {
                     if (is_numeric($a[$order])) {
                         return $b[$order] - $a[$order];
                     }
                     return strcmp($b[$order], $a[$order]);
                 };
             }
             usort($list, build_sorter($order));
         } else {
             function build_sorter($order)
             {
                 return function ($a, $b) use($order) {
                     if (is_numeric($a[$order])) {
                         return $a[$order] - $b[$order];
                     }
                     return strcmp($a[$order], $b[$order]);
                 };
             }
             usort($list, build_sorter($order));
         }
         $list = array_slice($list, $page * $page_size, $page_size);
     }
     if ($page_size) {
         foreach ($list as $ad) {
             $total['show']['amount']['transfer'] += $ad['transfer'];
             $total['show']['amount']['cpa'] += $ad['cpa'];
             $total['show']['amount']['out'] += $ad['rmb_out'] + $ad['task_out'];
             $total['show']['amount']['income'] += $ad['income'];
             $total['show']['happy_lock']['transfer'] += $ad['happy_lock_transfer'];
             $total['show']['happy_lock']['out'] += $ad['happy_lock_rmb'];
             $total['show']['happy_lock']['income'] += $ad['happy_lock_income'];
             $total['show']['without_happy_lock']['out'] += $ad['rmb_out'] + $ad['task_out'] - $ad['happy_lock_rmb'];
             $total['show']['without_happy_lock']['transfer'] += $ad['transfer'] - $ad['happy_lock_transfer'];
             $total['show']['without_happy_lock']['income'] += $ad['other_income'];
         }
         $total['show']['amount']['ratio'] = $total['show']['amount']['transfer'] ? round($total['show']['amount']['cpa'] / $total['show']['amount']['transfer'] * 100, 2) : 0;
         $total['show']['amount']['total_cost'] = round($total['show']['amount']['out'] * $ratio, 2);
         $total['show']['amount']['average_rmb'] = $total['show']['amount']['transfer'] ? round($total['show']['amount']['out'] * $ratio / $total['show']['amount']['transfer'], 2) : 0;
         $total['show']['amount']['profit'] = round($total['show']['amount']['income'] * TAX_RATIO - $total['show']['amount']['total_cost'], 2);
         $total['show']['amount']['profit_ratio'] = $total['show']['amount']['income'] ? round((TAX_RATIO - $total['show']['amount']['total_cost'] / $total['show']['amount']['income']) * 100, 2) : 0;
     }
     $result = array('list' => $list, 'amount' => $total, 'total' => $total['show']['amount']['count']);
     return $result;
 }
 public function get_stat($ad_app_type)
 {
     $today = date('Y-m-d');
     $start = empty($_REQUEST['start']) ? $today : $_REQUEST['start'];
     $end = empty($_REQUEST['end']) ? $today : $_REQUEST['end'];
     $pagesize = isset($_REQUEST['pagesize']) ? (int) $_REQUEST['pagesize'] : 10;
     $page = isset($_REQUEST['page']) ? (int) $_REQUEST['page'] : 0;
     $page_start = $page * $pagesize;
     $order = isset($_REQUEST['order']) ? trim($_REQUEST['order']) : 'create_time';
     $seq = isset($_REQUEST['seq']) ? trim($_REQUEST['seq']) : 'DESC';
     $im_cp = $_SESSION['role'] == Auth::$CP_PERMISSION;
     $me = $_SESSION['id'];
     $ad_app_type = trim($ad_app_type) == "android" ? ADModel::ANDROID : ADModel::IOS;
     $filters = array('ad_app_type' => $ad_app_type, 'keyword' => $_REQUEST['keyword'], 'ad_name' => $_REQUEST['ad_name'], 'channel' => $_REQUEST['channel'], $im_cp ? 'create_user' : 'salesman' => $me);
     $filters = Utils::array_pick_effect($filters);
     $ad_service = new AD();
     // 根据广告类型取出所有广告
     $ad_ids = $ad_service->get_ad_ids($filters);
     $result = array('oversea' => false, 'ads' => array());
     $total = array('rmb1' => 0, 'device1' => 0, 'native' => 0, 'native_transfer' => 0, 'task_num' => 0, 'task_rmb' => 0, 'task_ready' => 0, 'cpa' => 0, 'click' => 0, 'install' => 0, 'callback' => 0);
     $ids = array();
     $redis = $this->get_redis();
     $daily_service = new DailyStat();
     for ($date = $start; $date <= $end;) {
         list($ymd, $monthly, $date) = $daily_service->judge_date($date, $end);
         $key = 'diy_stat_ad_install_' . $ymd;
         $value = $redis->get($key);
         if ($value) {
             $redis->setTimeout($key, 86400 * 30);
         } else {
             $value = $daily_service->ad_stat_by_date($ymd, $monthly);
             $value = json_encode($value);
             if ($value && ($monthly || strtotime($ymd) < time() - 3600 * 30)) {
                 $redis->setex($key, 86400 * 30, $value);
             }
         }
         $stat = json_decode($value, true);
         foreach ($stat as $ad_stat) {
             $ad_id = $ad_stat['id'];
             if (!in_array($ad_id, $ad_ids)) {
                 continue;
             }
             $ids = array_unique(array_merge($ids, array($ad_id)));
             foreach ($total as $key => $value) {
                 $result['ads'][$ad_id][$key] += $ad_stat[$key];
                 $total[$key] += $ad_stat[$key];
             }
         }
     }
     $adinfos = $ad_service->get_all_ad_info($filters);
     foreach ($ids as $ad_id) {
         if ($adinfos[$ad_id]['oversea']) {
             continue;
         }
         if (!array_key_exists($ad_id, $adinfos)) {
             continue;
         }
         $comments = $ad_service->get_ad_comments_by_id($ad_id);
         $result['ads'][$ad_id] = array_merge($result['ads'][$ad_id], array('id' => $ad_id, 'channel_id' => $adinfos[$ad_id]['cid'], 'channel' => $adinfos[$ad_id]['channel'], 'agreement' => $adinfos[$ad_id]['agreement'], 'ad_name' => $adinfos[$ad_id]['ad_name'], 'comments' => $comments, 'ctime' => date('m-d', strtotime($adinfos[$ad_id]['create_time'])), 'others' => isset($adinfos[$ad_id]['others']) ? $adinfos[$ad_id]['others'] : '添加注释', 'sdk_type' => $adinfos[$ad_id]['ad_sdk_type'] == 7 ? 'promotions' : ($adinfos[$ad_id]['ad_sdk_type'] == 2 ? 'push' : ($adinfos[$ad_id]['ad_sdk_type'] == 4 ? 'wap' : 'ad_list not_promotions')), 'native_type' => $adinfos[$ad_id]['banner_url'] ? 'native' : '', 'ratio' => $result['ads'][$ad_id]['click'] ? round($result['ads'][$ad_id]['device1'] / $result['ads'][$ad_id]['click'] * 100, 2) : 0, 'click_ratio' => $result['ads'][$ad_id]['click'] ? round($result['ads'][$ad_id]['cpa'] / $result['ads'][$ad_id]['click'] * 100, 2) : 0, 'task_ratio' => $result['ads'][$ad_id]['task_ready'] ? round($result['ads'][$ad_id]['task_num'] / $result['ads'][$ad_id]['task_ready'] * 100, 2) : 0));
     }
     // 总计
     $total['ratio'] = $total['click'] ? round($total['transfer'] / $total['click'] * 100, 2) : 0;
     $total['click_ratio'] = $total['click'] ? round($total['cpa'] / $total['click'] * 100, 2) : 0;
     $total['task_ratio'] = $total['task_ready'] ? round($total['task_num'] / $total['task_ready'] * 100, 2) : 0;
     $result['total'] = array_merge(array('id' => 'amount', 'is_amount' => true), $total);
     $list = array_values($result['ads']);
     $total = count($list);
     // 排序
     $list = $this->get_order_list($list, $order, $seq);
     //分页
     $list = array_slice($list, $page_start, $pagesize);
     if (count($list) > 0) {
         array_push($list, $result['total']);
     }
     $is_android = $ad_app_type == 1 ? true : false;
     $this->output(array('code' => 0, 'msg' => 'get', 'list' => $list, 'total' => $total, 'options' => array('is_android' => $is_android)));
 }
 public function get_transfer_ad($ad_id)
 {
     $me = $_SESSION['id'];
     $start = $_REQUEST['start'];
     $end = $_REQUEST['end'];
     $ad_service = new AD();
     $channel = $ad_service->get_ad_channel_by_id($ad_id);
     $agreement_service = new Agreement();
     $agreement = $agreement_service->get_agreement_by_adid($ad_id);
     $agreement_id = $agreement['id'];
     $filters = array('salesman' => $me, 'channel' => $channel);
     // 取出用户在该渠道下的所有广告
     $ads = $ad_service->get_all_ad_info($filters);
     // 取出这段期间内有量的广告
     $quote_service = new Quote();
     $quote_ads = $quote_service->get_all_quote_ad($start, $end);
     $ids = array_unique(array_intersect(array_keys($ads), array_keys($quote_ads)));
     $list = array();
     $invoice_service = new Invoice();
     foreach ($ids as $id) {
         // 剔除该段时间内已经开过票的广告
         $nums = $invoice_service->is_invoice($id, $start, $end);
         if ($nums > 0) {
             continue;
         }
         // 剔除没有关联合同的广告
         if (!isset($ads[$id]['agreement_id'])) {
             continue;
         }
         $ad = array('ad_id' => $id, 'ad_name' => $ads[$id]['ad_name'], 'channel_id' => $ads[$id]['cid'], 'channel' => $agreement_id, 'alias' => $ads[$id]['channel']);
         $list[] = $ad;
     }
     $this->output(array('code' => 0, 'msg' => "get", 'list' => $list));
 }