StoreController.php 16 KB

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