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 init($id)
 {
     $me = $_SESSION['id'];
     $invoice_service = new Invoice();
     $admin_service = new Admin();
     $agreement_service = new Agreement();
     // 获取收款方业务负责人
     $chargers = $admin_service->get_chargers($me);
     $applicant = $admin_service->get_sales_info($me);
     $is_assistant = false;
     if ($chargers) {
         $is_assistant = true;
     }
     $options = array('types' => InvoiceModel::$TYPE, 'content_types' => InvoiceModel::$CONTENT_TYPE, 'is_assistant' => $is_assistant);
     if ($id === 'init') {
         $this->get_init_params($chargers, $applicant, $options);
     }
     $res = $invoice_service->get_invoice_by_id($id);
     if ($res['agreement_id']) {
         $agreement = $agreement_service->get_agreement_by_id(array('id' => $res['agreement_id']));
         // 结算单收款方信息
         $accept = InvoiceModel::$ACCEPT[$agreement['company_dianjoy']];
     }
     $products = $invoice_service->get_invoice_ad_by_invoiceid($id);
     $res = array_merge(array('products' => $products, 'chargers' => $chargers, 'company' => $agreement['company'], 'company_short' => $agreement['company_short'], 'agreement_number' => $agreement['agreement_id'], 'cycle' => $agreement['cycle'], 'rmb' => $agreement['rmb'], 'agreement_comment' => $agreement['comment'], 'ad_name' => $agreement['ad_name'], 'sign_date' => $agreement['sign_date'], 'company_dianjoy' => $agreement['company_dianjoy'], 'accept_company' => $accept['accept_company'], 'accept_bank' => $accept['accept_bank'], 'accept_account' => $accept['accept_account']), $res);
     if (count($products) > 0) {
         $res = array_merge($res, array('income' => round($res['income'] / 100, 2), 'income_first' => round($res['income_first'] / 100, 2), 'joy_income' => round($res['joy_income'] / 100, 2), 'red_ad_income' => round($res['red_ad_income'] / 100, 2), 'red_ios_income' => round($res['red_ios_income'] / 100, 2)));
     }
     if ($res['status'] == 40) {
         $options = array_merge(array('view' => true), $options);
     }
     $this->output(array('code' => 0, 'msg' => 'fetched', 'invoice' => $res, 'options' => $options));
 }