Order.php 5.0 KB

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