Order.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App;
  3. use App\Traits\ModelTimeFormat;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\Traits\LogModelChanging;
  6. class Order extends Model
  7. {
  8. use LogModelChanging;
  9. //
  10. use ModelTimeFormat;
  11. protected $fillable = [
  12. 'id', 'batch_id', 'owner_id', 'status',
  13. 'created_at', 'code', 'shop_id', 'client_code',
  14. 'logistic_id', 'consignee_name', 'consignee_phone', 'province',
  15. 'city', 'district', 'address','warehouse_id',
  16. 'wms_edittime', 'wms_status','order_type'];
  17. /*
  18. * wms订单号 code=>DOC_ORDER_HEADER[orderno]
  19. * wms客户订单号 client_code=>DOC_ORDER_HEADER['soreference1']
  20. * wms订单状态 wms_status=>
  21. * 仓库 warehouse_id=>DOC_ORDER_HEADER['WAREHOUSEID']
  22. */
  23. protected $appends = [
  24. // 'logisticNumbers',
  25. // 'amount',
  26. // 'commodityPackages'
  27. ];
  28. public function cancel(){
  29. $this['status'] = '取消';
  30. $this->update();
  31. }
  32. public function logistic()
  33. {
  34. return $this->hasOne(Logistic::class, 'id', 'logistic_id');
  35. }
  36. public function issue()
  37. {
  38. return $this->belongsTo(OrderIssue::class, 'id', 'order_id');
  39. }
  40. public function shop()
  41. {
  42. return $this->belongsTo(Shop::class, 'shop_id', 'id');
  43. }
  44. public function owner()
  45. {
  46. return $this->hasOne(Owner::class, 'id', 'owner_id');
  47. }
  48. public function packages()
  49. {
  50. return $this->hasMany(OrderPackage::class,'order_id','id');
  51. }
  52. public function warehouse()
  53. {
  54. return $this->belongsTo(Warehouse::class);
  55. }
  56. public function orderCommodities(){
  57. return $this->hasMany('App\OrderCommodity','order_id','id');
  58. }
  59. public function bin(){
  60. return $this->hasOne('App\OrderBin','order_id','id');
  61. // $bin= $this->hasOne('App\OrderBin','order_id','id');
  62. // if($bin->count()>0)return $bin;
  63. // $this->batch()->first()->assignBins();
  64. // return $this->hasOne('App\OrderBin','order_id','id');
  65. }
  66. public function batch(){
  67. return $this->belongsTo('App\Batch', 'batch_id','id');
  68. }
  69. public function getLogisticNumbersAttribute()
  70. {
  71. $packages = $this->packages;
  72. if (!$packages) {
  73. return [];
  74. } else {
  75. $arr = [];
  76. foreach ($packages as $package) {
  77. array_push($arr, $package->logistic_number);
  78. }
  79. return $arr;
  80. }
  81. }
  82. public function getAmountAttribute()
  83. {
  84. $packages = $this->packages;
  85. if (!$packages) {
  86. return 0;
  87. } else {
  88. $count = 0;
  89. foreach ($packages as $package) {
  90. foreach ($package->commodities as $item) {
  91. $count += $item->amount;
  92. }
  93. }
  94. return $count;
  95. }
  96. }
  97. public function getCommodityPackagesAttribute()
  98. {
  99. $packages = $this->packages;
  100. if (!$packages) {
  101. return 0;
  102. } else {
  103. $count = 0;
  104. foreach ($packages as $package) {
  105. foreach ($package->commodities as $item) {
  106. $count++;
  107. }
  108. }
  109. return $count;
  110. }
  111. }
  112. public function delete()
  113. {
  114. $this->packages()->delete();
  115. return parent::delete();
  116. }
  117. public function deleteSafe()
  118. {
  119. return parent::delete();
  120. }
  121. public function isEquals($order)
  122. {
  123. return $this['wms_edittime'] == $order['wms_edittime'];
  124. }
  125. public function assignValueByOrder($order)
  126. {
  127. $this['code'] = $order['code'] ;
  128. $this['batch_id'] = $order['batch_id'] ;
  129. $this['warehouse_id'] = $order['warehouse_id'] ;
  130. $this['owner_id'] = $order['owner_id'] ;
  131. $this['shop_id'] = $order['shop_id'] ;
  132. $this['logistic_id'] = $order['logistic_id'] ;
  133. $this['consignee_name'] = $order['consignee_name'] ;
  134. $this['consignee_phone'] = $order['consignee_phone'] ;
  135. $this['province'] = $order['province'] ;
  136. $this['city'] = $order['city'] ;
  137. $this['district'] = $order['district'] ;
  138. $this['address'] = $order['address'] ;
  139. $this['client_code'] = $order['client_code'] ;
  140. $this['wms_status'] = $order['wms_status'] ;
  141. $this['wms_edittime'] = $order['wms_edittime'];
  142. $this['order_type'] = $order['order_type'];
  143. $this['created_at'] =$order['created_at'];
  144. }
  145. }