public function get_notice($admin_id, $role, $latest)
 {
     $DB = $this->get_read_pdo();
     $m = new Mustache_Engine();
     $ad_service = new AD();
     $sql = "SELECT `type`\n            FROM `t_alarm_group`\n            WHERE `group`={$role}";
     $types = $DB->query($sql)->fetchAll(PDO::FETCH_COLUMN);
     $types = implode(',', $types);
     $type_sql = $types ? " OR `alarm_type` IN ({$types})" : '';
     // 只取最近一周,再早的估计也没啥处理的必要了
     $date = date('Y-m-d', time() - 86400 * 6);
     $sql = "SELECT a.`id`, `uid`, `user_id`, `app_id`, `ad_id`, a.`status`,\n              `create_time`, `op_time`, `description`, `handler`\n            FROM `t_admin_alarm_log` a LEFT JOIN `t_alarm_type` t ON a.alarm_type=t.id\n            WHERE (`admin_id`='{$admin_id}' {$type_sql})\n              AND `create_time`>'{$date}' AND a.`status`=0 AND a.`id`>{$latest}\n            ORDER BY `id` DESC";
     $alarms = $DB->query($sql)->fetchAll(PDO::FETCH_ASSOC);
     foreach ($alarms as &$alarm) {
         $alarm['id'] = (int) $alarm['id'];
         if ($alarm['ad_id']) {
             if (strlen($alarm['ad_id']) == 32) {
                 $ad = $ad_service->get_ad_info(array('id' => $alarm['ad_id']), 0, 1);
                 $alarm['name'] = $ad['ad_name'];
             }
         } else {
             if ($alarm['uid']) {
                 // 发票通知提醒
                 $invoice_id = $alarm['uid'];
                 $invoice_service = new Invoice();
                 $agreement_service = new Agreement();
                 $invoice = $invoice_service->get_invoice_by_id($invoice_id);
                 $agreement = $agreement_service->get_agreement_by_id(array('id' => $invoice['agreement_id']));
                 $alarm['channel'] = $agreement['company_short'];
                 $admin_service = new Admin();
                 $sale = $admin_service->get_sales_info($alarm['user_id']);
                 $alarm['sale'] = $sale['NAME'];
             }
         }
         $alarm['status'] = (int) $alarm['status'];
         $alarm['handler'] = $m->render($alarm['handler'], $alarm);
     }
     return $alarms;
 }
 public function is_invoice($ads, $start, $end)
 {
     $invoice_service = new Invoice();
     $is_invoice = false;
     $list = array();
     foreach ($ads as $ad) {
         $ad_id = $ad['id'];
         $nums = $invoice_service->is_invoice($ad_id, $start, $end);
         if ($nums > 0 || $ad['cpa'] == 0) {
             $is_invoice = true;
         }
         $ad = array_merge(array('is_invoice' => $is_invoice), $ad);
         array_push($list, $ad);
         $is_invoice = false;
     }
     return $list;
 }
 public function update_invoice_ad(array $attr = null)
 {
     $DB_write = $this->get_write_pdo();
     // 更新发票关联表
     $this->attributes = array_merge($this->attributes, $attr);
     $result = SQLHelper::update($DB_write, self::$T_INVOICE_AD, $attr, $this->id);
     if (!$result) {
         throw new Exception('更新广告失败', 20);
     }
     $invoice_service = new Invoice();
     $invoice_ad = $invoice_service->get_invoice_ad_by_id($this->id);
     $res = $invoice_service->get_invoice_ad_by_invoiceid($invoice_ad['i_id']);
     // 记录核减
     if (isset($attr['cpa_after']) || isset($attr['quote_rmb_after'])) {
         $income = $invoice_ad['cpa'] * $invoice_ad['quote_rmb'];
         $income_after = $invoice_ad['cpa_after'] * $invoice_ad['quote_rmb_after'];
         if ($income != $income_after) {
             $this->save_ad_cut(array_merge(array('income' => $income, 'income_after' => $income_after), $invoice_ad));
         }
     }
     $invoice = $invoice_service->get_invoice_by_id($invoice_ad['i_id']);
     // 验证状态
     $attr = $this->get_params_by_income($res, $invoice['comment']);
     // 核减发送通知
     if ($attr['status'] == 8) {
         $this->send_notice($invoice);
     }
     // 更新发票表
     $result = SQLHelper::update($DB_write, self::$T_INVOICE, $attr, $invoice['id']);
     if (!$result && $result != 0) {
         throw new Exception('更新发票失败', 21);
     }
     return $this->attributes;
 }
 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));
 }