StatCgController.php 3.8 KB

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