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 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,bcdiv($orderInfo['amount_profit'],$orderInfo['amount_pay'])); #佣金 $orderInfo['amount_cmn']=$goods['amount_cmn']; 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() ); } protected static function init() { self::beforeInsert(function (self $orderInfo){ //$orderInfo['amount_discount']=bcadd($orderInfo['amount_coupon'],$orderInfo['amount_coupon_kill']); }); } }