title = '账单统计'; $query = $this->_query($this->table); $date = input('date'); if ($date) { $date = str_replace('-', '', $date); $temp = explode('~', $date); $query->whereRaw('(replace(day, "-", "")+0) >= '. $temp[0] . ' and (replace(day, "-", "")+0) <= ' . $temp[1]); } $query->equal('day')->dateBetween('create_at'); $field = '*,(replace(day, "-", "")+0) as day_num'; $order = 'day_num desc,id desc'; $query->field($field); if (input('output') === 'json') { $result = $query->order($order)->page(true, false); $this->success('获取数据列表成功', $result); } else { $query->order($order)->page(); } } /** * 数据列表处理 * @param array $data * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ protected function _page_filter(&$data) { foreach ($data as &$vo) { $log['channel'] = AccountLogService::instance()->db()->where(array('day' => $vo['day'], 'type' => 1))->order('id asc')->select(); $log['merchant'] = AccountLogService::instance()->db()->where(array('day' => $vo['day'], 'type' => 2))->order('id asc')->select(); $log['account'] = AccountLogService::instance()->db()->where(array('day' => $vo['day'], 'type' => 3))->order('id asc')->select(); $vo['channel'] = $this->getTable('渠道', $log['channel']); $vo['merchant'] = $this->getTable('商户', $log['merchant']); $vo['account'] = $this->getTable('公户', $log['account']); $vo['merchant_total'] = round($vo['merchant_total'], 2); $vo['channel_total'] = round($vo['channel_total'], 2); $vo['account_total'] = round($vo['account_total'], 2); $vo['total'] = $vo['merchant_total'] + $vo['channel_total'] + $vo['account_total']; $ydata = AccountService::instance()->getDay($vo['day']); $vo['profit'] = '0.00'; if ($ydata) { $ytotal = $ydata['merchant_total'] + $ydata['channel_total'] + $ydata['account_total']; $vo['profit'] = round($vo['total']- $ytotal, 2); } } } private function getTotal($data) { $table = ''; $table .= ''; $table .= ''; $merchant = round($data['merchant_total'], 2); $channel = round($data['channel_total'], 2); $account = round($data['account_total'], 2); $total = $merchant + $channel + $account; $table .= ''; $table .= ''; $table .= ''; $table .= ''; $table .= ''; $table .= ''; $table .= ''; $table .= ''; $ydata = AccountService::instance()->getDay($data['day']); if ($ydata) { $ytotal = $ydata['merchant_total'] + $ydata['channel_total'] + $ydata['account_total']; $yue = round($total - $ytotal, 2); $table .= ''; $table .= ''; } $table .= '
类型 金额
商户 '.$merchant.'
渠道 '.$channel.'
公户 '.$account.'
合计 '.$total.'
利润 '.$yue.'
'; return $table; } private function getTable($name, $data) { $info = array(); $thead = array(); foreach ($data as $k => $v) { if (!$v['name']) { if ($v['type'] == -1) { $v['name'] = '余额'; } elseif ($v['type'] == 1) { $v['name'] = '收入'; } elseif ($v['type'] == 2) { $v['name'] = '支出'; } } if ($v['type'] == -1) { $v['type'] = 1; } $v['num'] = str_replace(',','',$v['num']); $v['num'] = (float) $v['num']; if (isset($info[$v['type_name']][$v['name']]) && $info[$v['type_name']][$v['name']]) { $info[$v['type_name']][$v['name']]['num'] += $v['num']; } else { $info[$v['type_name']][$v['name']] = $v; } $thead[$v['name']] = $v['name']; } $table = ''; $table .= ''; foreach ($thead as $k => $v) { $table .= ''; } $table .= ''; $total = array(); foreach ($info as $k => $v) { $table .= ''; $table .= ''; foreach ($thead as $k1 => $v1) { if (isset($v[$k1]) && $v[$k1]) { $total[$k1]['oper'] = $v[$k1]['oper']; if (!isset($total[$k1]['num'])) { $total[$k1]['num'] = 0; } $v[$k1]['num'] = (float) $v[$k1]['num']; $total[$k1]['num'] += $v[$k1]['num']; /* if ($v[$k1]['oper'] == 1) { $v[$k1]['num'] = '+' . $v[$k1]['num']; } elseif ($v[$k1]['oper'] == 2 && $v[$k1]['num'] > 0) { $v[$k1]['num'] = '-' . $v[$k1]['num']; } if ($v[$k1]['num'] == 0) { $v[$k1]['num'] = '0.00'; } */ if ($k1 == '对账') { if ($v[$k1]['num'] != $v['当日余额']['num']) { $v[$k1]['num'] = ''.$v[$k1]['num'].''; } } $table .= ''; } } $table .= ''; } $table .= ''; foreach ($total as $k2 => $v2) { // $v2['num'] = str_replace(',', '', number_format($v2['num'], 2)); /* if ($v2['oper'] == 1) { $v2['num'] = '+' . $v2['num']; } elseif ($v2['oper'] == 2 && $v2['num'] > 0) { $v2['num'] = '-' . $v2['num']; } */ $table .= ''; } $table .= ''; $table .= '
'.$name.''.$v.'
'.$k.''.$v[$k1]['num'].'
合计'.round($v2['num'], 2).'
'; return $table; } /** * 重新统计 * @auth true * @throws \think\db\exception\DbException */ public function reset() { $day = input('day'); \app\setting\service\AccountLogService::instance()->handle($day); $this->success('操作成功'); } }