123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace app\common\model;
- use think\db\Query;
- use think\Model;
- /**
- * @property Orders orders
- * @property Refund refund
- * @method static static|Query payed()
- * @method static static|Query filterHasUnRefund()
- */
- class OrderInfo extends Model
- {
- protected $autoWriteTimestamp=true;
- protected $hidden=[
- 'goods_bak'
- ];
- public function orders(){
- return $this->belongsTo(Orders::class,'order_id');
- }
- public function goodsBak(){
- return $this->belongsTo(OrderInfoGoods::class,'goods_bak_id');
- }
- public function refund(){
- return $this->belongsTo(Refund::class,'refund_id');
- }
- public function refunds(){
- return $this->hasMany(Refund::class,'order_info_id');
- }
- public static function saveInfo(Orders $orders,$goods){
- $orderInfo=new self();
- $orderInfoGoods=OrderInfoGoods::create([
- 'goods'=>$goods['goods'],
- 'sku'=>$goods['sku'],
- ]);
- $orderInfo['logo']=$goods['goods']['logo'][0];
- $orderInfo['user_id']=$orders['user_id'];
- $orderInfo['order_id']=$orders['id'];
- $orderInfo['goods_id']=$goods['goods_id'];
- $orderInfo['goods_sku_id']=$goods['goods_sku_id'];
- $orderInfo['num']=$goods['num'];
- $orderInfo['sku_name']=$goods['sku']['name'];
- $orderInfo['goods_name']=$goods['goods']['name'];
- $orderInfo['num_install']=$goods['num_install'];
- $orderInfo['coupon_id']=$goods['coupon_id']??0;
- $orderInfo['amount_coupon']=$goods['amount_coupon']??0;
- $orderInfo['amount_coupon_kill']=$goods['amount_coupon_kill']??0;
- $orderInfo['amount_coupon_level']=$goods['amount_coupon_level']??0;
- $orderInfo['amount_discount']=$goods['amount_discount']??0;
- $orderInfo['amount_total']=$goods['amount_total'];
- $orderInfo['amount_pay']=$goods['amount_pay'];
- $orderInfo['amount_install']=$goods['amount_install'];
- $orderInfo['amount']=$goods['sku']['amount'];
- $orderInfo['amount_kill']=$goods['sku']['amount_kill'];
- $orderInfo['amount_cost']=$goods['sku']['amount_cost'];
- $orderInfo['amount_cost_total']=bcmul($orderInfo['num'],$orderInfo['amount_cost']);
- $orderInfo['goods_bak_id']=$orderInfoGoods['id'];
- $orderInfo['amount_goods']=$goods['amount_goods'];
- $orderInfo['amount_goods_real']=$goods['amount_goods_real'];
- $orderInfo['category_id']=$goods['goods']['category_id'];
- $orderInfo['category_name']=Category::withTrashed()->where('id',$orderInfo['category_id'])->value('name');
- #毛利率
- $orderInfo['amount_profit']=bcsub($orderInfo['amount_pay'],$orderInfo['amount_cost_total']);
- $orderInfo['amount_profit_per']=bcmul(100,$orderInfo['amount_pay']>0?bcdiv($orderInfo['amount_profit'],$orderInfo['amount_pay']):0);
- #佣金
- $orderInfo['amount_cmn']=$goods['amount_cmn'];
- #秒杀商品
- $orderInfo['is_kill']=$goods['is_kill'];
- if(!$orderInfo->save()){
- throw_user('保存失败');
- }
- #
- OrderGoods::unique($orderInfo);
- return $orderInfo;
- }
- public function getGoodsAttr(){
- $info=$this;
- return $info['goodsBak']['goods'];
- }
- public function getSkuAttr(){
- $info=$this;
- return $info['goodsBak']['sku'];
- }
- public function scopePayed(Query $query){
- $query->whereExists(
- Orders::payed()->whereRaw("{$this->getTable()}.order_id=orders.id")->buildSql()
- );
- }
- public function scopeFilterHasUnRefund(Query $query){
- $query
- ->where(function (Query $query){
- $query->whereNull('refund_id')
- ->whereOrRaw(
- "exists(".
- sprintf("select * from order_info_refund where order_info_refund.id=order_info.refund_id and order_info_refund.refund_status in (%d,%d)",Refund::REFUND_REJECT,Refund::REFUND_CANCEL).
- ")"
- );
- });
- }
- protected static function init()
- {
- self::beforeInsert(function (self $orderInfo){
- //$orderInfo['amount_discount']=bcadd($orderInfo['amount_coupon'],$orderInfo['amount_coupon_kill']);
- });
- }
- }
|