Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.74 KB | None | 0 0
  1.     public function index()
  2.     {
  3.  
  4.         $failas = "akcijos.txt";
  5.         $directory  = "app/";
  6.         $failas = $directory.$failas;
  7.  
  8.         $myfile = fopen(storage_path($failas), "r");
  9.         $key = fread($myfile,filesize(storage_path($failas)));
  10.         fclose($myfile);
  11.  
  12.         $key = explode("||", $key);
  13.         $keyword = $key[0];
  14.  
  15.         //paieska, paprasta, isplestine
  16.         if($key[1]){
  17.             $pa = "%{$keyword}%";
  18.         }else{
  19.             $pa = "{$keyword}%";
  20.         }
  21.  
  22.         //reikia pasiziuret kuriose valstybese yra
  23.         //sutraukti musu gamybos, jei tik nori
  24.  
  25.         //Kai itraukiam akcija kodui, ir ieskom maziausia kaina, reikia ziureti ar turi konkretu sandseli, jei turi isskirti atskirai
  26.  
  27.  
  28.         $group = array();
  29.  
  30.         /*$group = DB::table('akcijos')
  31.         ->join('likutis', function ($q) use($pa) {
  32.             $q->on('akcijos.preke', '=', 'likutis.preke')
  33.             ->where('akcijos.preke', 'like', $pa);
  34.         })
  35.         ->select('akcijos.*', 'likutis.kaina as pradine')
  36.         ->get();*/
  37.         $directory  = "app/CSV_DATA/";
  38.         $failas = $directory."likutis.csv";
  39.        
  40.         if (($handle = fopen(storage_path($failas), "r")) !== FALSE) {
  41.             while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
  42.                 $duomenys = mb_convert_encoding($data, "UTF-8", "ISO-8859-13");
  43.                 $r[] = $duomenys[0];
  44.                 $rr[$duomenys[0]] = array();
  45.             }
  46.             fclose($handle);
  47.         }
  48.  
  49.         $query_p = Akcijos::query();
  50.         $query_p->whereIn('preke', $r);
  51.         //$query_p->where('preke', $r[$i]);
  52.         $re = $query_p->get();
  53.  
  54.         foreach ( $re as $value ) {
  55.             if(strtotime($value['galioja_iki']) > strtotime(date("Y-m-d H:i:s"))){
  56.             if (!array_key_exists($value['preke'], $group)){
  57.                 $group[$value['preke']] = $value;
  58.             }else{
  59.                if(floatval($value['kaina']) < floatval($group[$value['preke']]['kaina'])){
  60.                    if($value['sandelis'] == ""){
  61.                     $group[$value['preke']] = $value;
  62.                    }else{
  63.                     $group[$value['preke']."-||SAND"] = $value;  
  64.                    }
  65.                 }
  66.             }
  67.         }
  68.         }
  69.  
  70.         $group = array_merge($rr, $group);
  71.  
  72.          //Sudedam norimus likucius
  73.          $query = Likutis::query();
  74.          $query->whereIn('preke', $r);
  75.          $re = $query->get();
  76.  
  77.          foreach ( $re as $value ) {
  78.             if (array_key_exists($value['preke'], $group)){
  79.                 if (!array_key_exists('pradine', $group[$value['preke']])){
  80.                     $group[$value['preke']]['pradine'] = $value['kaina'];
  81.                 }
  82.                 if (!array_key_exists('preke', $group[$value['preke']])){
  83.                     $group[$value['preke']]['preke'] = $value['preke'];
  84.                 }
  85.                 //if($value['salis'] == 1){
  86.                     //neveikia sita vieta jei parasau gale []
  87.                     $group[$value['preke']]['sandeliai'][] = $value['sandelis'];
  88.                 //}
  89.                 $group[$value['preke']]['likutis'] += $value['kiekis'];
  90.         }else{
  91.             $group[$value['preke']]['kaina'] = 'NΔ–RA';
  92.             $group[$value['preke']]['preke'] = $value['preke'];
  93.             $group[$value['preke']]['pradine'] = $value['kaina'];
  94.  
  95.             $group[$value['preke']]['likutis'] += $value['kiekis'];
  96.         }
  97.         }
  98.  
  99.         ksort($group);
  100.         $group = array_values($group);
  101.    
  102.         return response()->json([
  103.             'status' => true,
  104.             'paieska' => $keyword,
  105.             'data' => $group,
  106.             'likutis' => $re,
  107.             'paieska_big' => $key[1],
  108.         ]);
  109.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement