| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use Yii;
- use yii\behaviors\TimestampBehavior;
- /**
- * This is the model class for table "{{%saas_user_price_log}}".
- *
- * @property integer $id
- * @property integer $user_id
- * @property string $order_no
- * @property integer $amount_type
- * @property integer $amount
- * @property string $desc
- * @property integer $before
- * @property integer $after
- * @property integer $log_type
- * @property integer $created_at
- */
- class SaasUserPriceLog extends \yii\db\ActiveRecord
- {
- /**
- * 收入类型
- */
- CONST LOG_TYPE_INCOME = 1;
- /**
- * 支出类型
- */
- CONST LOG_TYPE_EXPEND = 2;
- public static $type_budget = [
- self::LOG_TYPE_INCOME,
- self::LOG_TYPE_EXPEND
- ];
-
- const AMOUNT_TYPE_SUPPLIER_PARENT = 11;
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%saas_user_price_log}}';
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['saas_id', 'amount', 'desc', 'before', 'after', 'log_type'], 'required'],
- [['saas_id', 'created_at', 'log_type', 'amount_type'], 'integer'],
- [['amount', 'before', 'after'], 'number'],
- [['desc', 'order_no'], 'string'],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'saas_id' => 'saas_id',
- 'order_no' => '订单',
- 'amount_type' => '佣金类型:0:店铺分销,1:saas平台分销, 2:推荐分账, 3:服务商分账',
- 'amount' => '变动数',
- 'desc' => '变动说明',
- 'before' => '变动前',
- 'after' => '变动后',
- 'log_type' => '类型:1=收入,2=支出',
- 'created_at' => '添加时间',
- ];
- }
- /**
- *
- * @param $saas_id
- * @param $amount
- * @param $amount_type
- * @param $log_type
- * @param int $order_no
- * @param string $desc
- * @return bool
- */
- public static function saveLog($saas_id, $amount, $amount_type, $log_type, $order_no = '', $desc = "") {
- try{
- $t = \Yii::$app->db->beginTransaction();
- $form = new self();
- $saas = SaasUser::findOne($saas_id);
- if(!$saas){
- return false;
- }
- $form->saas_id = $saas_id;
- $form->amount = $amount;
- $form->amount_type = $amount_type;
- $form->before = $saas->price;
- if ($log_type == self::LOG_TYPE_INCOME) {
- $form->after = $saas->price + $amount;
- } else {
- $form->after = $saas->price - $amount;
- }
- $form->desc = $desc;
- $form->order_no = $order_no;
- $form->log_type = $log_type;
- $form->created_at = time();
- if ($form->save()) {
- if ($log_type == AccountLog::LOG_TYPE_INCOME) {
- $saas->price += $amount;
- $saas->total_price += $amount;
- } else {
- $saas->price -= $amount;
- }
- if($saas->save()){
- $t->commit();
- return true;
- }
- }
- } catch (\Exception $e) {
- \Yii::error(['saasUserPriceLog->saveLog', $form, $saas]);
- }
- $t->rollBack();
- return false;
- }
- }
|