Order.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. const STATUS = [
  40. '00' => "创建订单",
  41. '10' => "部分预配",
  42. '20' => "预配完成",
  43. '30' => "部分分配",
  44. '40' => "分配完成",
  45. '50' => "部分拣货",
  46. '60' => "拣货完成",
  47. '61' => "播种完成",
  48. '62' => "部分装箱",
  49. '63' => "完全装箱",
  50. '65' => "部分装车",
  51. '66' => "装车完成",
  52. '70' => "部分发运",
  53. '80' => "完全发运",
  54. '90' => "订单取消",
  55. '98' => "等待释放",
  56. '99' => "订单完成",
  57. ];
  58. public function cancel(){
  59. $this['status'] = '取消';
  60. $this->update();
  61. }
  62. public function logistic()
  63. {
  64. return $this->hasOne(Logistic::class, 'id', 'logistic_id');
  65. }
  66. public function issue()
  67. {
  68. return $this->belongsTo(OrderIssue::class, 'id', 'order_id');
  69. }
  70. public function shop()
  71. {
  72. return $this->belongsTo(Shop::class, 'shop_id', 'id');
  73. }
  74. public function owner()
  75. {
  76. return $this->hasOne(Owner::class, 'id', 'owner_id');
  77. }
  78. public function packages()
  79. {
  80. return $this->hasMany(OrderPackage::class,'order_id','id');
  81. }
  82. public function warehouse()
  83. {
  84. return $this->belongsTo(Warehouse::class);
  85. }
  86. public function orderCommodities(){
  87. return $this->hasMany('App\OrderCommodity','order_id','id');
  88. }
  89. public function bin(){
  90. return $this->hasOne('App\OrderBin','order_id','id');
  91. }
  92. public function batch(){
  93. return $this->belongsTo('App\Batch', 'batch_id','id');
  94. }
  95. public function getLogisticNumbersAttribute()
  96. {
  97. $packages = $this->packages;
  98. if (!$packages) {
  99. return [];
  100. } else {
  101. $arr = [];
  102. foreach ($packages as $package) {
  103. array_push($arr, $package->logistic_number);
  104. }
  105. return $arr;
  106. }
  107. }
  108. public function getAmountAttribute()
  109. {
  110. $packages = $this->packages;
  111. if (!$packages) {
  112. return 0;
  113. } else {
  114. $count = 0;
  115. foreach ($packages as $package) {
  116. foreach ($package->commodities as $item) {
  117. $count += $item->amount;
  118. }
  119. }
  120. return $count;
  121. }
  122. }
  123. public function getCommodityPackagesAttribute()
  124. {
  125. $packages = $this->packages;
  126. if (!$packages) {
  127. return 0;
  128. } else {
  129. $count = 0;
  130. foreach ($packages as $package) {
  131. foreach ($package->commodities as $item) {
  132. $count++;
  133. }
  134. }
  135. return $count;
  136. }
  137. }
  138. public function delete()
  139. {
  140. $this->packages()->delete();
  141. return parent::delete();
  142. }
  143. public function deleteSafe()
  144. {
  145. return parent::delete();
  146. }
  147. public function isEquals($order)
  148. {
  149. return $this['wms_edittime'] == $order['wms_edittime']
  150. && $this['batch_id'] == $order['batch_id']
  151. && $this['frozen'] == $order['frozen'];
  152. }
  153. public function assignValueByOrder($order)
  154. {
  155. $this['code'] = $order['code'] ;
  156. $this['batch_id'] = $order['batch_id'] ;
  157. $this['warehouse_id'] = $order['warehouse_id'] ;
  158. $this['owner_id'] = $order['owner_id'] ;
  159. $this['shop_id'] = $order['shop_id'] ;
  160. $this['logistic_id'] = $order['logistic_id'] ;
  161. $this['consignee_name'] = $order['consignee_name'] ;
  162. $this['consignee_phone'] = $order['consignee_phone'] ;
  163. $this['province'] = $order['province'] ;
  164. $this['city'] = $order['city'] ;
  165. $this['district'] = $order['district'] ;
  166. $this['address'] = $order['address'] ;
  167. $this['client_code'] = $order['client_code'] ;
  168. $this['wms_status'] = $order['wms_status'] ;
  169. $this['wms_edittime'] = $order['wms_edittime'];
  170. $this['frozen'] = $order['frozen'];
  171. $this['order_type'] = $order['order_type'];
  172. $this['created_at'] =$order['created_at'];
  173. }
  174. public static function getFrozen($string): string
  175. {
  176. $arr = [
  177. 'H' => '是',
  178. 'Y' => '否',
  179. 'N' => '否'
  180. ];
  181. return $arr[$string] ?? '否';
  182. }
  183. public function OracleDOCOrderHeader(): BelongsTo
  184. {
  185. return $this->belongsTo(OracleDOCOrderHeader::class,'code','orderno');
  186. }
  187. }