123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- namespace app\common\model;
- use app\admin\model\Admin;
- use app\admin\model\MobileOrderAdmin;
- use app\common\service\Refund;
- use app\common\service\SmsSend;
- use think\db\Query;
- use think\Model;
- /**
- * 配置模型
- * @method static static payed()
- * @method $this filterShow()
- * @method $this waitPay()
- */
- class MobileOrder extends Model
- {
- use TraitModel;
- const STATUS_WAIT_SEND=10;
- const STATUS_CAN_SEND=15;
- const STATUS_REFUND=30;
- const STATUS_REFUNDED=90;
- const STATUS_REFUNDED_REJECT=100;
- public static $status=[
- 0=>'待付款',
- self::STATUS_WAIT_SEND=>'已付款',
- self::STATUS_CAN_SEND=>'可发货',
- 17=>'有号码未发货',
- 20=>'待开卡',
- 25=>'已完成',
- self::STATUS_REFUND=>'申请退款',
- 50=>'已关闭',
- 60=>'无号码',
- 70=>'换卡',
- 80=>'争议单',
- self::STATUS_REFUNDED=>'已退款',
- self::STATUS_REFUNDED_REJECT=>'退款驳回',
- ];
- public static $refundAllowStatus=[
- self::STATUS_WAIT_SEND,
- 15,
- 17,
- 20,
- 60,
- 70,
- ];
- public static $flowStatus=[
- '15',
- '25',
- ];
- public static $frontStatus=[
- 0,
- self::STATUS_WAIT_SEND,
- self::STATUS_CAN_SEND,
- 17,20,25,90,50,
- ];
- public static $payTypes=[
- 1=>'微信',
- 2=>'支付宝',
- 3=>'京东',
- ];
- protected $hidden=[
- 'status_bak',
- ];
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = true;
- public function mobile(){
- return $this->belongsTo(Mobile::class);
- }
- public function info(){
- return $this->hasOne(MobileOrderInfo::class);
- }
- public function operation(){
- return $this->hasMany(MobileOrderOperation::class)->order('mobile_order_operation.id','desc');
- }
- public function admin(){
- return $this->hasMany(MobileOrderAdmin::class);
- }
- public function status(){
- return $this->hasMany(MobileOrderStatus::class);
- }
- public function refundLog(){
- return $this->hasMany(MobileOrderRefundLog::class);
- }
- public function proxy(){
- $columns=Admin::getTableInfo()['fields'];
- unset($columns['password']);
- return $this->belongsTo(Admin::class)->field($columns);
- }
- public function getAddressAttr($a,$b){
- return Area::getNameString($b['city']).$a;
- }
- public function setCityAttr($a){
- if(is_array($a)){
- return implode(',',$a);
- }
- return $a;
- }
- protected static function init()
- {
- self::beforeInsert(function (self $mobileOrder){
- $mobileOrder['order_no']=order_no();
- $mobileOrder['expire_time']=time()+86400;
- $mobileOrder['s_id']=$mobileOrder['mobile']['s_id'];
- $mobileOrder['brand']=$mobileOrder['mobile']['brand'];
- #计算盈利
- $mobileOrder['amount_profit']=bcsub($mobileOrder['amount'],$mobileOrder['amount_di']);
- });
- self::afterInsert(function (self $mobileOrder){
- $mobileOrder->info()->save([
- 'mobile'=>$mobileOrder['mobile'],
- 'info'=>$mobileOrder['mobile']['info'],
- ]);
- #下单短信通知
- if($mobileOrder['type']==1){
- //SmsSend::orderSubmit($mobileOrder['phone']);
- }
- });
- self::beforeUpdate(function (self $mobileOrder){
- $data=$mobileOrder->getChangedData();
- #已支付
- if(isset($data['status']) && $data['status']==self::STATUS_WAIT_SEND){
- //SysConfig::set('mo_ordered_num',SysConfig::look('mo_ordered_num',0)+1);
- Mobile::whenOrderPayed($mobileOrder->mobile);
- }
- #记录status
- if(isset($data['status'])){
- $mobileOrder->status()->save([
- 'status'=>$mobileOrder->origin['status'],
- ]);
- }
- #发货短信通知
- if(isset($data['status']) && $data['status']==20){
- SmsSend::orderSend($mobileOrder['phone'],$mobileOrder['trans_name'],$mobileOrder['trans_no']);
- }
- });
- }
- public function scopeExpired(Query $query){
- $query->where('status',0)->where('expire_time','<',time());
- }
- public function cancel(){
- if($this['pay_time'] || $this['pay_time']!=0){
- return;
- }
- $this['status']=50;
- $this->save();
- }
- public function continuePay(){
- if($this['expire_time']<time()){
- throw_user('订单已过期');
- }
- if($this['status']!=0){
- throw_user('订单状态有误');
- }
- if($this['type']!=1){
- throw_user('该产品无需支付');
- }
- }
- public function scopePayed(Query $query){
- $query->where('status','>=',self::STATUS_WAIT_SEND);
- }
- public function originData(){
- return $this->origin;
- }
- public function checkAllowRefund(){
- if(!in_array($this['status'],self::$refundAllowStatus)){
- throw_user('当前状态不允许退款');
- }
- }
- public function makeUserRefund($reason){
- $this['status']=self::STATUS_REFUND;
- $this['refund_reason']=$reason;
- $this->save();
- }
- public static function flowStatus(){
- $s=self::$status;
- $s[15]='已提运营';
- foreach ($s as $key=>$value){
- if(!in_array($key,self::$flowStatus)){
- unset($s[$key]);
- }
- }
- return $s;
- }
- public function frontStatus(){
- return self::$frontStatus;
- }
- public function scopeFilterShow(Query $query){
- $query->whereIn('mobile_order.status',$this->frontStatus());
- }
- public function makeRefund($from,$user,$data){
- $model=$this;
- $model['amount_refund'] = $data['amount'];
- $model['refund_no'] = session_create_id();
- $model['refund_reason']=$data['refund_reason'];
- $model['status_bak']=$model['status'];
- if ($data['pass']) {
- if ($data['amount'] > $model['amount']) {
- throw_user('退款金额不能大于付款金额');
- }
- Refund::setType($model)->refund();
- $model['status'] = self::STATUS_REFUNDED;
- SmsSend::orderRefund($model['phone']);
- #退款盈利哦
- $model['amount_profit']=bcsub($model['amount'],$model['amount_refund']);
- }else{
- $model['amount_refund']=0;
- $model['status'] = self::STATUS_REFUNDED_REJECT;
- }
- $model->save();
- $model->refundLog()->save(MobileOrderRefundLog::withRefund($from,$user,$data));
- }
- public function flowOrderSubmit(){
- $this['status']=15;
- }
- public function scopeWaitPay(Query $query){
- $query->where('status',0);
- }
- }
|