StatCgController.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. foreach ($list as $key => $val) {
  37. $productId = $val['productId'] ?? 0;
  38. $xhUnitType = $val['xhUnitType'] ?? 0;
  39. $xhUnitPrice = $val['xhUnitPrice'] ?? 0;
  40. $ratio = $val['ratio'] ?? 20;
  41. $beforeNum = bcsub($val['xhNum'], $val['refundNum'], 2);
  42. if ($xhUnitType == 0) {
  43. $num = $beforeNum;
  44. } else {
  45. $num = bcdiv($beforeNum, $ratio, 2);
  46. }
  47. $amount = bcmul($num, $xhUnitPrice, 2);
  48. $name = $productInfo[$productId]['name'] ?? '';
  49. $py = $productInfo[$productId]['py'] ?? '';
  50. $stat[$productId]['productId'] = $productId;
  51. $stat[$productId]['name'] = $name;
  52. $stat[$productId]['py'] = $py;
  53. if (isset($stat[$productId]['num'])) {
  54. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
  55. } else {
  56. $stat[$productId]['num'] = $num;
  57. }
  58. if (isset($stat[$productId]['amount'])) {
  59. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $amount, 2);
  60. } else {
  61. $stat[$productId]['amount'] = $amount;
  62. }
  63. }
  64. $stat = arrayUtil::arraySort($stat, 'num');
  65. util::success(['list' => $stat]);
  66. }
  67. }