|
|
@@ -39,23 +39,21 @@ class OrderIssue extends Model
|
|
|
* finance_confirm 财务确认
|
|
|
* hidden_tag 隐藏标识
|
|
|
*/
|
|
|
- protected $appends = [
|
|
|
-// 'secondLogisticNumber',
|
|
|
-// 'createLog',
|
|
|
-// 'endLog',
|
|
|
-// 'processingTime',
|
|
|
-// 'createUser',
|
|
|
-// 'endUser',
|
|
|
- ];
|
|
|
+ protected $appends = [];
|
|
|
|
|
|
public function order()
|
|
|
{
|
|
|
return $this->belongsTo(Order::class, 'order_id', 'id');
|
|
|
}
|
|
|
|
|
|
- public function rejectedBill()
|
|
|
+ public function rejectedBills()
|
|
|
{
|
|
|
- return $this->belongsTo(RejectedBill::class, 'logistic_number_return', 'logistic_number_return');
|
|
|
+ return $this->belongsToMany(RejectedBill::class,'order_issue_rejected_bill','order_issue_id','logistic_number_return','id','logistic_number_return');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function orderIssueRejectedBills()
|
|
|
+ {
|
|
|
+ return $this->hasMany(OrderIssueRejectedBill::class);
|
|
|
}
|
|
|
|
|
|
public function issueType()
|
|
|
@@ -83,11 +81,6 @@ class OrderIssue extends Model
|
|
|
return $this->hasOne('App\Order', 'client_code', 'second_client_no');
|
|
|
}
|
|
|
|
|
|
-// public function getSecondLogisticNumberAttribute()
|
|
|
-// {
|
|
|
-// return $this['secondOrder']['code'] ?? '';
|
|
|
-// }
|
|
|
-
|
|
|
public function getCreateLogAttribute()
|
|
|
{
|
|
|
return $this->logs->where('type', '创建')->first();
|
|
|
@@ -217,7 +210,109 @@ class OrderIssue extends Model
|
|
|
$this->update(['rejecting_status' => '差异退回']);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 同步退货状态
|
|
|
+ */
|
|
|
+ public function syncRejectingStatus()
|
|
|
+ {
|
|
|
+ if(!$this->order_id)return;
|
|
|
+ $rejectedItems = [];
|
|
|
+ $orderItems = [];
|
|
|
+ $rejectedBills = $this->rejectedBills;
|
|
|
+ if(!$rejectedBills)return;
|
|
|
+ $rejectedBillItems = RejectedBillItem::query()->with('quality')
|
|
|
+ ->whereIn('id_rejected_bill',function($query)use($rejectedBills){
|
|
|
+ $query->from('rejected_bills')->select('id')->whereIn('logistic_number_return',$rejectedBills->map(function($rejectedBil){
|
|
|
+ return $rejectedBil->logistic_number_return;
|
|
|
+ }));
|
|
|
+ })->get();
|
|
|
|
|
|
+ if($rejectedBillItems->count()===0){
|
|
|
+ $this->update(['rejecting_status' => '未退回']);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($rejectedBillItems->where('quality.name','残次')->count()>0){
|
|
|
+ $this->update(['rejecting_status' => '差异退回']);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $barcodeGoods = data_get($rejectedBillItems,'*.barcode_goods');
|
|
|
+ $commodityBarcodes = CommodityBarcode::query()->with('commodity')->whereIn('code',$barcodeGoods)->get();
|
|
|
+ $commodities = Commodity::query()->whereIn('sku',$barcodeGoods)->get();
|
|
|
+
|
|
|
+ foreach ($rejectedBillItems as $item) {
|
|
|
+ $barcode_goods = (string)$item->barcode_goods;
|
|
|
+ $commodity = $commodities->where('sku',$barcode_goods)->first();
|
|
|
+ if(!$commodity){
|
|
|
+ $commodityBarcode = $commodityBarcodes->where('code',$barcode_goods)->first();
|
|
|
+ $barcode_goods = $commodityBarcode->commodity->sku ?? $barcode_goods;
|
|
|
+ }
|
|
|
+ if (!isset($rejectedItems[$barcode_goods])){
|
|
|
+ $rejectedItems[$barcode_goods] = 0;
|
|
|
+ }
|
|
|
+ $rejectedItems[(string)$barcode_goods] += $item->amount;
|
|
|
+ }
|
|
|
+
|
|
|
+ $items = OrderPackageCommodities::query()->with('commodity')
|
|
|
+ ->whereIn('order_package_id',function($query){
|
|
|
+ $query->from('order_packages')->selectRaw('id')->where('order_id',$this->order_id);
|
|
|
+ })->get();
|
|
|
+
|
|
|
+ if(count($items) == 0){
|
|
|
+ $this->update(['rejecting_status' => '无']);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (count($items) > 0) {
|
|
|
+ foreach ($items as $item) {
|
|
|
+ $sku = $item['commodity']['sku'];
|
|
|
+ if (! isset($orderItems[$sku]) ?? false){
|
|
|
+ $orderItems[$sku] = 0;
|
|
|
+ }
|
|
|
+ $orderItems[$sku] += $item->amount;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $this->update(['rejecting_status' => '无']);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $rejectedExcess = 0; // 退回差异
|
|
|
+ $rejectedReview = 0; // 退回复核 $rejectedItems == $orderItems
|
|
|
+ foreach ($rejectedItems as $key => $items) {
|
|
|
+ if ($orderItems[$key] ?? false) {
|
|
|
+ if ($rejectedItems[$key] == $orderItems[$key])
|
|
|
+ $rejectedReview++;
|
|
|
+ } else
|
|
|
+ $rejectedExcess++;
|
|
|
+ }
|
|
|
+ if ($rejectedExcess > 0) {
|
|
|
+ $this->update(['rejecting_status' => '差异退回']);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 全部退回 部分退回 超量退回 差异退回 未退回 无
|
|
|
+ $isExcess = 0; // 超量 $orderItems < $rejectedItems
|
|
|
+ $isDiff = 0; // 部分 $orderItems > $rejectedItems
|
|
|
+ $isAccord = 0; // 相同 $orderItems == $rejectedItems
|
|
|
+ $isLack = 0; // 缺少 $orderItems != $rejectedItems
|
|
|
+ foreach ($orderItems as $key => $item) {
|
|
|
+ if ($rejectedItems[$key] ?? false) {
|
|
|
+ if ($orderItems[$key] < $rejectedItems[$key])
|
|
|
+ $isExcess++;
|
|
|
+ else if ($orderItems[$key] > $rejectedItems[$key])
|
|
|
+ $isDiff++;
|
|
|
+ else if ($orderItems[$key] == $rejectedItems[$key])
|
|
|
+ $isAccord++;
|
|
|
+ } else
|
|
|
+ $isLack++;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($isAccord == $rejectedReview && $isLack == 0 && $isExcess == 0 && $isDiff==0 && $rejectedExcess == 0 )
|
|
|
+ $this->update(['rejecting_status' => '全部退回']);
|
|
|
+ else if($isExcess > 0 && $isAccord == $rejectedReview && $isDiff == 0 && $isLack == 0)
|
|
|
+ $this->update(['rejecting_status' => '超量退回']);
|
|
|
+ else if($isDiff >=0 && $isAccord == $rejectedReview && $isLack >=0 && $rejectedExcess ==0 && $isExcess==0)
|
|
|
+ $this->update(['rejecting_status' => '部分退回']);
|
|
|
+ else if($isLack >= 0 && $rejectedExcess>=0 && $isDiff>=0 && $isLack>=0 && $isExcess>=0 && $rejectedReview>=0)
|
|
|
+ $this->update(['rejecting_status' => '差异退回']);
|
|
|
+ }
|
|
|
|
|
|
public function delete()
|
|
|
{
|
|
|
@@ -231,16 +326,46 @@ class OrderIssue extends Model
|
|
|
return parent::delete();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- // 同步退回单
|
|
|
- public function syncRejectedBill()
|
|
|
+ /**
|
|
|
+ * 同步退货单号
|
|
|
+ */
|
|
|
+ public function syncRejectedBills()
|
|
|
{
|
|
|
- if(!isset($this['logistic_number_return'])){
|
|
|
- $order = $this->order;
|
|
|
- $rejectedBill = RejectedBill::query()->where('logistic_number','like','原单退回'.'%')->where('order_number',$order->client_code)->whereNotNull('order_number')->first();
|
|
|
- if($rejectedBill){
|
|
|
- $this->update(['logistic_number_return'=>$rejectedBill->logistic_number_return,'is_new_rejecting'=>'有']);
|
|
|
+ $order_packages = $this->order->packages;
|
|
|
+ if($order_packages){
|
|
|
+ foreach ($order_packages as $order_package) {
|
|
|
+ $logistic_number = $order_package->logistic_number;
|
|
|
+ $rejectedBill = RejectedBill::query()->where('logistic_number_return',$logistic_number)->first();
|
|
|
+ if($rejectedBill){
|
|
|
+ if(OrderIssueRejectedBill::isExit($this->id,$logistic_number))continue;
|
|
|
+ $this->joinRejectedBill($logistic_number);
|
|
|
+ $this->is_new_rejecting = '有';
|
|
|
+ }
|
|
|
}
|
|
|
+ $this->save();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param array|string $logistic_number
|
|
|
+ * @return array|void
|
|
|
+ */
|
|
|
+ public function joinRejectedBill($logistic_number)
|
|
|
+ {
|
|
|
+ if(!$logistic_number)return null;
|
|
|
+ if(is_array($logistic_number)){
|
|
|
+ return $this->rejectedBills()->sync($logistic_number,false);
|
|
|
}
|
|
|
+ return $this->rejectedBills()->attach($logistic_number);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function unJoinRejectedBill($logistic_number)
|
|
|
+ {
|
|
|
+ return $this->rejectedBills()->detach($logistic_number);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function reviseJoinRejectedBill($logistic_number,$logistic_number_update)
|
|
|
+ {
|
|
|
+ return $this->rejectedBills()->updateExistingPivot($logistic_number,['logistic_number_return'=>$logistic_number_update]);
|
|
|
}
|
|
|
}
|