| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace App\Services;
- use App\Commodity;
- use App\Http\Controllers\Controller;
- use App\InventoryCompare;
- use App\Owner;
- use App\Services\common\QueryService;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Overtrue\Pinyin\Pinyin;
- use Ramsey\Uuid\Uuid;
- class InventoryCompareService
- {
- static private $missionCode;
- private function conditionQuery($SKU,$LotAtt05,$descr_c){
- $sql='select * from (select result.*,rownum rn from (';
- $sql.=' select customer.Descr_C as 货主,storeStatus.CUSTOMERID 客户,storeStatus.LocationID 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
- $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, storeStatus.ADDTIME 创建时间, ';
- $sql.=' lot.LotAtt04 批号 ';
- $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
- $sql.=' INV_LOT_LOC_ID storeStatus';
- $sql.=' left join BAS_Customer customer on customer.CustomerID=storeStatus.CUSTOMERID ';
- $sql.=' left join BAS_SKU sku on sku.SKU=storeStatus.SKU and sku.CUSTOMERID=storeStatus.CUSTOMERID ';
- $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM = storeStatus.LOTNUM AND lot.CUSTOMERID = storeStatus.CUSTOMERID ';
- $sql.=' group by storeStatus.LocationID,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
- $sql.=' ,sku.Descr_C,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
- $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,storeStatus.CUSTOMERID,storeStatus.ADDTIME ';
- $sql.=' )result where 1=1 ';
- if ($SKU)$sql.=" and 产品编码 like '".$SKU."' ";
- if ($LotAtt05)$sql .=" and 属性仓 like '".$LotAtt05."' ";
- if ($descr_c)$sql.=" and 货主 = '".$descr_c."' ";
- $sql.=' ) ';
- return DB::connection('oracle')->select($sql);
- }
- public function getCreatingMissionCode($ownerName=''){
- if(self::$missionCode)return self::$missionCode;
- $sequence=Cache::get('InventoryCompareMissionSequence');
- if(!$sequence||$sequence>998)$sequence=0;
- $sequence++;
- Cache::put('InventoryCompareMissionSequence',$sequence,300);
- $pinyin=new Pinyin();
- $ownerFirstLetter=strtoupper($pinyin->abbr($ownerName));
- self::$missionCode= $ownerFirstLetter.date ("ymd").'KCBD'.str_pad($sequence,3,"0",STR_PAD_LEFT);
- return self::$missionCode;
- }
- public function createInventoryCompare_underImport($SKU, $customLocation, $amount, $ownerId){
- $owner=Owner::find($ownerId);
- $request=[
- '产品编码'=>$SKU,
- '属性仓'=>$customLocation,
- '数量'=>$amount,
- '货主'=>$owner['name'],
- ];
- $zpAmount=0;
- $ccAmount=0;
- $amountTotal=0;
- $creatingMissionCode = $this->getCreatingMissionCode($owner['name']);
- $commodityId=Commodity::where('sku',$SKU)->where('owner_id',$ownerId)->value('id');
- $inventoryCompare=new InventoryCompare();
- $inventoryCompare->owner_id=$ownerId;
- $inventoryCompare->commodity_id=$commodityId;
- $inventoryCompare->mission_code=Uuid::uuid1();
- $inventoryCompare->custom_location=$customLocation;
- $inventoryCompare->created_at=Carbon::now()->format('Y-m-d H:i:s');
- $inventoryCompare->quality='正品';
- $inventoryCompare->amount_in_sys=0;
- $inventoryCompare->amount_in_compare=$amount;
- $inventoryCompare->differ=0-$amount;
- $inventoryCompare->mission_code= $creatingMissionCode;
- $inventoryCompare->save();
- Controller::logS(__METHOD__,"修改库存对比任务号__".__FUNCTION__,json_encode($creatingMissionCode));
- return $inventoryCompare;
- }
- public function createInventoryCompare($SKU,$LotAtt05,$amount,$ownerId){
- $ownerName=Owner::where('id',$ownerId)->value('name');
- $request=[
- '产品编码'=>$SKU,
- '属性仓'=>$LotAtt05,
- '数量'=>$amount,
- '货主'=>$ownerId,
- ];
- $wmsInventories=$this->conditionQuery($SKU,$LotAtt05,$ownerName);
- if (count($wmsInventories)==0) return null;
- $zpAmount=0;
- $ccAmount=0;
- $amountTotal=0;
- foreach ($wmsInventories as $wmsInventory){
- $commodity=Commodity::query()->firstOrCreate([
- 'owner_id'=>$ownerId,
- 'sku'=>$wmsInventory->产品编码,
- 'name'=>$wmsInventory->商品名称,
- ]);
- Controller::logS(__METHOD__,"根据wms产品编码和货主查询或创建商品信息__".__FUNCTION__,json_encode($wmsInventory));
- $commodity->newBarcode($wmsInventory->产品条码);
- Controller::logS(__METHOD__,"根据wms产品条码和商品id查询或创建商品条码信息__".__FUNCTION__,json_encode($wmsInventory));
- $amountTotal=$amountTotal+$wmsInventory->在库数量;
- if ($wmsInventory->质量状态=='DJ'||$wmsInventory->质量状态=='ZP'){
- $zpAmount=$zpAmount+$wmsInventory->在库数量;
- }
- if ($wmsInventory->质量状态=='CC'){
- $ccAmount=$ccAmount+$wmsInventory->在库数量;
- }
- }
- $unknownStatusAmount=$amountTotal-$ccAmount-$zpAmount;
- $creatingMissionCode = $this->getCreatingMissionCode($ownerName);
- $commodityId=Commodity::where('sku',$SKU)->where('owner_id',$ownerId)->value('id');
- $inventoryCompare=new InventoryCompare();
- $inventoryCompare->owner_id=$ownerId;
- $inventoryCompare->commodity_id=$commodityId;
- $inventoryCompare->mission_code=Uuid::uuid1();
- $inventoryCompare->custom_location=$LotAtt05;
- $inventoryCompare->created_at=Carbon::now()->format('Y-m-d H:i:s');
- $inventoryCompare->mission_code=$creatingMissionCode;
- if ($zpAmount!=0){
- $inventoryCompare->quality='正品';
- $inventoryCompare->amount_in_sys=$zpAmount;
- $inventoryCompare->amount_in_compare=$amount;
- $inventoryCompare->differ=$zpAmount-$amount;
- }
- if ($ccAmount!=0){
- $inventoryCompare->quality='次品';
- $inventoryCompare['amount_in_sys']=$ccAmount;
- }
- if ($unknownStatusAmount!=0){
- $inventoryCompare->quality='未知';
- $inventoryCompare->amount_in_sys=$unknownStatusAmount;
- }
- $inventoryCompare->save();
- Controller::logS(__METHOD__,"创建库存对比__".__FUNCTION__,json_encode($request));
- Controller::logS(__METHOD__,"修改库存对比任务号__".__FUNCTION__,json_encode($creatingMissionCode));
- return $inventoryCompare;
- }
- private function conditionQueryInventoryCompare(array $param){
- $differ=$param['differ']??'';
- if ($differ=='有'){
- $inventoryCompares = InventoryCompare::query()->with(['owner','commodity'=>function($query){
- $query->with('barcodes');
- }])->where('differ','>',0)->orderByDesc('id');
- }elseif ($differ=='无'){
- $inventoryCompares = InventoryCompare::query()->with(['owner','commodity'=>function($query){
- $query->with('barcodes');
- }])->where('differ','<',0)->orderByDesc('id');
- }else{
- $inventoryCompares = InventoryCompare::query()->with(['owner','commodity'=>function($query){
- $query->with('barcodes');
- }])->orderByDesc('id');
- }
- unset($param['differ']);
- $columnQueryRules=[
- 'owner_id' => ['multi' => ','],
- 'date_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
- 'date_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
- 'mission_code' => ['timeLimit' => 20],
- ];
- $inventoryCompares = app(QueryService::class)->query($param,$inventoryCompares,$columnQueryRules);
- return $inventoryCompares;
- }
- public function getInventoryCompare(array $param){
- return $this->conditionQueryInventoryCompare($param)->paginate($param['paginate'] ?? 50);
- }
- }
|