PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/SurveyController.php

https://gitlab.com/ryanmanzer/OVS-Laravel
PHP | 300 lines | 261 code | 19 blank | 20 comment | 25 complexity | fbec0c973963af420038ba11567c0c5f MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers;
  3. use DB;
  4. use Psy\CodeCleaner\AssignThisVariablePass;
  5. use Gate;
  6. use App\survey;
  7. Use App\Retailer;
  8. use App\Vendor;
  9. use App\Product;
  10. use App\SurveyProductSource;
  11. use Illuminate\Http\Request;
  12. use \Redirect;
  13. use App\Http\Requests;
  14. use App\SurveyProductCert;
  15. use App\SurveyProduct;
  16. Use App\SeafoodSpecies;
  17. class SurveyController extends Controller
  18. {
  19. public function __construct()
  20. {
  21. $this->middleware('auth',['except'=>'welcome']);
  22. }
  23. //FUNCTION THAT HANLDES UPDATING SURVEY STATUS
  24. public function updateStatus(Request $request, Survey $survey)
  25. {
  26. if(Gate::allows('survey-edit',$survey))
  27. {
  28. $survey->surveyStatus = $request->surveyStatus;
  29. $survey->ovs_downloadFlag=1;
  30. $survey->modified_by = $request->user()->id;
  31. if($request->surveyStatus =="Assigned to Vendor")
  32. {
  33. $survey->dateIssued = date("Y-m-d");
  34. }
  35. $survey->save();
  36. return back();
  37. }
  38. else
  39. {
  40. return Redirect::back();
  41. }
  42. }
  43. //FUNCTION FOR LOADING THE NEW SURVEY FORM
  44. public function newSurvey(Request $request)
  45. {
  46. //return "new Survey funtion called!";
  47. $user=$request->user;
  48. $retailers=Retailer::all();
  49. $vendors=Vendor::all();
  50. return view('survey.new',compact('user','retailers','vendors'));
  51. }
  52. //FUNCTION TO SHOW ALL PRODUCTS ASSIGNED TO A SPECIFIC SURVEY
  53. //DUE TO REOGRGANIZATION OF CONCEPTS, THIS WILL NOW
  54. // RETURN THE INSTANCES OF THE SURVEYPRODUCT TABLE ASSOCIATED WITH THIS
  55. //SURVEY
  56. public function products(Request $request,Survey $survey, $open_sp = NULL)
  57. {
  58. if(Gate::allows('survey-view',$survey))
  59. {
  60. $user = $request->user();
  61. $sp_list=ordSurveyProducts($survey); //this wil return a collection of surveyProduct records
  62. $done=$survey->complete;
  63. if(is_null($open_sp))
  64. {
  65. return view('survey.parts.productsTable',compact('sp_list','survey','user','done'));
  66. } else {
  67. // otherwise you need to pass all the data needed to open the assign source
  68. $osp = SurveyProduct::where('id', $open_sp)->first();
  69. $sps=$osp->sps; //returning the surveyproductsource records assiciated with this survey product
  70. $sps_ids=$sps->pluck('fk_source_id')->toArray();
  71. $sfCat=$osp->product->seafoodCategory->id;
  72. //getting all potential sources for this vendor
  73. $sources=$user->org->sources()
  74. ->where([
  75. ['show','=',1],
  76. ['fk_seafoodCategory_id','=',$sfCat],
  77. ])->get();
  78. //getting all potential certifications for this vendor
  79. $vendor_certs=$user->org->certifications;
  80. $prod_certs=$osp->certification;//getting collection of certifications for this product
  81. $cert_ids=$prod_certs->pluck('certification_id')->toArray();
  82. return view('survey.parts.productsTable',compact('sp_list', 'survey', 'sps','sources','user','vendor_certs','prod_certs','done','sps_ids','cert_ids', 'osp'));
  83. }
  84. }
  85. else
  86. {
  87. return Redirect::back();
  88. }
  89. }
  90. //FUNCTION TO LOAD THE FORM TO DISPLAY/ASSIGN SOURCES FOR PRODUCTS
  91. public function assign(Request $request, SurveyProduct $sp)
  92. {
  93. if(Gate::allows('sp-edit',$sp))
  94. {
  95. $user=$request->user();
  96. $sps=$sp->sps; //returning the surveyproductsource records assiciated with this survey product
  97. $sps_ids=$sps->pluck('fk_source_id')->toArray();
  98. $sfCat=$sp->product->seafoodCategory->id;
  99. //getting all potential sources for this vendor
  100. $sources=$user->org->sources()
  101. ->where([
  102. ['show','=',1],
  103. ['fk_seafoodCategory_id','=',$sfCat],
  104. ])->get();
  105. //getting all potential certifications for this vendor
  106. $vendor_certs=$user->org->certifications;
  107. $prod_certs=$sp->certification;//getting collection of certifications for this product
  108. $cert_ids=$prod_certs->pluck('certification_id')->toArray();
  109. $done=$sp->survey->complete; //indicating if survey is completed by vendor
  110. //returning the view in survey/parts/productSources.blade.php with the variables
  111. //for surveyProducts, SurveyProductSources, and Sources/
  112. return view('survey.parts.productSources',compact('sp','sps','sources','user','vendor_certs','prod_certs','done','sps_ids','cert_ids'));
  113. }
  114. else
  115. {
  116. return Redirect::back();
  117. }
  118. }
  119. //FUNCTION TO DELETE A SPECIFIC SURVEYPRODUCTSOURCE RECORD
  120. public function destroySPS(Request $request, SurveyProductSource $sps)
  121. {
  122. if(Gate::allows('sps-edit',$sps))
  123. {
  124. $sp=$sps->sp;$sp->ovs_downloadFlag=1;$sp->save();
  125. SurveyProductSource::destroy($sps->id); //destroying this specific record
  126. echo '1';
  127. }
  128. else
  129. {
  130. echo '0';
  131. /*return Redirect::back();*/
  132. }
  133. }
  134. //function to create a SurveyProductSource record assigned to a specific SurveyProduct
  135. public function assignSPS(Request $request, SurveyProduct $sp)
  136. {
  137. $this->validate($request,[
  138. 'Proportion_Of_Product'=>'required',
  139. 'Source'=>'required',
  140. ]);
  141. if(Gate::allows('sp-edit',$sp))
  142. {
  143. $pop = $request->Proportion_Of_Product/100;
  144. $prodPer=sourcePercent($sp->product);
  145. if($pop == 0) //if the user didn't specify a percent of product
  146. {
  147. errorMessage('Please assign a percent of product for this source.');
  148. }
  149. elseif($pop > 1) //if they try to specify more than 100%
  150. {
  151. errorMessage('Please do not assign more than 100% for any source.');
  152. }
  153. elseif($pop+$prodPer > 1)
  154. {
  155. $remain=(1-$prodPer) * 100;
  156. errorMessage('This would allocate more than 100% to this product.');
  157. }
  158. else
  159. {
  160. $sp->ovs_downloadFlag=1; $sp->save();
  161. $sps = new SurveyProductSource();
  162. $sps->fk_source_id = $request->Source;
  163. $sps->proportionOfProduct= $pop;
  164. $sps->fk_product_id = $sp->fk_product_id;
  165. $sps->fk_survey_id = $sp->fk_survey_id;
  166. $sps->surveyProducts_id = $sp->id;
  167. $sps->created_by = $request->user()->id;
  168. $sps->ovs_downloadFlag=1;
  169. $sps->fk_vendor_id = $sp->fk_vendor_id;
  170. $sps->save();
  171. flashMessage('Source Added to product','success');
  172. }
  173. echo "1";
  174. }
  175. else
  176. {
  177. echo "0";
  178. }
  179. }
  180. //Function used to return a read-only view of product sourcing for
  181. //NGO staff to view.
  182. public function viewSources(Request $request, SurveyProduct $sp)
  183. {
  184. if(Gate::allows('sp-edit',$sp))
  185. {
  186. $user=$request->user();
  187. $sps=$sp->sps;
  188. return view('survey.parts.productSources',compact('user','sps',"sp"));
  189. }
  190. else
  191. {
  192. echo "0";
  193. }
  194. }
  195. public function assignCert(Request $request, SurveyProduct $sp)
  196. {
  197. if(Gate::allows('sp-edit',$sp))
  198. {
  199. $new_cert= new SurveyProductCert();
  200. $new_cert->survey_product_id=$sp->id;
  201. $new_cert->certification_id=$request->certification_id;
  202. $new_cert->vendor_id=$sp->fk_vendor_id;
  203. $new_cert->ovs_downloadFlag=1;
  204. $new_cert->save();
  205. flashMessage('Certification assigned to product','success');
  206. echo "1";
  207. }
  208. else {
  209. echo "0";
  210. }
  211. }
  212. public function removeCert(Request $request, $id)
  213. {
  214. $spc=SurveyProductCert::find($id);
  215. $sp=$spc->survey_product;
  216. if(Gate::allows('sp-edit',$sp))
  217. {
  218. SurveyProductCert::destroy($spc->id);
  219. echo "1";
  220. }
  221. else {
  222. flashMessage('Something appears to have gone wrong','danger');
  223. }
  224. }
  225. public function assignRepack(Request $request, SurveyProduct $sp)
  226. {
  227. if(Gate::allows('sp-edit',$sp))
  228. {
  229. $sp->repack=$request->repack;
  230. $sp->ovs_downloadFlag=1;
  231. $sp->save();
  232. echo "1";
  233. }
  234. else {
  235. echo "0";
  236. }
  237. }
  238. public function updatePercent(Request $request, SurveyProduct $sp)
  239. {
  240. if(Gate::allows('sp-edit',$sp))
  241. {
  242. $percent = sourcePercent($sp->product)*100 .' %';
  243. echo $percent;
  244. }
  245. else {
  246. echo "0";
  247. }
  248. }
  249. public function newModalTitle(Request $request, SurveyProduct $sp)
  250. {
  251. if(Gate::allows('sp-edit',$sp))
  252. {
  253. $msg = 'Enter New '.$sp->product->seafoodCategory->category.' Source Information';
  254. echo $msg;
  255. }
  256. else {
  257. echo "0";
  258. }
  259. }
  260. public function sfCatGroup(Request $request, SurveyProduct $sp)
  261. {
  262. if(Gate::allows('sp-edit',$sp))
  263. {
  264. return view('source.parts.sfCatForm',compact('sp'));
  265. }
  266. else {
  267. echo "0";
  268. }
  269. }
  270. public function sfGroup(Request $request, SurveyProduct $sp)
  271. {
  272. if(Gate::allows('sp-edit',$sp))
  273. {
  274. $sfs = SeafoodSpecies::all();
  275. return view('source.parts.sfGroup',compact('sp', 'sfs'));
  276. }
  277. else {
  278. echo "0";
  279. }
  280. }
  281. }