StatIncomeCategoryService.php 2.1 KB

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