Order.php 4.3 KB

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