Advertisement
vgani4

original vacfil

Jun 29th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.74 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Repositories\Filters\Vacancies;
  4.  
  5. use Illuminate\Http\Request;
  6. use DB;
  7. use App\Models\Access\User\User;
  8. use App\Models\Certificate;
  9. use App\Models\FleetLists;
  10. use Illuminate\Pagination\LengthAwarePaginator as Paginator;
  11.  
  12.  
  13. class VacanciesFilter
  14. {
  15.  
  16. private $request;
  17.  
  18. private $sql;
  19.  
  20. private $where;
  21.  
  22. private $from;
  23.  
  24. private $group;
  25.  
  26. private $having;
  27.  
  28. private $limit;
  29.  
  30. private $order;
  31.  
  32. private $perPage;
  33.  
  34. private $page;
  35.  
  36. private $total;
  37.  
  38. private $user;
  39.  
  40. public function __construct($user)
  41. {
  42. $this->request = request();
  43. $this->page = $this->request->has('page') ?
  44. $this->request->page - 1: null;
  45. $this->perPage = 10;
  46. $this->user = $user;
  47. }
  48.  
  49.  
  50.  
  51. public function vacancies(){
  52. $this->initSql();
  53. $this->applyFilters();
  54. $sql = $this->sql." "
  55. ."\n" .$this->from ." "
  56. ."\n". $this->where ." "
  57. ."\n". $this->group ." "
  58. ."\n". $this->having ." "
  59. ;
  60.  
  61. $fullSql = $sql." ".$this->order." ".$this->limit;
  62.  
  63.  
  64. // dd($fullSql);
  65.  
  66. $this->total = DB::select($sql);
  67. $res = DB::select($fullSql);
  68. // dd($res);
  69. return $this->preparePaginate($res);
  70.  
  71. }
  72.  
  73. private function initSql()
  74. {
  75. $this->sql = "
  76. SELECT u.id, u.first_name, u.updated_at,
  77. IF(ISNULL(u.last_name), '', SUBSTRING(u.last_name, 1, 1)) as last_name,
  78. fl.name as vname, fl.id as vid,
  79. pi.date_of_readiness as dreadiness,
  80. pi.contact_phone as phone,
  81.  
  82. (SELECT short_name FROM countries WHERE countries.id=pi.citizenship) as flag,
  83. (SELECT name FROM countries WHERE countries.id=pi.citizenship) as flag_full,
  84. YEAR(NOW()) - YEAR(pi.date_of_birth) as date_of_birth,
  85. (SELECT name from rank_lists
  86. where rank_lists.id=pi.rank_list_id) as rname,
  87. ( SELECT COALESCE( SUM(DATEDIFF(service.sign_off_date, service.sign_on_date))/365, '')
  88. FROM service
  89. WHERE service.rank_list_id=pi.rank_list_id
  90. AND service.user_id=u.id
  91. ) as yrank,
  92.  
  93. (SELECT COUNT(cv_reviews.id) FROM cv_reviews
  94. WHERE cv_reviews.seaman_id=u.id
  95. AND cv_reviews.crewing_id=". $this->user->id. "
  96. ) as review,
  97. (SELECT expiration FROM certificates WHERE organisation='CoC Validation Certificate' AND certificates.user_id=u.id) as validation,
  98.  
  99. (SELECT (SUM(percents.general)+SUM(percents.biometric)+SUM(percents.medical)+SUM(percents.personal)+SUM(percents.training)+SUM(percents.summary)+SUM(percents.service)) as total FROM percents
  100. WHERE percents.user_id = u.id
  101. ) as strength,
  102.  
  103. (SELECT service.sign_off_date FROM service
  104. where service.user_id=u.id
  105. ORDER BY service.sign_on_date DESC LIMIT 1
  106. ) as lsod,
  107.  
  108.  
  109. (SELECT service.rank_list_id FROM service
  110. where service.user_id=u.id
  111. ORDER BY service.sign_on_date DESC LIMIT 1
  112. ) as lrankId,
  113.  
  114. (SELECT name FROM rank_lists
  115. where id=lrankId
  116. ) as lrank,
  117.  
  118.  
  119. ( SELECT COALESCE( SUM(DATEDIFF(service.sign_off_date, service.sign_on_date))/30, '')
  120. FROM service WHERE service.user_id = u.id AND service.rank_list_id = pi.rank_list_id
  121. ) as sdog,
  122. (SELECT name from files where (fileable_id=u.id and fileable_type = 'App\\\Models\\\Access\\\User\\\User')) as logo
  123.  
  124. ";
  125.  
  126. $this->from = "
  127. FROM users as u LEFT JOIN assigned_roles as ar ON u.id=ar.user_id
  128. LEFT JOIN roles as r ON ar.role_id =r.id
  129. LEFT JOIN personal_info as pi ON u.id=pi.user_id
  130. LEFT JOIN fleet_lists as fl ON pi.fleet_list_id = fl.id
  131. ";
  132.  
  133. $this->where = " WHERE r.name='Seaman' AND u.confirmed=1 AND u.status=1 AND deleted_at IS NULL ";
  134. $this->group = "";
  135. $this->having = "HAVING 1=1 ";
  136. $this->order = " ORDER BY updated_at DESC ";
  137.  
  138.  
  139. $this->limit = " LIMIT "
  140. .($this->page ? $this->page*$this->perPage . ", " .$this->perPage : $this->perPage);
  141. }
  142.  
  143.  
  144. private function preparePaginate($res)
  145. {
  146. return new Paginator($res, count($this->total),
  147. $this->perPage, $this->page + 1,
  148. [
  149. 'path' => $this->request->url(),
  150. 'query' => $this->request->query()
  151. ]
  152. );
  153. }
  154.  
  155. private function vessel($ids)
  156. {
  157. if(is_array($ids)){
  158. $ids = implode(',', array_map('intval', $ids));
  159. $this->where .= ' AND pi.fleet_list_id IN('.$ids.')';
  160. }
  161. else{
  162. $this->where .= ' AND pi.fleet_list_id='.$ids;
  163. }
  164. }
  165.  
  166.  
  167. private function rank($ids)
  168. {
  169. if(is_array($ids)){
  170. $ids = implode(',',$ids);
  171. $this->where .= ' AND pi.rank_list_id IN('.$ids.')';
  172. }
  173. else{
  174. $this->where .= ' AND pi.rank_list_id='.$ids;
  175. }
  176. }
  177.  
  178.  
  179.  
  180.  
  181. private function yearrank($amount)
  182. {
  183.  
  184. if (!$this->request->has('vessel') ||
  185. !$this->request->has('rank') ||
  186. !$this->request->has('yearrank') ) {
  187. return;
  188. }
  189.  
  190.  
  191. $this->sql .= ", (SELECT SUM(DATEDIFF(service.sign_off_date, service.sign_on_date))/365
  192. FROM service
  193. WHERE service.rank_list_id in(".implode(',', (array)$this->request->rank).")
  194. AND service.user_id=u.id
  195. ) as selectedrank ";
  196.  
  197.  
  198. // $this->where = " WHERE r.name='Seaman' ";
  199. $this->having .= " AND selectedrank >=". $this->request->yearrank;
  200. $this->having .= " AND yrank > 0";
  201. }
  202.  
  203.  
  204. public function tankeryearrank()
  205. {
  206. if (!$this->request->has('vessel') ||
  207. !$this->request->has('rank') ||
  208. !$this->request->has('tankeryearrank') ) {
  209. return;
  210. }
  211.  
  212. $this->sql .= ", (SELECT SUM(DATEDIFF(service.sign_off_date, service.sign_on_date))/365
  213. FROM service
  214. WHERE service.rank_list_id in(".implode(',',(array)$this->request->rank).")
  215. AND service.user_id=u.id
  216. AND service.type IN (".implode(',', (array)($this->request->vessel)).")
  217. ) as tankerselectedrank ";
  218.  
  219. // $this->where = " WHERE r.name='Seaman' ";
  220. $this->having .= " AND tankerselectedrank >=". $this->request->tankeryearrank;
  221. }
  222.  
  223.  
  224. /**
  225. * @param $SurnameId
  226. */
  227. private function searchSeaman($SurnameId)
  228. {
  229. if(is_numeric($SurnameId)){
  230. $this->where .= " AND u.id LIKE '%".$SurnameId."%'";
  231. }
  232. else {
  233.  
  234. $this->where .= " AND (IF((SELECT COUNT(cv_reviews.id) FROM cv_reviews
  235. WHERE cv_reviews.seaman_id=u.id
  236. AND cv_reviews.crewing_id=". $this->user->id. "
  237. ), u.last_name LIKE '".$SurnameId."%', '' )";
  238. $this->where .= " OR u.first_name LIKE '" .$SurnameId."%'";
  239. $this->where .= " ) ";
  240. }
  241. }
  242.  
  243.  
  244.  
  245. private function englishlevel($id)
  246. {
  247. $this->where .= " AND pi.english_proficiency>=".$id;
  248. }
  249.  
  250. private function citizenship($id)
  251. {
  252. // $this->where .= " AND pi.citizenship=".$id;
  253. $this->where .= " AND (pi.citizenship=".$id;
  254. $this->where .= " OR pi.citizenship_1=".$id;
  255. $this->where .= " OR pi.citizenship_2=".$id;
  256. $this->where .= " ) ";
  257.  
  258. }
  259.  
  260. private function salary($amount)
  261. {
  262. //$this->where .= " AND pi.salary_expected>".$amount;
  263. if(!$this->request->has('sto')) {
  264. return;
  265. }
  266. $sfrom = $this->request->sfrom ?? 0;
  267. $this->where .= " AND pi.salary_expected BETWEEN "
  268. .$sfrom." AND ".$this->request->sto." ";
  269.  
  270.  
  271. }
  272.  
  273. private function duration($amount)
  274. {
  275. if(!$this->request->has('dfrom') || !$this->request->has('dto')) {
  276. return;
  277. }
  278.  
  279. $this->where .= " AND pi.salary_expected>".$amount;
  280. }
  281.  
  282. private function yold()
  283. {
  284. if(!$this->request->has('yoldfrom') || !$this->request->has('yoldto')) {
  285. return;
  286. }
  287. $this->having .= " AND date_of_birth BETWEEN "
  288. .$this->request->yoldfrom." AND ".$this->request->yoldto." ";
  289.  
  290. }
  291.  
  292. private function getseadog($amount)
  293. {
  294. $request = $this->request;
  295. $rookie = $request->has('rookie') ? 1 : 0 ;
  296. $skilled = $request->has('skilled') ? 3 : 0 ;
  297. $seadog = $request->has('seadog') ? 7 : 0 ;
  298. $res = $rookie + $skilled + $seadog;
  299.  
  300. switch($res){
  301. case 1:
  302. $this->having .= " AND sdog<=12";
  303. break;
  304. case 3:
  305. $this->having .= " AND sdog>12 AND sdog<=60";
  306. break;
  307. case 4:
  308. $this->having .= " AND sdog<=60";
  309. break;
  310. case 7:
  311. $this->having .= " AND sdog>60";
  312. break;
  313. case 8:
  314. $this->having .= " AND sdog<=12 OR sdog>60";
  315. break;
  316. case 10:
  317. $this->having .= " AND sdog>12";
  318. break;
  319. }
  320. }
  321.  
  322.  
  323.  
  324. private function sort($s)
  325. {
  326. //$sortBy = " sdog ";
  327. $sortBy = " ";
  328.  
  329. switch($s) {
  330. case 'newest':
  331. $sortBy = " u.updated_at ";
  332. $this->order = " ORDER BY ".$sortBy." DESC";
  333. break;
  334. case 'expirienced':
  335. $sortBy = " yrank ";
  336. $this->order = " ORDER BY ".$sortBy." DESC";
  337. break;
  338. case 'readiness':
  339. $sortBy = " dreadiness ";
  340. $this->order = " ORDER BY ".$sortBy." ASC";
  341. break;
  342. case 'strength':
  343. $sortBy = " strength ";
  344. $this->order = " ORDER BY ".$sortBy." DESC";
  345. break;
  346. }
  347.  
  348.  
  349. }
  350.  
  351. private function filters()
  352. {
  353. return $this->request->all();
  354. }
  355.  
  356. private function applyFilters()
  357. {
  358. foreach ($this->filters() as $name => $value) {
  359. if(method_exists($this, $name)){
  360. call_user_func_array([$this, $name], array_filter([$value]));
  361. }
  362. }
  363.  
  364. }
  365.  
  366. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement