MobileOrder.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace app\common\model;
  3. use app\admin\model\Admin;
  4. use app\admin\model\MobileOrderAdmin;
  5. use app\common\service\Refund;
  6. use app\common\service\SmsSend;
  7. use think\db\Query;
  8. use think\Model;
  9. /**
  10. * 配置模型
  11. * @method static static payed()
  12. * @method $this filterShow()
  13. * @method $this waitPay()
  14. */
  15. class MobileOrder extends Model
  16. {
  17. use TraitModel;
  18. const STATUS_WAIT_SEND=10;
  19. const STATUS_CAN_SEND=15;
  20. const STATUS_REFUND=30;
  21. const STATUS_REFUNDED=90;
  22. const STATUS_REFUNDED_REJECT=100;
  23. public static $status=[
  24. 0=>'待付款',
  25. self::STATUS_WAIT_SEND=>'已付款',
  26. self::STATUS_CAN_SEND=>'可发货',
  27. 17=>'有号码未发货',
  28. 20=>'待开卡',
  29. 25=>'已完成',
  30. self::STATUS_REFUND=>'申请退款',
  31. 50=>'已关闭',
  32. 60=>'无号码',
  33. 70=>'换卡',
  34. 80=>'争议单',
  35. self::STATUS_REFUNDED=>'已退款',
  36. self::STATUS_REFUNDED_REJECT=>'退款驳回',
  37. ];
  38. public static $refundAllowStatus=[
  39. self::STATUS_WAIT_SEND,
  40. 15,
  41. 17,
  42. 20,
  43. 60,
  44. 70,
  45. ];
  46. public static $flowStatus=[
  47. '15',
  48. '25',
  49. ];
  50. public static $frontStatus=[
  51. 0,
  52. self::STATUS_WAIT_SEND,
  53. self::STATUS_CAN_SEND,
  54. 17,20,25,90,50,
  55. ];
  56. public static $payTypes=[
  57. 1=>'微信',
  58. 2=>'支付宝',
  59. 3=>'京东',
  60. ];
  61. protected $hidden=[
  62. 'status_bak',
  63. ];
  64. // 自动写入时间戳字段
  65. protected $autoWriteTimestamp = true;
  66. public function mobile(){
  67. return $this->belongsTo(Mobile::class);
  68. }
  69. public function info(){
  70. return $this->hasOne(MobileOrderInfo::class);
  71. }
  72. public function operation(){
  73. return $this->hasMany(MobileOrderOperation::class)->order('mobile_order_operation.id','desc');
  74. }
  75. public function admin(){
  76. return $this->hasMany(MobileOrderAdmin::class);
  77. }
  78. public function status(){
  79. return $this->hasMany(MobileOrderStatus::class);
  80. }
  81. public function refundLog(){
  82. return $this->hasMany(MobileOrderRefundLog::class);
  83. }
  84. public function proxy(){
  85. $columns=Admin::getTableInfo()['fields'];
  86. unset($columns['password']);
  87. return $this->belongsTo(Admin::class)->field($columns);
  88. }
  89. public function getAddressAttr($a,$b){
  90. return Area::getNameString($b['city']).$a;
  91. }
  92. public function setCityAttr($a){
  93. if(is_array($a)){
  94. return implode(',',$a);
  95. }
  96. return $a;
  97. }
  98. protected static function init()
  99. {
  100. self::beforeInsert(function (self $mobileOrder){
  101. $mobileOrder['order_no']=order_no();
  102. $mobileOrder['expire_time']=time()+86400;
  103. $mobileOrder['s_id']=$mobileOrder['mobile']['s_id'];
  104. $mobileOrder['brand']=$mobileOrder['mobile']['brand'];
  105. #计算盈利
  106. $mobileOrder['amount_profit']=bcsub($mobileOrder['amount'],$mobileOrder['amount_di']);
  107. });
  108. self::afterInsert(function (self $mobileOrder){
  109. $mobileOrder->info()->save([
  110. 'mobile'=>$mobileOrder['mobile'],
  111. 'info'=>$mobileOrder['mobile']['info'],
  112. ]);
  113. #下单短信通知
  114. if($mobileOrder['type']==1){
  115. //SmsSend::orderSubmit($mobileOrder['phone']);
  116. }
  117. });
  118. self::beforeUpdate(function (self $mobileOrder){
  119. $data=$mobileOrder->getChangedData();
  120. #已支付
  121. if(isset($data['status']) && $data['status']==self::STATUS_WAIT_SEND){
  122. //SysConfig::set('mo_ordered_num',SysConfig::look('mo_ordered_num',0)+1);
  123. Mobile::whenOrderPayed($mobileOrder->mobile);
  124. }
  125. #记录status
  126. if(isset($data['status'])){
  127. $mobileOrder->status()->save([
  128. 'status'=>$mobileOrder->origin['status'],
  129. ]);
  130. }
  131. #发货短信通知
  132. if(isset($data['status']) && $data['status']==20){
  133. SmsSend::orderSend($mobileOrder['phone'],$mobileOrder['trans_name'],$mobileOrder['trans_no']);
  134. }
  135. });
  136. }
  137. public function scopeExpired(Query $query){
  138. $query->where('status',0)->where('expire_time','<',time());
  139. }
  140. public function cancel(){
  141. if($this['pay_time'] || $this['pay_time']!=0){
  142. return;
  143. }
  144. $this['status']=50;
  145. $this->save();
  146. }
  147. public function continuePay(){
  148. if($this['expire_time']<time()){
  149. throw_user('订单已过期');
  150. }
  151. if($this['status']!=0){
  152. throw_user('订单状态有误');
  153. }
  154. if($this['type']!=1){
  155. throw_user('该产品无需支付');
  156. }
  157. }
  158. public function scopePayed(Query $query){
  159. $query->where('status','>=',self::STATUS_WAIT_SEND);
  160. }
  161. public function originData(){
  162. return $this->origin;
  163. }
  164. public function checkAllowRefund(){
  165. if(!in_array($this['status'],self::$refundAllowStatus)){
  166. throw_user('当前状态不允许退款');
  167. }
  168. }
  169. public function makeUserRefund($reason){
  170. $this['status']=self::STATUS_REFUND;
  171. $this['refund_reason']=$reason;
  172. $this->save();
  173. }
  174. public static function flowStatus(){
  175. $s=self::$status;
  176. $s[15]='已提运营';
  177. foreach ($s as $key=>$value){
  178. if(!in_array($key,self::$flowStatus)){
  179. unset($s[$key]);
  180. }
  181. }
  182. return $s;
  183. }
  184. public function frontStatus(){
  185. return self::$frontStatus;
  186. }
  187. public function scopeFilterShow(Query $query){
  188. $query->whereIn('mobile_order.status',$this->frontStatus());
  189. }
  190. public function makeRefund($from,$user,$data){
  191. $model=$this;
  192. $model['amount_refund'] = $data['amount'];
  193. $model['refund_no'] = session_create_id();
  194. $model['refund_reason']=$data['refund_reason'];
  195. $model['status_bak']=$model['status'];
  196. if ($data['pass']) {
  197. if ($data['amount'] > $model['amount']) {
  198. throw_user('退款金额不能大于付款金额');
  199. }
  200. Refund::setType($model)->refund();
  201. $model['status'] = self::STATUS_REFUNDED;
  202. SmsSend::orderRefund($model['phone']);
  203. #退款盈利哦
  204. $model['amount_profit']=bcsub($model['amount'],$model['amount_refund']);
  205. }else{
  206. $model['amount_refund']=0;
  207. $model['status'] = self::STATUS_REFUNDED_REJECT;
  208. }
  209. $model->save();
  210. $model->refundLog()->save(MobileOrderRefundLog::withRefund($from,$user,$data));
  211. }
  212. public function flowOrderSubmit(){
  213. $this['status']=15;
  214. }
  215. public function scopeWaitPay(Query $query){
  216. $query->where('status',0);
  217. }
  218. }