123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- <?php
- class refill_companyControl extends SystemControl
- {
- public function __construct()
- {
- parent::__construct();
- }
- public function indexOp()
- {
- $cond = [];
- $company_mod = Model('refill_company');
- if(!empty($_GET['co_name'])) {
- $cond['co_name'] = ['like', '%'.$_GET['co_name'].'%'];
- }
- $_GET['co_type'] = $_GET['co_type'] ?? 'merchant';
- if(!empty($_GET['co_type'])) {
- $cond['co_type'] = $_GET['co_type'];
- }
- if(!empty($_GET['opened'])) {
- $cond['opened'] = $_GET['opened'];
- }
- $list = $company_mod->getCompanyList($cond, 50);
- $company_debts = rcache("company-debts", 'refill-');
- foreach ($list as $key => $value)
- {
- $co_id = $value['co_id'];
- if(array_key_exists($co_id, $company_debts)) {
- $list[$key]['debt'] = number_format($company_debts[$co_id],2,'.','');
- }else{
- $list[$key]['debt'] = '/';
- }
- }
- Tpl::output('list', $list);
- Tpl::output('show_page', $company_mod->showpage());
- Tpl::showpage('refill.company');
- }
- public function changeStateOp()
- {
- $co_id = intval($_GET['co_id']);
- $opened = intval($_GET['opened']);
- $company_mod = Model('refill_company');
- $company_info = $company_mod->getCompanyInfo(['co_id' => $co_id]);
- if (empty($company_info) || !in_array($opened, [1, 2])) {
- showMessage('操作成功');
- }
- $resp = $company_mod->editCompany(['opened' => $opened], ['co_id' => $co_id]);
- if (!$resp) {
- showMessage('操作失败', 'index.php?act=refill_company&op=index', 'html', 'error');
- }
- showMessage('操作成功');
- }
- public function company_delOp()
- {
- $co_id = intval($_GET['co_id']);
- $company_mod = Model('refill_company');
- $company_info = $company_mod->getCompanyInfo(['co_id' => $co_id]);
- $resp = $company_mod->delCompany($co_id);
- if (!$resp) {
- showMessage('删除失败');
- }
- $co_type = $company_info['co_type'];
- $res = true;
- if ($co_type == 'merchant') {
- $res = Model('')->table('merchant')->where(['co_id' => $co_id])->update(['co_id' => 0]);
- } elseif ($co_type == 'provider') {
- $res = Model('')->table('refill_provider')->where(['co_id' => $co_id])->update(['co_id' => 0]);
- }
- if (!$res) {
- showMessage('操作失败');
- }
- showMessage('操作成功');
- }
- public function addOp()
- {
- $company_mod = Model('refill_company');
- if (chksubmit())
- {
- $obj_validate = new Validator();
- $obj_validate->validateparam = [
- ["input" => $_POST["co_type"], "require" => "true", "message" => '必须选择公司类型'],
- ["input" => $_POST["co_name"], "require" => "true", "message" => '公司名称不能为空'],
- ["input" => $_POST["bank_name"], "require" => "true", "message" => '收款银行名称不能为空'],
- ["input" => $_POST["bank_username"], "require" => "true", "message" => '开户人名称不能为空'],
- ["input" => $_POST["bank_card_no"], "require" => "true", "message" => '收款卡号不能为空']
- ];
- $error = $obj_validate->validate();
- if ($error != '') {
- showMessage($error);
- } else {
- $insert_array['co_name'] = $_POST['co_name'];
- $insert_array['co_type'] = $_POST['co_type'];
- $insert_array['bank_name'] = $_POST['bank_name'];
- $insert_array['bank_username'] = $_POST['bank_username'];
- $insert_array['bank_card_no'] = $_POST['bank_card_no'];
- $insert_array['max_debt'] = $_POST['max_debt'] ?? 0;
- $insert_array['province'] = $_POST['province'] ?? '';
- $insert_array['city'] = $_POST['city'] ?? '';
- $insert_array['bank_code'] = $_POST['bank_code'] ?? '';
- $insert_array['remark'] = $_POST['remark'] ?? '';
- $insert_array['oper_time'] = time();
- $co_id = $company_mod->addCompany($insert_array);
- if ($co_id) {
- $cid = $_POST['cid'] ?? '';
- $this->co_id_bind($cid, $co_id, $insert_array['co_type']);
- showMessage('添加成功', 'index.php?act=refill_company&op=index');
- } else {
- showMessage('添加失败');
- }
- }
- }
- else
- {
- Tpl::showpage('refill.company.add');
- }
- }
- public function editOp()
- {
- $co_id = $_GET['co_id'] ?? $_POST['co_id'];
- $company_mod = Model('refill_company');
- $company_info = $company_mod->getCompanyInfo(['co_id' => $co_id]);
- if (empty($company_info)) {
- showMessage('公司信息不存在');
- }
- if (chksubmit()) {
- $update['co_type'] = $_POST['co_type'];
- $update['co_name'] = $_POST['co_name'];
- $update['bank_name'] = trim($_POST['bank_name']);
- $update['bank_username'] = trim($_POST['bank_username']);
- $update['bank_card_no'] = trim($_POST['bank_card_no']);
- $update['max_debt'] = trim($_POST['max_debt']);
- $update['province'] = trim($_POST['province']);
- $update['city'] = trim($_POST['city']);
- $update['bank_code'] = trim($_POST['bank_code']);
- $update['remark'] = trim($_POST['remark']);
- $update['oper_time'] = time();
- $result = $company_mod->editCompany($update, ['co_id' => $co_id]);
- if (!$result) {
- showMessage('编辑失败');
- }
- $cid = $_POST['cid'] ?? '';
- $this->co_id_bind($cid, $co_id, $company_info['co_type']);
- $this->log("修改公司信息,公司ID:{$co_id},公司名称:{$company_info['co_name']}");
- showMessage('编辑成功','index.php?act=refill_company&op=index');
- }
- else
- {
- $cid = [];
- if($company_info['co_type'] == 'merchant') {
- $cid = Model('')->table('merchant')->where(['co_id' => $co_id])->field('mchid')->select();
- $cid = array_column($cid, 'mchid');
- }elseif ($company_info['co_type'] == 'provider') {
- $cid = Model('')->table('refill_provider')->where(['co_id' => $co_id])->field('store_id')->select();
- $cid = array_column($cid, 'store_id');
- }
- Tpl::output('cid', implode(',', $cid));
- Tpl::output('company_info', $company_info);
- Tpl::showpage('refill.company.edit');
- }
- }
- private function co_id_bind($cid, $co_id, $co_type)
- {
- if(!empty($cid)) {
- $cid = explode(',', $cid);
- if($co_type == 'merchant'){
- $cond['mchid'] = ['in', $cid];
- Model('')->table('merchant')->where(['co_id' => $co_id])->update(['co_id' => 0]);
- Model('')->table('merchant')->where($cond)->update(['co_id' => $co_id]);
- }elseif ($co_type == 'provider'){
- $cond['store_id'] = ['in', $cid];
- Model('')->table('refill_provider')->where(['co_id' => $co_id])->update(['co_id' => 0]);
- Model('')->table('refill_provider')->where($cond)->update(['co_id' => $co_id]);
- }
- }
- }
- public function remit_cfgOp()
- {
- if(chksubmit())
- {
- unset($_POST['form_submit']);
- $cache = [];
- foreach ($_POST as $key => $value) {
- $cache[$key] = $value;
- }
- wcache('remit', ['cfg' => serialize($cache)], 'refill-');
- showMessage('编辑成功');
- }
- else
- {
- $config = rcache('remit', 'refill-');
- if (empty($config)) {
- $config = [];
- } else {
- $config = unserialize($config['cfg']);
- }
- Tpl::output('config', $config);
- Tpl::showpage('remit.config');
- }
- }
- public function remit_recordOp()
- {
- $mod_remit = Model('refill_company_remit');
- $cond = [];
- $start_unixtime = intval(strtotime($_GET['query_start_time']));
- $end_unixtime = intval(strtotime($_GET['query_end_time']));
- if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
- $cond['add_time'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
- } elseif ($start_unixtime > 0) {
- $cond['add_time'] = ['egt', $start_unixtime];
- } elseif ($end_unixtime > 0) {
- $cond['add_time'] = ['lt', $end_unixtime];
- }
- $list = $mod_remit->getRemitList($cond, 50);
- foreach ($list as $key => $value)
- {
- $list[$key]['amount'] = number_format($value['amount'],4,'.',',');
- }
- $refill_company = $this->refill_companys([]);
- Tpl::output('list', $list);
- Tpl::output('refill_company', $refill_company);
- Tpl::output('show_page', $mod_remit->showpage());
- Tpl::showpage('refill.company.remit');
- }
- public function remit_delOp()
- {
- $mod_remit = Model('refill_company_remit');
- $remit_id = $_GET['remit_id'];
- $this->check_remit($remit_id);
- $result = $mod_remit->DelRemit($remit_id);
- if ($result) {
- showMessage('删除成功');
- } else {
- showMessage('删除失败');
- }
- }
- public function remit_editOp()
- {
- $remit_id = $_GET['remit_id'] ?? $_POST['remit_id'];
- $remit = $this->check_remit($remit_id);
- $params = json_decode($remit['params'], true);
- $provider_list = $this->providers();
- foreach ($provider_list as $provider) {
- $co_id = $provider['co_id'];
- if(empty($co_id)) continue;
- $pid = $provider['provider_id'];
- $account_id = intval($provider['account_id']);
- if($account_id > 0) {
- $mid_pids[$account_id] = $pid;
- $mids[] = $account_id;
- }
- $provider['available_predeposit'] = 0;
- $providers[$pid] = $provider;
- }
- $refill_company = $this->refill_companys(['co_type' => refill_companyModel::co_type_provider]);
- if(chksubmit())
- {
- $remits = $_POST['remits'];
- $strs = $_POST['strs'];
- $operation = $_POST['operation'];
- $remit_total = 0;
- foreach ($strs as $key => $str)
- {
- $remit = $remits[$key];
- if(empty($remit)) continue;
- $item = explode('-', $str);
- $co_id = $item[0];
- $pid = $item[1];
- $data[$co_id][$pid] = $remit;
- $remit_total += $remit;
- }
- if(empty($data)) {
- showMessage('未提交数据');
- }
- $remit_record = [
- 'amount' => $remit_total,
- 'params' => json_encode($data),
- 'operation' => $operation,
- 'oper_time' => time()
- ];
- Model('refill_company_remit')->editRemit($remit_record, ['remit_id' => $remit_id]);
- showMessage('操作成功', 'index.php?act=refill_company&op=remit_record');
- }
- else
- {
- $member_data = Model('member')->field('member_id,available_predeposit')->where(['member_id' => ['in',$mids]])->select();
- foreach ($member_data as $member)
- {
- $mid = intval($member['member_id']);
- if(array_key_exists($mid,$mid_pids)) {
- $pid = $mid_pids[$mid];
- $providers[$pid]['available_predeposit'] = $member['available_predeposit'];
- }
- }
- foreach ($params as $co_id => $item)
- {
- $data = [];
- $data['co_name'] = $refill_company[$co_id]['co_name'];
- foreach ($item as $pid => $remit_money)
- {
- $providers[$pid]['remit'] = $remit_money;
- $data['providers'][] = $providers[$pid];
- }
- $result[] = $data;
- }
- Tpl::output('remit_data', $result);
- Tpl::output('remit', $remit);
- Tpl::showpage('provider.remit.edit');
- }
- }
- public function remit_commitOp()
- {
- $mod = Model('refill_company_remit');
- $remit_id = $_GET['remit_id'];
- $remit = $this->check_remit($remit_id);
- $params = json_decode($remit['params'], true);
- $provider_list = $this->providers();
- foreach ($provider_list as $provider) {
- $providers[$provider['provider_id']] = $provider;
- }
- $bz = '批量打款';
- foreach ($params as $item)
- {
- try {
- $trans = new trans_wapper($mod, __METHOD__);
- $remit_total = 0;
- foreach ($item as $pid => $money)
- {
- $amount_data = [
- 'pointsnum' => $money,
- 'operation' => $remit['operation'],
- 'bz' => $bz
- ];
- $this->credit_save_money($money, 'add', $providers[$pid]['account_id'], $bz);
- $this->ct_provider_amount($amount_data, $providers[$pid]);
- $remit_total += $money;
- }
- $trans->commit();
- } catch (Exception $e) {
- $trans->rollback();
- Log::record("remit_commit err: {$e->getMessage()}", Log::ERR);
- showMessage('提交失败');
- }
- }
- $mod->editRemit(['remit_state' => refill_company_remitModel::remit_have], ['remit_id' => $remit_id]);
- showMessage('操作成功!');
- }
- private function ct_provider_amount($params, $provider_info)
- {
- $input['provider_id'] = $provider_info['provider_id'];
- $input['memeber_id'] = $provider_info['account_id'];
- $input['amount'] = $params['pointsnum'];
- $input['operation'] = $params['operation'];
- $input['add_time'] = time();
- $input['bz'] = $params['bz'] ?? '';
- return Model('provider_amount')->addAmount($input);
- }
- private function check_remit($remit_id)
- {
- $remit = Model('refill_company_remit')->getRemit($remit_id);
- if(empty($remit)) {
- showMessage('记录不存在');
- }
- return $remit;
- }
- public function remit_exportOp()
- {
- $remit_id = $_GET['remit_id'];
- $remit = $this->check_remit($remit_id);
- $params = json_decode($remit['params']);
- $remit_cfg = $this->remit_cfg();
- $remit_max = floatval($remit_cfg['remit_max']);
- $refill_company = $this->refill_companys(['co_type' => refill_companyModel::co_type_provider]);
- foreach ($params as $co_id => $item)
- {
- $remit_total = 0;
- foreach ($item as $money)
- {
- $remit_total += $money;
- }
- if( $remit_total < $remit_max) {
- $refill_company[$co_id]['money'] = $remit_total;
- $remit_data[] = $refill_company[$co_id];
- }else{
- for ( $i = $remit_total / $remit_max; $i>=0; --$i )
- {
- if($i < 1) {
- $refill_company[$co_id]['money'] = fmod($remit_total, $remit_max);
- } else {
- $refill_company[$co_id]['money'] = $remit_max;
- }
- $remit_data[] = $refill_company[$co_id];
- }
- }
- }
- $this->Export_file($remit['title'], $remit_data);
- }
- private function Export_file($file_name, $remit_data)
- {
- Language::read('export');
- import('libraries.excel');
- $excel_obj = new Excel();
- $excel_data = [];
- //设置样式
- $excel_obj->setStyle(['id' => 's_title', 'Font' => ['FontName' => '宋体', 'Size' => '12', 'Bold' => '1']]);
- //header
- $excel_data[0][] = ['styleid' => 's_title', 'data' => '序号'];
- $excel_data[0][] = ['styleid' => 's_title', 'data' => '收款人姓名'];
- $excel_data[0][] = ['styleid' => 's_title', 'data' => '银行卡号'];
- $excel_data[0][] = ['styleid' => 's_title', 'data' => '银行编码'];
- $excel_data[0][] = ['styleid' => 's_title', 'data' => '省/直辖市'];
- $excel_data[0][] = ['styleid' => 's_title', 'data' => '城市'];
- $excel_data[0][] = ['styleid' => 's_title', 'data' => '开户行名称'];
- $excel_data[0][] = ['styleid' => 's_title', 'data' => '付款金额'];
- $excel_data[0][] = ['styleid' => 's_title', 'data' => '0(对私付款)/1(对公付款)'];
- //data
- foreach ($remit_data as $key => $data) {
- if($data['money'] <= 0) continue;
- $tmp = [];
- $tmp[] = ['data' => $key+1];
- $tmp[] = ['data' => $data['bank_username']];
- $tmp[] = ['data' => $data['bank_card_no']];
- $tmp[] = ['data' => $data['bank_code']];
- $tmp[] = ['data' => $data['province']];
- $tmp[] = ['data' => $data['city']];
- $tmp[] = ['data' => $data['bank_name']];
- $tmp[] = ['data' => $data['money']];
- $tmp[] = ['data' => 1];
- $excel_data[] = $tmp;
- }
- $excel_data = $excel_obj->charset($excel_data, CHARSET);
- $excel_obj->addArray($excel_data);
- $excel_obj->addWorksheet($excel_obj->charset(L('exp_od_order'), CHARSET));
- $excel_obj->generateXML($file_name);
- }
- }
|