|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Admin\Controller;
|
|
|
|
|
|
|
|
use Base\Service\PromoteCompanyService;
|
|
|
|
|
|
|
|
class PromoteCompanyWarningController extends ThinkController
|
|
|
|
{
|
|
|
|
private $modelName = 'PromoteCompanyWarning';
|
|
|
|
|
|
|
|
public function _initialize()
|
|
|
|
{
|
|
|
|
$this->admininfo = $_SESSION['onethink_admin']['user_auth'];
|
|
|
|
parent::_initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function monthly()
|
|
|
|
{
|
|
|
|
$this->search('monthly');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function total()
|
|
|
|
{
|
|
|
|
$this->search('total');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function search($type)
|
|
|
|
{
|
|
|
|
$month = I('month', '');
|
|
|
|
$companyId = I('company_id', 0);
|
|
|
|
$isExport = I('is_export', 0);
|
|
|
|
$isSignContact = I('is_sign_contact', 0);
|
|
|
|
$where = [
|
|
|
|
'_string' => '1=1 and b.company_id is not null',
|
|
|
|
'c.company_belong' => ['in', [1, 2]],
|
|
|
|
];
|
|
|
|
if ($type == 'monthly') {
|
|
|
|
$where['b.is_warning'] = 1;
|
|
|
|
}
|
|
|
|
if ($companyId != 0) {
|
|
|
|
$where['b.company_id'] = $companyId;
|
|
|
|
}
|
|
|
|
if ($isSignContact != -1) {
|
|
|
|
$where['c.is_sign_contact'] = $isSignContact;
|
|
|
|
}
|
|
|
|
if ($month != '') {
|
|
|
|
$monthNumber = date('Ym', strtotime($month.'-01'));
|
|
|
|
$where['b.month'] = $monthNumber;
|
|
|
|
}
|
|
|
|
$where['c.cooperation_status'] = 1;
|
|
|
|
|
|
|
|
$joinDataTable = 'tab_company_monthly_data';
|
|
|
|
$columns = ['b.company_id', 'c.company_name', 'c.is_sign_contact', 'b.pay_amount', 'c.cooperation_status'];
|
|
|
|
$orderBy = 'b.company_id asc';
|
|
|
|
if ($type == 'total') {
|
|
|
|
$subSql = M('company_monthly_data', 'tab_')->field('company_id')->where(['is_warning' => 1])->select(false);
|
|
|
|
|
|
|
|
$warningAmount = C('PRO_COM_TOTAL_AMOUNT', null, 100000);
|
|
|
|
$tmpTable = M('company_monthly_data', 'tab_')
|
|
|
|
->field(['company_id', 'sum(pay_amount) pay_amount'])
|
|
|
|
->where(['_string' => 'company_id not in(' . $subSql . ')'])
|
|
|
|
->group('company_id')
|
|
|
|
->having('pay_amount >= ' . $warningAmount)
|
|
|
|
->select(false);
|
|
|
|
$joinDataTable = '(' . $tmpTable . ')';
|
|
|
|
} else {
|
|
|
|
$columns[] = 'b.month';
|
|
|
|
$orderBy = 'b.month desc, b.company_id asc';
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = M('promote_company', 'tab_')->alias('c')
|
|
|
|
->field($columns)
|
|
|
|
->join('left join ' . $joinDataTable . ' b on b.company_id=c.id')
|
|
|
|
->where($where)
|
|
|
|
->order($orderBy);
|
|
|
|
|
|
|
|
$items = [];
|
|
|
|
$page = '';
|
|
|
|
if ($isExport) {
|
|
|
|
$items = $query->select();
|
|
|
|
} else {
|
|
|
|
[$items, $page, $count] = $this->paginate($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
$companyPromotes = [];
|
|
|
|
$admins = [];
|
|
|
|
$adminDepartments = [];
|
|
|
|
if (count($items) > 0) {
|
|
|
|
$companyIds = array_column($items, 'company_id');
|
|
|
|
$promotes = M('promote', 'tab_')->field(['id', 'account', 'admin_id', 'company_id'])->where(['company_id' => ['in', $companyIds], 'level' => 1])->select();
|
|
|
|
$adminIds = array_column($promotes, 'admin_id');
|
|
|
|
$admins = M('member', 'sys_')->field(['uid', 'real_name'])->where(['uid' => ['in', $adminIds]])->select();
|
|
|
|
$adminDepartments = getAdminDepartmentList($adminIds);
|
|
|
|
$admins = index_by_column('uid', $admins);
|
|
|
|
|
|
|
|
foreach ($promotes as $promote) {
|
|
|
|
$companyPromotes[$promote['company_id']][] = $promote;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$records = [];
|
|
|
|
foreach ($items as $key => $item) {
|
|
|
|
$item['month'] = date('Y-m', strtotime($item['month'].'01'));
|
|
|
|
$item['is_sign_contact_text'] = $item['is_sign_contact'] == 1 ? '是' : '否';
|
|
|
|
$item['cooperation_status_text'] = $item['cooperation_status'] == 1 ? '合作中' : '终止合作';
|
|
|
|
$itemPromotes = $companyPromotes[$item['company_id']];
|
|
|
|
$promoteCount = count($itemPromotes);
|
|
|
|
$index = 0;
|
|
|
|
foreach ($itemPromotes as $promote) {
|
|
|
|
if ($index == 0) {
|
|
|
|
$item['rowspan'] = $promoteCount;
|
|
|
|
} else {
|
|
|
|
$item['rowspan'] = 0;
|
|
|
|
}
|
|
|
|
$item['market_department_name'] = $adminDepartments[$promote['admin_id']] ?? '无';
|
|
|
|
$admin = $admins[$promote['admin_id']] ?? null;
|
|
|
|
$item['market_user_name'] = $admin ? $admin['real_name'] : '无';
|
|
|
|
$item['account'] = $promote['account'];
|
|
|
|
$index ++;
|
|
|
|
$records[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$records = [];
|
|
|
|
foreach ($items as $key => $item) {
|
|
|
|
if (isset($item['month'])) {
|
|
|
|
$item['month'] = date('Y-m', strtotime($item['month'].'01'));
|
|
|
|
}
|
|
|
|
$item['is_sign_contact_text'] = $item['is_sign_contact'] == 1 ? '是' : '否';
|
|
|
|
$item['cooperation_status_text'] = $item['cooperation_status'] == 1 ? '合作中' : '终止合作';
|
|
|
|
$itemPromotes = $companyPromotes[$item['company_id']];
|
|
|
|
$promoteCount = count($itemPromotes);
|
|
|
|
$index = 0;
|
|
|
|
foreach ($itemPromotes as $promote) {
|
|
|
|
if ($index == 0) {
|
|
|
|
$item['rowspan'] = $promoteCount;
|
|
|
|
} else {
|
|
|
|
$item['rowspan'] = 0;
|
|
|
|
}
|
|
|
|
$item['market_department_name'] = $adminDepartments[$promote['admin_id']] ?? '无';
|
|
|
|
$admin = $admins[$promote['admin_id']] ?? null;
|
|
|
|
$item['market_user_name'] = $admin ? $admin['real_name'] : '无';
|
|
|
|
$item['account'] = $promote['account'];
|
|
|
|
$index ++;
|
|
|
|
$records[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$pageTypeNames = [
|
|
|
|
'monthly' => '合同签订当月流水预警',
|
|
|
|
'total' => '合同签订累计流水预警',
|
|
|
|
];
|
|
|
|
$pageTypeName = $pageTypeNames[$type];
|
|
|
|
|
|
|
|
$this->assign('records', $records);
|
|
|
|
$this->assign('pageType', $type);
|
|
|
|
$this->assign('pageTypeName', $pageTypeName);
|
|
|
|
|
|
|
|
if($isExport){
|
|
|
|
addOperationLog(['op_type'=>3,'key'=>getNowDate(),'op_name'=>'导出推广公司','url'=>U('PromoteCompany/lists'),'menu'=>'推广员-公司档案管理-' . $pageTypeName]);
|
|
|
|
$this->display("export");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($page) {
|
|
|
|
$this->assign('_page', $page);
|
|
|
|
}
|
|
|
|
|
|
|
|
$totalWarningAmount = C('PRO_COM_TOTAL_AMOUNT', null, 100000);
|
|
|
|
$monthlyWarningAmount = C('PRO_COM_MONTHLY_AMOUNT', null, 50000);
|
|
|
|
|
|
|
|
$this->assign('totalWarningAmount', $totalWarningAmount);
|
|
|
|
$this->assign('monthlyWarningAmount', $monthlyWarningAmount);
|
|
|
|
|
|
|
|
$companies = M('promote_company', 'tab_')->field(['company_name', 'id'])->where(['company_belong' => ['in', [1, 2]]])->select();
|
|
|
|
$this->assign('companies', $companies);
|
|
|
|
$this->display('records');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveSetting()
|
|
|
|
{
|
|
|
|
$monthlyAmount = I('monthly_amount', 0);
|
|
|
|
$totalAmount = I('total_amount', 0);
|
|
|
|
$beforeTotalAmount = C('PRO_COM_TOTAL_AMOUNT');
|
|
|
|
$beforeMonthlyAmount = C('PRO_COM_MONTHLY_AMOUNT');
|
|
|
|
|
|
|
|
if (!is_numeric($monthlyAmount)) {
|
|
|
|
$this->ajaxReturn([
|
|
|
|
'status' => 0,
|
|
|
|
'message' => '月达标金额格式错误'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_numeric($totalAmount)) {
|
|
|
|
$this->ajaxReturn([
|
|
|
|
'status' => 0,
|
|
|
|
'message' => '累计达标金额格式错误'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($monthlyAmount < 0) {
|
|
|
|
$this->ajaxReturn([
|
|
|
|
'status' => 0,
|
|
|
|
'message' => '月达标金额需大于0'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($totalAmount < 0) {
|
|
|
|
$this->ajaxReturn([
|
|
|
|
'status' => 0,
|
|
|
|
'message' => '累计达标金额需大于0'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($monthlyAmount != $beforeMonthlyAmount) {
|
|
|
|
M('config', 'sys_')->where(['name' => 'PRO_COM_MONTHLY_AMOUNT'])->save(['value' => $monthlyAmount]);
|
|
|
|
S('DB_CONFIG_DATA', null);
|
|
|
|
$service = new PromoteCompanyService();
|
|
|
|
$month = date('Y-m');
|
|
|
|
$service->setCompanyMontlyIsWarning($month);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($totalAmount != $beforeTotalAmount) {
|
|
|
|
M('config', 'sys_')->where(['name' => 'PRO_COM_TOTAL_AMOUNT'])->save(['value' => $totalAmount]);
|
|
|
|
S('DB_CONFIG_DATA', null);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->ajaxReturn([
|
|
|
|
'status' => 1,
|
|
|
|
'message' => '操作成功'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|