| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace hd\controllers;
- use bizHd\ghs\classes\GhsClass;
- use bizHd\product\classes\ProductClass;
- use bizHd\purchase\classes\PurchaseItemClass;
- use common\components\arrayUtil;
- use common\components\dateUtil;
- use Yii;
- use common\components\util;
- class StatCgController extends BaseController
- {
- //采购统计
- public function actionProfile()
- {
- $get = Yii::$app->request->get();
- $where = ['mainId' => $this->mainId];
- $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'];
- $startDate = date("Y-m-d 00:00:00", strtotime($start));
- $endDate = date("Y-m-d 23:59:59", strtotime($end));
- $where['payTime'] = ['between', [$startDate, $endDate]];
- if (!empty($get['ghsId'])) {
- $where['ghsId'] = $get['ghsId'];
- }
- $showGhsParam = $get['showGhsParam'] ?? 0;
- $ghsProduct = [];
- if ($showGhsParam == 1) {
- $ghsId = $get['ghsId'] ?? 0;
- $ghs = GhsClass::getById($ghsId, true);
- if (!empty($ghs)) {
- $ghsMainId = $ghs->mainId ?? 0;
- $ghsProduct = ProductClass::getAllByCondition(['mainId' => $ghsMainId, 'delStatus' => 0], null, 'id,name,itemId', 'itemId');
- }
- }
- $list = PurchaseItemClass::getAllByCondition($where, null, '*');
- if (empty($list)) {
- util::success(['list' => $list]);
- }
- $ids = array_column($list, 'productId');
- $ids = array_unique(array_filter($ids));
- $productInfo = ProductClass::getByIds($ids, null, 'id');
- $stat = [];
- $totalNum = 0;
- $totalAmount = 0;
- foreach ($list as $key => $val) {
- $productId = $val['productId'] ?? 0;
- $ptItemId = $val['itemId'] ?? 0;
- $xhUnitType = $val['xhUnitType'] ?? 0;
- $xhUnitPrice = $val['xhUnitPrice'] ?? 0;
- $ratio = $val['ratio'] ?? 20;
- $beforeNum = bcsub($val['xhNum'], $val['refundNum'], 2);
- if ($xhUnitType == 0) {
- $num = $beforeNum;
- } else {
- $num = bcdiv($beforeNum, $ratio, 2);
- }
- $amount = bcmul($num, $xhUnitPrice, 2);
- $totalNum = bcadd($totalNum, $num, 2);
- $totalAmount = bcadd($totalAmount, $amount, 2);
- $myCurrentInfo = $productInfo[$productId] ?? [];
- if ($showGhsParam == 1) {
- $myGhsItem = $ghsProduct[$ptItemId] ?? [];
- $name = $myGhsItem['name'] ?? '未找到';
- $py = $myGhsItem['py'] ?? '';
- } else {
- $name = $myCurrentInfo['name'] ?? '';
- $py = $myCurrentInfo['py'] ?? '';
- }
- $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'], $amount, 2);
- } else {
- $stat[$productId]['amount'] = $amount;
- }
- }
- $stat = arrayUtil::arraySort($stat, 'num');
- $totalNum = floatval($totalNum);
- $totalAmount = floatval($totalAmount);
- util::success(['list' => $stat, 'totalNum' => $totalNum, 'totalAmount' => $totalAmount]);
- }
- }
|