Order.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. $bin= $this->hasOne('App\OrderBin','order_id','id');
  61. if($bin->count()>0)return $bin;
  62. $this->batch()->first()->assignBins();
  63. return $this->hasOne('App\OrderBin','order_id','id');
  64. }
  65. public function batch(){
  66. return $this->belongsTo('App\Batch', 'batch_id','id');
  67. }
  68. public function getLogisticNumbersAttribute()
  69. {
  70. $packages = $this->packages;
  71. if (!$packages) {
  72. return [];
  73. } else {
  74. $arr = [];
  75. foreach ($packages as $package) {
  76. array_push($arr, $package->logistic_number);
  77. }
  78. return $arr;
  79. }
  80. }
  81. public function getAmountAttribute()
  82. {
  83. $packages = $this->packages;
  84. if (!$packages) {
  85. return 0;
  86. } else {
  87. $count = 0;
  88. foreach ($packages as $package) {
  89. foreach ($package->commodities as $item) {
  90. $count += $item->amount;
  91. }
  92. }
  93. return $count;
  94. }
  95. }
  96. public function getCommodityPackagesAttribute()
  97. {
  98. $packages = $this->packages;
  99. if (!$packages) {
  100. return 0;
  101. } else {
  102. $count = 0;
  103. foreach ($packages as $package) {
  104. foreach ($package->commodities as $item) {
  105. $count++;
  106. }
  107. }
  108. return $count;
  109. }
  110. }
  111. public function delete()
  112. {
  113. $this->packages()->delete();
  114. return parent::delete();
  115. }
  116. public function deleteSafe()
  117. {
  118. return parent::delete();
  119. }
  120. public function isEquals($order)
  121. {
  122. return $this['wms_edittime'] == $order['wms_edittime'];
  123. }
  124. public function assignValueByOrder($order)
  125. {
  126. $this['code'] = $order['code'] ;
  127. $this['batch_id'] = $order['batch_id'] ;
  128. $this['warehouse_id'] = $order['warehouse_id'] ;
  129. $this['owner_id'] = $order['owner_id'] ;
  130. $this['shop_id'] = $order['shop_id'] ;
  131. $this['logistic_id'] = $order['logistic_id'] ;
  132. $this['consignee_name'] = $order['consignee_name'] ;
  133. $this['consignee_phone'] = $order['consignee_phone'] ;
  134. $this['province'] = $order['province'] ;
  135. $this['city'] = $order['city'] ;
  136. $this['district'] = $order['district'] ;
  137. $this['address'] = $order['address'] ;
  138. $this['client_code'] = $order['client_code'] ;
  139. $this['wms_status'] = $order['wms_status'] ;
  140. $this['wms_edittime'] = $order['wms_edittime'];
  141. $this['order_type'] = $order['order_type'];
  142. $this['created_at'] =$order['created_at'];
  143. }
  144. }