| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
- namespace ghs\controllers;
- use biz\stat\classes\StatItemClass;
- use bizGhs\order\classes\OrderClass;
- use bizGhs\order\classes\OrderItemClass;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\shop\classes\ShopAdminClass;
- use bizGhs\stat\classes\StatSaleClass;
- use common\components\arrayUtil;
- use common\components\dateUtil;
- use common\components\noticeUtil;
- use Yii;
- use common\components\util;
- class StatItemController extends BaseController
- {
- //花材销量 ssh 20230223
- public function actionShow()
- {
- ini_set('memory_limit', '2045M');
- set_time_limit(0);
- //查看财务的权限
- $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
- $hasPower = 0;
- if (getenv('YII_ENV') == 'production') {
- //小向花卉
- if ($this->mainId == 23390) {
- if (in_array($this->adminId, [31105])) {
- $hasPower = 1;
- }
- }
- } else {
- if ($this->mainId == 644) {
- if (in_array($this->adminId, [1230])) {
- $hasPower = 1;
- }
- }
- }
- if ($lookMoney == 0 && $hasPower == 0) {
- //盛丰员工可以看花材销售数据
- if ($this->mainId != 44282) {
- util::fail('没有财务权限哦');
- }
- }
- $get = Yii::$app->request->get();
- $sort = $get['sort'] ?? 'num';
- $where = [];
- $where['mainId'] = $this->mainId;
- $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['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $staffId = $get['staffId'] ?? 0;
- $list = OrderClass::getAllByCondition($where, null, '*');
- if (empty($list)) {
- util::success(['list' => []]);
- }
- $productInfo = ProductClass::getAllByCondition(['mainId' => $this->mainId], null, 'id,name,py,cgStaffId', 'id');
- $stat = [];
- $totalAmount = 0;
- $totalNum = 0;
- $totalMl = 0;
- foreach ($list as $order) {
- $orderSn = $order['orderSn'] ?? '';
- $status = $order['status'] ?? 0;
- $book = $order['book'] ?? 0;
- if ($status == 5 || $status == 1 || ($book == 1 && $status == 2)) {
- continue;
- }
- $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($itemList)) {
- foreach ($itemList as $item) {
- $productId = $item->productId ?? 0;
- $name = $productInfo[$productId]['name'] ?? '';
- $py = $productInfo[$productId]['py'] ?? '';
- if (!empty($staffId)) {
- $currentCgStaffId = $productInfo[$productId]['cgStaffId'] ?? 0;
- if ($staffId != $currentCgStaffId) {
- continue;
- }
- }
- $num = bcsub($item['xhNum'], $item['refundNum'], 2);
- $unitPrice = $item['xhUnitPrice'] ?? 0;
- $unitCost = $item['cost'] ?? 0;
- $unitGross = bcsub($unitPrice, $unitCost, 2);
- $currentAmount = bcmul($unitPrice, $num, 2);
- $currentGross = bcmul($unitGross, $num, 2);
- $totalNum = bcadd($totalNum, $num, 2);
- $totalAmount = bcadd($totalAmount, $currentAmount, 2);
- $totalMl = bcadd($totalMl, $currentGross, 2);
- if ($currentGross < 0) {
- //noticeUtil::push($item->id . ' 毛利是负的', '15280215347');
- }
- $stat[$productId]['productId'] = $productId;
- $stat[$productId]['name'] = $name;
- $stat[$productId]['py'] = $py;
- if (isset($stat[$productId]['num'])) {
- $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
- } else {
- $stat[$productId]['num'] = $num;
- }
- if (isset($stat[$productId]['amount'])) {
- $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentAmount, 2);
- } else {
- $stat[$productId]['amount'] = $currentAmount;
- }
- if (isset($stat[$productId]['gross'])) {
- $stat[$productId]['gross'] = bcadd($stat[$productId]['gross'], $currentGross, 2);
- } else {
- $stat[$productId]['gross'] = $currentGross;
- }
- }
- }
- }
- //增加毛利率
- foreach ($stat as $key => $val) {
- $profit = $val['gross'] ?? 0;
- $price = $val['amount'] ?? 0;
- if ($price > 0) {
- $mll = bcdiv($profit, $price, 3);
- $mll = bcmul($mll, 100);
- $mll = round($mll, 1);
- } else {
- $mll = 0;
- }
- $stat[$key]['mll'] = $mll;
- }
- $rank = 'num';
- if ($sort == 'gross') {
- $rank = 'gross';
- }
- if ($sort == 'amount') {
- $rank = 'amount';
- }
- if ($sort == 'mll') {
- $rank = 'mll';
- }
- $stat = arrayUtil::arraySort($stat, $rank);
- $export = $get['export'] ?? 0;
- if ($export == 1) {
- $mainId = $this->mainId;
- StatSaleClass::exportItemSale($stat, $mainId);
- }
- util::success(['list' => $stat, 'totalAmount' => floatval($totalAmount), 'totalNum' => floatval($totalNum), 'totalMl' => $totalMl,]);
- }
- //花材销量 ssh 20211021
- public function actionProfile()
- {
- //查看财务的权限
- $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
- if ($lookMoney == 0) {
- util::fail('无法查看');
- }
- $get = Yii::$app->request->get();
- $where = [];
- $where['shopId'] = $this->shopId;
- if (isset($get['shopId']) && !empty($get['shopId'])) {
- $where['shopId'] = $get['shopId'];
- }
- $searchTime = $get['searchTime'] ?? 'today';
- if (!empty($searchTime)) {
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
- }
- $list = StatItemClass::getAllByCondition($where, null, '*');
- if (empty($list)) {
- util::success(['list' => $list]);
- }
- $ids = array_column($list, 'itemId');
- $ids = array_unique(array_filter($ids));
- $productInfo = ProductClass::getByIds($ids, null, 'id');
- $stat = [];
- foreach ($list as $key => $val) {
- $itemId = $val['itemId'] ?? 0;
- $idsData[$itemId] = 0;
- $num = bcsub($val['num'], $val['refundNum'], 2);
- $amount = bcsub($val['amount'], $val['refundAmount'], 2);
- $name = $productInfo[$itemId]['name'] ?? '';
- $py = $productInfo[$itemId]['py'] ?? '';
- $stat[$itemId]['productId'] = $itemId;
- $stat[$itemId]['name'] = $name;
- $stat[$itemId]['py'] = $py;
- if (isset($stat[$itemId]['num'])) {
- $stat[$itemId]['num'] = bcadd($stat[$itemId]['num'], $num, 2);
- } else {
- $stat[$itemId]['num'] = $num;
- }
- if (isset($stat[$itemId]['amount'])) {
- $stat[$itemId]['amount'] = bcadd($stat[$itemId]['amount'], $amount, 2);
- } else {
- $stat[$itemId]['amount'] = $amount;
- }
- }
- $stat = arrayUtil::arraySort($stat, 'num');
- util::success(['list' => $stat]);
- }
- }
|