Order.php 4.9 KB

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