| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- namespace bizHd\stat\services;
- use bizHd\base\services\BaseService;
- use bizHd\goods\services\CategoryRelateService;
- use common\components\business;
- use common\components\stringUtil;
- use common\components\util;
- use Yii;
- class StatIncomeCategoryService extends BaseService
- {
-
- public static $baseFile = '\bizHd\stat\classes\StatIncomeCategoryClass';
-
- //添加更新记录 ssh 2010.8.20
- public static function replaceData($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'];
- $categoryId = $data['categoryId'];
- $amount = $data['amount'];
- $payWay = $data['payWay'];
- $income = self::getByCondition(['sjId' => $sjId, 'time' => $time, 'categoryId' => $categoryId], true);
- if (empty($income)) {
- $income = self::add($data, true);
- } else {
- $income->amount += $amount;
- }
- $income = business::savePayWayAmount($income, $amount, $payWay);
- $income->save();
-
- /**每个月的增加**/
- StatIncomeCategoryMonthService::replaceData($data);
-
- return true;
- }
-
- //取最近的按分类的收入 ssh 2019.9.26
- public static function getLatestIncome($start, $end,$sjId)
- {
- $list = self::getAllList('categoryId,amount', ['sjId' => $sjId, 'time' => ['between', [$start, $end]]], 'time asc');
- $totalAmount = 0;
- $categoryList = CategoryRelateService::getCategoryData();
- $data = [];
- foreach ($categoryList as $categoryId => $category) {
- $data[$categoryId]['name'] = $category['showName'];
- $data[$categoryId]['value'] = 0;
- }
- if (!empty($list)) {
- foreach ($list as $val) {
- $categoryId = $val['categoryId'];
- $amount = $val['amount'];
- $data[$categoryId]['value'] = stringUtil::calcAdd($data[$categoryId]['value'], $amount);
- $totalAmount = stringUtil::calcAdd($totalAmount, $amount);
- }
- }
- $categoryName = array_column($categoryList, 'showName');
- return ['data' => array_values($data), 'categoryName' => $categoryName, 'totalAmount' => $totalAmount];
- }
-
- }
|