Order.php 5.4 KB

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