| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\plugins\scanCodePay\models;
- use app\models\AccountLog;
- use app\models\Option;
- use Yii;
- use Codeception\PHPUnit\ResultPrinter\HTML;
- /**
- * This is the model class for table "{{%scan_code_pay_order}}".
- *
- * @property integer $id
- * @property integer $store_id
- * @property integer $user_id
- * @property string $order_no
- * @property string $total_price
- * @property string $pay_price
- * @property string $express_price
- * @property string $send_price
- * @property string $take_price
- * @property string $first_share_profit
- * @property string $second_share_profit
- * @property string $third_share_profit
- * @property string $name
- * @property string $mobile
- * @property string $address
- * @property string $remark
- * @property integer $is_pay
- * @property integer $pay_type
- * @property integer $pay_time
- * @property integer $send_time
- * @property string $express
- * @property string $express_no
- * @property integer $confirm_time
- * @property integer $is_comment
- * @property integer $apply_delete
- * @property integer $created_at
- * @property integer $is_delete
- * @property integer $is_price
- * @property integer $parent_id
- * @property string $first_price
- * @property string $second_price
- * @property string $third_price
- * @property string $coupon_sub_price
- * @property string $content
- * @property integer $is_offline
- * @property integer $clerk_id
- * @property string $address_data
- * @property string $offline_qrcode
- * @property string $before_update_price
- * @property integer $shop_id
- * @property string $discount
- * @property integer $user_coupon_id
- * @property string $integral
- * @property integer $give_integral
- * @property integer $parent_id_1
- * @property integer $parent_id_2
- * @property integer $is_sale
- * @property string $words
- * @property string $version
- * @property string $express_price_1
- * @property integer $mch_id
- * @property integer $is_recycle
- * @property string $seller_comments
- * @property integer $order_union_id
- * @property string $rebate
- * @property string $before_update_express
- * @property integer $is_transfer
- * @property integer $type
- * @property integer $share_user_first_id
- * @property integer $share_user_second_id
- * @property integer $share_user_third_id
- * @property integer $is_share
- * @property string $share_price
- * @property string $first_share_price
- * @property string $second_share_price
- * @property string $third_share_price
- * @property integer $is_show
- * @property integer $order_origin
- * @property integer $is_open_offline
- * @property integer $trade_status
- * @property string $transaction_id
- * @property integer $alipay_trade_no
- * @property integer $is_platform
- * @property integer $combine_money
- * @property integer $is_combine_pay
- * @property integer $share_send_type
- * @property integer $share_order_profit
- * @property integer $is_use_platform_mch
- * @property string $adapay_payment_id
- * @property integer $is_yunst_sharing
- * @property integer $md_id
- * @property integer $mch_is_price
- * @property integer $mch_is_price_time
- */
- class Order extends \yii\db\ActiveRecord
- {
- /**
- * 订单流转状态:默认
- */
- const ORDER_FLOW_DEFAULT = -1;
- /**
- * 订单流转状态:待发货
- */
- const ORDER_FLOW_NO_SEND = 0;
- /**
- * 订单流转状态:已取消
- */
- const ORDER_FLOW_CANCEL = 1;
- /**
- * 订单流转状态:已发货
- */
- const ORDER_FLOW_SEND = 2;
- /**
- * 订单流转状态:已完成
- */
- const ORDER_FLOW_CONFIRM = 3;
- public $valid_order_flow = [
- self::ORDER_FLOW_DEFAULT,
- self::ORDER_FLOW_NO_SEND,
- self::ORDER_FLOW_SEND,
- self::ORDER_FLOW_CANCEL,
- self::ORDER_FLOW_CONFIRM
- ];
- /**
- * 申请取消订单: 默认
- */
- const ORDER_APPLY_DELETE_DEFAULT = 0;
- /**
- * 申请取消订单: 申请中
- */
- const ORDER_APPLY_DELETE = 1;
- /**
- * 订单来源 公众号或网站
- */
- const ORDER_SOURCE_WEB = 1;
- /**
- * 订单来源 app
- */
- const ORDER_SOURCE_APP = 2;
- /**
- * 订单来源 小程序
- */
- const ORDER_SOURCE_MINI = 3;
- /**
- * 用户取消类型
- */
- const CANCEL_USER_TYPE = 0;
- /**
- * 后台取消类型
- */
- const CANCEL_BACK_TYPE = 1;
- /**
- * 是否取消(手动):已取消
- */
- const IS_DELETE_TRUE = 1;
- /**
- * 是否取消(手动):未取消
- */
- const IS_DELETE_FALSE = 0;
- /**
- * 是否支付:已支付
- */
- const IS_PAY_TRUE = 1;
- /**
- * 是否支付:未支付
- */
- const IS_PAY_FALSE = 0;
- /**
- * 支付方式:未支付
- */
- const PAY_TYPE_UNPAID = 0;
- /**
- * 支付方式:微信支付
- */
- const PAY_TYPE_WECHAT = 1;
- /**
- * 支付方式:货到付款
- */
- const PAY_TYPE_COD = 2;
- /**
- * 支付方式:余额支付
- */
- const PAY_TYPE_BALANCE_PAID = 3;
- /**
- * 订单类型:商城订单
- */
- const ORDER_TYPE_STORE = 0;
- /**
- * 订单类型:卡券兑换订单
- */
- const ORDER_TYPE_POND = 1;
- /**
- * 订单是否显示:显示
- */
- const IS_SHOW_TRUE = 1;
- /**
- * 订单是否显示:不显示
- */
- const IS_SHOW_FALSE = 0;
- /**
- * 是否过售后时间:是
- */
- const IS_SALE_TRUE = 1;
- /**
- * 是否过售后时间:否
- */
- const IS_SALE_FALSE = 0;
- /**
- * 自提订单
- */
- const IS_OFFLINE_TRUE = 1;
- const IS_OFFLINE_FALSE = 0;
- /**
- * 类型
- */
- // const SEND_TYPE_DADA = 1;
- // const SEND_TYPE_UU = 2;
- // const SEND_TYPE_PEISONG = 3;
- /**
- * 同城配送
- */
- const IS_SAME_CITY_NO = 0; //否
- const IS_SAME_CITY_YES = 1; //是
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%scan_code_pay_order}}';
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['store_id', 'user_id', 'order_no', 'first_price', 'second_price', 'third_price'], 'required'],
- [['store_id', 'user_id', 'is_pay', 'pay_type', 'pay_time', 'send_time',
- 'confirm_time', 'is_comment', 'apply_delete', 'created_at', 'is_delete', 'is_price', 'parent_id',
- 'is_offline', 'clerk_id', 'shop_id', 'user_coupon_id', 'give_integral', 'parent_id_1',
- 'parent_id_2', 'is_sale', 'mch_id', 'order_union_id', 'is_transfer', 'type', 'is_show',
- 'is_open_offline', 'order_origin', 'trade_status', 'is_platform', 'is_combine_pay', 'share_send_type', 'is_use_platform_mch', 'md_id'], 'integer'],
- [['total_price', 'pay_price', 'express_price', 'first_price', 'second_price', 'third_price',
- 'coupon_sub_price', 'before_update_price', 'discount', 'express_price_1', 'rebate',
- 'before_update_express', 'share_price', 'combine_money', 'share_order_profit', 'mch_is_price', 'mch_is_price_time'], 'number'],
- [['address_data', 'content', 'offline_qrcode', 'integral', 'words', 'seller_comments', 'transaction_id'], 'string'],
- [['order_no', 'name', 'mobile', 'express', 'express_no', 'version', 'alipay_trade_no'], 'string', 'max' => 255],
- [['address', 'remark'], 'string', 'max' => 1000],
- [['adapay_payment_id'], 'safe'],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'share_user_first_id' => 'share_user_first_id',
- 'share_user_second_id' => 'share_user_second_id',
- 'share_user_third_id' => 'share_user_third_id',
- 'is_share' => 'ID',
- 'store_id' => 'Store ID',
- 'user_id' => '用户id',
- 'order_no' => '订单号',
- 'total_price' => '订单总费用(包含运费)',
- 'pay_price' => '实际支付总费用(含运费)',
- 'express_price' => '运费',
- 'send_price' => '赠送用户金额',
- 'take_price' => '扣除用户联盟券抵扣金额',
- 'first_share_price' => '一级分销',
- 'second_share_price' => '二级分销',
- 'third_share_price' => '三级分销',
- 'name' => '收货人姓名',
- 'mobile' => '收货人手机',
- 'address' => '收货地址',
- 'remark' => '订单备注',
- 'is_pay' => '支付状态:0=未支付,1=已支付',
- 'pay_type' => '支付方式:1=微信支付',
- 'pay_time' => '支付时间',
- 'send_time' => '发货时间',
- 'express' => '物流公司',
- 'express_no' => 'Express No',
- 'confirm_time' => '确认收货时间',
- 'is_comment' => '是否已评价:0=未评价,1=已评价',
- 'apply_delete' => '是否申请取消订单:0=否,1=申请取消订单',
- 'created_at' => 'Addtime',
- 'is_delete' => 'Is Delete',
- 'is_price' => '是否发放佣金',
- 'parent_id' => '用户上级ID',
- 'first_price' => '一级佣金',
- 'second_price' => '二级佣金',
- 'third_price' => '三级佣金',
- 'coupon_sub_price' => '优惠券抵消金额',
- 'content' => 'Content',
- 'is_offline' => '是否到店自提 0--否 1--是',
- 'clerk_id' => '核销员user_id',
- 'address_data' => '收货地址信息,json格式',
- 'offline_qrcode' => '核销码',
- 'before_update_price' => '修改前的价格',
- 'shop_id' => '自提自提点ID',
- 'discount' => '会员折扣',
- 'user_coupon_id' => '使用的优惠券ID',
- 'integral' => '积分使用',
- 'give_integral' => '是否发放积分【1=> 已发放 , 0=> 未发放】',
- 'parent_id_1' => '用户上二级ID',
- 'parent_id_2' => '用户上三级ID',
- 'is_sale' => '是否超过售后时间',
- 'words' => '商家留言',
- 'version' => '版本',
- 'express_price_1' => '减免的运费',
- 'mch_id' => '入驻商户id',
- 'seller_comments' => '商家备注',
- 'order_union_id' => '合并订单的id',
- 'rebate' => '自购返利',
- 'before_update_express' => '价格修改前的运费',
- 'is_transfer' => '是否已转入商户账户:0=否,1=是',
- 'type' => '0普通订单1卡券兑换订单',
- 'share_price' => '发放佣金的金额',
- 'is_show' => '是否显示 0--不显示 1--显示(软删除用)',
- 'order_origin' => '订单来源 1:公众号或者网站 2:app 3:小程序',
- 'is_open_offline' => '是否是线下购物车下的订单',
- 'trade_status' => '订单状态,-1:默认,0:待发货,1:已取消,2:已发货,3:已确认',
- 'is_use_platform_mch' => '供应链系统下单时是否使用平台商户号,1使用,0未使用',
- 'transaction_id' => '微信商户号'
- ];
- }
- public function afterSave($insert, $changedAttributes)
- {
- parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
- if (intval($this->attributes['is_pay'])) {
- self::send_user_integral($this);
- }
- }
- public function getOrderDetail()
- {
- return $this->hasMany(OrderDetail::className(), ['order_id' => 'id'])->alias('od')
- ->leftJoin(['g' => Goods::tableName()], 'g.id=od.goods_id')->select(['od.*', 'g.name', 'g.attr goods_attr', 'g.cost_price']);
- }
- public function getDetail()
- {
- return $this->hasMany(OrderDetail::className(), ['order_id' => 'id']);
- }
- public function getGoods()
- {
- return $this->hasMany(Goods::className(), ['id' => 'goods_id'])->alias('g')
- ->viaTable(OrderDetail::tableName() . ' od', ['order_id' => 'id']);
- }
- public function getShop()
- {
- return $this->hasOne(Shop::className(), ['id' => 'shop_id']);
- }
- public function getClerk()
- {
- return $this->hasOne(User::className(), ['id' => 'clerk_id']);
- }
- public function getOrderForm()
- {
- return $this->hasMany(OrderForm::className(), ['order_id' => 'id'])->where(['is_delete' => 0]);
- }
- public function getUser()
- {
- return $this->hasOne(User::className(), ['id' => 'user_id']);
- }
- public function getRefund()
- {
- return $this->hasMany(OrderRefund::className(), ['order_id' => 'id']);
- }
- public function beforeSave($insert)
- {
- $this->content = \yii\helpers\Html::encode($this->content);
- return parent::beforeSave($insert);
- }
- public function getPondDetail()
- {
- return $this->hasOne(OrderDetail::className(), ['order_id' => 'id']);
- }
- public function getPondGoods()
- {
- return $this->hasOne(Goods::className(), ['id' => 'goods_id'])->alias('g')
- ->viaTable(OrderDetail::tableName() . ' od', ['order_id' => 'id']);
- }
- //发放积分
- public static function send_user_integral($order)
- {
- debug_log(['发放积分'], 'send_user_integral.log');
- try {
- $face_send_integral_profit = Option::get('face_send_integral_profit', $order->store_id, 'store', '0')['value'];
- debug_log(['face_send_integral_profit' => $face_send_integral_profit, 'store_id' => $order->store_id], 'send_user_integral.log');
- if ($face_send_integral_profit <= 0) {
- throw new \Exception('');
- }
- $user_id = $order->user_id;
- debug_log(['user_id' => $user_id], 'send_user_integral.log');
- if ($user_id) {
- $total_price = $order->total_price;
- debug_log(['total_price' => $total_price], 'send_user_integral.log');
- $integral = bcdiv(bcmul($total_price, $face_send_integral_profit, 2), 100, 2);
- AccountLog::saveLog($user_id, $integral, AccountLog::TYPE_INTEGRAL,
- AccountLog::LOG_TYPE_INCOME, AccountLog::ORDER_TYPE_FACE_PAY,
- $order->id, "商城当面付订单营销赠送积分, 订单号:{$order->order_no}");
- return [
- 'code' => 0,
- 'msg' => '发送成功'
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '用户未找到'
- ];
- } catch (\Exception $e) {
- debug_log([
- 'msg' => $e->getMessage(),
- 'line' => $e->getLine(),
- 'file' => $e->getFile()
- ], 'send_user_integral.log');
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 统计一个时间段 入住商或者当面付订单的数量以及订单的金额
- * @param $mchId
- * @param $startTime
- * @param $endTime
- * @return array|\yii\db\ActiveRecord|null
- * @author: hankaige
- * @Time: 2025/7/15 10:12
- */
- public static function getMchOrder($mchId = 0, $startTime = 0, $endTime = 0){
- $query = self::find()->where(['mch_id' => $mchId,'is_pay' => self::IS_PAY_TRUE,'is_delete' => self::IS_DELETE_FALSE]);
- if($startTime > 0){
- $query->andWhere(['>=','created_at',$startTime]);
- }
- if($endTime){
- $query->andWhere(['<=','created_at',$endTime]);
- }
- return $query->select('count(id) AS order_num,SUM(total_price) as order_price')->asArray()->one();
- }
- }
|