Order.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Support\Facades\Auth;
  4. use Illuminate\Support\Facades\Validator;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Support\Facades\Log;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8. class Order extends Model
  9. {
  10. use SoftDeletes;
  11. protected $table='order';
  12. protected $fillable = ['agent_id','user_id','ordersn','price',
  13. 'address_id','desc','status','is_turn','turn_is','breakup_id',
  14. 'manual_ordersn','is_manual','original_ordersn'];
  15. private static $rules=[
  16. 'mobile'=>[
  17. 'required',
  18. 'regex:/^1[345789][0-9]{9}$/'
  19. ],
  20. 'name'=>'required',
  21. 'address'=>'required',
  22. ];
  23. private static $validate = [
  24. //提示信息
  25. 'message' => [
  26. 'mobile.required'=>'手机号不能为空.',
  27. 'mobile.regex'=>'手机号格式有误.',
  28. 'name.required'=>'名称不能为空.',
  29. 'address.required'=>'地址不能为空.',
  30. ]
  31. ];
  32. public function validator($data)
  33. {
  34. return Validator::make(
  35. $data,
  36. self::$rules,
  37. self::$validate['message']
  38. );
  39. }
  40. //生成订单号
  41. private function createOrderNo($id){
  42. $ordersn=time().str_pad($id, 6, '0', STR_PAD_LEFT).mt_rand(1000,9999);
  43. return $ordersn;
  44. }
  45. //添加手动订单
  46. public function createManualOrder($user,$addressId,$desc){
  47. $data['user_id']=$user->id;
  48. $data['sender_id']='1';//公司发货(发货人id)
  49. $data['admin_id']=Auth::user()->id;
  50. $data['manualsn']=$this->createOrderNo($user->id);
  51. $data['address_id']=$addressId?$addressId:0;
  52. $data['desc']=$desc;
  53. $data['status']=1;//已发货
  54. $manOrder=OrderManual::create($data);//创建手动订单
  55. $result['manOrderSn']=$data['manualsn'];
  56. $result['manOrderId']=$manOrder->id;
  57. return $result;
  58. }
  59. //添加订单信息
  60. public function createOrder($manOrder,$user,$crown){
  61. $data['agent_id']=$crown['agent_id'];
  62. $data['user_id']=$crown['user_id'];
  63. $data['ordersn']=$this->createOrderNo($user->id);
  64. $data['price']=$crown['price'];
  65. $data['address_id']=$crown['address_id'];
  66. $data['desc']=$crown['desc'];
  67. $data['status']='2';//待收货
  68. $data['is_turn']='0';
  69. $data['turn_is']='0';
  70. $data['breakup_id']=NULL;
  71. $data['original_ordersn']='';
  72. $data['manual_ordersn']=$manOrder['manOrderSn'];
  73. $data['is_manual']='1';
  74. $order=Order::create($data);//添加订单信息
  75. return $order->id;
  76. }
  77. //添加订单商品信息
  78. public function createOrderGoods($val,$v,$user,$orderId,$manOrder){
  79. $orderskuprice=GoodsSkuPrice::where('goods_id',$val['goodId'])
  80. ->where('sku_id',$v['id'])
  81. ->where('user_level',$user->level)->first();
  82. $data['order_id']=$orderId;
  83. $data['breakup_id']='';
  84. $data['manual_id']=12;//$manOrder['manOrderId'];
  85. $data['goods_id']=$val['goodId'];
  86. $data['sku_id']=$v['id'];
  87. $data['price']=$orderskuprice->price;
  88. $data['num']=$v['num'];
  89. $data['totalprice']=$orderskuprice->price*$v['num'];
  90. OrderGoods::create($data);
  91. }
  92. //添加地址
  93. public function createAddressInfo($input){
  94. $data['user_id']='1';//公司id
  95. $data['username']=$input['name'];
  96. $data['mobile']=$input['mobile'];
  97. $data['address']=$input['address'];
  98. $result=Address::create($data);
  99. return $result->id;
  100. }
  101. public function orderbreakup()
  102. {
  103. return $this->hasMany('App\OrderBreakup','ordersn','ordersn');
  104. }
  105. public function ordergoods()
  106. {
  107. return $this->hasMany('App\Models\OrderGoods','order_id');
  108. }
  109. public function user()
  110. {
  111. return $this->belongsTo('App\Models\User','user_id')->withTrashed();
  112. }
  113. public function agent()
  114. {
  115. return $this->belongsTo('App\Models\User','agent_id')->withTrashed();
  116. }
  117. public function address()
  118. {
  119. return $this->belongsTo('App\Models\Address','address_id');
  120. }
  121. //订单审核
  122. public static function order_audit($order_id,$order,$name){
  123. //修改订单
  124. Ordertest::where('id',$order_id)->update(['status'=>1,'is_back'=>0]);
  125. //记录操作日志
  126. Orderlog::create([
  127. 'order_id'=>$order_id,
  128. 'user_id'=>$order->user_id,
  129. 'log_name'=>$name,
  130. 'log_title'=>'订货单订单审核',
  131. 'log_text'=>'订货单已通过订单审核'
  132. ]);
  133. }
  134. //财务审核
  135. public static function financial_audit($order_id,$order,$name){
  136. //修改订单
  137. Ordertest::where('id',$order_id)->update(['status'=>2,'is_back'=>0]);
  138. //记录操作日志
  139. Orderlog::create([
  140. 'order_id'=>$order_id,
  141. 'user_id'=>$order->user_id,
  142. 'log_name'=>$name,
  143. 'log_title'=>'订货单财务审核',
  144. 'log_text'=>'订货单已通过财务审核'
  145. ]);
  146. }
  147. //发货审核
  148. public static function send_audit($order_id,$order,$name){
  149. //修改订单
  150. Ordertest::where('id',$order_id)->update(['status'=>3,'is_back'=>0]);
  151. //记录操作日志
  152. Orderlog::create([
  153. 'order_id'=>$order_id,
  154. 'user_id'=>$order->user_id,
  155. 'log_name'=>$name,
  156. 'log_title'=>'订货单发货审核',
  157. 'log_text'=>'订货单已通过发货审核'
  158. ]);
  159. }
  160. //订单回退
  161. public static function order_back(){
  162. }
  163. //财务回退
  164. public static function financial_back($order_id,$order,$name,$remark){
  165. //修改订单
  166. Ordertest::where('id',$order_id)->update(['status'=>0,'is_back'=>1]);
  167. //记录操作日志
  168. Orderlog::create([
  169. 'order_id'=>$order_id,
  170. 'user_id'=>$order->user_id,
  171. 'log_name'=>$name,
  172. 'log_title'=>'订货单财务审核',
  173. 'log_text'=>'订货单未通过财务审核,退回重审'
  174. ]);
  175. //备注
  176. if($remark){
  177. OrderRemark::create([
  178. 'order_id'=>$order_id,
  179. 'name'=>$name,
  180. 'level'=>'',
  181. 'type'=>2,
  182. 'remark'=>$remark
  183. ]);
  184. }
  185. }
  186. //发货回退
  187. public static function send_back($order_id,$order,$name,$remark){
  188. //修改订单
  189. Ordertest::where('id',$order_id)->update(['status'=>1,'is_back'=>1]);
  190. //记录操作日志
  191. Orderlog::create([
  192. 'order_id'=>$order_id,
  193. 'user_id'=>$order->user_id,
  194. 'log_name'=>$name,
  195. 'log_title'=>'订货单发货审核',
  196. 'log_text'=>'订货单未通过发货审核,退回重审'
  197. ]);
  198. //备注
  199. if($remark){
  200. OrderRemark::create([
  201. 'order_id'=>$order_id,
  202. 'name'=>$name,
  203. 'level'=>'',
  204. 'type'=>2,
  205. 'remark'=>$remark
  206. ]);
  207. }
  208. }
  209. //收货回退
  210. public static function receive_back($order_id,$order,$name,$remark){
  211. //修改订单
  212. Ordertest::where('id',$order_id)->update(['status'=>2,'is_back'=>1,'receive_time'=>NULL,'track_time'=>NULL]);
  213. //记录操作日志
  214. Orderlog::create([
  215. 'order_id'=>$order_id,
  216. 'user_id'=>$order->user_id,
  217. 'log_name'=>$name,
  218. 'log_title'=>'订货单发货后审核',
  219. 'log_text'=>'订货单发货后,退回重审'
  220. ]);
  221. //备注
  222. if($remark){
  223. OrderRemark::create([
  224. 'order_id'=>$order_id,
  225. 'name'=>$name,
  226. 'level'=>'',
  227. 'type'=>2,
  228. 'remark'=>$remark
  229. ]);
  230. }
  231. }
  232. }