PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/app/Http/Controllers/VendorController.php

https://gitlab.com/ryanmanzer/OVS-Laravel
PHP | 364 lines | 313 code | 31 blank | 20 comment | 29 complexity | 6f2762562f7028c182d3a69877248ac7 MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Gate;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resonse;
  6. use Illuminate\Support\Facades\Redirect;
  7. Use App\Vendor;
  8. Use App\Source;
  9. Use App\SeafoodSpecies;
  10. Use App\SeafoodCategories;
  11. Use App\methodCategories;
  12. Use App\harvestMethods;
  13. Use App\Country;
  14. use App\Http\Requests;
  15. use App\Product;
  16. use App\CertificationType;
  17. use App\Certification;
  18. use App\SurveyProductCert;
  19. use App\SurveyProductSource;
  20. use App\SurveyProduct;
  21. use App\User;
  22. class VendorController extends Controller
  23. {
  24. public function __construct()
  25. {
  26. $this->middleware('auth',['except'=>'welcome']);
  27. }
  28. //setting basic middleware that any call to this controller with use when constructing itself
  29. //The construct method is called any time a object of this class is instantiated.
  30. public function show(Request $request)
  31. { // We will assume that if we got this far the user (whose data is in the request object) will
  32. // be associated with a a vendor. Therefore we only need to provide the request to get all our
  33. // data
  34. // Getting the categories we will need to start building our New Source form
  35. $sfCat=SeafoodCategories::all(); //gathering all seafood categories
  36. $gearCat=methodCategories::all(); //gathering all gear categories
  37. $sfs=SeafoodSpecies::all();//loading all seafood species
  38. $hMeths=harvestMethods::all();//loading all harvest methods
  39. $countries=Country::orderBy('sortOrder')->get();//loading all countries
  40. //Getting the vendor information from the user
  41. $user=$request->user();
  42. $vendor=$user->org; //getting the vendor record from the user
  43. $surveys_count=$vendor->surveys->count(); //getting a count of surveys for this vendor
  44. $vendID=$vendor->id; //getting vendor ID
  45. $result=getTabsScripts($user);
  46. $tabs=$result['tabs']; $loadComp=$result['loadComp'];
  47. $certTypes=CertificationType::all();
  48. return view('vendor.home',
  49. ['vendor'=>$vendor,'surveys_count'=>$surveys_count,
  50. 'sfCat'=>$sfCat,'gearCat'=>$gearCat,'seafood'=>$sfs,'methods'=>$hMeths,
  51. 'countries'=>$countries,'certTypes'=>$certTypes]);
  52. }
  53. // ROUTING VENDOR TO PAGE FOR EDITING VENDOR DETAILS
  54. public function edit(Request $request, Vendor $vendor)
  55. {
  56. if(Gate::allows('org-check',$vendor))
  57. {
  58. $org=$vendor;
  59. $user=$request->user();
  60. return view('vendor.parts.accountInfo.edit_contact_info',compact('org','user'));
  61. }
  62. else
  63. {
  64. return Redirect::back();
  65. }
  66. }
  67. // PROCESSING THE UPDATE TO THE VENDOR RECORD
  68. public function update(Request $request, Vendor $vendor)
  69. {
  70. $this->validate($request,
  71. [
  72. 'contact_email' => 'required',
  73. 'contact_name' => 'required',
  74. ]);
  75. if(Gate::allows('org-check',$vendor))
  76. {
  77. $vendor->modified_by=$request->user()->id;
  78. $vendor->ovs_downloadFlag=1;
  79. $vendor->update($request->except('_token'));
  80. return "1";
  81. }
  82. else
  83. {
  84. return "0";
  85. }
  86. }
  87. // THIS RETURNS THE CONTACT INFO FOR VENDOR HOME VIEW - AJAX
  88. public function info(Request $request, Vendor $vendor)
  89. {
  90. if(Gate::allows('org-check',$vendor))
  91. {
  92. $org=$vendor;
  93. $type='vendor';
  94. $user_accounts = User::where('org_id', $vendor->id)
  95. ->where('user_type', 'vendor')
  96. ->get();
  97. return view('vendor.parts.account_info',compact('org','user', 'type' , 'user_accounts'));
  98. /* return response()->view('general.info',compact('org','type')); */
  99. }
  100. else {
  101. return Redirect::back();
  102. }
  103. }
  104. public function contactInfo(Request $request, Vendor $vendor)
  105. {
  106. if(Gate::allows('org-check',$vendor))
  107. {
  108. $org=$vendor;
  109. $type='vendor';
  110. return view('vendor.parts.accountInfo.show_contact_info',compact('org', 'type'));
  111. /* return response()->view('general.info',compact('org','type')); */
  112. }
  113. else {
  114. return Redirect::back();
  115. }
  116. }
  117. // RETURNS SURVEYS FOR A PARTICULAR VENDOR AND IS PART OF VENDOR HOME VIEW - AJAX
  118. public function surveys(Request $request, Vendor $vendor)
  119. {
  120. $user=$request->user();
  121. if(Gate::allows('org-check',$vendor))
  122. {
  123. $surveys=$vendor->surveys;
  124. return response()->view('vendor.parts.survey_list',compact('surveys','user'));
  125. }
  126. elseif (Gate::allows('retailer-vender',$vendor))
  127. {
  128. $surveys=$vendor->surveys()->where('fk_retailer_id',$request->user()->org_id);
  129. return response()->view('vendor.parts.survey_list',compact('surveys','user'));
  130. }
  131. else {
  132. echo "0";
  133. }
  134. }
  135. // RETURNS VENDOR SOURCES AND IS PART OF VENDOR HOME VIEW - AJAX
  136. //Added ooptional paramter to pass a source id whihc will be open in edit
  137. public function sources(Request $request, Vendor $vendor, Source $source = NULL)
  138. {
  139. if(Gate::allows('org-check',$vendor))
  140. {
  141. $sources_count=$vendor->sources->where('showSource',1)->count(); //getting paginated sources
  142. $sources=orderedSources($vendor);
  143. If(!is_null($source))
  144. {
  145. $edit_source = $source;
  146. session()->flash('view_only',true);
  147. $sps = SurveyProductSource::where('fk_source_id', $source->id)->get();
  148. }
  149. return response()->view('vendor.parts.sources_list',compact('sources','sources_count','vendor', 'edit_source' , 'sps'));
  150. }
  151. else {
  152. return Redirect::back();
  153. }
  154. }
  155. public function productSources(Request $request, Product $product)
  156. {
  157. if(Gate::allows('product-check',$product))
  158. {
  159. $sps=$product->sps;
  160. $sp=$product->sp;
  161. $user=$request->user();
  162. return response()->view('vendor.parts.productSources',compact('sps','sp','user','product'));
  163. }
  164. else
  165. {
  166. return Redirect::back();
  167. }
  168. }
  169. /**
  170. * New Source form building functions
  171. */
  172. // GET LIST OF SEAFOOD CATEGORIES
  173. public function sf_list(Request $request, $id)
  174. {
  175. if ($id=='undefined')
  176. {
  177. $seafood=SeafoodSpecies::all();
  178. }
  179. else if ($id==0)
  180. {
  181. $seafood=SeafoodSpecies::all();
  182. }
  183. else
  184. {
  185. $sfCat = SeafoodCategories::find(intval($id));
  186. $seafood=$sfCat->seafood;
  187. }
  188. return response()->view('vendor.parts.newSource.sf_list',compact('seafood'));
  189. }
  190. //GET LIST OF METHOD CATEGORIES
  191. public function cat_list(Request $request,$wf)
  192. {
  193. $cats=methodCategories::where('wild_farmed',$wf)->get();
  194. return response()->view('vendor.parts.newSource.cat_list',compact('cats'));
  195. }
  196. //GET FULL LIST OF HARVEST METHODS
  197. public function meth_list(Request $request,$catid)
  198. {
  199. if($catid =="undefined" or $catid==0)
  200. {
  201. $methods = harvestMethods::all();
  202. }
  203. else {
  204. $methods=harvestMethods::where('fk_methodCategory_id',intval($catid))->get();
  205. }
  206. return response()->view('vendor.parts.newSource.meth_list',compact('methods'));
  207. }
  208. public function products(Request $request, Vendor $vendor)
  209. {
  210. if(Gate::allows('org-check',$vendor))
  211. {
  212. $products = orderProducts($vendor);
  213. $mypath='https://' . $request->fullUrl();
  214. flashMessage($mypath,'info');
  215. return response()->view('vendor.parts.products',compact('products','mypath'));
  216. }
  217. else
  218. {
  219. return Redirect::back();
  220. }
  221. }
  222. public function productShow(Product $product)
  223. {
  224. $vendor = Vendor::find($product->fk_vendor_id);
  225. if(Gate::allows('org-check',$vendor))
  226. {
  227. $sp=$product->sp;
  228. $sps=$product->sps;
  229. session()->flash('view_only',true);
  230. return response()->view('survey.parts.productSources',compact('sp','sps'));
  231. }
  232. else
  233. {
  234. echo "0";
  235. }
  236. }
  237. public function thrSurveys(Request $request, Vendor $vendor)
  238. {
  239. if(Gate::allows('org-check',$vendor))
  240. {
  241. $thrSurveys = $vendor->THR_surveys;
  242. return view('vendor.parts.trace_hr.survey_list',compact('thrSurveys'));
  243. }
  244. else
  245. {
  246. echo "0";
  247. }
  248. }
  249. public function certs(Request $request, Vendor $vendor)
  250. {
  251. if(Gate::allows('org-check',$vendor))
  252. {
  253. $certs=$vendor->certifications;
  254. $cert_types=CertificationType::orderBy('certType')->get();
  255. return view('vendor.parts.cert_list',compact('certs','cert_types','vendor'));
  256. }
  257. }
  258. public function confirmRemoveCert(Request $request, Certification $cert)
  259. {
  260. $productcerts=SurveyProductCert::where('certification_id',$cert->id)->get();
  261. if (count($productcerts) == 0) {
  262. $msg = 'Are you sure you want to remove this certification?';
  263. } else {
  264. $msg='This certification is listed for the following products:<br><br>';
  265. foreach ($productcerts as $productcert) {
  266. $msg .= $productcert->survey_product->product->retailer->name.' '.$productcert->survey_product->product->retailerProductCode.'<br>';
  267. }
  268. $msg .= '<br>Removing this certification will affect the reported products.<br><br>Are you sure you want to remove this certification?';
  269. }
  270. echo $msg;
  271. }
  272. public function addCert(Request $request, Vendor $vendor)
  273. {
  274. $this->validate($request,[
  275. 'certification_type_id'=>'required',
  276. 'certification_number'=>'required',
  277. 'description'=>'required',
  278. ]);
  279. if(Gate::allows('org-check',$vendor))
  280. {
  281. $data=$request->all();
  282. $cert=new Certification($data);
  283. $vendor->addCert($cert);
  284. echo "1";
  285. }
  286. else {
  287. echo "0";
  288. }
  289. }
  290. public function rmCert(Request $request, Certification $cert)
  291. {
  292. if(Gate::allows('cert-check',$cert))
  293. {
  294. $productcerts=SurveyProductCert::where('certification_id',$cert->id)->get();
  295. foreach($productcerts as $productcert ){
  296. SurveyProductCert::destroy($productcert->id);
  297. }
  298. $cert->delete();
  299. echo "1";
  300. }
  301. else {
  302. echo "0";
  303. }
  304. }
  305. public function newSource(Request $request, Vendor $vendor, $sp = NULL)
  306. {
  307. $sfCat=SeafoodCategories::all(); //gathering all seafood categories
  308. $countries=Country::orderBy('sortOrder')->get();//loading all countries
  309. if(is_null($sp))
  310. {
  311. return view('vendor.parts.new_source_refresh', compact ('vendor', 'sfCat','countries'));
  312. } else {
  313. $sp = SurveyProduct::where('id', $sp)->first();
  314. $sfs=SeafoodSpecies::where('fk_seafoodCategory_id', "=", $sp->product->fk_seafoodCategory_id)->get();
  315. return view('vendor.parts.new_source_refresh', compact ('vendor', 'sfCat','countries', 'sp', 'sfs'));
  316. }
  317. }
  318. public function refreshSfCat(Request $request, Vendor $vendor)
  319. {
  320. $sfCat=SeafoodCategories::all(); //gathering all seafood categories
  321. return view('source.parts.sfCatForm', compact ('sfCat' ) );
  322. }
  323. public function refreshSf(Request $request, Vendor $vendor)
  324. {
  325. $sfCat=SeafoodCategories::all(); //gathering all seafood categories
  326. return view('source.parts.sfGroup');
  327. }
  328. public function newCertModal(Request $request, Vendor $vendor)
  329. {
  330. $cert_types=CertificationType::orderBy('certType')->get();
  331. return view('vendor.parts.add_cert_modal', compact ('vendor', 'cert_types'));
  332. }
  333. }