public function export_payment($start, $end, $owner)
 {
     $daily_service = new DailyStat();
     $filters = array('salesman' => $owner);
     $result = $daily_service->get_daily_stat($start, $end, $filters, null, null, 0, 0);
     $list = $result['list'];
     $payments = $this->get_payment_by_owner($start, $end, $owner);
     foreach ($list as $key => $value) {
         $list[$key]['ad_app_type'] = $value['ad_app_type'] == ADModel::ANDROID ? 'Android' : 'iOS';
         $list[$key]['status'] = $value['status'] == ADModel::ONLINE ? 'ON' : 'OFF';
         $list[$key]['channel_type'] = ChannelModel::$TYPE[$value['channel_type']];
         $list[$key]['payment_status'] = '';
         foreach ($payments[$value['id']] as $payment) {
             if (!$payment['payment'] || !$payment['rmb']) {
                 $list[$key]['payment_status'] = 'N';
             } else {
                 $list[$key]['paid_time'] .= $payment['paid_time'] . ' ' . $payment['payment_person'] . ' ';
                 $list[$key]['invoice_time'] .= $payment['invoice_time'] . ' ';
                 $list[$key]['invoice_rmb'] .= $payment['invoice_rmb'] . ' ';
                 $list[$key]['real_rmb'] .= $payment['real_rmb'] / 100 . ' ';
                 $list[$key]['comment'] .= $payment['comment'] . ' ';
             }
         }
         $list[$key] = Utils::array_pick($list[$key], Payment::$FIELD_PAYMENT_EXPORT);
     }
     $title = array('渠道名', '渠道全称', '渠道类型', '广告名', '渠道号', '创建时间', '状态', '平台', '广告类型', '扣分', '排序', '激活支出', '深度任务支出', '收入', '红包锁屏收入', '其他收入', 'CPA', '渠道CPA', '单价', '实际价格', '损益价格', '毛利润', '毛利润率(%)', '结算率(%)', '商务负责人', '商务执行人', '是否回款', '回款时间', '开票时间', '开票金额', '实际回款金额', '备注');
     array_unshift($list, $title);
     $excel_service = new Excel();
     $excel_service->export($list, "{$start}至{$end}广告数据分析");
 }
 /**
  * 取单个广告按日数据分析
  * @param $id
  */
 public function get_daily_ad($id)
 {
     list($start, $end) = $this->getFilter();
     $daily_service = new DailyStat();
     $result = $daily_service->get_daily_ad_stat($id, $start, $end);
     $sublist = array($result['ad']);
     $this->output(array('list' => $result['list'], 'sublist' => $sublist, 'code' => 0, 'msg' => 'ok'));
 }
 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)));
 }