StatIncomeSourceService.php 2.2 KB

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