StatIncomeUsageService.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace bizHd\stat\services;
  3. use bizHd\base\services\BaseService;
  4. use bizHd\goods\services\UsageRelationService;
  5. use common\components\business;
  6. use common\components\stringUtil;
  7. use common\components\validate;
  8. use common\components\util;
  9. use Yii;
  10. class StatIncomeUsageService extends BaseService
  11. {
  12. public static $baseFile = '\bizHd\stat\classes\StatIncomeUsageClass';
  13. //添加更新记录 ssh 2010.8.20
  14. public static function replaceData($data)
  15. {
  16. $valid = ['usageId', 'amount', 'sjId', 'payWay'];
  17. validate::easyCheck($valid, $data);
  18. $time = date("Ymd");
  19. if (isset($data['time'])) {
  20. //如果传time参数必须是时间戳
  21. if (strtotime(date('Y-m-d H:i:s', $data['time'])) != $data['time']) {
  22. util::fail('time不是时间戳');
  23. }
  24. $time = date("Ymd", $data['time']);
  25. $data['time'] = $time;
  26. }
  27. $sjId = $data['sjId'];
  28. $usageId = $data['usageId'];
  29. $amount = $data['amount'];
  30. $payWay = $data['payWay'];
  31. $income = self::getByCondition(['sjId' => $sjId, 'time' => $time, 'usageId' => $usageId], true);
  32. if (empty($income)) {
  33. $income = self::add($data, true);
  34. } else {
  35. $income->amount += $data['amount'];
  36. }
  37. business::savePayWayAmount($income, $amount, $payWay);
  38. /**每个月增加**/
  39. StatIncomeUsageMonthService::replaceData($data);
  40. $income->save();
  41. }
  42. public static function getLatestIncome($start, $end,$sjId)
  43. {
  44. $list = self::getAllList('usageId,amount', ['sjId' => $sjId, 'time' => ['between', [$start, $end]]], 'time asc');
  45. $totalAmount = 0;
  46. $usageList = UsageRelationService::getMyUsage();
  47. $data = [];
  48. foreach ($usageList as $usageId => $category) {
  49. $data[$usageId]['name'] = $category['showName'];
  50. $data[$usageId]['value'] = 0;
  51. }
  52. if (!empty($list)) {
  53. foreach ($list as $val) {
  54. $usageId = $val['usageId'];
  55. $amount = $val['amount'];
  56. $data[$usageId]['value'] = stringUtil::calcAdd($data[$usageId]['value'], $amount);
  57. $totalAmount = stringUtil::calcAdd($totalAmount, $amount);
  58. }
  59. }
  60. $usageName = array_column($usageList, 'showName');
  61. return ['data' => array_values($data), 'usageName' => $usageName, 'totalAmount' => $totalAmount];
  62. }
  63. }