Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.91 KB | None | 0 0
  1. public function showUploadedRecord( Request $request ){
  2.  
  3.  
  4.  
  5. //add sort order if it does not exists or remove sort if it exists
  6. $full_path = url()->full();
  7. $url_array_full = parse_url($full_path);
  8.  
  9.  
  10.  
  11. if( !empty($url_array_full['query'])){
  12.  
  13. //it already has sorted column and order
  14. if( $request->has('column') && $request->has('order') && !empty($request->column) && !empty($request->order) ){
  15.  
  16. parse_str($url_array_full['query'], $output_array);
  17.  
  18. unset($output_array['column']);
  19. unset($output_array['order']);
  20. $queries = http_build_query($output_array);
  21.  
  22.  
  23.  
  24. $port = !empty($url_array_full['port']) ? $url_array_full['port'] : '';
  25.  
  26. if(!empty( $queries )){
  27. $sort_path = $url_array_full['scheme'] . '://' . $url_array_full['host'] .':'. $port . $url_array_full['path'] .'?'. $queries .'&';
  28. }else{
  29. $sort_path = $url_array_full['scheme'] . '://' . $url_array_full['host'] .':'. $port . $url_array_full['path'] .'?'. $queries;
  30. }
  31.  
  32.  
  33. }else{
  34. $sort_path = $full_path.'&';
  35. }
  36. }else{
  37. $sort_path = $full_path.'?';
  38. }
  39.  
  40.  
  41. //DB::connection()->enableQueryLog();
  42. $query = DB::table('shipment_data');
  43.  
  44.  
  45.  
  46.  
  47. $columns = array('Amazon order id', 'Merchant order id', 'Shipment id', 'Shipment item id', 'Amazon order item id', 'Merchant order item id', 'Purchase date', 'Payments date', 'Shipment date', 'Reporting date', 'Buyer email', 'Buyer name', 'Buyer phone number', 'Sku', 'Product name', 'Quantity shipped', 'Currency', 'Item price','Item tax', 'Shipping price', 'Shipping tax', 'Gift wrap price', 'Gift wrap tax', 'Ship service level', 'Recipient name', 'Ship address 1', 'Ship address 2','Ship address 3','Ship city', 'Ship state', 'Ship postal code', 'Ship country','Ship phone number', 'Bill address 1', 'Bill address 2', 'Bill address 3', 'Bill city', 'Bill state', 'Bill postal code', 'Bill country', 'Item promotion discount', 'Ship promotion discount', 'Carrier', 'Estimated arrival date', 'Fulfillment center id', 'Fulfillment channel', 'Sales channel', 'Date Created', 'Date Modified','Brand');
  48.  
  49. $column = $request->has('column') && in_array($request->column, $columns) ? $request->column : $columns[6];
  50. $sort_order = $request->has('order') && strtolower($request->order) == 'desc' ? 'DESC' : 'ASC';
  51.  
  52. $up_or_down = str_replace(array('ASC','DESC'), array('up','down'), $sort_order);
  53. $asc_or_desc = $sort_order == 'ASC' ? 'desc' : 'asc';
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. $upload_id = $request->upload_id;
  61. if( $upload_id == 'kabaflair' ){
  62. $brand_id = '1';
  63. }elseif( $upload_id == 'lightningfast' ){
  64. $brand_id = '2';
  65. }
  66.  
  67.  
  68. $query->selectRaw('*, shipment_data.id AS secret_id');
  69. $query->leftJoin('csv_data', 'csv_data.id', '=', 'csv_data_id');
  70. $query->leftJoin('brands', 'brands.id', '=', 'shipment_data.brand_id');
  71.  
  72.  
  73. $where_array = array();
  74.  
  75. if( $upload_id == 'kabaflair' || $upload_id == 'lightningfast') {
  76. $where_array[] = ['shipment_data.brand_id', '=', $brand_id];
  77. }else{
  78. $where_array[] = ['csv_data_id', '=', $upload_id];
  79. }
  80.  
  81. //set default filters after filter submission
  82. $filters_array = array();
  83. var_dump($filters_array);
  84.  
  85.  
  86.  
  87.  
  88. if( $request->has('filters') && !empty($request->filters)){
  89.  
  90. foreach( $request->filters as $key => $value ){
  91. $db_column = strtolower($value);
  92. $db_column = str_replace(' ', '_', $db_column);
  93. $filters_array[$key]['filter'] = $value;
  94. echo $key;
  95. echo $value;
  96. if( strpos( strtolower($value), "date" ) != false ){
  97. $date_start = $request->date_start[$key];
  98. $date_end = $request->date_end[$key];
  99.  
  100. $filters_array[$key]['date_start'] = $date_start;
  101. $filters_array[$key]['date_end'] = $date_end;
  102.  
  103.  
  104.  
  105. //if dates are provided check if start date is greater than end date
  106. if( $date_start > $date_end){
  107. return redirect()->back()->withErrors(['Start date should be before or same as end date']);
  108. }elseif( $date_start == $date_end ){
  109. $query->whereRaw('CAST( `'.$db_column.'` as DATE) = ?' ,[$date_start]);
  110. }else{
  111.  
  112. $query->whereRaw('CAST( `'.$db_column.'` as DATE) BETWEEN ? AND ?' ,[$date_start, $date_end]);
  113. }
  114.  
  115. }else{
  116. if( !empty($request->search[$key]) ){
  117. $search = $request->search[$key];
  118. if( $db_column == 'buyer_name' ){
  119. $where_array[] = [$db_column, 'like', '%'.$search.'%'];
  120. }else{
  121.  
  122. //$filters_array[$key]['search'] = $date_end;
  123. $where_array[] = [$db_column, '=', $search];
  124. }
  125.  
  126. }
  127.  
  128. }
  129.  
  130. }
  131. }
  132.  
  133.  
  134.  
  135. $where_array[] = ['deleted', '=', '0'];
  136. $query->where($where_array);
  137.  
  138.  
  139. if( $request->has('column') && $request->has('order') && !empty($request->column) && !empty($request->order) ){
  140.  
  141. $order_column = strtolower($request->column);
  142. $order_column = str_replace(' ', '_', $order_column);
  143.  
  144.  
  145. if( $order_column == 'brand' ){
  146. $order_column = 'shipment_data.brand_id';
  147. }
  148. $order = $request->order;
  149. }else{
  150. $order_column = 'purchase_date';
  151. $order = 'desc';
  152. }
  153.  
  154. //get filtered ids ready for multiple deletion
  155. $filtered_ids = array();
  156. if( $request->has('filters') && !empty($request->filters)){
  157. $filtered_ids = $query->pluck('shipment_data.secret_id');
  158. }
  159.  
  160.  
  161. $query->orderBy( $order_column, $order);
  162. $records = $query->paginate(50);
  163.  
  164.  
  165.  
  166.  
  167. //var_dump($records);
  168. //var_dump(DB::getQueryLog());
  169.  
  170. return view( 'pages/show_records', compact('records', 'upload_id', 'up_or_down','asc_or_desc', 'column', 'sort_path', 'filters_array', 'filtered_ids') );
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement