| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- namespace App;
- use Illuminate\Database\Eloquent\Model;
- use App\Traits\ModelTimeFormat;
- class Package extends Model
- {
- use ModelTimeFormat;
- protected $fillable=[
- 'owner_id','logistic_number','delivery_number','batch_number','batch_rule','recipient','recipient_mobile','logistic_id',
- 'measuring_machine_id','weight','length','width','height','bulk','paper_box_id','status','weighed_at','order_code'
- ];
- protected $appends=[
- 'owner_name',
- 'logistic_name',
- 'measuringMachine_name',
- 'measuring_machine_name',
- 'measuringMachine_status',
- 'paperBox_name',
- 'WMSReflectPackage_name'
- ];
- public function owner(){
- return $this->belongsTo('App\Owner','owner_id','id');
- }
- public function logistic(){
- return $this->belongsTo('App\Logistic','logistic_id','id');
- }
- public function measuringMachine(){
- return $this->belongsTo('App\MeasuringMachine','measuring_machine_id','id');
- }
- public function paperBox(){
- return $this->belongsTo('App\PaperBox','paper_box_id','id');
- }
- public function WMSReflectPackage(){
- return $this->hasOne('App\WMSReflectPackage','SOReference5','logistic_number');
- }
- public function save(array $options=[]){
- unset($this->_temOracleInfo);
- unset($this->temOwner);
- unset($this->temLogistic);
- parent::save($options);
- }
- public function isActivityBatch(){
- return ($this['batch_rule'] && strstr($this['batch_rule'],'组合') && $this['batch_number'] );
- }
- public function unifyThisMeasureUnderBatch(){
- $this->fetchPaperBox();
- Package::where(['batch_number'=>$this['batch_number']])->update(['weight'=>$this['weight']??'',
- 'length'=>$this['length']??'',
- 'width'=>$this['width']??'',
- 'height'=>$this['height']??'',
- 'paper_box_id'=>$this['paper_box_id']??'']);
- }
- public function fetchLogisticFromOracle(){
- if(!$this['oracleInfo'])return null;
- if($this['temLogistic'])return $this['temLogistic'];
- $logistic= Logistic::where('code',$this['oracleInfo']['SOReference5'])->first();
- if(!$logistic){
- Logistic::create(['code'=>$this['oracleInfo']['SOReference5'],'name'=>$this['oracleInfo']['SOReference5']]);
- $this->log(__METHOD__, __FUNCTION__, "富勒下发找不到快递公司,添加{$this['oracleInfo']['customerid']}" , null);
- }
- if(!$logistic)return null;
- $this['temLogistic']=$logistic;
- $this['logistic_id'] = $logistic['id'];
- return $logistic;
- }
- public function fetchOwnerFromOracle(){
- if(!$this['oracleInfo'])return null;
- if($this['temOwner'])return $this['temOwner'];
- $owner= Owner::where('code',$this['oracleInfo']['customerid'])->first();
- if(!$owner){
- Owner::create(['code'=>$this['oracleInfo']['customerid'],'name'=>$this['oracleInfo']['customerid']]);
- $this->log(__METHOD__, __FUNCTION__, "富勒下发找不到货主,添加{$this['oracleInfo']['customerid']}" , null);
- }
- if(!$owner)return null;
- $this['temOwner']=$owner;
- $this['owner_id'] = $owner['id'];
- return $owner;
- }
- public function fetchAllFromOracle(){
- if(!$this['oracleInfo'])return null;
- $this->fetchOwnerFromOracle();
- $this->fetchLogisticFromOracle();
- $this['recipient'] = $this['oracleInfo']['Consigneename'];
- if($this['oracleInfo']['SOReference5'])
- $this['logistic_number'] = $this['oracleInfo']['SOReference5'];
- $this['batch_rule'] = isset($this['oracleInfo']['userdefine2'])?$this['oracleInfo']['userdefine2']:'';
- }
- public function getOracleInfoAttribute()
- {
- if(!$this['logistic_number'])return '';
- if(isset($this->_temOracleInfo)&&$this->_temOracleInfo)return $this->_temOracleInfo;
- $allocationDetail=OracleActAllocationDetails::select([
- 'doc_order_header.userdefine1',
- 'doc_order_header.userdefine2',
- 'doc_order_header.SOReference5',
- 'doc_order_header.orderno',
- 'doc_order_header.customerid',
- 'doc_order_header.Consigneename'
- ])
- ->where('picktotraceid',$this['logistic_number'])
- ->leftJoin('DOC_Order_Header','act_allocation_details.orderno','doc_order_header.orderno');
- $this->_temOracleInfo=$allocationDetail->first();
- return $this->_temOracleInfo;
- }
- public function getOwnerNameAttribute()
- {
- return $this['owner']? $this['owner']['name']:null;
- }
- public function getLogisticNameAttribute(){
- return $this['logistic']? $this['logistic']['name']:null;
- }
- public function getMeasuringMachineNameAttribute(){
- return $this['measuringMachine']? $this['measuringMachine']['name']:null;
- }
- public function getMeasuringMachineStatusAttribute(){
- return $this['measuringMachine']? $this['measuringMachine']['status']:null;
- }
- public function getPaperBoxNameAttribute(){
- return $this['paperBox']? $this['paperBox']['model']:null;
- }
- public function getWMSReflectPackageNameAttribute(){
- return $this['WMSReflectPackage']? $this['WMSReflectPackage']['TASKID']:null;
- }
- //寻找相近纸箱ID
- public function fetchPaperBox($max, $centre, $min, $owner_id=null){
- if($this['paper_box_id'])return $this['paper_box_id'];
- $sumDiffer=0;
- $maxDiffer=0;
- $paperBox_id=null;
- if(!$owner_id) $owner_id = $this['owner_id'];
- if(!$owner_id) {
- $owner = $this->fetchOwnerFromOracle();
- $owner_id = $owner['id'];
- if(!$owner_id)return null;
- }
- $boxes=Owner::select('id')->with('paperBoxes')->find($owner_id);
- $targetPaperBox=null;
- foreach ($boxes->paperBoxes as $i=>$paperBox){
- if ($paperBox->length==$max&&$paperBox->width==$centre&&$paperBox->height==$min){
- $targetPaperBox=$paperBox;
- break;
- }
- $lengthDiffer=abs($paperBox->length-$max);
- $widthDiffer=abs($paperBox->width-$centre);
- $heightDiffer=abs($paperBox->height-$min);
- $thisMaxDiffer=($lengthDiffer>=($widthDiffer>=$heightDiffer?$widthDiffer:$heightDiffer)?$lengthDiffer:($widthDiffer>=$heightDiffer?$widthDiffer:$heightDiffer));
- if($i==0){
- $maxDiffer=$thisMaxDiffer;
- $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
- $targetPaperBox=$paperBox;
- }
- if ($thisMaxDiffer==$maxDiffer){
- if($sumDiffer>($lengthDiffer+$widthDiffer+$heightDiffer)){
- $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
- $targetPaperBox=$paperBox;
- }
- }
- if ($thisMaxDiffer<$maxDiffer){
- $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
- $maxDiffer=$thisMaxDiffer;
- $targetPaperBox=$paperBox;
- }
- }
- if($targetPaperBox)$this['paper_box_id']=$targetPaperBox['id'];
- return $targetPaperBox['id'];
- }
- }
|