diff --git a/app/common/repositories/community/CommunityRepository.php b/app/common/repositories/community/CommunityRepository.php index 29e139e5..18ea9a83 100644 --- a/app/common/repositories/community/CommunityRepository.php +++ b/app/common/repositories/community/CommunityRepository.php @@ -682,35 +682,39 @@ class CommunityRepository extends BaseRepository $entrustInfo->update($value); continue; } - $purchaseRecord = PurchaseRecord::where('unique', $value['product_attr_unique'])->find(); - $totalNumber = PurchaseRecord::where('unique', $value['product_attr_unique'])->sum('number'); - $totalSalesVolume = PurchaseRecord::where('unique', $value['product_attr_unique'])->sum('sales_volume'); - if (empty($purchaseRecord) || ($totalNumber - $totalSalesVolume) <= 0) { - throw new ValidateException('进货记录不存在或已售罄'); - } - if (($totalNumber - $totalSalesVolume) < $value['number']) { - throw new ValidateException('库存不足'); - } + + // $purchaseRecord = PurchaseRecord::where('unique', $value['product_attr_unique'])->find(); + // $totalNumber = PurchaseRecord::where('unique', $value['product_attr_unique'])->sum('number'); + // $totalSalesVolume = PurchaseRecord::where('unique', $value['product_attr_unique'])->sum('sales_volume'); + // if (empty($purchaseRecord) || ($totalNumber - $totalSalesVolume) <= 0) { + // throw new ValidateException('进货记录不存在或已售罄'); + // } + // if (($totalNumber - $totalSalesVolume) < $value['number']) { + // throw new ValidateException('库存不足'); + // } if ($value) { - $insert[] = [ - 'community_id' => $id, - 'purchase_record_id' => $purchaseRecord['id'], - 'product_id' => $purchaseRecord['product_id'], - 'product_attr_unique' => $purchaseRecord['unique'], - 'mer_id' => $purchaseRecord['mer_id'], - 'entrust_mer_id' => $entrustMerId, - 'entrust_day' => $entrustDay, - 'number' => $value['number'], - 'price' => $value['price'], - 'update_time' => date('Y-m-d H:i:s'), - ]; + $attrValue = ProductAttrValue::where('unique', $value['product_attr_unique'])->find(); + if($attrValue){ + $insert[] = [ + 'community_id' => $id, + 'purchase_record_id' => 0,//$purchaseRecord['id'], + 'product_id' => $attrValue['product_id'], + 'product_attr_unique' => $attrValue['unique'], + 'mer_id' => $attrValue['mer_id'], + 'entrust_mer_id' => $entrustMerId, + 'entrust_day' => $entrustDay, + 'number' => $value['number'], + 'price' => $value['price'], + 'update_time' => date('Y-m-d H:i:s'), + ]; + $attrValue->stock -= $value['number']; + $attrValue->save(); + } } - $purchaseRecord->product->stock -= $value['number']; - $purchaseRecord->product->save(); - $attrValue = ProductAttrValue::where('product_id', $purchaseRecord['product_id'])->where('unique', $purchaseRecord['unique'])->find(); - $attrValue->stock -= $value['number']; - $attrValue->save(); + // $purchaseRecord->product->stock -= $value['number']; + // $purchaseRecord->product->save(); + } if ($insert) { Entrust::getInstance()->insertAll($insert);