Advertisement
armen4g

Untitled

Jan 22nd, 2021
1,265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.90 KB | None | 0 0
  1. Data fetched from DB with filter and sort:
  2.  
  3. array:6 [
  4.   0 => array:8 [
  5.     "id" => 60
  6.     "country_id" => 811
  7.     "network" => "1"
  8.     "product_id" => 3
  9.     "group_zone" => null
  10.     "modifier" => "5"
  11.     "created_at" => null
  12.     "updated_at" => null
  13.   ]
  14.   1 => array:8 [
  15.     "id" => 53
  16.     "country_id" => 811
  17.     "network" => "1"
  18.     "product_id" => 1
  19.     "group_zone" => null
  20.     "modifier" => "3"
  21.     "created_at" => null
  22.     "updated_at" => null
  23.   ]
  24.   2 => array:8 [
  25.     "id" => 55
  26.     "country_id" => null
  27.     "network" => null
  28.     "product_id" => 3
  29.     "group_zone" => null
  30.     "modifier" => "2"
  31.     "created_at" => null
  32.     "updated_at" => null
  33.   ]
  34.   3 => array:8 [
  35.     "id" => 56
  36.     "country_id" => null
  37.     "network" => null
  38.     "product_id" => 3
  39.     "group_zone" => null
  40.     "modifier" => "2"
  41.     "created_at" => null
  42.     "updated_at" => null
  43.   ]
  44.   4 => array:8 [
  45.     "id" => 54
  46.     "country_id" => null
  47.     "network" => null
  48.     "product_id" => 2
  49.     "group_zone" => null
  50.     "modifier" => "2"
  51.     "created_at" => null
  52.     "updated_at" => null
  53.   ]
  54.   5 => array:8 [
  55.     "id" => 57
  56.     "country_id" => 812
  57.     "network" => null
  58.     "product_id" => 1
  59.     "group_zone" => null
  60.     "modifier" => "2"
  61.     "created_at" => null
  62.     "updated_at" => null
  63.   ]
  64. ]
  65.  
  66. Answer:
  67.  
  68. Illuminate\Support\Collection^ {#2351
  69.  #items: array:3 [
  70.    1 => "3"
  71.     2 => "2"
  72.     3 => "5"
  73.   ]
  74. }
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.     public function setModifiersForProducts($products, $network = null, Country $country = null)
  82.     {
  83.         $country = $country ?? $this->getCurrentCountry();
  84.         $country_id = $country->id;
  85.         $group_zone = $country->inEEA() ? "EEA" : null;
  86.  
  87.         $products_count = count($products);
  88.  
  89.         $result = ProductPriceModifier
  90.             ::where(fn($q) => $q
  91.                 ->where("network", $network)
  92.                 ->orWhere(fn($q) => $q->whereEmpty("network")))
  93.             ->where(fn($q) => $q
  94.                 ->where("country_id", $country_id)
  95.                 ->orWhere(fn($q) => $q->whereEmpty("country_id"))
  96.                 ->orWhere("group_zone", $group_zone))
  97.             ->where(fn($q) => $q
  98.                 ->whereIn("product_id", $products)
  99.                 ->orWhere(fn($q) => $q->whereEmpty("product_id")))
  100.             ->orderBy('network', 'desc')
  101.             ->orderBy('product_id', 'desc')
  102.             ->orderBy('country_id', 'desc')
  103.             ->get();
  104.  
  105.         dump($result->toArray());
  106.  
  107.         $ans = collect($products)->mapWithKeys(fn($item) => [$item => null]);
  108.  
  109.         while (true) {
  110.             if ($result->first()->network_id !== null && $result->first()->product_id === null) {
  111.                 $ans = $ans->map(fn($item, $key) => $item ?? $result->first()->modifier);
  112.                 break;
  113.             }
  114.  
  115.             $product_id = $result->first()->product_id;
  116.  
  117.             if ($ans[$product_id] === null) {
  118.                 $ans[$product_id] = $result->first()->modifier;
  119.                 $products_count--;
  120.             }
  121.  
  122.             if ($products_count === 0) {
  123.                 break;
  124.             }
  125.  
  126.             while ($result->first() && $result->first()['product_id'] === $product_id) {
  127.                 $result->shift();
  128.             }
  129.         }
  130.  
  131.         dump($ans);
  132.     }
  133.  
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement