InventoryAccountController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exports\Export;
  4. use App\InventoryAccount;
  5. use App\InventoryAccountMission;
  6. use App\Owner;
  7. use App\Services\InventoryAccountService;
  8. use App\Services\OwnerService;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Auth;
  11. use Illuminate\Support\Facades\Gate;
  12. use Maatwebsite\Excel\Facades\Excel;
  13. class InventoryAccountController extends Controller
  14. {
  15. public function __construct()
  16. {
  17. app()->singleton('inventoryAccountService',InventoryAccountService::class);
  18. }
  19. //创建盘点任务
  20. public function createStockInventoryMission(Request $request){
  21. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  22. // $date_start=$request->input('formData.date_start');
  23. // $date_end=$request->input('formData.date_end');
  24. // $ownerId=$request->input('formData.owner_id')[0];
  25. $date_start=$request->input('date_start');
  26. $date_end=$request->input('date_end');
  27. $ownerId=$request->input('owner_id');
  28. $inventoryAccount=app('inventoryAccountService')->createMission($date_start,$date_end,$ownerId);
  29. $inventoryAccount=InventoryAccount::with('owner')->find($inventoryAccount->id);
  30. if (is_null($inventoryAccount)) return ['success'=>false,'data'=>'参数错误!'];
  31. return ['success'=>true,'data'=>$inventoryAccount];
  32. }
  33. //删除盘点任务
  34. public function deleteStockInventoryMission($id){
  35. if(!Gate::allows('库存管理-盘点')){return['success'=>0,'status'=>'没有权限'];}
  36. if(is_null($id)){return ['success'=>false,'data'=>'传入id为空'];}
  37. $inventoryAccount=InventoryAccount::where('id',$id)->delete();
  38. return ['success'=>true,'data'=>$inventoryAccount];
  39. }
  40. //盘点-任务页面
  41. public function mission(Request $request,OwnerService $ownerService){
  42. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  43. $paginateParams = $request->input();
  44. $queryParam=$request->all();
  45. $inventoryAccounts=app('inventoryAccountService')->paginate($queryParam);
  46. $owners=$ownerService->getSelection();
  47. return view('inventory.stockInventory.mission',compact('owners','inventoryAccounts','paginateParams'));
  48. }
  49. //进入盘点中或复盘页面
  50. public function enterStockInventory($id,Request $request){
  51. if($request->input('listMode') ?? false){
  52. if (!Gate::allows('库存管理-盘点-查看')){return redirect(url('/')); }
  53. }else{
  54. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  55. }
  56. if (!$id) return ['success'=>false,'data'=>'参数错误!'];
  57. $inventoryAccount=InventoryAccount::with('owner')->find($id);
  58. $inventoryAccountMissions=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->where('inventory_account_id',$id)->orderBy('difference_amount','desc')->get();
  59. return view('inventory.stockInventory.inventoryMission',compact('inventoryAccount','inventoryAccountMissions'));
  60. }
  61. //依据盘点任务id进行 --盘点
  62. public function stockInventory(Request $request){
  63. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  64. $location=$request->input('location');
  65. $barcode=$request->input('barcode');
  66. $inventoryId=$request->input('inventoryId');
  67. $count=$request->input('count');
  68. if (is_null($count)) return ['success'=>false,'data'=>'盘点数不能为空!'];
  69. /** @var InventoryAccountService $inventoryAccountMission */
  70. $inventoryAccountService=app('inventoryAccountService');
  71. $inventoryAccountMission=$inventoryAccountService->stockInventory($location,$barcode,$count,$inventoryId);
  72. if (!$inventoryAccountMission)return ['success'=>false,'data'=>'参数错误!'];
  73. /** @var InventoryAccountService $inventoryService */
  74. $inventoryService=app('inventoryAccountService');
  75. $inventoryAccount=$inventoryService->updateInventory($inventoryId);
  76. $stockInventoryPersons=$inventoryAccountMission->stockInventoryPersons;
  77. return ['success'=>true,'inventoryMission'=>$inventoryAccountMission,'inventory'=>$inventoryAccount,'stockInventoryPersons'=>$stockInventoryPersons];
  78. }
  79. //根据该库存和产品条码查询该条盘点记录
  80. public function searchStockInventoryRecord(Request $request){
  81. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  82. $location=$request->input('location');
  83. $barcode=$request->input('barcode');
  84. $inventoryId=$request->input('inventoryId');
  85. $inventoryAccountMission=app('inventoryAccountService')->searchStockInventoryRecord($location,$barcode,$inventoryId);
  86. if (!$inventoryAccountMission)return ['success'=>false,'data'=>'参数错误!'];
  87. $stockInventoryPersons=$inventoryAccountMission->stockInventoryPersons;
  88. return ['success'=>true,'data'=>$inventoryAccountMission,'stockInventoryPersons'=>$stockInventoryPersons];
  89. }
  90. //盘点任务导出
  91. public function stockInventoryExport(Request $request){
  92. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  93. ini_set('max_execution_time',3500);
  94. ini_set('memory_limit','3526M');
  95. if ($request->checkAllSign){
  96. $request->offsetUnset('checkAllSign');
  97. $queryParam=$request->all();
  98. $inventoryAccounts=app('inventoryAccountService')->get($queryParam);
  99. }else{
  100. $queryParam=$request->all();
  101. $inventoryAccounts=app('inventoryAccountService')->some($queryParam);
  102. }
  103. $row=[[
  104. 'id'=>'盘点编号',
  105. 'status'=>'盘点状态',
  106. 'created_at'=>'创建时间',
  107. 'owner_id'=>'货主',
  108. 'type'=>'任务类型',
  109. 'start_at'=>'起始时间',
  110. 'end_at'=>'结束时间',
  111. 'total'=>'记录数',
  112. 'processed'=>'已盘数',
  113. 'surplus'=>'剩余数',
  114. 'difference'=>'复盘差异',
  115. 'returned'=>'复盘归位',
  116. ]];
  117. $list=[];
  118. for ($i=0; $i<count($inventoryAccounts);$i++){
  119. $inventoryAccount=$inventoryAccounts[$i];
  120. $w=[
  121. 'id'=>isset($inventoryAccount->id)?$inventoryAccount->id:'',
  122. 'status'=>isset($inventoryAccount->status)?$inventoryAccount->status:'',
  123. 'created_at'=>isset($inventoryAccount->created_at)?$inventoryAccount->created_at:'',
  124. 'owner_id'=>isset($inventoryAccount->owner->name)?$inventoryAccount->owner->name:'',
  125. 'type'=>isset($inventoryAccount->type)?$inventoryAccount->type:'',
  126. 'start_at'=>isset($inventoryAccount->start_at)?$inventoryAccount->start_at:'',
  127. 'end_at'=>isset($inventoryAccount->end_at)?$inventoryAccount->end_at:'',
  128. 'total'=>isset($inventoryAccount->total)?$inventoryAccount->total:'',
  129. 'processed'=>isset($inventoryAccount->processed)?$inventoryAccount->processed:'',
  130. 'surplus'=>isset($inventoryAccount->surplus)?$inventoryAccount->surplus:'',
  131. 'difference'=>isset($inventoryAccount->difference)?$inventoryAccount->difference:'',
  132. 'returned'=>isset($inventoryAccount->returned)?$inventoryAccount->returned:'',
  133. ];
  134. $list[$i]=$w;
  135. }
  136. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-盘点任务记录单.xlsx');
  137. }
  138. public function stockInventoryEnd(Request $request){
  139. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  140. $id=$request->input('id');
  141. if (!$id) return ['success'=>false,'data'=>'参数错误!'];
  142. $inventoryAccount=InventoryAccount::query()->where('id',$id)->update(['status'=>'复盘中']);
  143. $this->log(__METHOD__,'结束初盘任务'.__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  144. if ($inventoryAccount>0) return ['success'=>true,'data'=>'复盘中'];
  145. return ['success'=>false,'data'=>'参数错误!'];
  146. }
  147. public function syncOwners(OwnerService $ownerService){
  148. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  149. $owners=$ownerService->syncOwnersData();
  150. if (!$owners)return ['success'=>false,'data'=>'同步货主失败!'];
  151. return ['success'=>true,'data'=>$owners];
  152. }
  153. public function 修改质量状态(Request $request){
  154. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  155. $id=$request->input('id');
  156. $location=$request->location;
  157. $sku=$request->sku;
  158. $quality=$request->quality;
  159. $ownerCode=$request->ownerCode;
  160. $inventoryAccountMission=app('inventoryAccountService')->修改质量状态($id,$location,$sku,$quality,$ownerCode);
  161. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  162. if ($inventoryAccountMission==null) return ['success'=>false,'data'=>'WMS中不存在该条记录!'];
  163. return ['success'=>true,'data'=>'质量状态修改成功'];
  164. }
  165. public function 完结盘点任务($id){
  166. if(!Gate::allows('库存管理-盘点-完结')){return['success'=>false,'status'=>'没有权限'];}
  167. if (!$id)return['success'=>false,'status'=>'参数错误!'];
  168. $inventoryAccount=app('inventoryAccountService')->完结盘点任务($id);
  169. if (!$inventoryAccount)return['success'=>false,'status'=>'修改完结状态失败!'];
  170. return['success'=>true,'data'=>$inventoryAccount];
  171. }
  172. public function 增加系统之外的库位记录(Request $request){
  173. if(!Gate::allows('库存管理-盘点')){return['success'=>false,'data'=>'没有权限'];}
  174. $location=$request->input('location');
  175. $barcode=$request->input('barcode');
  176. $inventoryId=$request->input('inventoryId');
  177. $count=$request->input('count');
  178. $inventoryAccountMission=app('inventoryAccountService')->增加系统之外的库位记录($location,$barcode,$inventoryId,$count);
  179. if (!$inventoryAccountMission)return ['success'=>false,'data'=>'添加系统之外的库位记录失败!'];
  180. $inventoryAccountMission=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->where('id',$inventoryAccountMission->id)->first();
  181. $stockInventoryPersons=$inventoryAccountMission->stockInventoryPersons;
  182. return ['success'=>true,'inventoryAccountMission'=>$inventoryAccountMission,'stockInventoryPersons'=>$stockInventoryPersons];
  183. }
  184. public function 盘点选中任务(Request $request){
  185. if(!Gate::allows('库存管理-盘点')){return['success'=>false,'data'=>'没有权限'];}
  186. $id=$request->input('id');
  187. $count=$request->count;
  188. $inventoryId=$request->input('inventoryId');
  189. $produced_at=$request->input('produced_at');
  190. $valid_at=$request->input('valid_at');
  191. $batch_number=$request->input('batch_number');
  192. if (is_null($count)) return ['success'=>false,'data'=>'盘点数不能为空!'];
  193. if ($produced_at||$valid_at||$batch_number){
  194. /** @var InventoryAccountService $inventoryAccountMission */
  195. $inventoryAccountService=app('inventoryAccountService');
  196. $inventoryAccountMission=$inventoryAccountService ->盘点生产日期_失效日期_批号有改动任务($id,$count,$inventoryId,$produced_at,$valid_at,$batch_number);
  197. if (!$inventoryAccountMission)return ['success'=>false,'data'=>'参数错误!'];
  198. /** @var InventoryAccountService $inventoryService */
  199. $inventoryService=app('inventoryAccountService');
  200. $inventoryAccount=$inventoryService->updateInventory($inventoryId);
  201. return ['success'=>true,'inventoryMission'=>$inventoryAccountMission,'inventory'=>$inventoryAccount];
  202. }else{
  203. /** @var InventoryAccountService $inventoryAccountMission */
  204. $inventoryAccountService=app('inventoryAccountService');
  205. $inventoryAccountMission=$inventoryAccountService ->盘点选中任务($id,$count,$inventoryId);
  206. if (!$inventoryAccountMission)return ['success'=>false,'data'=>'参数错误!'];
  207. /** @var InventoryAccountService $inventoryService */
  208. $inventoryService=app('inventoryAccountService');
  209. $inventoryAccount=$inventoryService->updateInventory($inventoryId);
  210. $stockInventoryPersons=$inventoryAccountMission->stockInventoryPersons;
  211. return ['success'=>true,'inventoryMission'=>$inventoryAccountMission,'inventory'=>$inventoryAccount,'stockInventoryPersons'=>$stockInventoryPersons];
  212. }
  213. }
  214. public function 删除盘点记录(Request $request){
  215. if(!Gate::allows('库存管理-盘点-删除')){return['success'=>false,'data'=>'没有权限'];}
  216. $inventoryAccountMissionId=$request->input('inventoryAccountMissionId');
  217. $inventoryAccountId=$request->input('inventoryAccountId');
  218. if(is_null($inventoryAccountMissionId)){return ['success'=>false,'data'=>'传入id为空'];}
  219. $inventoryAccountMission=InventoryAccountMission::query()->where('id',$inventoryAccountMissionId)->delete();
  220. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  221. if ($inventoryAccountMission>0){
  222. $inventoryAccount=InventoryAccount::query()->find($inventoryAccountId);
  223. $inventoryAccount->total=$inventoryAccount->total-1;
  224. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  225. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  226. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  227. $inventoryAccount->update();
  228. $this->log(__METHOD__,'删除盘点记录时修改盘点任务信息'.__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  229. }
  230. return ['success'=>true,'data'=>$inventoryAccountMission];
  231. }
  232. }