Order.php 4.8 KB

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