| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- <?php
- namespace ghs\controllers;
- use bizGhs\expend\classes\ExpendBxClass;
- use bizGhs\expend\classes\ExpendClass;
- use bizGhs\shop\classes\MainClass;
- use bizGhs\shop\classes\ShopAdminClass;
- use bizGhs\shop\classes\ShopMoneyClass;
- use common\components\dateUtil;
- use common\components\dict;
- use common\components\noticeUtil;
- use common\components\util;
- use Yii;
- class ExpendController extends BaseController
- {
- //数据统计 ssh 20241122
- public function actionStat()
- {
- ini_set('memory_limit', '2045M');
- set_time_limit(0);
- $get = Yii::$app->request->get();
- $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $where = ['mainId' => $this->mainId];
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $where['delStatus'] = 0; //过滤掉已删除的
- $list = ExpendClass::getAllByCondition($where, null, '*', null);
- $arr = [];
- $cat = [];
- $map = dict::getDict('expendTypeMap');
- foreach ($map as $id => $name) {
- $cat[$id] = ['num' => 0, 'amount' => 0, 'name' => $name];
- }
- $totalExpend = 0;
- if (!empty($list)) {
- foreach ($list as $expend) {
- $staffId = $expend['staffId'] ?? 0;
- $amount = $expend['amount'] ?? 0;
- $staffName = $expend['staffName'] ?? '';
- $bx = $expend['bx'] ?? 0;
- $type = $expend['type'] ?? 0;
- $currentAmount = $amount;
- if ($bx == 0) {
- $hasAmount = 0;
- $unAmount = $amount;
- } else {
- $hasAmount = $amount;
- $unAmount = 0;
- }
- if (isset($arr[$staffId])) {
- //总支出
- $arr[$staffId]['totalAmount'] = floatval(bcadd($arr[$staffId]['totalAmount'], $currentAmount));
- //已报销
- $arr[$staffId]['hasAmount'] = floatval(bcadd($arr[$staffId]['hasAmount'], $hasAmount));
- //待报销
- $arr[$staffId]['amount'] = floatval(bcadd($arr[$staffId]['amount'], $unAmount));
- $arr[$staffId]['num'] = bcadd($arr[$staffId]['num'], 1);
- } else {
- $arr[$staffId]['totalAmount'] = floatval($currentAmount);
- $arr[$staffId]['hasAmount'] = floatval($hasAmount);
- $arr[$staffId]['amount'] = floatval($unAmount);
- $arr[$staffId]['staffName'] = $staffName;
- $arr[$staffId]['num'] = 1;
- }
- $totalExpend = bcadd($totalExpend, $currentAmount, 2);
- if (isset($cat[$type])) {
- $cat[$type]['num']++;
- $cat[$type]['amount'] = floatval(bcadd($cat[$type]['amount'], $amount, 2));
- }
- }
- }
- util::success(['list' => $arr, 'totalExpend' => $totalExpend, 'catList' => $cat]);
- }
- //报销汇总统计 ssh 20240308
- public function actionUnBxList()
- {
- ini_set('memory_limit', '2045M');
- set_time_limit(0);
- $where = ['mainId' => $this->mainId, 'bx' => 0, 'delStatus' => 0];
- $list = ExpendClass::getAllByCondition($where, null, '*', null);
- $arr = [];
- if (!empty($list)) {
- foreach ($list as $expend) {
- $staffId = $expend['staffId'] ?? 0;
- $amount = $expend['amount'] ?? 0;
- $staffName = $expend['staffName'] ?? '';
- $currentAmount = $amount;
- $unAmount = $amount;
- if (isset($arr[$staffId])) {
- $arr[$staffId]['amount'] = bcadd($arr[$staffId]['amount'], $unAmount, 2);
- $arr[$staffId]['num'] = bcadd($arr[$staffId]['num'], 1);
- } else {
- $arr[$staffId]['totalAmount'] = $currentAmount;
- $arr[$staffId]['amount'] = $unAmount;
- $arr[$staffId]['staffName'] = $staffName;
- $arr[$staffId]['num'] = 1;
- }
- }
- }
- util::success(['list' => $arr]);
- }
- //报销金额 ssh 20240308
- public function actionBx()
- {
- $staff = $this->shopAdmin;
- if (isset($staff->finance) == false || $staff->finance == 0) {
- util::fail('请财务操作');
- }
- $mainId = $this->mainId;
- if (getenv('YII_ENV') == 'production') {
- if (in_array($mainId, [23390])) {
- if (!in_array($this->adminId, [24655])) {
- util::fail('请财务人员操作');
- }
- }
- } else {
- if (in_array($mainId, [644])) {
- if (!in_array($this->adminId, [919])) {
- //util::fail('请财务人员操作哈');
- }
- }
- }
- $get = Yii::$app->request->get();
- $postAmount = $get['amount'] ?? 0;
- $staffId = $get['staffId'] ?? 0;
- if ($postAmount <= 0) {
- util::fail('没有需要报销的金额');
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $condition = ['mainId' => $this->mainId, 'bx' => 0, 'staffId' => $staffId, 'delStatus' => 0];
- $list = ExpendClass::getAllByCondition($condition, null, '*', null, true);
- $total = 0;
- $staff = $this->shopAdmin;
- $bxData = [
- 'mainId' => $this->mainId,
- 'staffId' => $staff->id ?? 0,
- 'staffName' => $staff->name ?? '',
- ];
- $bx = ExpendBxClass::add($bxData, true);
- $bxId = $bx->id ?? 0;
- foreach ($list as $it) {
- $amount = $it->amount ?? 0;
- $total = bcadd($total, $amount, 2);
- $it->bx = 1;
- $it->bxId = $bxId;
- $it->save();
- }
- $bx->amount = $total;
- $bx->save();
- if (floatval($postAmount) != floatval($total)) {
- util::fail('金额错误,请刷新页面,实际:' . floatval($total));
- }
- $transaction->commit();
- util::complete();
- } catch (\Exception $e) {
- $transaction->rollBack();
- $msg = $e->getMessage();
- noticeUtil::push("报销出错了,错误信息:{$msg}", '15280215347');
- util::fail();
- }
- }
- //类型列表 ssh 20240308
- public function actionGetTypeList()
- {
- $list = dict::getDict('expendTypeList');
- util::success(['list' => $list]);
- }
- public function actionList()
- {
- $get = Yii::$app->request->get();
- $bx = $get['bx'] ?? -1;
- $type = $get['type'] ?? -1;
- $staffId = $get['staffId'] ?? 0;
- $where = [];
- $where['mainId'] = $this->mainId;
- if (!empty($staffId)) {
- $where['staffId'] = $staffId;
- }
- if ($type != -1) {
- $where['type'] = $type;
- }
- if ($bx != -1) {
- $where['bx'] = $bx;
- }
- $where['delStatus'] = 0;
- $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'thisYear';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $respond = ExpendClass::getExpendList($where);
- $map = dict::getDict('expendTypeMap');
- if (!empty($respond['list'])) {
- foreach ($respond['list'] as $key => $val) {
- $type = $val['type'] ?? 0;
- $name = $map[$type] ?? '未命名';
- $respond['list'][$key]['typeName'] = $name;
- }
- }
- util::success($respond);
- }
- //新增支出 ssh 20220710
- public function actionAddExpend()
- {
- $mainId = $this->mainId;
- if (getenv('YII_ENV') == 'production') {
- if (in_array($mainId, [23390])) {
- if (!in_array($this->adminId, [24655])) {
- util::fail('请财务人员操作吧');
- }
- } else {
- }
- } else {
- if (in_array($mainId, [644])) {
- if (!in_array($this->adminId, [919])) {
- //util::fail('请财务人员操作哦');
- }
- }
- }
- //避免重复提交
- util::checkRepeatCommit($this->adminId, 10);
- $post = Yii::$app->request->post();
- $type = $post['type'] ?? -1;
- $amount = $post['amount'] ?? 0;
- $remark = $post['remark'] ?? '';
- $bx = $post['bx'] ?? -1;
- if (!is_numeric($amount) || $amount <= 0) {
- util::fail('请输入正确的金额');
- }
- if ($bx == -1) {
- util::fail('请选报销状态');
- }
- if ($type == -1) {
- util::fail('请选择类型');
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $payWay = $post['payWay'] ?? 0;
- if ($payWay == dict::getDict('payWay', 'cash')) {
- //扣除现金
- $mainId = $this->mainId;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有门店信息');
- }
- $balance = bcsub($main->money, $amount, 2);
- if ($balance < 0) {
- util::fail('现金不足');
- }
- $main->money = $balance;
- $main->save();
- $staffId = $this->shopAdminId;
- $staff = $this->shopAdmin;
- $staffName = $staff->name ?? '';
- $map = dict::getDict('expendTypeMap');
- $expendName = $map[$type] ?? '未知';
- $event = $staffName . $expendName . '支出' . $amount . '元';
- $capitalType = dict::getDict('capitalType', 'expendRegister', 'id');
- $data = [];
- $data['balance'] = $balance;
- $data['mainId'] = $mainId;
- $data['sjId'] = $this->sjId;
- $data['amount'] = $amount;
- $data['staffId'] = $staffId;
- $data['staffName'] = $staffName;
- $data['ptStyle'] = dict::getDict('ptStyle', 'ghs');
- $data['capitalType'] = $capitalType;
- $data['event'] = $event;
- $data['remark'] = $remark;
- ShopMoneyClass::addData($data);
- }
- $remark = $post['remark'] ?? '';
- $getStaffId = $post['getStaffId'] ?? 0;
- $staff = ShopAdminClass::getById($getStaffId, true);
- if (empty($staff)) {
- util::fail('没有找到员工');
- }
- if ($staff->mainId != $this->mainId) {
- util::fail('没有员工');
- }
- $staffName = $staff->name ?? '';
- $ptStyle = dict::getDict('ptStyle', 'ghs');
- $payTime = date("Y-m-d H:i:s");
- $historyDate = $post['historyDate'] ?? '';
- if (!empty($historyDate)) {
- $payTime = $historyDate . date(" H:i:s");
- }
- $expendData = [
- 'type' => $type,
- 'amount' => $amount,
- 'remark' => $remark,
- 'mainId' => $this->mainId,
- 'staffId' => $getStaffId,
- 'staffName' => $staffName,
- 'ptStyle' => $ptStyle,
- 'payWay' => $payWay,
- 'bx' => $bx,
- 'payTime' => $payTime,
- ];
- $respond = ExpendClass::add($expendData, true);
- if ($bx == 1) {
- $bxData = [
- 'mainId' => $this->mainId,
- 'staffId' => $getStaffId,
- 'staffName' => $staffName,
- 'remark' => '登记时已报销',
- ];
- $bxInfo = ExpendBxClass::add($bxData, true);
- $bxId = $bxInfo->id ?? 0;
- $bxInfo->amount = $amount;
- $bxInfo->save();
- $respond->bxId = $bxId;
- $respond->save();
- }
- $transaction->commit();
- util::complete();
- } catch (\Exception $e) {
- $transaction->rollBack();
- $msg = $e->getMessage();
- noticeUtil::push("登记支出失败,错误信息:{$msg}", '15280215347');
- }
- }
- //删除支出
- public function actionDelExpend()
- {
- $post = Yii::$app->request->post();
- $staff = $this->shopAdmin;
- if ($staff->finance == 0) {
- util::fail('没有财务权限');
- }
- $staffId = $this->shopAdminId;
- $id = intval($post['id']);
- $expend = ExpendClass::getById($id, true);
- if ($expend->mainId != $this->mainId) {
- util::fail('没有找到支出数据');
- }
- if ($expend->delStatus == 1) {
- util::fail('此支出数据已是删除状态');
- }
- // 处理报销表数据
- if ($expend->bxId != 0) {
- $expendBx = ExpendBxClass::getById($expend->bxId, true);
- $expendBx->amount = bcsub($expendBx->amount, $expend->amount, 2);
- //怕remark字段不足,这边缩短长度
- $expendBx->remark = $expendBx->remark . "|删除支出ID " . $expend->id;
- $expendBx->save();
- }
- $expend->delStaffId = $staffId;
- $expend->delStaffName = $staff->name;
- $expend->delStatus = 1;
- $rs = $expend->save();
- if ($rs) {
- util::complete();
- } else {
- util::fail('删除支出失败');
- }
- }
- //修改账单日期 ssh 20241122
- public function actionChangePayTime()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $date = $get['date'] ?? '';
- if (empty($date)) {
- util::fail('没有日期');
- }
- $expend = ExpendClass::getById($id, true);
- if (empty($expend)) {
- util::fail('没有找到报销');
- }
- if ($expend->mainId != $this->mainId) {
- util::fail('不是你的报销');
- }
- $addTime = $expend->addTime;
- $expend->payTime = $date . date(" H:i:s", strtotime($addTime));
- $expend->save();
- util::complete('修改成功');
- }
- }
|