StoreController.php 17 KB

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