| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace bizHd\stat\services;
- use bizHd\base\services\BaseService;
- use bizHd\goods\services\UsageRelationService;
- use common\components\business;
- use common\components\stringUtil;
- use common\components\validate;
- use common\components\util;
- use Yii;
- class StatIncomeUsageService extends BaseService
- {
-
- public static $baseFile = '\bizHd\stat\classes\StatIncomeUsageClass';
-
- //添加更新记录 ssh 2010.8.20
- public static function replaceData($data)
- {
- $valid = ['usageId', 'amount', 'sjId', 'payWay'];
- validate::easyCheck($valid, $data);
- $time = date("Ymd");
- if (isset($data['time'])) {
- //如果传time参数必须是时间戳
- if (strtotime(date('Y-m-d H:i:s', $data['time'])) != $data['time']) {
- util::fail('time不是时间戳');
- }
- $time = date("Ymd", $data['time']);
- $data['time'] = $time;
- }
- $sjId = $data['sjId'];
- $usageId = $data['usageId'];
- $amount = $data['amount'];
- $payWay = $data['payWay'];
- $income = self::getByCondition(['sjId' => $sjId, 'time' => $time, 'usageId' => $usageId], true);
- if (empty($income)) {
- $income = self::add($data, true);
- } else {
- $income->amount += $data['amount'];
- }
- business::savePayWayAmount($income, $amount, $payWay);
-
- /**每个月增加**/
- StatIncomeUsageMonthService::replaceData($data);
-
- $income->save();
- }
- public static function getLatestIncome($start, $end,$sjId)
- {
- $list = self::getAllList('usageId,amount', ['sjId' => $sjId, 'time' => ['between', [$start, $end]]], 'time asc');
- $totalAmount = 0;
- $usageList = UsageRelationService::getMyUsage();
- $data = [];
- foreach ($usageList as $usageId => $category) {
- $data[$usageId]['name'] = $category['showName'];
- $data[$usageId]['value'] = 0;
- }
- if (!empty($list)) {
- foreach ($list as $val) {
- $usageId = $val['usageId'];
- $amount = $val['amount'];
- $data[$usageId]['value'] = stringUtil::calcAdd($data[$usageId]['value'], $amount);
- $totalAmount = stringUtil::calcAdd($totalAmount, $amount);
- }
- }
- $usageName = array_column($usageList, 'showName');
- return ['data' => array_values($data), 'usageName' => $usageName, 'totalAmount' => $totalAmount];
- }
- }
|