Order.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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', '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 cancel(){
  26. $this['status'] = '取消';
  27. $this->update();
  28. }
  29. public function logistic()
  30. {
  31. return $this->hasOne(Logistic::class, 'id', 'logistic_id');
  32. }
  33. public function issue()
  34. {
  35. return $this->belongsTo(OrderIssue::class, 'id', 'order_id');
  36. }
  37. public function shop()
  38. {
  39. return $this->belongsTo(Shop::class, 'shop_id', 'id');
  40. }
  41. public function owner()
  42. {
  43. return $this->hasOne(Owner::class, 'id', 'owner_id');
  44. }
  45. public function packages()
  46. {
  47. return $this->hasMany(OrderPackage::class,'order_id','id');
  48. }
  49. public function warehouse()
  50. {
  51. return $this->belongsTo(Warehouse::class);
  52. }
  53. public function orderCommodities(){
  54. return $this->hasMany('App\OrderCommodity','order_id','id');
  55. }
  56. public function bin(){
  57. $bin= $this->hasOne('App\OrderBin','order_id','id');
  58. if($bin->count()>0)return $bin;
  59. $this->batch()->first()->assignBins();
  60. return $this->hasOne('App\OrderBin','order_id','id');
  61. }
  62. public function batch(){
  63. return $this->belongsTo('App\Batch', 'batch_id','id');
  64. }
  65. public function getLogisticNumbersAttribute()
  66. {
  67. $packages = $this->packages;
  68. if (!$packages) {
  69. return [];
  70. } else {
  71. $arr = [];
  72. foreach ($packages as $package) {
  73. array_push($arr, $package->logistic_number);
  74. }
  75. return $arr;
  76. }
  77. }
  78. public function getAmountAttribute()
  79. {
  80. $packages = $this->packages;
  81. if (!$packages) {
  82. return 0;
  83. } else {
  84. $count = 0;
  85. foreach ($packages as $package) {
  86. foreach ($package->commodities as $item) {
  87. $count += $item->amount;
  88. }
  89. }
  90. return $count;
  91. }
  92. }
  93. public function getCommodityPackagesAttribute()
  94. {
  95. $packages = $this->packages;
  96. if (!$packages) {
  97. return 0;
  98. } else {
  99. $count = 0;
  100. foreach ($packages as $package) {
  101. foreach ($package->commodities as $item) {
  102. $count++;
  103. }
  104. }
  105. return $count;
  106. }
  107. }
  108. public function delete()
  109. {
  110. $this->packages()->delete();
  111. return parent::delete();
  112. }
  113. public function deleteSafe()
  114. {
  115. return parent::delete();
  116. }
  117. public function isEquals($order)
  118. {
  119. return
  120. $this['code'] == $order['code'] &&
  121. $this['warehouse_id'] == $order['warehouse_id'] &&
  122. $this['owner_id'] == $order['owner_id'] &&
  123. $this['shop_id'] == $order['shop_id'] &&
  124. $this['logistic_id'] == $order['logistic_id'] &&
  125. $this['consignee_name'] == $order['consignee_name'] &&
  126. $this['consignee_phone'] == $order['consignee_phone'] &&
  127. $this['province'] == $order['province'] &&
  128. $this['city'] == $order['city'] &&
  129. $this['district'] == $order['district'] &&
  130. $this['address'] == $order['address'] &&
  131. $this['client_code'] = $order['client_code'] &&
  132. $this['wms_status'] == $order['wms_status'] &&
  133. $this['wms_edittime'] == $order['wms_edittime'] &&
  134. (string)$this['created_at'] == (string)$order['created_at'];
  135. }
  136. public function assignValueByOrder($order)
  137. {
  138. $this['code'] = $order['code'] ;
  139. $this['warehouse_id'] = $order['warehouse_id'] ;
  140. $this['owner_id'] = $order['owner_id'] ;
  141. $this['shop_id'] = $order['shop_id'] ;
  142. $this['logistic_id'] = $order['logistic_id'] ;
  143. $this['consignee_name'] = $order['consignee_name'] ;
  144. $this['consignee_phone'] = $order['consignee_phone'] ;
  145. $this['province'] = $order['province'] ;
  146. $this['city'] = $order['city'] ;
  147. $this['district'] = $order['district'] ;
  148. $this['address'] = $order['address'] ;
  149. $this['client_code'] = $order['client_code'] ;
  150. $this['wms_status'] = $order['wms_status'] ;
  151. $this['wms_edittime'] = $order['wms_edittime'];
  152. $this['created_at'] =$order['created_at'];
  153. }
  154. }