StatIncomeSourceService.php 2.3 KB

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