StatIncomeSourceService.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace bizMall\stat\services;
  3. use bizMall\base\services\BaseService;
  4. use bizMall\user\classes\UserClass;
  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 StatIncomeSourceService extends BaseService
  11. {
  12. public static $baseFile = '\bizMall\stat\classes\StatIncomeSourceClass';
  13. //添加更新记录 ssh 2010.8.20
  14. public static function replaceData($data)
  15. {
  16. $time = isset($data['time']) ? $data['time'] : date("Ymd");
  17. if (strtotime($time) == false) {
  18. util::fail('不是日期格式');
  19. }
  20. $payWay = $data['payWay'];
  21. $sjId = $data['sjId'];
  22. $sourceType = $data['sourceType'];
  23. $amount = $data['amount'];
  24. if ($amount <= 0) {
  25. return true;
  26. }
  27. $addTime = time();
  28. $income = self::getByCondition(['sjId' => $sjId, 'time' => $time, 'sourceType' => $sourceType], true);
  29. if (empty($income)) {
  30. $data['time'] = $time;
  31. $data['addTime'] = $addTime;
  32. $income = self::add($data, true);
  33. } else {
  34. $income->amount += $amount;
  35. }
  36. $income = business::savePayWayAmount($income, $amount, $payWay);
  37. $income->save();
  38. /**每个月增加**/
  39. $data['time'] = date("Ym", strtotime($time));
  40. StatIncomeSourceMonthService::replaceData($data);
  41. return true;
  42. }
  43. //取最近的按来源分的收入 ssh 2019.9.21
  44. public static function getLatestSource($start, $end, $sjId)
  45. {
  46. $list = self::getAllList('sourceType,amount', ['sjId' => $sjId, 'time' => ['between', [$start, $end]]], 'time asc');
  47. $data = [];
  48. $sourceType = UserClass::$sourceType;
  49. $totalAmount = 0;
  50. if (!empty($list)) {
  51. foreach ($sourceType as $id => $item) {
  52. $data[$id]['name'] = $item;
  53. $data[$id]['value'] = 0;
  54. }
  55. foreach ($list as $key => $val) {
  56. $currentSourceType = $val['sourceType'];
  57. $amount = $val['amount'];
  58. $data[$currentSourceType]['value'] = stringUtil::calcAdd($data[$currentSourceType]['value'], $amount);
  59. $totalAmount = stringUtil::calcAdd($totalAmount, $amount);
  60. }
  61. }
  62. return ['data' => array_values($data), 'sourceTypeName' => $sourceType, 'totalAmount' => $totalAmount];
  63. }
  64. }