StatCgController.php 3.8 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. if (empty($ghsId)) {
  34. util::fail('请先选择供货商');
  35. }
  36. $ghs = GhsClass::getById($ghsId, true);
  37. $ghsMainId = $ghs->mainId ?? 0;
  38. $ghsProduct = ProductClass::getAllByCondition(['mainId' => $ghsMainId, 'delStatus' => 0], null, 'id,name,itemId', 'itemId');
  39. }
  40. $list = PurchaseItemClass::getAllByCondition($where, null, '*');
  41. if (empty($list)) {
  42. util::success(['list' => $list]);
  43. }
  44. $ids = array_column($list, 'productId');
  45. $ids = array_unique(array_filter($ids));
  46. $productInfo = ProductClass::getByIds($ids, null, 'id');
  47. $stat = [];
  48. $totalNum = 0;
  49. $totalAmount = 0;
  50. foreach ($list as $key => $val) {
  51. $productId = $val['productId'] ?? 0;
  52. $ptItemId = $val['itemId'] ?? 0;
  53. $xhUnitType = $val['xhUnitType'] ?? 0;
  54. $xhUnitPrice = $val['xhUnitPrice'] ?? 0;
  55. $ratio = $val['ratio'] ?? 20;
  56. $beforeNum = bcsub($val['xhNum'], $val['refundNum'], 2);
  57. if ($xhUnitType == 0) {
  58. $num = $beforeNum;
  59. } else {
  60. $num = bcdiv($beforeNum, $ratio, 2);
  61. }
  62. $amount = bcmul($num, $xhUnitPrice, 2);
  63. $totalNum = bcadd($totalNum, $num, 2);
  64. $totalAmount = bcadd($totalAmount, $amount, 2);
  65. $myCurrentInfo = $productInfo[$productId] ?? [];
  66. if ($showGhsParam == 1) {
  67. $myGhsItem = $ghsProduct[$ptItemId] ?? [];
  68. $name = $myGhsItem['name'] ?? '未找到';
  69. $py = $myGhsItem['py'] ?? '';
  70. } else {
  71. $name = $myCurrentInfo['name'] ?? '';
  72. $py = $myCurrentInfo['py'] ?? '';
  73. }
  74. $stat[$productId]['productId'] = $productId;
  75. $stat[$productId]['name'] = $name;
  76. $stat[$productId]['py'] = $py;
  77. if (isset($stat[$productId]['num'])) {
  78. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
  79. } else {
  80. $stat[$productId]['num'] = $num;
  81. }
  82. if (isset($stat[$productId]['amount'])) {
  83. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $amount, 2);
  84. } else {
  85. $stat[$productId]['amount'] = $amount;
  86. }
  87. }
  88. $stat = arrayUtil::arraySort($stat, 'num');
  89. $totalNum = floatval($totalNum);
  90. $totalAmount = floatval($totalAmount);
  91. util::success(['list' => $stat, 'totalNum' => $totalNum, 'totalAmount' => $totalAmount]);
  92. }
  93. }