OrderInfo.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\common\model;
  3. use think\db\Query;
  4. use think\Model;
  5. /**
  6. * @property Orders orders
  7. * @property Refund refund
  8. * @method static static payed()
  9. */
  10. class OrderInfo extends Model
  11. {
  12. protected $autoWriteTimestamp=true;
  13. protected $hidden=[
  14. 'goods_bak'
  15. ];
  16. public function orders(){
  17. return $this->belongsTo(Orders::class,'order_id');
  18. }
  19. public function goodsBak(){
  20. return $this->belongsTo(OrderInfoGoods::class,'goods_bak_id');
  21. }
  22. public function refund(){
  23. return $this->belongsTo(Refund::class,'refund_id');
  24. }
  25. public static function saveInfo(Orders $orders,$goods){
  26. $orderInfo=new self();
  27. $orderInfoGoods=OrderInfoGoods::create([
  28. 'goods'=>$goods['goods'],
  29. 'sku'=>$goods['sku'],
  30. ]);
  31. $orderInfo['logo']=$goods['goods']['logo'][0];
  32. $orderInfo['user_id']=$orders['user_id'];
  33. $orderInfo['order_id']=$orders['id'];
  34. $orderInfo['goods_id']=$goods['goods_id'];
  35. $orderInfo['goods_sku_id']=$goods['goods_sku_id'];
  36. $orderInfo['num']=$goods['num'];
  37. $orderInfo['sku_name']=$goods['sku']['name'];
  38. $orderInfo['goods_name']=$goods['goods']['name'];
  39. $orderInfo['num_install']=$goods['num_install'];
  40. $orderInfo['coupon_id']=$goods['coupon_id']??0;
  41. $orderInfo['amount_coupon']=$goods['amount_coupon']??0;
  42. $orderInfo['amount_coupon_kill']=$goods['amount_coupon_kill']??0;
  43. $orderInfo['amount_coupon_level']=$goods['amount_coupon_level']??0;
  44. $orderInfo['amount_discount']=$goods['amount_discount']??0;
  45. $orderInfo['amount_total']=$goods['amount_total'];
  46. $orderInfo['amount_pay']=$goods['amount_pay'];
  47. $orderInfo['amount_install']=$goods['amount_install'];
  48. $orderInfo['amount']=$goods['sku']['amount'];
  49. $orderInfo['amount_kill']=$goods['sku']['amount_kill'];
  50. $orderInfo['amount_cost']=$goods['sku']['amount_cost'];
  51. $orderInfo['amount_cost_total']=bcmul($orderInfo['num'],$orderInfo['amount_cost']);
  52. $orderInfo['goods_bak_id']=$orderInfoGoods['id'];
  53. $orderInfo['amount_goods']=$goods['amount_goods'];
  54. $orderInfo['amount_goods_real']=$goods['amount_goods_real'];
  55. $orderInfo['category_id']=$goods['goods']['category_id'];
  56. $orderInfo['category_name']=Category::withTrashed()->where('id',$orderInfo['category_id'])->value('name');
  57. #毛利率
  58. $orderInfo['amount_profit']=bcsub($orderInfo['amount_pay'],$orderInfo['amount_cost_total']);
  59. $orderInfo['amount_profit_per']=bcmul(100,bcdiv($orderInfo['amount_profit'],$orderInfo['amount_pay']));
  60. #佣金
  61. $orderInfo['amount_cmn']=$goods['amount_cmn'];
  62. if(!$orderInfo->save()){
  63. throw_user('保存失败');
  64. }
  65. #
  66. OrderGoods::unique($orderInfo);
  67. return $orderInfo;
  68. }
  69. public function getGoodsAttr(){
  70. $info=$this;
  71. return $info['goodsBak']['goods'];
  72. }
  73. public function getSkuAttr(){
  74. $info=$this;
  75. return $info['goodsBak']['sku'];
  76. }
  77. public function scopePayed(Query $query){
  78. $query->whereExists(
  79. Orders::payed()->whereRaw("{$this->getTable()}.order_id=orders.id")->buildSql()
  80. );
  81. }
  82. protected static function init()
  83. {
  84. self::beforeInsert(function (self $orderInfo){
  85. //$orderInfo['amount_discount']=bcadd($orderInfo['amount_coupon'],$orderInfo['amount_coupon_kill']);
  86. });
  87. }
  88. }