StoreController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Depository;
  4. use App\Owner;
  5. use App\Services\StoreService;
  6. use App\Store;
  7. use App\StoreItems;
  8. use App\Warehouse;
  9. use App\WMSReflectReceive;
  10. use Carbon\Carbon;
  11. use Illuminate\Contracts\Foundation\Application;
  12. use Illuminate\Contracts\View\Factory;
  13. use Illuminate\Database\Eloquent\Builder;
  14. use Illuminate\Http\RedirectResponse;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Http\Response;
  17. use Illuminate\Routing\Redirector;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Gate;
  20. use Illuminate\Support\Facades\Validator;
  21. use Illuminate\View\View;
  22. use App\Http\Controllers\Api\thirdPart\flux\StoreController as FStoreController;
  23. class StoreController extends Controller
  24. {
  25. public function storage(Request $request)
  26. {
  27. if(!Gate::allows('入库管理-入库-查询')){ return redirect(url('/')); }
  28. /** @var StoreService $storeService */
  29. $storeService=app(StoreService::class);
  30. $stores=$storeService->paginate($request->input());
  31. $warehouses=Warehouse::query()->get();
  32. $owners=app("OwnerService")->getIntersectPermitting();
  33. return view('store.inStorage.index',compact('stores','warehouses','owners'));
  34. }
  35. /**
  36. * Display a listing of the resource.
  37. *
  38. * @return Application|Factory|Response|View
  39. */
  40. public function index()
  41. {
  42. if(!Gate::allows('入库管理-快速入库-查询')){ return redirect(url('/')); }
  43. $stores=Store::query()->with(['warehouse','owner'])
  44. ->where('is_fast_stored','快速入库')
  45. ->orderBy('id','DESC')->paginate(50);
  46. return view('store.fast.index',['stores'=>$stores]);
  47. }
  48. /**
  49. * Show the form for creating a new resource.
  50. *
  51. * @return Application|Factory|Response|View
  52. */
  53. public function create()
  54. {
  55. if(!Gate::allows('入库管理-快速入库-录入')){ return redirect(url('/')); }
  56. return view('store.fast.create');
  57. }
  58. /**
  59. * Store a newly created resource in storage.
  60. *
  61. * @param Request $request
  62. * @return RedirectResponse|Response|Redirector
  63. */
  64. public function store(Request $request)
  65. {
  66. if(!Gate::allows('入库管理-快速入库-录入')){ return redirect(url('/')); }
  67. $this->validator($request);
  68. if ($request->test)$result = $this->quickStorage($request->input('asn_code'),$request->input('quality') == '正品' ? 'ZP' : 'CC',$request->input('depository_code'));
  69. else $result = $this->quickStorage_temp($request->input('asn_code'),$request->input('quality'),$request->input('depository_code'));
  70. $response = redirect('store/fast/create');
  71. if ($result['success'])return redirect('store/fast/create')->with('successTip',$result['data']);
  72. return $response->with('successError',$result['data']);
  73. }
  74. public function quickStorage_temp($asn,$quality,$depository_code){
  75. $WMSReflectReceive=WMSReflectReceive::with('skus')->where('ASNNO',$asn)->first();
  76. if (!$WMSReflectReceive)return ['success'=>false, 'data'=>"ASN编号不存在!"];
  77. $warehouse=Warehouse::query()->where('code',$WMSReflectReceive->WAREHOUSEID)->first();
  78. if (!$warehouse&&$WMSReflectReceive->WAREHOUSEID){
  79. $warehouse=new Warehouse([
  80. 'name'=>$WMSReflectReceive->WAREHOUSEID,
  81. 'code'=>$WMSReflectReceive->WAREHOUSEID
  82. ]);
  83. $warehouse->save();
  84. }
  85. $owner=Owner::query()->where('code',$WMSReflectReceive->CUSTOMERID)->first();
  86. if (!$owner&&$WMSReflectReceive->CUSTOMERID){
  87. $owner=new Owner([
  88. 'name'=>$WMSReflectReceive->CUSTOMERID,
  89. 'code'=>$WMSReflectReceive->CUSTOMERID
  90. ]);
  91. $owner->save();
  92. }
  93. $store=Store::query()->where('asn_code',$WMSReflectReceive->ASNNO)->first();
  94. if(!$store){
  95. $store=new Store([
  96. 'asn_code'=>$WMSReflectReceive->ASNNO,
  97. 'warehouse_id'=>$warehouse->id,
  98. 'owner_id'=>$owner->id,
  99. 'stored_method'=>'快速入库',
  100. 'status'=>'未入库',
  101. 'remark'=>$WMSReflectReceive->NOTES,
  102. 'is_fast_stored'=>'快速入库',
  103. ]);
  104. $store->save();
  105. $customDepository=(function()use($depository_code){
  106. $customDepository=Depository::query()->where('code',$depository_code)->first();
  107. if($depository_code){
  108. if (!$customDepository){
  109. $depository=new Depository([
  110. 'name'=>$depository_code,
  111. 'code'=>$depository_code
  112. ]);
  113. $depository->save();
  114. }
  115. }
  116. return $customDepository;
  117. })();
  118. if ($WMSReflectReceive->skus){
  119. foreach ($WMSReflectReceive->skus as $sku){
  120. $depository=(function()use($sku,$customDepository){
  121. if($customDepository)return $customDepository;
  122. $depository=Depository::query()->where('code',$sku->LOTATT05)->first();
  123. if (!$depository){
  124. if (!$sku->LOTATT05)return $depository;
  125. $depository=new Depository([
  126. 'name'=>$sku->LOTATT05,
  127. 'code'=>$sku->LOTATT05
  128. ]);
  129. $depository->save();
  130. }
  131. return $depository;
  132. })();
  133. $storeItem=new StoreItems([
  134. 'store_id'=>$store->id,
  135. 'asn_line_code'=>$sku->ASNLINENO,
  136. 'name'=>$sku->SKUDESCRC,
  137. 'sku'=>$sku->SKU,
  138. 'barcode'=>$sku->ALTERNATE_SKU1,
  139. 'quality'=>$quality,
  140. 'status'=>'未入库',
  141. ]);
  142. if ($depository)$storeItem->depository_id=$depository->id;
  143. $storeItem->save();
  144. }
  145. }
  146. }
  147. /** @var Store $store */
  148. $store=Store::with('storeItems')->where('asn_code',$asn)->first();
  149. $storeApi=new FStoreController();
  150. $result=$storeApi->accomplishToWMS($store,[
  151. 'quality'=>$quality,
  152. 'depository_code'=>$depository_code,
  153. 'follow_code'=>$store['id'],
  154. ]);
  155. if ($result){
  156. $store->status='已入库';
  157. $store->save();
  158. if ($store->storeItems){
  159. $store->storeItems->each(function ($storeItem)use($quality){
  160. $storeItem->status='已入库';
  161. $storeItem->quality=$quality;
  162. $storeItem->save();
  163. });
  164. }
  165. return ['success'=>true, 'data'=>"成功!"];
  166. }
  167. return ['success'=>false, 'data'=>"失败!请检查错误日志"];
  168. }
  169. public function quickStorage($asnno,$quality,$depository_code,$isVerification = true)
  170. {
  171. $task = Store::query()->where("asn_code",$asnno)
  172. ->where(function ($query){
  173. /** @var Builder $query */
  174. $query->where("stored_method","快速入库")
  175. ->orWhere('is_fast_stored','快速入库');
  176. })
  177. ->where("status","已入库")->first();
  178. if ($task)return ['success'=>false, 'data'=>"已被快速入库"];
  179. $db = DB::connection('oracle');
  180. $query = DB::raw("SELECT * FROM DOC_ASN_HEADER WHERE ASNNO = ?");
  181. $asn = $db->selectOne($query,[$asnno]);
  182. if (!$asn) return ['success'=>false, 'data'=>"单据号不存在"];
  183. if ($asn->asnstatus != '00')return ['success'=>false, 'data'=>'单据号状态非创建订单'];
  184. if ($isVerification){
  185. if (array_search($asn->customerid,array_values(config('stores.owners'))) === false)return ['success'=>false, 'data'=>"不允许该货主快速入库"];
  186. if (array_search($asn->asntype,array_values(config('stores.types'))) === false)return ['success'=>false, 'data'=>"该单据类型不允许被入库"];
  187. }
  188. $query = DB::raw("SELECT b.ALTERNATE_SKU1,h.WAREHOUSEID,h.asnno,d.ASNLINENO,d.SKUDESCRC,h.CUSTOMERID,d.SKU,d.PACKID,d.RECEIVEDQTY_EACH,d.EXPECTEDQTY_EACH,d.LOTATT01,d.LOTATT02,d.lotatt04,".
  189. "d.lotatt05,d.lotatt08,d.USERDEFINE1,d.USERDEFINE2,d.USERDEFINE3,d.USERDEFINE4,d.USERDEFINE5,d.RECEIVINGLOCATION FROM DOC_ASN_DETAILS d ".
  190. " LEFT JOIN BAS_SKU b ON d.CUSTOMERID = b.CUSTOMERID AND d.SKU = b.SKU INNER JOIN DOC_ASN_HEADER h ON d.ASNNO = h.ASNNO WHERE h.ASNNO = ?");
  191. $details = $db->select($query,[$asnno]);
  192. $username = config('database.connections.oracle.username');
  193. $password = config('database.connections.oracle.password');
  194. $host = config('database.connections.oracle.host');
  195. $service_name = config('database.connections.oracle.service_name');
  196. $conn = oci_connect($username, $password, $host . '/' . $service_name,"utf8");
  197. $sql_sp = "begin SPASN_Receiving_Process(:IN_Warehouse, :In_Process_Action, :In_ASNNo_C, :In_ASNLineNo_C, :In_FMTraceID_C, :In_New_TraceID_C, :In_ProductStatus," .
  198. ":In_ProductStatus_Descr, :In_HoldRejectCode_C, :In_HoldRejectReason_C, :In_PONo_C, :In_CustomerID, :In_SKU, :In_ReceivedQty, :In_RejectedQty,:In_UOM, :In_PackID," .
  199. " :In_ContainerID, :In_LotAtt01_C, :In_LotAtt02_C, :In_LotAtt03_C, :In_LotAtt04_C, :In_LotAtt05_C, :In_LotAtt06_C," .
  200. ":In_LotAtt07_C, :In_LotAtt08_C, :In_LotAtt09_C, :In_LotAtt10_C, :In_LotAtt11_C, :In_LotAtt12_C," .
  201. ":In_TotalCubic, :In_TotalGrossWeight, :In_TotalNetWeight, :In_TotalPrice, :In_UserDefine1, :In_UserDefine2,:In_UserDefine3, :In_UserDefine4, :In_UserDefine5, :In_FMLocation," .
  202. ":In_TOLocation_C,:In_QC_Type_C, :In_PlanToLoc_C,:In_ReceivingTime, :In_LPN, :In_Operator, :IN_RCVModule, :IN_RCVStation, :In_Language, :In_UserID, :OUT_Return_Code); end;";
  203. $items = [];
  204. $toDay = Carbon::now()->toDateTimeString();
  205. $depositories = [];
  206. $traceId = rand(100,999);
  207. foreach ($details as $detail) {
  208. $result = $this->executeSP($detail,$asnno,$depository_code,$db,$quality,$conn,$sql_sp,$traceId);
  209. if (substr($result, 0, 3) != '000') {
  210. oci_close($conn);
  211. app('LogService')->log(__METHOD__,"快速入库-FLUX收货失败","ASNNO:".$asnno.";ERROR:".$result);
  212. return ['success' => false, 'data' => $detail->asnlineno.'收货失败,错误代码:'.$result];
  213. }
  214. if (!isset($depositories[$detail->lotatt05]) && $detail->lotatt05){
  215. $depository = app('DepositoryService')->firstOrCreate(["code"=>$detail->lotatt05],["code"=>$detail->lotatt05,"name"=>$detail->lotatt05]);
  216. $depositories[$detail->lotatt05] = $depository->id;
  217. }
  218. $item = [
  219. "asn_line_code" => $detail->asnlineno,
  220. "name" => $detail->skudescrc,
  221. "sku" => $detail->sku,
  222. "barcode" => $detail->alternate_sku1,
  223. "amount" => $detail->expectedqty_each,
  224. "quality" => $quality == 'ZP' ? '正品' : '残次',
  225. "status" => "已入库",
  226. "created_at" => $toDay
  227. ];
  228. if (isset($depositories[$detail->lotatt05]))$item["depository_id"] = $depositories[$detail->lotatt05];
  229. $items[] = $item;
  230. }
  231. oci_close($conn);
  232. $warehouse = app('WarehouseService')->firstOrCreate(["code"=>$asn->warehouseid],["code"=>$asn->warehouseid,"name"=>$asn->warehouseid]);
  233. $owner = app('OwnerService')->firstOrCreate(['code'=>$asn->customerid],['code'=>$asn->customerid,"name"=>$asn->customerid]);
  234. $store = app('StoreService')->create([
  235. 'asn_code'=>$asnno,
  236. 'warehouse_id'=>$warehouse->id,
  237. 'owner_id'=>$owner->id,
  238. 'stored_method'=>'快速入库',
  239. 'status'=>'已入库',
  240. 'remark'=>$asn->notes,
  241. ]);
  242. app('LogService')->log(__METHOD__,"快速入库",json_encode($store));
  243. foreach ($items as &$item){
  244. $item["store_id"] = $store->id;
  245. }
  246. app('StoreItemService')->insert($items);
  247. app('LogService')->log(__METHOD__,"快速录入子项",json_encode($items));
  248. return ['success'=>true,"data"=>"已成功将“".$asnno."”入库"];
  249. }
  250. private function executeSP($detail, $asnno, $depository_code, $db, $quality, $conn, $sql_sp,$traceId){
  251. $IN_Warehouse = $detail->warehouseid ?? '';
  252. $In_Process_Action = '3';
  253. $In_ASNNo_C = $detail->asnno ?? '';
  254. $In_ASNLineNo_C = $detail->asnlineno ?? '';
  255. $In_FMTraceID_C = '';
  256. $In_New_TraceID_C = $traceId;
  257. $In_ProductStatus = '00';
  258. $In_ProductStatus_Descr = '正常';
  259. $In_HoldRejectCode_C = 'OK';
  260. $In_HoldRejectReason_C = '正常';
  261. $In_PONo_C = '';
  262. $In_CustomerID = $detail->customerid ?? '';
  263. $In_SKU = $detail->sku ?? '';
  264. $In_ReceivedQty = (string)((int)$detail->expectedqty_each - (int)$detail->receivedqty_each) ?? '';
  265. $In_RejectedQty = '';
  266. $In_UOM = 'EA';
  267. $In_PackID = $detail->packid ?? '';
  268. $In_ContainerID = '';
  269. $In_LotAtt01_C = $detail->lotatt01 ?? '';
  270. $In_LotAtt02_C = $detail->lotatt02 ?? '';
  271. $In_LotAtt03_C = '';
  272. $In_LotAtt04_C = $detail->lotatt04 ?? '';
  273. $In_LotAtt05_C = $detail->lotatt05 ?? '';
  274. $In_LotAtt06_C = '';
  275. $In_LotAtt07_C = '';
  276. $In_LotAtt08_C = $detail->lotatt08 ?? '';
  277. $In_LotAtt09_C = '';
  278. $In_LotAtt10_C = '';
  279. $In_LotAtt11_C = '';
  280. $In_LotAtt12_C = '';
  281. $In_TotalCubic = '0.00';
  282. $In_TotalGrossWeight = '0.00';
  283. $In_TotalNetWeight = '0.00';
  284. $In_TotalPrice = '0.00';
  285. $In_UserDefine1 = $detail->userdefine1 ?? '';
  286. $In_UserDefine2 = $detail->userdefine2 ?? '';
  287. $In_UserDefine3 = $detail->userdefine3 ?? '';
  288. $In_UserDefine4 = $detail->userdefine4 ?? '';
  289. $In_UserDefine5 = $detail->userdefine5 ?? '';
  290. $In_FMLocation = 'STAGE' . $detail->warehouseid;
  291. $In_TOLocation_C = 'STAGE' . $detail->warehouseid;
  292. $In_QC_Type_C = 'OK';
  293. $In_PlanToLoc_C = '';
  294. $In_ReceivingTime = '';
  295. $In_LPN = '*';
  296. $In_Operator = 'WCS';
  297. $IN_RCVModule = '';
  298. $IN_RCVStation = '';
  299. $In_Language = 'cn';
  300. $In_UserID = 'WCS';
  301. $result = '';
  302. if ($depository_code && (strtoupper($depository_code) != strtoupper($detail->receivinglocation))){
  303. $query = DB::raw("UPDATE DOC_ASN_DETAILS SET RECEIVINGLOCATION = ? WHERE ASNNO = ? AND ASNLINENO = ?");
  304. $db->update($query,[$depository_code,$detail->asnno,$detail->asnlineno]);
  305. $db->commit();
  306. app('LogService')->log(__METHOD__,"快速入库-修改FULX属性仓","ASNNO:".$asnno.";原仓:".$detail->receivinglocation.";修改为:".$depository_code);
  307. }
  308. if ($quality && ($quality != $detail->lotatt08)){
  309. $query = DB::raw("UPDATE DOC_ASN_DETAILS SET LOTATT08 = ? WHERE ASNNO = ? AND ASNLINENO = ?");
  310. $db->update($query,[$quality,$detail->asnno,$detail->asnlineno]);
  311. $db->commit();
  312. app('LogService')->log(__METHOD__,"快速入库-修改FULX质量状态","ASNNO:".$asnno.";原质量:".$detail->lotatt08.";修改为:".$quality);
  313. $In_LotAtt08_C = $quality;
  314. }
  315. $stmt = oci_parse($conn, $sql_sp);
  316. oci_bind_by_name($stmt, ':IN_Warehouse', $IN_Warehouse);
  317. oci_bind_by_name($stmt, ':In_Process_Action', $In_Process_Action);
  318. oci_bind_by_name($stmt, ':In_ASNNo_C', $In_ASNNo_C);
  319. oci_bind_by_name($stmt, ':In_ASNLineNo_C', $In_ASNLineNo_C);
  320. oci_bind_by_name($stmt, ':In_FMTraceID_C', $In_FMTraceID_C);
  321. oci_bind_by_name($stmt, ':In_New_TraceID_C', $In_New_TraceID_C);
  322. oci_bind_by_name($stmt, ':In_ProductStatus', $In_ProductStatus);
  323. oci_bind_by_name($stmt, ':In_ProductStatus_Descr', $In_ProductStatus_Descr);
  324. oci_bind_by_name($stmt, ':In_HoldRejectCode_C', $In_HoldRejectCode_C);
  325. oci_bind_by_name($stmt, ':In_HoldRejectReason_C', $In_HoldRejectReason_C);
  326. oci_bind_by_name($stmt, ':In_PONo_C', $In_PONo_C);
  327. oci_bind_by_name($stmt, ':In_CustomerID', $In_CustomerID);
  328. oci_bind_by_name($stmt, ':In_SKU', $In_SKU);
  329. oci_bind_by_name($stmt, ':In_ReceivedQty', $In_ReceivedQty);
  330. oci_bind_by_name($stmt, ':In_RejectedQty', $In_RejectedQty);
  331. oci_bind_by_name($stmt, ':In_UOM', $In_UOM);
  332. oci_bind_by_name($stmt, ':In_PackID', $In_PackID);
  333. oci_bind_by_name($stmt, ':In_ContainerID', $In_ContainerID);
  334. oci_bind_by_name($stmt, ':In_LotAtt01_C', $In_LotAtt01_C);
  335. oci_bind_by_name($stmt, ':In_LotAtt02_C', $In_LotAtt02_C);
  336. oci_bind_by_name($stmt, ':In_LotAtt03_C', $In_LotAtt03_C);
  337. oci_bind_by_name($stmt, ':In_LotAtt04_C', $In_LotAtt04_C);
  338. oci_bind_by_name($stmt, ':In_LotAtt05_C', $In_LotAtt05_C);
  339. oci_bind_by_name($stmt, ':In_LotAtt06_C', $In_LotAtt06_C);
  340. oci_bind_by_name($stmt, ':In_LotAtt07_C', $In_LotAtt07_C);
  341. oci_bind_by_name($stmt, ':In_LotAtt08_C', $In_LotAtt08_C);
  342. oci_bind_by_name($stmt, ':In_LotAtt09_C', $In_LotAtt09_C);
  343. oci_bind_by_name($stmt, ':In_LotAtt10_C', $In_LotAtt10_C);
  344. oci_bind_by_name($stmt, ':In_LotAtt11_C', $In_LotAtt11_C);
  345. oci_bind_by_name($stmt, ':In_LotAtt12_C', $In_LotAtt12_C);
  346. oci_bind_by_name($stmt, ':In_TotalCubic', $In_TotalCubic);
  347. oci_bind_by_name($stmt, ':In_TotalGrossWeight', $In_TotalGrossWeight);
  348. oci_bind_by_name($stmt, ':In_TotalNetWeight', $In_TotalNetWeight);
  349. oci_bind_by_name($stmt, ':In_TotalPrice', $In_TotalPrice);
  350. oci_bind_by_name($stmt, ':In_UserDefine1', $In_UserDefine1);
  351. oci_bind_by_name($stmt, ':In_UserDefine2', $In_UserDefine2);
  352. oci_bind_by_name($stmt, ':In_UserDefine3', $In_UserDefine3);
  353. oci_bind_by_name($stmt, ':In_UserDefine4', $In_UserDefine4);
  354. oci_bind_by_name($stmt, ':In_UserDefine5', $In_UserDefine5);
  355. oci_bind_by_name($stmt, ':In_FMLocation', $In_FMLocation);
  356. oci_bind_by_name($stmt, ':In_TOLocation_C', $In_TOLocation_C);
  357. oci_bind_by_name($stmt, ':In_QC_Type_C', $In_QC_Type_C);
  358. oci_bind_by_name($stmt, ':In_PlanToLoc_C', $In_PlanToLoc_C);
  359. oci_bind_by_name($stmt, ':In_ReceivingTime', $In_ReceivingTime);
  360. oci_bind_by_name($stmt, ':In_LPN', $In_LPN);
  361. oci_bind_by_name($stmt, ':In_Operator', $In_Operator);
  362. oci_bind_by_name($stmt, ':IN_RCVModule', $IN_RCVModule);
  363. oci_bind_by_name($stmt, ':IN_RCVStation', $IN_RCVStation);
  364. oci_bind_by_name($stmt, ':In_Language', $In_Language);
  365. oci_bind_by_name($stmt, ':In_UserID', $In_UserID);
  366. oci_bind_by_name($stmt, ':OUT_Return_Code', $result,300);
  367. oci_execute($stmt);
  368. return $result;
  369. }
  370. public function validator(Request $request){
  371. $validator=Validator::make($request->input(),[
  372. 'asn_code'=>['required'],
  373. 'quality'=>['required'],
  374. 'depository_code'=>['nullable','string'],
  375. ],[
  376. 'required'=>':attribute 为必填项',
  377. 'unique'=>':attribute 已存在',
  378. ],[
  379. 'asn_code'=>'ASN编号',
  380. 'quality'=>'货物类型'
  381. ])->validate();
  382. return $validator;
  383. }
  384. }