StatCgController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\ghs\classes\GhsClass;
  4. use bizHd\product\classes\ProductClass;
  5. use bizHd\purchase\classes\PurchaseItemClass;
  6. use common\components\arrayUtil;
  7. use common\components\dateUtil;
  8. use Yii;
  9. use common\components\util;
  10. class StatCgController extends BaseController
  11. {
  12. //采购统计
  13. public function actionProfile()
  14. {
  15. $get = Yii::$app->request->get();
  16. $where = ['mainId' => $this->mainId];
  17. $searchTime = !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  18. $startTime = $get['startTime'] ?? '';
  19. $endTime = $get['endTime'] ?? '';
  20. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  21. $start = $period['startTime'];
  22. $end = $period['endTime'];
  23. $startDate = date("Y-m-d 00:00:00", strtotime($start));
  24. $endDate = date("Y-m-d 23:59:59", strtotime($end));
  25. $where['payTime'] = ['between', [$startDate, $endDate]];
  26. if (!empty($get['ghsId'])) {
  27. $where['ghsId'] = $get['ghsId'];
  28. }
  29. $showGhsParam = $get['showGhsParam'] ?? 0;
  30. $ghsProduct = [];
  31. if ($showGhsParam == 1) {
  32. $ghsId = $get['ghsId'] ?? 0;
  33. $ghs = GhsClass::getById($ghsId, true);
  34. if (!empty($ghs)) {
  35. $ghsMainId = $ghs->mainId ?? 0;
  36. $ghsProduct = ProductClass::getAllByCondition(['mainId' => $ghsMainId, 'delStatus' => 0], null, 'id,name,itemId', 'itemId');
  37. }
  38. }
  39. $list = PurchaseItemClass::getAllByCondition($where, null, '*');
  40. if (empty($list)) {
  41. util::success(['list' => $list]);
  42. }
  43. $ids = array_column($list, 'productId');
  44. $ids = array_unique(array_filter($ids));
  45. $productInfo = ProductClass::getByIds($ids, null, 'id');
  46. $stat = [];
  47. $totalNum = 0;
  48. $totalAmount = 0;
  49. foreach ($list as $key => $val) {
  50. $productId = $val['productId'] ?? 0;
  51. $ptItemId = $val['itemId'] ?? 0;
  52. $xhUnitType = $val['xhUnitType'] ?? 0;
  53. $xhUnitPrice = $val['xhUnitPrice'] ?? 0;
  54. $ratio = $val['ratio'] ?? 20;
  55. $beforeNum = bcsub($val['xhNum'], $val['refundNum'], 2);
  56. if ($xhUnitType == 0) {
  57. $num = $beforeNum;
  58. } else {
  59. $num = bcdiv($beforeNum, $ratio, 2);
  60. }
  61. $amount = bcmul($num, $xhUnitPrice, 2);
  62. $totalNum = bcadd($totalNum, $num, 2);
  63. $totalAmount = bcadd($totalAmount, $amount, 2);
  64. $myCurrentInfo = $productInfo[$productId] ?? [];
  65. if ($showGhsParam == 1) {
  66. $myGhsItem = $ghsProduct[$ptItemId] ?? [];
  67. $name = $myGhsItem['name'] ?? '未找到';
  68. $py = $myGhsItem['py'] ?? '';
  69. } else {
  70. $name = $myCurrentInfo['name'] ?? '';
  71. $py = $myCurrentInfo['py'] ?? '';
  72. }
  73. $stat[$productId]['productId'] = $productId;
  74. $stat[$productId]['name'] = $name;
  75. $stat[$productId]['py'] = $py;
  76. if (isset($stat[$productId]['num'])) {
  77. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
  78. } else {
  79. $stat[$productId]['num'] = $num;
  80. }
  81. if (isset($stat[$productId]['amount'])) {
  82. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $amount, 2);
  83. } else {
  84. $stat[$productId]['amount'] = $amount;
  85. }
  86. }
  87. $stat = arrayUtil::arraySort($stat, 'num');
  88. $totalNum = floatval($totalNum);
  89. $totalAmount = floatval($totalAmount);
  90. util::success(['list' => $stat, 'totalNum' => $totalNum, 'totalAmount' => $totalAmount]);
  91. }
  92. }