|
|
@@ -4,7 +4,9 @@ namespace App\Http\Controllers;
|
|
|
|
|
|
use App\Commodity;
|
|
|
use App\Imports\CommodityImport;
|
|
|
-use App\OracleBasSKU;
|
|
|
+use App\Services\CommodityBarcodeService;
|
|
|
+use App\Services\CommodityService;
|
|
|
+use App\Services\LogService;
|
|
|
use Exception;
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
use Illuminate\Http\Request;
|
|
|
@@ -175,27 +177,42 @@ class CommodityController extends Controller
|
|
|
|
|
|
|
|
|
public function syncOwnerCommodities($owner_id,$owner_code){
|
|
|
+ $today = date('Y-m-d H:i:s');
|
|
|
+ $codeColumn = ['alternate_sku1','alternate_sku2'];
|
|
|
$map = [];
|
|
|
$skus = [];
|
|
|
$wmsCommodities = app('oracleBasSkuService')->getOwnerCommodities(['code' => $owner_code]);
|
|
|
foreach ($wmsCommodities as $index => $wmsCommodity){
|
|
|
$map[$wmsCommodity->sku] = $index;
|
|
|
$skus[] = $wmsCommodity->sku;
|
|
|
+ $trimSku = rtrim($wmsCommodity->sku,"*");
|
|
|
+ if ($trimSku != $wmsCommodity->sku){
|
|
|
+ $skus[] = $trimSku;
|
|
|
+ $map[$trimSku] = $index;
|
|
|
+ }
|
|
|
}
|
|
|
+ /** @var CommodityService $commodityService */
|
|
|
+ $commodityService = app('commodityService');
|
|
|
+ /** @var CommodityBarcodeService $commodityBarcodeService */
|
|
|
+ $commodityBarcodeService = app('commodityBarcodeService');
|
|
|
|
|
|
- $commodities = app('commodityService')->getOwnerCommodities(['id' => $owner_id, 'sku'=>$skus]);
|
|
|
-
|
|
|
+ $commodities = $commodityService->getOwnerCommodities(['owner_id' => $owner_id, 'sku'=>$skus]);
|
|
|
$updateCommodities = [];
|
|
|
$updateCommodities[] = [
|
|
|
- 'id', 'name', 'length', 'width', 'height', 'volumn',
|
|
|
+ 'id', 'sku', 'name', 'length', 'width', 'height', 'volumn',
|
|
|
];
|
|
|
+ $barcodeMap = [];
|
|
|
+ $commoditiesId = [];
|
|
|
+ $barcodes = [];
|
|
|
foreach ($commodities as $commodity){
|
|
|
$wms = $wmsCommodities[$map[$commodity->sku]];
|
|
|
- if (($commodity->length != $wms->skulength) || ($commodity->width != $wms->skuwidth)
|
|
|
- || ($commodity->height != $wms->skuhigh) || ($commodity->volumn != $wms->cube)
|
|
|
- || ($commodity->name != $wms->descr_c)){
|
|
|
+ $trimSku = rtrim($wms->sku,"*");
|
|
|
+ if (($commodity->sku != $trimSku) || ($commodity->length != $wms->skulength)
|
|
|
+ || ($commodity->width != $wms->skuwidth) || ($commodity->name != $wms->descr_c)
|
|
|
+ || ($commodity->height != $wms->skuhigh) || ($commodity->volumn != $wms->cube)){
|
|
|
$updateCommodities[] = [
|
|
|
'id'=>$commodity->id,
|
|
|
+ 'sku'=>$trimSku,
|
|
|
'name' => $wms->descr_c,
|
|
|
'length' => $wms->skulength,
|
|
|
'width' => $wms->skuwidth,
|
|
|
@@ -203,17 +220,51 @@ class CommodityController extends Controller
|
|
|
'volumn' => $wms->cube
|
|
|
];
|
|
|
}
|
|
|
+ foreach ($codeColumn as $column){
|
|
|
+ if ($wms[$column]){
|
|
|
+ $wmsCode = rtrim($wms[$column],"*");
|
|
|
+ $barcodeMap[$wmsCode] = $commodity->id;
|
|
|
+ $barcodes[] = $wmsCode;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $commoditiesId[] = $commodity->id;
|
|
|
unset($wmsCommodities[$map[$commodity->sku]]);
|
|
|
- unset($map[$commodity->sku]);
|
|
|
+ if (isset($map[$wms->sku]))unset($map[$commodity->sku]);
|
|
|
+ if (isset($map[$trimSku]))unset($map[$trimSku]);
|
|
|
}
|
|
|
- unset($commodities);
|
|
|
- unset($skus);
|
|
|
+ unset($commodities,$skus);
|
|
|
|
|
|
- if (count($updateCommodities) > 1)app('commodityService')->batchUpdate($updateCommodities);
|
|
|
+ if (count($updateCommodities) > 1){
|
|
|
+ $commodityService->batchUpdate($updateCommodities);
|
|
|
+ LogService::log(__METHOD__,"同步商品-批量更新",json_encode($updateCommodities));
|
|
|
+ $commodityBarcodes = $commodityBarcodeService->get(['commodity_id'=>$commoditiesId, 'code'=>$barcodes]);
|
|
|
+
|
|
|
+ unset($commoditiesId,$barcodes);
|
|
|
+ foreach ($commodityBarcodes as $barcode){
|
|
|
+ if (($barcodeMap[$barcode->code] ?? false) && $barcodeMap[$barcode->code] == $barcode->commodity_id){
|
|
|
+ unset($barcodeMap[$barcode->code]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (count($barcodeMap) > 0){
|
|
|
+ $barcodeInsert = [];
|
|
|
+ foreach ($barcodeMap as $key => $value){
|
|
|
+ $barcodeInsert[] = [
|
|
|
+ 'commodity_id'=>$value,
|
|
|
+ 'code' => $key,
|
|
|
+ 'created_at' => $today
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $commodityBarcodeService->insert($barcodeInsert);
|
|
|
+ LogService::log(__METHOD__,"同步商品-录入条码",json_encode($barcodeInsert));
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
$createCommodities = [];
|
|
|
- $today = date('Y-m-d H:i:s');
|
|
|
+ $barcodeMap = [];
|
|
|
+ $skus = [];
|
|
|
+
|
|
|
foreach ($map as $sku => $index){
|
|
|
+ if (substr($sku,-1) == "*")continue;
|
|
|
$wms = $wmsCommodities[$index];
|
|
|
$createCommodities[] = [
|
|
|
'owner_id' => $owner_id,
|
|
|
@@ -225,9 +276,32 @@ class CommodityController extends Controller
|
|
|
'volumn' => $wms->cube,
|
|
|
"created_at" => $today,
|
|
|
];
|
|
|
+ $barcodeMap[$wms->sku] = [];
|
|
|
+ foreach ($codeColumn as $column){
|
|
|
+ if ($wms[$column]){
|
|
|
+ $barcodeMap[$wms->sku][] = rtrim($wms[$column],"*");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $skus[] = $wms->sku;
|
|
|
unset($wmsCommodities[$index]);
|
|
|
}
|
|
|
|
|
|
- if (count($createCommodities) > 0)app('commodityService')->insert($createCommodities);
|
|
|
+ if (count($createCommodities) > 0){
|
|
|
+ $commodityService->insert($createCommodities);
|
|
|
+ LogService::log(__METHOD__,"同步商品-录入商品",json_encode($createCommodities));
|
|
|
+ $commodities = $commodityService->get(['owner_id'=>$owner_id , 'sku'=>$skus]);
|
|
|
+ $barcodeInsert = [];
|
|
|
+ foreach ($commodities as $commodity){
|
|
|
+ foreach ($barcodeMap[$commodity->sku] as $code){
|
|
|
+ $barcodeInsert[] = [
|
|
|
+ 'commodity_id'=>$commodity->id,
|
|
|
+ 'code' => $code,
|
|
|
+ 'created_at' => $today
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $commodityBarcodeService->insert($barcodeInsert);
|
|
|
+ LogService::log(__METHOD__,"同步商品-录入条码",json_encode($barcodeInsert));
|
|
|
+ }
|
|
|
}
|
|
|
}
|