StoreController.php 18 KB

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