| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace biz\stat\services;
- use biz\base\services\BaseService;
- use biz\user\classes\UserClass;
- use common\components\business;
- use common\components\stringUtil;
- use common\components\validate;
- use common\components\util;
- use Yii;
- class StatIncomeSourceService extends BaseService
- {
-
- public static $baseFile = '\biz\stat\classes\StatIncomeSourceClass';
-
- //添加更新记录 shish 2010.8.20
- public static function replaceData($data)
- {
- $valid = ['merchantId', 'sourceType', 'amount', 'payWay'];
- validate::easyCheck($valid, $data);
- $time = isset($data['time']) ? $data['time'] : date("Ymd");
- if (strtotime($time) == false) {
- util::fail('不是日期格式');
- }
- $payWay = $data['payWay'];
- $merchantId = $data['merchantId'];
- $sourceType = $data['sourceType'];
- $amount = $data['amount'];
- $addTime = time();
- $income = self::getByCondition(['merchantId' => $merchantId, 'time' => $time, 'sourceType' => $sourceType], true);
- if (empty($income)) {
- $data['time'] = $time;
- $data['addTime'] = $addTime;
- $income = self::add($data, true);
- } else {
- $income->amount += $amount;
- }
- $income = business::savePayWayAmount($income, $amount, $payWay);
- $income->save();
-
- /**每个月增加**/
- StatIncomeSourceMonthService::replaceData($data);
-
- return true;
- }
-
- //取最近的按来源分的收入 shish 2019.9.21
- public static function getLatestSource($start, $end, $merchantId)
- {
- $list = self::getAllList('sourceType,amount', ['merchantId' => $merchantId, 'time' => ['between', [$start, $end]]], 'time asc');
- $data = [];
- $sourceType = UserClass::$sourceType;
- $totalAmount = 0;
- if (!empty($list)) {
- foreach ($sourceType as $id => $item) {
- $data[$id]['name'] = $item;
- $data[$id]['value'] = 0;
- }
- foreach ($list as $key => $val) {
- $currentSourceType = $val['sourceType'];
- $amount = $val['amount'];
- $data[$currentSourceType]['value'] = stringUtil::calcAdd($data[$currentSourceType]['value'], $amount);
- $totalAmount = stringUtil::calcAdd($totalAmount, $amount);
- }
- }
-
- return ['data' => array_values($data), 'sourceTypeName' => $sourceType, 'totalAmount' => $totalAmount];
- }
-
- }
|