StatCgController.php 3.0 KB

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