Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

FormResourceController.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace App\Http\Controllers\v3\Api;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use Carbon\Carbon;
  6. use App\LatestModel\Staff;
  7. use App\LatestModel\StaffDetail;
  8. use App\LatestModel\Dealer;
  9. use App\LatestModel\DealerDetail;
  10. use App\LatestModel\Module\Form;
  11. use App\LatestModel\Module\FormStatus;
  12. use App\LatestModel\Module\Subscriber;
  13. use App\LatestModel\Module\WorkOrder;
  14. use App\LatestModel\Module\Docket;
  15. use App\LatestModel\Module\PackageDetail;
  16. use App\LatestModel\Module\Company;
  17. use App\LatestModel\Module\Coverage;
  18. use App\LatestModel\Module\Product;
  19. use App\Http\Resources\FormResource;
  20. class FormResourceController extends BaseController
  21. {
  22. /**
  23. * @var ServiceCategory
  24. */
  25. protected $form;
  26. /**
  27. * ServiceController constructor.
  28. *
  29. * @param ServiceCategory $ServiceCategory
  30. */
  31. public function __construct(Form $form)
  32. {
  33. $this->form = $form;
  34. }
  35. public function filterByTypeADate($type, $startdate, $enddate){
  36. if($type == 'All' && $startdate == ''){
  37. $form = Form::where('type_service', '!=', '');
  38. return $form;
  39. }else if($type == 'All' && $startdate != ''){
  40. $dateS = Carbon::createFromFormat('Y-m-d', $startdate);
  41. $start = $dateS->copy()->startOfDay();
  42. $dateE = Carbon::createFromFormat('Y-m-d', $enddate);
  43. $end = $dateE->copy()->endOfDay();
  44. $form = Form::whereBetween('created_at', array($start, $end));
  45. return $form;
  46. }else if($type != 'All' && $startdate == ''){
  47. $form = Form::where('type_service', $type);
  48. return $form;
  49. }else if($type != 'All' && $startdate != ''){
  50. $dateS = Carbon::createFromFormat('Y-m-d', $startdate);
  51. $start = $dateS->copy()->startOfDay();
  52. $dateE = Carbon::createFromFormat('Y-m-d', $enddate);
  53. $end = $dateE->copy()->endOfDay();
  54. $form = Form::where('type_service', $type)->whereBetween('created_at', array($start, $end));
  55. return $form;
  56. }
  57. }
  58. public function filterByPackage($type, $startdate, $enddate, $application){
  59. $form = $this->filterByTypeADate($type, $startdate, $enddate);
  60. if($application == 'All'){
  61. return $form;
  62. }else{
  63. $form = $form->where('type_application', $application);
  64. return $form;
  65. }
  66. }
  67. public function filterByCompany($type, $startdate, $enddate, $application, $company){
  68. $form = $this->filterByPackage($type, $startdate, $enddate, $application);
  69. if($company == 'All'){
  70. return $form;
  71. }else{
  72. $form = $form->where('company_id', $company);
  73. return $form;
  74. }
  75. }
  76. public function filterBySubscriber($type, $startdate, $enddate, $application, $company, $subcriber){
  77. $form = $this->filterByCompany($type, $startdate, $enddate, $application, $company);
  78. $form = $form->with(['Subscriber' => function($q) use($subcriber){
  79. $q->where('subscriber_id', 'LIKE', $subcriber);
  80. }],'PackageDetail','WorkOrder');
  81. return $form;
  82. }
  83. public function filterByName($type, $startdate, $enddate, $application, $company, $name){
  84. $form = $this->filterByCompany($type, $startdate, $enddate, $application, $company);
  85. $form = $form->with(['Subscriber' => function($q) use($name){
  86. $q->where('name', 'LIKE', "%{$name}%")->orWhere('company_name', 'LIKE', "%{$name}%");
  87. }],'PackageDetail','WorkOrder');
  88. return $form;
  89. }
  90. public function filterByUnit($type, $startdate, $enddate, $application, $company, $unit){
  91. $form = $this->filterByCompany($type, $startdate, $enddate, $application, $company);
  92. $form = $form->with(['Subscriber' => function($q) use($unit){
  93. $q->where('unit_no', $unit);
  94. }],'PackageDetail','WorkOrder');
  95. return $form;
  96. }
  97. public function filterByBuilding($type, $startdate, $enddate, $application, $company, $building){
  98. $form = $this->filterByCompany($type, $startdate, $enddate, $application, $company);
  99. $form = $form->with(['Subscriber' => function($q) use($building){
  100. $q->where('building_name', $building);
  101. }],'PackageDetail','WorkOrder');
  102. return $form;
  103. }
  104. /**
  105. * Display a listing of the resource.
  106. *
  107. * @return \Illuminate\Http\Response
  108. */
  109. public function index(Request $request)
  110. {
  111. $type = $request->fbtype;
  112. $application = $request->fbpackage;
  113. $company = $request->fbdealer;
  114. $startdate = $request->startdate;
  115. $enddate = $request->enddate;
  116. $subcriber = $request->subcriber_id;
  117. $name = $request->name;
  118. $unit = $request->unit_no;
  119. $building = $request->building;
  120. $status = $request->status;
  121. $nested_data = array();
  122. if($subcriber != '') {
  123. $form = $this->filterBySubscriber($type, $startdate, $enddate, $application, $company, $subcriber);
  124. $form = $form->orderBy('created_at','ASC')->get();
  125. }else if($name == ''){
  126. $form = $this->filterByName($type, $startdate, $enddate, $application, $company, $name);
  127. $form = $form->orderBy('created_at','ASC')->get();
  128. }else if($unit == ''){
  129. $form = $this->filterByUnit($type, $startdate, $enddate, $application, $company, $unit);
  130. $form = $form->orderBy('created_at','ASC')->get();
  131. }else if($building == ''){
  132. $form = $this->filterByBuilding($type, $startdate, $enddate, $application, $company, $building);
  133. $form = $form->orderBy('created_at','ASC')->get();
  134. }else{
  135. $form = $this->filterByCompany($type, $startdate, $enddate, $application, $company);
  136. $form = $form->with('Subscriber','PackageDetail','WorkOrder')->orderBy('created_at','ASC')->get();
  137. }
  138. if($status == 'new'){
  139. foreach ($form as $key => $f) {
  140. if(!empty($f->Subscriber) && !empty($f->PackageDetail) && empty($f->WorkOrder)){
  141. $nested_data[] = $f;
  142. }
  143. }
  144. }
  145. // return $compound;
  146. return \DataTables::of($nested_data)->addIndexColumn()
  147. ->addColumn('index', function($row) {
  148. $curr = Carbon::now();
  149. $dtC = Carbon::parse($row['created_at'])->setTimezone('Asia/Kuala_Lumpur');
  150. if($curr->diffInDays($dtC) <= 3){
  151. $html = 'New';
  152. }else{ $html = ''; }
  153. return $html;
  154. })->rawColumns(['index'])->make(true);
  155. }
  156. /**
  157. * Show the form for creating a new resource.
  158. *
  159. * @return \Illuminate\Http\Response
  160. */
  161. public function create()
  162. {
  163. //
  164. }
  165. /**
  166. * Store a newly created resource in storage.
  167. *
  168. * @param \Illuminate\Http\Request $request
  169. * @return \Illuminate\Http\Response
  170. */
  171. public function store(Request $request)
  172. {
  173. //
  174. }
  175. /**
  176. * Display the specified resource.
  177. *
  178. * @param int $id
  179. * @return \Illuminate\Http\Response
  180. */
  181. public function show($id)
  182. {
  183. //
  184. }
  185. /**
  186. * Show the form for editing the specified resource.
  187. *
  188. * @param int $id
  189. * @return \Illuminate\Http\Response
  190. */
  191. public function edit($id)
  192. {
  193. //
  194. }
  195. /**
  196. * Update the specified resource in storage.
  197. *
  198. * @param \Illuminate\Http\Request $request
  199. * @param int $id
  200. * @return \Illuminate\Http\Response
  201. */
  202. public function update(Request $request, $id)
  203. {
  204. //
  205. }
  206. /**
  207. * Remove the specified resource from storage.
  208. *
  209. * @param int $id
  210. * @return \Illuminate\Http\Response
  211. */
  212. public function destroy($id)
  213. {
  214. //
  215. }
  216. }