Order.php 5.6 KB

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