Order.php 4.8 KB

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