Order.php 4.6 KB

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