StatIncomeSourceService.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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, $merchantId)
  43. {
  44. $list = self::getAllList('sourceType,amount', ['merchantId' => $merchantId, 'time' => ['between', [$start, $end]]], 'time asc');
  45. $data = [];
  46. $sourceType = UserClass::$sourceType;
  47. $totalAmount = 0;
  48. if (!empty($list)) {
  49. foreach ($sourceType as $id => $item) {
  50. $data[$id]['name'] = $item;
  51. $data[$id]['value'] = 0;
  52. }
  53. foreach ($list as $key => $val) {
  54. $currentSourceType = $val['sourceType'];
  55. $amount = $val['amount'];
  56. $data[$currentSourceType]['value'] = stringUtil::calcAdd($data[$currentSourceType]['value'], $amount);
  57. $totalAmount = stringUtil::calcAdd($totalAmount, $amount);
  58. }
  59. }
  60. return ['data' => array_values($data), 'sourceTypeName' => $sourceType, 'totalAmount' => $totalAmount];
  61. }
  62. }