[
    {
        "id": 1,
        "formula_name": "EuropeExpressFormula",
        "formula": "$country_distance_cost = 0;\r\n$requested_volumes = $volume;\r\n\r\n        $pricePerKM = 0.7;\r\n\r\n        $countryPricePerKM = [\r\n            \"BE\"=>0.6,\r\n            \"BG\"=>0.6,\r\n            \"CZ\"=>0.65,\r\n            \"DK\"=>0.65,\r\n            \"EE\"=>0.8,\r\n            \"FI\"=>0.75,\r\n            \"GR\"=>0.65,\r\n            \"HU\"=>0.6,\r\n            \"IE\"=>0.75,\r\n            \"IT\"=>0.75,\r\n            \"LV\"=>0.7,\r\n            \"LT\"=>0.7,\r\n            \"NO\"=>0.8,\r\n            \"PL\"=>0.6,\r\n            \"PT\"=>0.7,\r\n            \"RO\"=>0.6,\r\n            \"SK\"=>0.65,\r\n            \"ES\"=>0.65,\r\n            \"SE\"=>0.75,\r\n            \"CH\"=>0.8,\r\n            \"UK\"=>0.8,\r\n            \"FR\"=>0.7,\r\n            \"AT\"=>0.7\r\n        ];\r\n\r\n        foreach ($distance_by_country as $country => $country_distance) {\r\n            $distance -= $country_distance;\r\n\r\n            if (array_key_exists($country, $countryPricePerKM)) {\r\n                $thisCountryCost = $country_distance * $countryPricePerKM[$country];\r\n            } else {\r\n                $thisCountryCost = $country_distance * $pricePerKM;\r\n            }\r\n\r\n            $country_distance_cost += $thisCountryCost;\r\n        }\r\n\r\n        $ferriesCost = 0;\r\n\r\n        foreach ($ferries as $ferry) {\r\n            $ferriesCost += $ferry[\"price\"];\r\n        }\r\n\r\n        $distanceCost = $distance * $pricePerKM;\r\n\r\n        $costOfRoute = $country_distance_cost + $distanceCost + $ferriesCost;\r\n        \r\n        $costModifier = [\r\n            ['volume' => 1, 'modifier' => 0.50],\r\n            ['volume' => 2, 'modifier' => 0.50],\r\n            ['volume' => 3, 'modifier' => 0.50],\r\n            ['volume' => 4, 'modifier' => 0.50],\r\n            ['volume' => 5, 'modifier' => 0.50],\r\n            ['volume' => 6, 'modifier' => 0.55],\r\n            ['volume' => 7, 'modifier' => 0.60],\r\n            ['volume' => 8, 'modifier' => 0.65],\r\n            ['volume' => 9, 'modifier' => 0.70],\r\n            ['volume' => 10, 'modifier' => 0.75],\r\n            ['volume' => 11, 'modifier' => 0.80],\r\n            ['volume' => 12, 'modifier' => 0.85],\r\n            ['volume' => 13, 'modifier' => 0.90],\r\n            ['volume' => 14, 'modifier' => 0.95],\r\n            ['volume' => 15, 'modifier' => 1],\r\n            ['volume' => 16, 'modifier' => 1],\r\n            ['volume' => 17, 'modifier' => 1],\r\n            ['volume' => 18, 'modifier' => 1],\r\n            ['volume' => 19, 'modifier' => 1],\r\n            ['volume' => 20, 'modifier' => 1]\r\n        ];\r\n\r\n        $modifiedCost = [];\r\n\r\n        foreach ($costModifier as $item) {\r\n             $modifiedCost[$item['volume']] = round(($costOfRoute ) * $item['modifier']);\r\n        }\r\n\r\n        $customerCost = [];\r\n\r\n        foreach ($volume as $value) {\r\n            $customerCost[$value] = $modifiedCost[$value];\r\n        }\r\n\r\n        $markup = 500;\r\n\r\n        $geoMarkup = [\r\n                        \"BE\"=>400,\r\n                        \"BG\"=>300,\r\n                        \"CZ\"=>300,\r\n                        \"DK\"=>600,\r\n                        \"EE\"=>700,\r\n                        \"FI\"=>700,\r\n                        \"GR\"=>400,\r\n                        \"HU\"=>300,\r\n                        \"IE\"=>600,\r\n                        \"IT\"=>600,\r\n                        \"LV\"=>300,\r\n                        \"LT\"=>300,\r\n                        \"NO\"=>600,\r\n                        \"PL\"=>400,\r\n                        \"PT\"=>450,\r\n                        \"RO\"=>300,\r\n                        \"SK\"=>400,\r\n                        \"ES\"=>600,\r\n                        \"SE\"=>500,\r\n                        \"CH\"=>800,\r\n                        \"GB\"=>450,\r\n                        \"FR\"=>450,\r\n                        \"AT\"=>450\r\n         ];\r\n\r\n        if (($to_c_code == \"GB\" || $from_c_code == \"GB\") && isset($geoMarkup[$to_c_code])) {\r\n            $markup = $geoMarkup[$to_c_code];\r\n        }\r\n\r\n        if ($to_c_code == \"IE\" || $from_c_code== \"IE\") {\r\n            $markup = $geoMarkup[\"IE\"];\r\n        }\r\n\r\n        $fixedMarkup = $markup/2;\r\n\r\n        $volumeModifier = [\r\n            ['volume' => 1, 'modifier' => 0.05],\r\n            ['volume' => 2, 'modifier' => 0.10],\r\n            ['volume' => 3, 'modifier' => 0.15],\r\n            ['volume' => 4, 'modifier' => 0.20],\r\n            ['volume' => 5, 'modifier' => 0.25],\r\n            ['volume' => 6, 'modifier' => 0.30],\r\n            ['volume' => 7, 'modifier' => 0.35],\r\n            ['volume' => 8, 'modifier' => 0.40],\r\n            ['volume' => 9, 'modifier' => 0.45],\r\n            ['volume' => 10, 'modifier' => 0.50],\r\n            ['volume' => 11, 'modifier' => 0.55],\r\n            ['volume' => 12, 'modifier' => 0.60],\r\n            ['volume' => 13, 'modifier' => 0.65],\r\n            ['volume' => 14, 'modifier' => 0.70],\r\n            ['volume' => 15, 'modifier' => 0.75],\r\n            ['volume' => 16, 'modifier' => 0.80],\r\n            ['volume' => 17, 'modifier' => 0.85],\r\n            ['volume' => 18, 'modifier' => 0.90],\r\n            ['volume' => 19, 'modifier' => 0.95],\r\n            ['volume' => 20, 'modifier' => 1]\r\n        ];\r\n\r\n        $prices = [];\r\n\r\n        foreach ($volumeModifier as $item) {\r\n             $prices[$item['volume']] = round($modifiedCost[$item['volume']]+$fixedMarkup+(($fixedMarkup ) * $item['modifier']));\r\n        }\r\n\r\n      \r\n        $total_price = 0;\r\n        $total_price = $prices[$volume[0]];\r\n        $customerPrices = [];\r\n\r\n        foreach ($volume as $volume) {\r\n            $customerPrices[$volume] = $prices[$volume];\r\n        }\r\n\r\n        return [\r\n            'has_null_ferries' => collect($ferries)->contains(function ($value) {\r\n                return $value['price'] == 0;\r\n            }),\r\n            'cost_of_route' => round($costOfRoute),\r\n            'markup' =>  $markup,\r\n            'ferries' => $ferries,\r\n            'mass' => $mass,\r\n            'volume' => $requested_volumes,\r\n            'total_distance_through_zones' => collect($zones)->sum('distance'),\r\n            'formula_id' => 1,\r\n            'formula_name' => $formula,\r\n            'total_price' => $total_price,\r\n            'prices' => $customerPrices,\r\n            'total_duration' => $trip_duration,\r\n            'from' => $from,\r\n            'to' => $to,\r\n            'distance_by_country' => $distance_by_country,\r\n            'cost_of_route_volume' => $customerCost\r\n        ];",
        "formula_encode": null,
        "user_id": 1,
        "created_at": "2021-07-09 10:45:58",
        "updated_at": "2022-12-22 05:05:52"
    },
    {
        "id": 2,
        "formula_name": "Test Formula",
        "formula": "$country_distance_cost = 0;\r\n$requested_volumes = $volume;\r\n\r\n        $pricePerKM = 0.7;\r\n\r\n        $countryPricePerKM = [\r\n            \"BE\"=>0.6,\r\n            \"BG\"=>0.6,\r\n            \"CZ\"=>0.65,\r\n            \"DK\"=>0.65,\r\n            \"EE\"=>0.8,\r\n            \"FI\"=>0.75,\r\n            \"GR\"=>0.65,\r\n            \"HU\"=>0.6,\r\n            \"IE\"=>0.75,\r\n            \"IT\"=>0.75,\r\n            \"LV\"=>0.7,\r\n            \"LT\"=>0.7,\r\n            \"NO\"=>0.8,\r\n            \"PL\"=>0.6,\r\n            \"PT\"=>0.7,\r\n            \"RO\"=>0.6,\r\n            \"SK\"=>0.65,\r\n            \"ES\"=>0.65,\r\n            \"SE\"=>0.75,\r\n            \"CH\"=>0.8,\r\n            \"UK\"=>0.8,\r\n            \"FR\"=>0.7,\r\n            \"AT\"=>0.7\r\n        ];\r\n        $totalDistance = $distance;\r\n        foreach ($distance_by_country as $country => $country_distance) {\r\n            $distance -= $country_distance;\r\n\r\n            if (array_key_exists($country, $countryPricePerKM)) {\r\n                $thisCountryCost = $country_distance * $countryPricePerKM[$country];\r\n            } else {\r\n                $thisCountryCost = $country_distance * $pricePerKM;\r\n            }\r\n\r\n            $country_distance_cost += $thisCountryCost;\r\n        }\r\n\r\n        $ferriesCost = 0;\r\n\r\n        foreach ($ferries as $ferry) {\r\n            $ferriesCost += $ferry[\"price\"];\r\n        }\r\n\r\n        $distanceCost = $distance * $pricePerKM;\r\n\r\n        $costOfRoute = $country_distance_cost + $distanceCost + $ferriesCost;\r\n        \r\n        $costModifier = [\r\n            ['volume' => 1, 'modifier' => 0.50],\r\n            ['volume' => 2, 'modifier' => 0.50],\r\n            ['volume' => 3, 'modifier' => 0.50],\r\n            ['volume' => 4, 'modifier' => 0.50],\r\n            ['volume' => 5, 'modifier' => 0.50],\r\n            ['volume' => 6, 'modifier' => 0.55],\r\n            ['volume' => 7, 'modifier' => 0.60],\r\n            ['volume' => 8, 'modifier' => 0.65],\r\n            ['volume' => 9, 'modifier' => 0.70],\r\n            ['volume' => 10, 'modifier' => 0.75],\r\n            ['volume' => 11, 'modifier' => 0.80],\r\n            ['volume' => 12, 'modifier' => 0.85],\r\n            ['volume' => 13, 'modifier' => 0.90],\r\n            ['volume' => 14, 'modifier' => 0.95],\r\n            ['volume' => 15, 'modifier' => 1],\r\n            ['volume' => 16, 'modifier' => 1],\r\n            ['volume' => 17, 'modifier' => 1],\r\n            ['volume' => 18, 'modifier' => 1],\r\n            ['volume' => 19, 'modifier' => 1],\r\n            ['volume' => 20, 'modifier' => 1]\r\n        ];\r\n\r\n        $modifiedCost = [];\r\n\r\n        foreach ($costModifier as $item) {\r\n             $modifiedCost[$item['volume']] = round(($costOfRoute ) * $item['modifier']);\r\n        }\r\n\r\n        $customerCost = [];\r\n\r\n        foreach ($volume as $value) {\r\n            $customerCost[$value] = $modifiedCost[$value];\r\n        }\r\n\r\n        $markup = 500;\r\n\r\n        $geoMarkup = [\r\n                        \"BE\"=>400,\r\n                        \"BG\"=>300,\r\n                        \"CZ\"=>300,\r\n                        \"DK\"=>600,\r\n                        \"EE\"=>700,\r\n                        \"FI\"=>700,\r\n                        \"GR\"=>400,\r\n                        \"HU\"=>300,\r\n                        \"IE\"=>600,\r\n                        \"IT\"=>600,\r\n                        \"LV\"=>300,\r\n                        \"LT\"=>300,\r\n                        \"NO\"=>600,\r\n                        \"PL\"=>400,\r\n                        \"PT\"=>450,\r\n                        \"RO\"=>300,\r\n                        \"SK\"=>400,\r\n                        \"ES\"=>600,\r\n                        \"SE\"=>500,\r\n                        \"CH\"=>800,\r\n                        \"GB\"=>450,\r\n                        \"FR\"=>450,\r\n                        \"AT\"=>450\r\n         ];\r\n\r\n        if ($to_c_code == \"GB\" || $from_c_code== \"GB\") {\r\n            $markup = $geoMarkup[$to_c_code];\r\n        }\r\n\r\n        if ($to_c_code == \"IE\" || $from_c_code== \"IE\") {\r\n            $markup = $geoMarkup[\"IE\"];\r\n        }\r\n\r\n        $fixedMarkup = $markup/2;\r\n\r\n        $volumeModifier = [\r\n            ['volume' => 1, 'modifier' => 0.05],\r\n            ['volume' => 2, 'modifier' => 0.10],\r\n            ['volume' => 3, 'modifier' => 0.15],\r\n            ['volume' => 4, 'modifier' => 0.20],\r\n            ['volume' => 5, 'modifier' => 0.25],\r\n            ['volume' => 6, 'modifier' => 0.30],\r\n            ['volume' => 7, 'modifier' => 0.35],\r\n            ['volume' => 8, 'modifier' => 0.40],\r\n            ['volume' => 9, 'modifier' => 0.45],\r\n            ['volume' => 10, 'modifier' => 0.50],\r\n            ['volume' => 11, 'modifier' => 0.55],\r\n            ['volume' => 12, 'modifier' => 0.60],\r\n            ['volume' => 13, 'modifier' => 0.65],\r\n            ['volume' => 14, 'modifier' => 0.70],\r\n            ['volume' => 15, 'modifier' => 0.75],\r\n            ['volume' => 16, 'modifier' => 0.80],\r\n            ['volume' => 17, 'modifier' => 0.85],\r\n            ['volume' => 18, 'modifier' => 0.90],\r\n            ['volume' => 19, 'modifier' => 0.95],\r\n            ['volume' => 20, 'modifier' => 1]\r\n        ];\r\n\r\n        $prices = [];\r\n\r\n        foreach ($volumeModifier as $item) {\r\n             $prices[$item['volume']] = round($modifiedCost[$item['volume']]+$fixedMarkup+(($fixedMarkup ) * $item['modifier']));\r\n        }\r\n\r\n      \r\n        $total_price = 0;\r\n        $total_price = $prices[$volume[0]];\r\n        $customerPrices = [];\r\n\r\n        foreach ($volume as $volume) {\r\n            if($totalDistance < 400){\r\n                $customerPrices[$volume] = $prices[$volume] + 200;\r\n            }\r\n            else{\r\n                $customerPrices[$volume] = $prices[$volume];\r\n            }\r\n        }\r\n\r\n        return [\r\n            'has_null_ferries' => collect($ferries)->contains(function ($value) {\r\n                return $value['price'] == 0;\r\n            }),\r\n            'cost_of_route' => round($costOfRoute),\r\n            'markup' =>  $markup,\r\n            'ferries' => $ferries,\r\n            'mass' => $mass,\r\n            'volume' => $requested_volumes,\r\n            'total_distance_through_zones' => collect($zones)->sum('distance'),\r\n            'formula_id' => 1,\r\n            'formula_name' => $formula,\r\n            'total_price' => $total_price,\r\n            'prices' => $customerPrices,\r\n            'total_duration' => $trip_duration,\r\n            'from' => $from,\r\n            'to' => $to,\r\n            'distance_by_country' => $distance_by_country,\r\n            'cost_of_route_volume' => $customerCost,\r\n            'distance' => $totalDistance\r\n        ];",
        "formula_encode": null,
        "user_id": 1,
        "created_at": "2021-08-09 01:15:41",
        "updated_at": "2022-09-13 09:23:06"
    },
    {
        "id": 3,
        "formula_name": "VanOne",
        "formula": "$withinEuUnsupportedCountries = [\r\n            'NO' => 'Norway',\r\n            'CH' => 'Switzerland',\r\n            'IE' => 'Ireland',\r\n            'MT' => 'Malta'\r\n        ];\r\n        $withinEuSupportedCountries = [\r\n            'AT' => \"Austria\",\r\n            'BE' => \"Belgium\",\r\n            'BG' => 'Bulgaria',\r\n            'HR' => 'Croatia',\r\n            'CZ' => 'Czech Republic',\r\n            'DK' => 'Denmark',\r\n            'EE' => 'Estonia',\r\n            'FI' => 'Finland',\r\n            'FR' => 'France',\r\n            'DE' => 'Germany',\r\n            'GR' => 'Greece',\r\n            'HU' => 'Hungary',\r\n            'IT' => 'Italy',\r\n            'LV' => 'Latvia',\r\n            'LT' => 'Lithuania',\r\n            'LU' => 'Luxembourg',\r\n            'NL' => 'Netherlands',\r\n            'PL' => 'Poland',\r\n            'PT' => 'Portugal',\r\n            'RO' => 'Romania',\r\n            'SK' => 'Slovakia',\r\n            'SI' => 'Slovenia',\r\n            'ES' => 'Spain',\r\n            'SE' => 'Sweden',\r\n        ];\r\n\r\n\r\n        $sizeMinPrices = [\r\n            5=>127.29,\r\n            10=>169.72,\r\n            15=>169.72,\r\n            30=>339.44\r\n        ];\r\n        $sizes = ['5', '10', '15'];\r\n\r\n        /* get pricing data*/\r\n        $withInEu = array_key_exists($to_c_code, $withinEuSupportedCountries)\r\n            && array_key_exists($from_c_code, $withinEuSupportedCountries);\r\n        if ($withInEu) {\r\n            $pricingDataCountryCode = 'EU';\r\n        } elseif ($to_c_code == 'GB') {\r\n            $pricingDataCountryCode = $from_c_code;\r\n        } else {\r\n            $pricingDataCountryCode = $to_c_code;\r\n        }\r\n        if (!isset($vanOnePricingData[$pricingDataCountryCode])) {\r\n            /*default pricing data*/\r\n            Log::report(['Message'=>'Vanone pricing data not found for from '.$from.' to '.$to.' default pricing was used'],'warning');\r\n            $pricingDataCountryCode = 'ZZ';\r\n        }\r\n        $pricingData = $vanOnePricingData[$pricingDataCountryCode];\r\n        /*check null ferries*/\r\n        $nullFerries = false;\r\n        $nullFerryName = '';\r\n        $totalFerryCost = 0;\r\n        foreach ($ferries as $ferry) {\r\n            if ($ferry['price'] == 0) {\r\n                $nullFerries = true;\r\n                $nullFerryName = $ferry['name'];\r\n                break;\r\n            } else {\r\n                $totalFerryCost += $ferry['price'];\r\n            }\r\n        }\r\n        if ($nullFerries) {\r\n            return [\r\n                'success' => false,\r\n                'message' => \"Cost of ferry: \" . $nullFerryName . \" was not set, for the route: From \" . $from . \" to \" . $to\r\n            ];\r\n        }\r\n        /*calculate customized ferry cost*/\r\n        $customFerryCost=0;\r\n        $totalZoneCost = 0;\r\n            foreach ($zones as $zone) {\r\n                if ($zone['zone'] == 'dover_ferry') {\r\n                    $customFerryCost = $zone['price'];\r\n                }else{\r\n                    if ($zone[\"fixed\"]){\r\n                        $zonePrice = $zone['price'];\r\n                    }else {\r\n                        $zonePrice = $zone['price']*$zone['distance'];\r\n                    }\r\n                    $totalZoneCost = $totalZoneCost+$zonePrice;\r\n                }\r\n            }\r\n         $totalFerryCost+=$customFerryCost;\r\n        /*calculate place of supply*/\r\n        $euDistance = 0;\r\n        foreach ($distance_by_country as $code => $countryDistance) {\r\n            if ($code != 'NO' && $code != 'UK' && $code != 'CH') {\r\n                $euDistance += $countryDistance;\r\n            }\r\n        }\r\n        $placeOfSupplyAr = array_filter($distance_by_country, function ($key) {\r\n            return ($key == 'NO' || $key == 'UK' || $key == 'CH');\r\n        }, ARRAY_FILTER_USE_KEY);\r\n        $placeOfSupplyAr['EU'] = $euDistance;\r\n        asort($placeOfSupplyAr);\r\n        $placeOfSupply = array_key_last($placeOfSupplyAr);\r\n\r\n        //Ireland exception\r\n        if (strpos(mb_strtolower($from), 'northern ireland')) {\r\n            $placeOfSupply = \"UK\";\r\n        } elseif (strpos(mb_strtolower($to), \"northern ireland\")) {\r\n            $placeOfSupply = \"EU\";\r\n        }\r\n        /*get vat rate*/\r\n        $vatRate = 0;\r\n        if($placeOfSupply==\"UK\") {\r\n            $vatRate = 0.2;\r\n        }\r\n        /*get margin*/\r\n        $margin = 0;\r\n        if (($from_c_code=='GB'|| $to_c_code==\"GB\") && $placeOfSupply=='EU') {\r\n            if ($from_c_code=='GB') {\r\n                 $margin = $vanOnePricingData[$to_c_code][\"eu_country_margin\"]['import_margin'] ?? 0;\r\n            }elseif ($to_c_code=='GB') {\r\n                $margin = $vanOnePricingData[$from_c_code][\"eu_country_margin\"]['export_margin'] ?? 0;\r\n            }\r\n        }\r\n\r\n        /*get customs price*/\r\n        $exportCustomsCost = $vanOnePricingData[$from_c_code]['customs_price'][$to_c_code]['export']['cost'] ?? 0;\r\n        $importCustomsCost = $vanOnePricingData[$to_c_code]['customs_price'][$from_c_code]['import']['cost'] ?? 0;\r\n        $exportCustomsMargin = $vanOnePricingData[$from_c_code]['customs_price'][$to_c_code]['export']['margin'] ?? 0;\r\n        $importCustomsMargin = $vanOnePricingData[$to_c_code]['customs_price'][$from_c_code]['import']['margin'] ?? 0;\r\n\r\n        $customsCost = $exportCustomsCost + $importCustomsCost;\r\n        $customsPrice = $customsCost + $exportCustomsMargin + $importCustomsMargin;\r\n\r\n\r\n        /*calculate final prices*/\r\n        $sizePricing = [];\r\n        foreach ($sizes as $size) {\r\n            $sizeData = $pricingData['size_data'][$size];\r\n            $ppkPercentage = $sizeData['ppk_percentage'];\r\n            $distancePricingData = $sizeData['distance_pricing'][$distanceType];\r\n            $ppkCost = $distancePricingData['ppk_cost'];\r\n            $ferryCostPercentage = $distancePricingData['ferry_cost_percentage'];\r\n            $markUpPercentage = $distancePricingData['markup_percentage'];\r\n            $totalPPKCost = (($ppkCost * $distance) * $ppkPercentage);\r\n            $markUp = $pricingData['total_markup'] * $markUpPercentage;\r\n            $ferryCost = $totalFerryCost * $ferryCostPercentage;\r\n            $minCost = ($distance<500)?$sizeMinPrices[$size]:0;\r\n            $total = $totalPPKCost + $markUp + $ferryCost + $customsPrice + $minCost + $totalZoneCost;\r\n            $total=$total+($total*$margin);\r\n            $totalWithVat = $total*(1+$vatRate);\r\n            $sizePricing [$size] = [\r\n                'ppk_cost' => $totalPPKCost,\r\n                'markUp' => $markUp,\r\n                'ferryCost' => $ferryCost,\r\n                'total' => $total,\r\n                'totalWithVat' => $totalWithVat,\r\n            ];\r\n        }\r\n        /*add 30m3 prices*/\r\n        $sizePricing[30] = [\r\n            'ppk_cost' => ($sizePricing[15]['ppk_cost']) * 2,\r\n            'markUp' =>  ($sizePricing[15]['markUp']) * 2,\r\n            'ferryCost' =>  ($sizePricing[15]['ferryCost']) * 2,\r\n            'total' =>  ($sizePricing[15]['total']) * 2,\r\n            'totalWithVat' =>  ($sizePricing[15]['totalWithVat']) * 2,\r\n        ];\r\n        $costData = [];\r\n        foreach ($sizePricing as $size => $cost) {\r\n            $costData[$size]=[\r\n                'ppk_cost'=>FormulaService::formatNumber($cost['ppk_cost'],2),\r\n                'markUp'=>FormulaService::formatNumber($cost['markUp'],2),\r\n                'ferryCost'=>FormulaService::formatNumber($cost['ferryCost'],2),\r\n            ];\r\n        }\r\n        $data = [\r\n            \"calculations\" => [\r\n                \"Total\" => [\r\n                    \"5m^3\" => FormulaService::formatNumber($sizePricing[5]['totalWithVat']),\r\n                    \"10m^3\" => FormulaService::formatNumber($sizePricing[10]['totalWithVat']),\r\n                    \"15m^3\" => FormulaService::formatNumber($sizePricing[15]['totalWithVat']),\r\n                    \"30m^3\" => FormulaService::formatNumber($sizePricing[30]['totalWithVat']),\r\n                ],\r\n                \"Duration\" => $durationData,\r\n                \"Place_of_Supply\" => $placeOfSupply,\r\n                \"Customs_price\" => FormulaService::formatNumber($customsPrice,2),\r\n                \"Customs_cost\" => FormulaService::formatNumber($customsCost,2),\r\n                \"Customs_pricing_data\"=>[\r\n                                'exportCustomsCost'=> $exportCustomsCost,\r\n                                'exportCustomsMargin'=>$exportCustomsMargin,\r\n                                'importCustomsCost'=>$importCustomsCost,\r\n                                'importCustomsMargin'=>$importCustomsMargin\r\n                            ],\r\n                \"totalDistance\"=>$distance,\r\n                \"Markup\" => FormulaService::formatNumber($sizePricing[15]['markUp'],2),\r\n                \"cost\" => $costData\r\n            ],\r\n        ];\r\n        return [\r\n            'success' => true,\r\n            'data' => $data,\r\n           'timeStamp'=>time()\r\n        ];",
        "formula_encode": null,
        "user_id": 3,
        "created_at": "2022-11-08 11:23:49",
        "updated_at": "2023-05-26 09:33:27"
    },
    {
        "id": 4,
        "formula_name": "europeExpressFormula V2.0",
        "formula": "$belowMinDistancePPk = 0.65;\r\n            /* get distance key*/\r\n            if ($distance < 300) {\r\n                $distanceType = \"below_300\";\r\n            } elseif ($distance <= 500) {\r\n                $distanceType = \"below_500\";\r\n            } elseif ($distance <= 1000) {\r\n                $distanceType = \"below_1000\";\r\n            } elseif ($distance <= 2000) {\r\n                $distanceType = \"below_2000\";\r\n            } elseif ($distance <= 3000) {\r\n                $distanceType = \"below_3000\";\r\n            } else {\r\n                $distanceType = \"above_3000\";\r\n            }\r\n            /*add customized ferry cost*/\r\n            foreach ($zones as $zone) {\r\n                if ($zone['zone'] == 'dover_ferry') {\r\n                    $ferries[] = [\r\n                        \"name\" => $zone['zone'],\r\n                        \"price\" => $zone['price'],\r\n                    ];\r\n                }\r\n            }\r\n            /*get ferry cost*/\r\n            $nullFerries = false;\r\n            $totalFerryCost = 0;\r\n            foreach ($ferries as $ferry) {\r\n                if ($ferry['price'] == 0) {\r\n                    $nullFerries = true;\r\n                    break;\r\n                } else {\r\n                    $totalFerryCost += $ferry['price'];\r\n                }\r\n            }\r\n            /*calculate ppk costs*/\r\n            $totalSizePpkCosts=[];\r\n            foreach ($distance_by_country as $country => $country_distance) {\r\n                if ($country==\"UK\") {\r\n                    $country = \"GB\";\r\n                }\r\n                if (!isset($pricingData[$country])) {\r\n                    $country = \"ZZ\";\r\n                }\r\n                $totalPPK = $pricingData[$country][\"total_ppk\"];\r\n                $distancePPK = $totalPPK *  $pricingData[$country][\"distance_ppk_rate\"][$distanceType];\r\n                foreach ($volume as $size) {\r\n                    $sizeRate = $pricingData[$country][\"size_rates\"][$size];\r\n                    $sizePPK = $distancePPK *  $sizeRate[\"ppk_rate\"];\r\n                    $ppkCost = $sizePPK * $country_distance;\r\n                    $prevCountrySizeTotal = $totalSizePpkCosts[$size]??0;\r\n                    $totalSizePpkCosts[$size]= $prevCountrySizeTotal+$ppkCost;\r\n                }\r\n            }\r\n            /* add min ppk cost for quotes below 300km*/\r\n            $minDistanceCost = 0;\r\n            if($distanceType==\"below_300\"){\r\n                $distanceGap = 300 - $distance;\r\n                $minDistanceCost = $distanceGap * $belowMinDistancePPk;\r\n            }\r\n\r\n            if (!isset($pricingData[$to_c_code])) {\r\n                $to_c_code = \"ZZ\";\r\n            }\r\n\r\n            $totalMarkup = $pricingData[$to_c_code][\"total_markup\"];\r\n            $sizePrices =[];\r\n            $markups=[];\r\n            foreach ($volume as $size) {\r\n                $sizeRate = $pricingData[$to_c_code][\"size_rates\"][$size];\r\n                $sizeMarkUp = $totalMarkup *  $sizeRate[\"markup_rate\"];\r\n                $markups[$size]= $sizeMarkUp;\r\n                $totalCost = $sizeMarkUp + $totalSizePpkCosts[$size] + $totalFerryCost + $minDistanceCost;\r\n                $sizePrices[$size] = FormulaService::formatNumber($totalCost);\r\n            }\r\n            return [\r\n                'has_null_ferries' => $nullFerries,\r\n                'cost_of_route' => $totalSizePpkCosts,\r\n                'markup' =>  $markups,\r\n                'ferries' => $ferries,\r\n                'mass' => $mass,\r\n                'volume' => $volume,\r\n                'total_distance_through_zones' => collect($zones)->sum('distance'),\r\n                'total_price' => $sizePrices[$volume[0]],\r\n                'prices' => $sizePrices,\r\n                'total_duration' => $trip_duration,\r\n                'from' => $from,\r\n                'to' => $to,\r\n                'distance_by_country' => $distance_by_country,\r\n                'total_distance'=>$distance\r\n            ];",
        "formula_encode": null,
        "user_id": 1,
        "created_at": "2023-01-03 02:53:51",
        "updated_at": "2023-01-03 02:53:51"
    },
    {
        "id": 5,
        "formula_name": "muveone",
        "formula": "$withinEuUnsupportedCountries = [\r\n                'NO' => 'Norway',\r\n                'CH' => 'Switzerland',\r\n                'IE' => 'Ireland',\r\n                'MT' => 'Malta'\r\n            ];\r\n            $withinEuSupportedCountries = [\r\n                'AT' => \"Austria\",\r\n                'BE' => \"Belgium\",\r\n                'BG' => 'Bulgaria',\r\n                'HR' => 'Croatia',\r\n                'CZ' => 'Czech Republic',\r\n                'DK' => 'Denmark',\r\n                'EE' => 'Estonia',\r\n                'FI' => 'Finland',\r\n                'FR' => 'France',\r\n                'DE' => 'Germany',\r\n                'GR' => 'Greece',\r\n                'HU' => 'Hungary',\r\n                'IT' => 'Italy',\r\n                'LV' => 'Latvia',\r\n                'LT' => 'Lithuania',\r\n                'LU' => 'Luxembourg',\r\n                'NL' => 'Netherlands',\r\n                'PL' => 'Poland',\r\n                'PT' => 'Portugal',\r\n                'RO' => 'Romania',\r\n                'SK' => 'Slovakia',\r\n                'SI' => 'Slovenia',\r\n                'ES' => 'Spain',\r\n                'SE' => 'Sweden',\r\n            ];\r\n\r\n\r\n            $sizeMinPrices = [\r\n                5 => 127.29,\r\n                10 => 169.72,\r\n                15 => 169.72,\r\n                30 => 339.44\r\n            ];\r\n            $sizes = ['5', '10', '15'];\r\n\r\n            /* get pricing data*/\r\n            $withInEu = array_key_exists($to_c_code, $withinEuSupportedCountries)\r\n                && array_key_exists($from_c_code, $withinEuSupportedCountries);\r\n            if ($withInEu) {\r\n                $pricingDataCountryCode = 'EU';\r\n            } elseif ($to_c_code == 'GB') {\r\n                $pricingDataCountryCode = $from_c_code;\r\n            } else {\r\n                $pricingDataCountryCode = $to_c_code;\r\n            }\r\n            if (!isset($muveOnePricingData[$pricingDataCountryCode])) {\r\n                /*default pricing data*/\r\n                Log::report(['Message' => 'Muveone pricing data not found for from ' . $from . ' to ' . $to . ' default pricing was used'], 'warning');\r\n                $pricingDataCountryCode = 'ZZ';\r\n            }\r\n            $pricingData = $muveOnePricingData[$pricingDataCountryCode];\r\n            /*check null ferries*/\r\n            $nullFerries = false;\r\n            $totalFerryCost = 0;\r\n            foreach ($ferries as $ferry) {\r\n                if ($ferry['price'] == 0) {\r\n                    $nullFerries = true;\r\n                    break;\r\n                } else {\r\n                    $totalFerryCost += $ferry['price'];\r\n                }\r\n            }\r\n\r\n            /*calculate customized ferry cost*/\r\n            $customFerryCost = 0;\r\n            $totalZoneCost = 0;\r\n            foreach ($zones as $zone) {\r\n                if ($zone['zone'] == 'dover_ferry') {\r\n                    $customFerryCost = $zone['price'];\r\n                }else{\r\n                    if ($zone[\"fixed\"]){\r\n                        $zonePrice = $zone['price'];\r\n                    }else {\r\n                        $zonePrice = $zone['price']*$zone['distance'];\r\n                    }\r\n                    $totalZoneCost = $totalZoneCost+$zonePrice;\r\n                }\r\n            }\r\n            $totalFerryCost += $customFerryCost;\r\n            /*calculate place of supply*/\r\n            $euDistance = 0;\r\n            foreach ($distance_by_country as $code => $countryDistance) {\r\n                if ($code != 'NO' && $code != 'UK' && $code != 'CH') {\r\n                    $euDistance += $countryDistance;\r\n                }\r\n            }\r\n            $placeOfSupplyAr = array_filter($distance_by_country, function ($key) {\r\n                return ($key == 'NO' || $key == 'UK' || $key == 'CH');\r\n            }, ARRAY_FILTER_USE_KEY);\r\n            $placeOfSupplyAr['EU'] = $euDistance;\r\n            asort($placeOfSupplyAr);\r\n            $placeOfSupply = array_key_last($placeOfSupplyAr);\r\n\r\n            //Ireland exception\r\n            if (strpos(mb_strtolower($from), 'northern ireland')) {\r\n                $placeOfSupply = \"UK\";\r\n            } elseif (strpos(mb_strtolower($to), \"northern ireland\")) {\r\n                $placeOfSupply = \"EU\";\r\n            }\r\n            /*get vat rate*/\r\n            $vatRate = 0;\r\n            if ($placeOfSupply == \"UK\") {\r\n                $vatRate = 0.2;\r\n            }\r\n            /*add margin if location includes uk and place of supply EU*/\r\n            $margin = 0;\r\n            if (($from_c_code == 'GB' || $to_c_code == \"GB\") && $placeOfSupply == 'EU') {\r\n                if ($from_c_code == 'GB') {\r\n                    $margin = $muveOnePricingData[$to_c_code][\"eu_country_margin\"]['import_margin'] ?? 0;\r\n                } elseif ($to_c_code == 'GB') {\r\n                    $margin = $muveOnePricingData[$from_c_code][\"eu_country_margin\"]['export_margin'] ?? 0;\r\n                }\r\n            }\r\n\r\n\r\n\r\n            /*calculate final prices*/\r\n            $sizePricing = [];\r\n            foreach ($sizes as $size) {\r\n                $sizeData = $pricingData['size_data'][$size];\r\n                $ppkPercentage = $sizeData['ppk_percentage'];\r\n                $distancePricingData = $sizeData['distance_pricing'][$distanceType];\r\n                $ppkCost = $distancePricingData['ppk_cost'];\r\n                $ferryCostPercentage = $distancePricingData['ferry_cost_percentage'];\r\n                $markUpPercentage = $distancePricingData['markup_percentage'];\r\n                $totalPPKCost = (($ppkCost * $distance) * $ppkPercentage);\r\n                $markUp = ($pricingData['total_markup'] * $markUpPercentage)+100;\r\n                $ferryCost = $totalFerryCost * $ferryCostPercentage;\r\n                $minCost = ($distance < 500) ? $sizeMinPrices[$size] : 0;\r\n                $total = $totalPPKCost + $markUp + $ferryCost + $minCost+$totalZoneCost;\r\n                $total = $total + ($total * $margin);\r\n                $totalWithVat = $total * (1 + $vatRate);\r\n                $sizePricing [$size] = [\r\n                    \"ppk_percentage\"=>$ppkPercentage,\r\n                    \"markup_percentage\"=>$markUpPercentage,\r\n                    \"ferry_cost_percentage\"=>$ferryCostPercentage,\r\n                    \"total_ferry_cost\"=>$totalFerryCost,\r\n                    'size_ppk_cost' => $ppkCost,\r\n                    'size_markUp' => $pricingData['total_markup'],\r\n                    \"zone_cost\"=>$totalZoneCost,\r\n                    \"min_cost\"=>$minCost,\r\n                    'ppk_cost' => $totalPPKCost,\r\n                    'markUp' => $markUp,\r\n                    'ferryCost' => $ferryCost,\r\n                    'total' => $total,\r\n                    'totalWithVat' => $totalWithVat,\r\n                ];\r\n            }\r\n            /*add 30m3 prices*/\r\n            $sizePricing[30] = [\r\n                \"ppk_percentage\"=>$sizePricing[15][\"ppk_percentage\"],\r\n                \"markup_percentage\"=>$sizePricing[15][\"markup_percentage\"],\r\n                \"ferry_cost_percentage\"=>$sizePricing[15][\"ferry_cost_percentage\"],\r\n                \"total_ferry_cost\"=>$sizePricing[15][\"total_ferry_cost\"],\r\n                'size_ppk_cost' => $sizePricing[15][\"size_ppk_cost\"],\r\n                'size_markUp' =>$sizePricing[15][\"size_markUp\"],\r\n                \"min_cost\"=>$sizePricing[15][\"min_cost\"]*2,\r\n                \"zone_cost\"=>$sizePricing[15][\"zone_cost\"]*2,\r\n                'ppk_cost' => ($sizePricing[15]['ppk_cost']) * 2,\r\n                'markUp' => ($sizePricing[15]['markUp']) * 2,\r\n                'ferryCost' => ($sizePricing[15]['ferryCost']) * 2,\r\n                'total' => ($sizePricing[15]['total']) * 2,\r\n                'totalWithVat' => ($sizePricing[15]['totalWithVat']) * 2,\r\n            ];\r\n\r\n            $markups=[];\r\n            $sizePrices =[];\r\n            $totalSizePpkCosts=[];\r\n            foreach ($volume as $size) {\r\n                $sizeMarkUp =$sizePricing[$size]['markUp'];\r\n                $markups[$size]= $sizeMarkUp;\r\n                $totalCost = $sizePricing[$size]['totalWithVat'];\r\n                $sizePrices[$size] =FormulaService::formatNumber($totalCost);\r\n                $totalSizePpkCosts[$size]= $sizePricing[$size]['ppk_cost'];\r\n            }\r\n            $allSizePrices = collect($sizePricing)->mapWithKeys(function ($value, $key) {\r\n                return [$key =>FormulaService::formatNumber($value['totalWithVat'])];\r\n            })->toArray();\r\n            return [\r\n                'has_null_ferries' => $nullFerries,\r\n                'cost_of_route' => $totalSizePpkCosts,\r\n                'markup' =>  $markups,\r\n                'ferries' => $ferries,\r\n                'mass' => $mass,\r\n                'volume' => $volume,\r\n                'total_distance_through_zones' => collect($zones)->sum('distance'),\r\n                'total_price' => $sizePrices[$volume[0]],\r\n                'prices' => $sizePrices,\r\n                'total_duration' => $trip_duration,\r\n                'from' => $from,\r\n                'to' => $to,\r\n                'distance_by_country' => $distance_by_country,\r\n                'total_distance'=>$distance,\r\n                'place_of_supply' => $placeOfSupply,\r\n                'all_size_prices' => $allSizePrices,\r\n            ];",
        "formula_encode": null,
        "user_id": 2,
        "created_at": "2023-05-26 16:33:22",
        "updated_at": "2024-05-13 10:58:21"
    },
    {
        "id": 11,
        "formula_name": "muveone test",
        "formula": "$withinEuUnsupportedCountries = [\r\n                'NO' => 'Norway',\r\n                'CH' => 'Switzerland',\r\n                'IE' => 'Ireland',\r\n                'MT' => 'Malta'\r\n            ];\r\n            $withinEuSupportedCountries = [\r\n                'AT' => \"Austria\",\r\n                'BE' => \"Belgium\",\r\n                'BG' => 'Bulgaria',\r\n                'HR' => 'Croatia',\r\n                'CZ' => 'Czech Republic',\r\n                'DK' => 'Denmark',\r\n                'EE' => 'Estonia',\r\n                'FI' => 'Finland',\r\n                'FR' => 'France',\r\n                'DE' => 'Germany',\r\n                'GR' => 'Greece',\r\n                'HU' => 'Hungary',\r\n                'IT' => 'Italy',\r\n                'LV' => 'Latvia',\r\n                'LT' => 'Lithuania',\r\n                'LU' => 'Luxembourg',\r\n                'NL' => 'Netherlands',\r\n                'PL' => 'Poland',\r\n                'PT' => 'Portugal',\r\n                'RO' => 'Romania',\r\n                'SK' => 'Slovakia',\r\n                'SI' => 'Slovenia',\r\n                'ES' => 'Spain',\r\n                'SE' => 'Sweden',\r\n            ];\r\n\r\n\r\n            $sizeMinPrices = [\r\n                5 => 127.29,\r\n                10 => 169.72,\r\n                15 => 169.72,\r\n                30 => 339.44\r\n            ];\r\n            $sizes = ['5', '10', '15'];\r\n\r\n            /* get pricing data*/\r\n            $withInEu = array_key_exists($to_c_code, $withinEuSupportedCountries)\r\n                && array_key_exists($from_c_code, $withinEuSupportedCountries);\r\n            if ($withInEu) {\r\n                $pricingDataCountryCode = 'EU';\r\n            } elseif ($to_c_code == 'GB') {\r\n                $pricingDataCountryCode = $from_c_code;\r\n            } else {\r\n                $pricingDataCountryCode = $to_c_code;\r\n            }\r\n            if (!isset($muveOnePricingData[$pricingDataCountryCode])) {\r\n                /*default pricing data*/\r\n                Log::report(['Message' => 'Muveone pricing data not found for from ' . $from . ' to ' . $to . ' default pricing was used'], 'warning');\r\n                $pricingDataCountryCode = 'ZZ';\r\n            }\r\n            $pricingData = $muveOnePricingData[$pricingDataCountryCode];\r\n            /*check null ferries*/\r\n            $nullFerries = false;\r\n            $totalFerryCost = 0;\r\n            foreach ($ferries as $ferry) {\r\n                if ($ferry['price'] == 0) {\r\n                    $nullFerries = true;\r\n                    break;\r\n                } else {\r\n                    $totalFerryCost += $ferry['price'];\r\n                }\r\n            }\r\n\r\n            /*calculate customized ferry cost*/\r\n            $customFerryCost = 0;\r\n            $totalZoneCost = 0;\r\n            foreach ($zones as $zone) {\r\n                if ($zone['zone'] == 'dover_ferry') {\r\n                    $customFerryCost = $zone['price'];\r\n                }else{\r\n                    if ($zone[\"fixed\"]){\r\n                        $zonePrice = $zone['price'];\r\n                    }else {\r\n                        $zonePrice = $zone['price']*$zone['distance'];\r\n                    }\r\n                    $totalZoneCost = $totalZoneCost+$zonePrice;\r\n                }\r\n            }\r\n            $totalFerryCost += $customFerryCost;\r\n            /*calculate place of supply*/\r\n            $euDistance = 0;\r\n            foreach ($distance_by_country as $code => $countryDistance) {\r\n                if ($code != 'NO' && $code != 'UK' && $code != 'CH') {\r\n                    $euDistance += $countryDistance;\r\n                }\r\n            }\r\n            $placeOfSupplyAr = array_filter($distance_by_country, function ($key) {\r\n                return ($key == 'NO' || $key == 'UK' || $key == 'CH');\r\n            }, ARRAY_FILTER_USE_KEY);\r\n            $placeOfSupplyAr['EU'] = $euDistance;\r\n            asort($placeOfSupplyAr);\r\n            $placeOfSupply = array_key_last($placeOfSupplyAr);\r\n\r\n            //Ireland exception\r\n            if (strpos(mb_strtolower($from), 'northern ireland')) {\r\n                $placeOfSupply = \"UK\";\r\n            } elseif (strpos(mb_strtolower($to), \"northern ireland\")) {\r\n                $placeOfSupply = \"EU\";\r\n            }\r\n            /*get vat rate*/\r\n            $vatRate = 0;\r\n            if ($placeOfSupply == \"UK\") {\r\n                $vatRate = 0.2;\r\n            }\r\n            /*add margin if location includes uk and place of supply EU*/\r\n            $margin = 0;\r\n            if (($from_c_code == 'GB' || $to_c_code == \"GB\") && $placeOfSupply == 'EU') {\r\n                if ($from_c_code == 'GB') {\r\n                    $margin = $muveOnePricingData[$to_c_code][\"eu_country_margin\"]['import_margin'] ?? 0;\r\n                } elseif ($to_c_code == 'GB') {\r\n                    $margin = $muveOnePricingData[$from_c_code][\"eu_country_margin\"]['export_margin'] ?? 0;\r\n                }\r\n            }\r\n\r\n\r\n\r\n            /*calculate final prices*/\r\n            $sizePricing = [];\r\n            foreach ($sizes as $size) {\r\n                $sizeData = $pricingData['size_data'][$size];\r\n                $ppkPercentage = $sizeData['ppk_percentage'];\r\n                $distancePricingData = $sizeData['distance_pricing'][$distanceType];\r\n                $ppkCost = $distancePricingData['ppk_cost'];\r\n                $ferryCostPercentage = $distancePricingData['ferry_cost_percentage'];\r\n                $markUpPercentage = $distancePricingData['markup_percentage'];\r\n                $totalPPKCost = (($ppkCost * $distance) * $ppkPercentage);\r\n                $markUp = ($pricingData['total_markup'] * $markUpPercentage)+100;\r\n                $ferryCost = $totalFerryCost * $ferryCostPercentage;\r\n                $minCost = ($distance < 500) ? $sizeMinPrices[$size] : 0;\r\n                $total = $totalPPKCost + $markUp + $ferryCost + $minCost+$totalZoneCost;\r\n                $total = $total + ($total * $margin);\r\n                $totalWithVat = $total * (1 + $vatRate);\r\n                $sizePricing [$size] = [\r\n                    \"ppk_percentage\"=>$ppkPercentage,\r\n                    \"markup_percentage\"=>$markUpPercentage,\r\n                    \"ferry_cost_percentage\"=>$ferryCostPercentage,\r\n                    \"total_ferry_cost\"=>$totalFerryCost,\r\n                    'size_ppk_cost' => $ppkCost,\r\n                    'size_markUp' => $pricingData['total_markup'],\r\n                    \"zone_cost\"=>$totalZoneCost,\r\n                    \"min_cost\"=>$minCost,\r\n                    'ppk_cost' => $totalPPKCost,\r\n                    'markUp' => $markUp,\r\n                    'ferryCost' => $ferryCost,\r\n                    'total' => $total,\r\n                    'totalWithVat' => $totalWithVat,\r\n                ];\r\n            }\r\n            /*add 30m3 prices*/\r\n            $sizePricing[30] = [\r\n                \"ppk_percentage\"=>$sizePricing[15][\"ppk_percentage\"],\r\n                \"markup_percentage\"=>$sizePricing[15][\"markup_percentage\"],\r\n                \"ferry_cost_percentage\"=>$sizePricing[15][\"ferry_cost_percentage\"],\r\n                \"total_ferry_cost\"=>$sizePricing[15][\"total_ferry_cost\"],\r\n                'size_ppk_cost' => $sizePricing[15][\"size_ppk_cost\"],\r\n                'size_markUp' =>$sizePricing[15][\"size_markUp\"],\r\n                \"min_cost\"=>$sizePricing[15][\"min_cost\"]*2,\r\n                \"zone_cost\"=>$sizePricing[15][\"zone_cost\"]*2,\r\n                'ppk_cost' => ($sizePricing[15]['ppk_cost']) * 2,\r\n                'markUp' => ($sizePricing[15]['markUp']) * 2,\r\n                'ferryCost' => ($sizePricing[15]['ferryCost']) * 2,\r\n                'total' => ($sizePricing[15]['total']) * 2,\r\n                'totalWithVat' => ($sizePricing[15]['totalWithVat']) * 2,\r\n            ];\r\n\r\n            $markups=[];\r\n            $sizePrices =[];\r\n            $totalSizePpkCosts=[];\r\n            foreach ($volume as $size) {\r\n                $sizeMarkUp =$sizePricing[$size]['markUp'];\r\n                $markups[$size]= $sizeMarkUp;\r\n                $totalCost = $sizePricing[$size]['totalWithVat'];\r\n                $sizePrices[$size] =FormulaService::formatNumber($totalCost);\r\n                $totalSizePpkCosts[$size]= $sizePricing[$size]['ppk_cost'];\r\n            }\r\n            $allSizePrices = collect($sizePricing)->mapWithKeys(function ($value, $key) {\r\n                return [$key =>FormulaService::formatNumber($value['totalWithVat'])];\r\n            })->toArray();\r\n            return [\r\n                'has_null_ferries' => $nullFerries,\r\n                'cost_of_route' => $totalSizePpkCosts,\r\n                'markup' =>  $markups,\r\n                'ferries' => $ferries,\r\n                'mass' => $mass,\r\n                'volume' => $volume,\r\n                'total_distance_through_zones' => collect($zones)->sum('distance'),\r\n                'total_price' => $sizePrices[$volume[0]],\r\n                'prices' => $sizePrices,\r\n                'total_duration' => $trip_duration,\r\n                'from' => $from,\r\n                'to' => $to,\r\n                'distance_by_country' => $distance_by_country,\r\n                'total_distance'=>$distance,\r\n                'place_of_supply' => $placeOfSupply,\r\n                'all_size_prices' => $allSizePrices,\r\n            ];",
        "formula_encode": null,
        "user_id": 2,
        "created_at": "2023-06-15 05:33:58",
        "updated_at": "2024-05-13 10:38:01"
    },
    {
        "id": 12,
        "formula_name": "Muveone Thushan",
        "formula": "$withinEuUnsupportedCountries = [\r\n                'NO' => 'Norway',\r\n                'CH' => 'Switzerland',\r\n                'IE' => 'Ireland',\r\n                'MT' => 'Malta'\r\n            ];\r\n            $withinEuSupportedCountries = [\r\n                'AT' => \"Austria\",\r\n                'BE' => \"Belgium\",\r\n                'BG' => 'Bulgaria',\r\n                'HR' => 'Croatia',\r\n                'CZ' => 'Czech Republic',\r\n                'DK' => 'Denmark',\r\n                'EE' => 'Estonia',\r\n                'FI' => 'Finland',\r\n                'FR' => 'France',\r\n                'DE' => 'Germany',\r\n                'GR' => 'Greece',\r\n                'HU' => 'Hungary',\r\n                'IT' => 'Italy',\r\n                'LV' => 'Latvia',\r\n                'LT' => 'Lithuania',\r\n                'LU' => 'Luxembourg',\r\n                'NL' => 'Netherlands',\r\n                'PL' => 'Poland',\r\n                'PT' => 'Portugal',\r\n                'RO' => 'Romania',\r\n                'SK' => 'Slovakia',\r\n                'SI' => 'Slovenia',\r\n                'ES' => 'Spain',\r\n                'SE' => 'Sweden',\r\n            ];\r\n\r\n\r\n            $sizeMinPrices = [\r\n                5 => 127.29,\r\n                10 => 169.72,\r\n                15 => 169.72,\r\n                30 => 339.44\r\n            ];\r\n            $sizes = ['5', '10', '15'];\r\n\r\n            /* get pricing data*/\r\n            $withInEu = array_key_exists($to_c_code, $withinEuSupportedCountries)\r\n                && array_key_exists($from_c_code, $withinEuSupportedCountries);\r\n            if ($withInEu) {\r\n                $pricingDataCountryCode = 'EU';\r\n            } elseif ($to_c_code == 'GB') {\r\n                $pricingDataCountryCode = $from_c_code;\r\n            } else {\r\n                $pricingDataCountryCode = $to_c_code;\r\n            }\r\n            if (!isset($muveOnePricingData[$pricingDataCountryCode])) {\r\n                /*default pricing data*/\r\n                Log::report(['Message' => 'Muveone pricing data not found for from ' . $from . ' to ' . $to . ' default pricing was used'], 'warning');\r\n                $pricingDataCountryCode = 'ZZ';\r\n            }\r\n            $pricingData = $muveOnePricingData[$pricingDataCountryCode];\r\n            /*check null ferries*/\r\n            $nullFerries = false;\r\n            $totalFerryCost = 0;\r\n            foreach ($ferries as $ferry) {\r\n                if ($ferry['price'] == 0) {\r\n                    $nullFerries = true;\r\n                    break;\r\n                } else {\r\n                    $totalFerryCost += $ferry['price'];\r\n                }\r\n            }\r\n\r\n            /*calculate customized ferry cost*/\r\n            $customFerryCost = 0;\r\n            $totalZoneCost = 0;\r\n            foreach ($zones as $zone) {\r\n                if ($zone['zone'] == 'dover_ferry') {\r\n                    $customFerryCost = $zone['price'];\r\n                }else{\r\n                    if ($zone[\"fixed\"]){\r\n                        $zonePrice = $zone['price'];\r\n                    }else {\r\n                        $zonePrice = $zone['price']*$zone['distance'];\r\n                    }\r\n                    $totalZoneCost = $totalZoneCost+$zonePrice;\r\n                }\r\n            }\r\n            $totalFerryCost += $customFerryCost;\r\n            /*calculate place of supply*/\r\n            $euDistance = 0;\r\n            foreach ($distance_by_country as $code => $countryDistance) {\r\n                if ($code != 'NO' && $code != 'UK' && $code != 'CH') {\r\n                    $euDistance += $countryDistance;\r\n                }\r\n            }\r\n            $placeOfSupplyAr = array_filter($distance_by_country, function ($key) {\r\n                return ($key == 'NO' || $key == 'UK' || $key == 'CH');\r\n            }, ARRAY_FILTER_USE_KEY);\r\n            $placeOfSupplyAr['EU'] = $euDistance;\r\n            asort($placeOfSupplyAr);\r\n            $placeOfSupply = array_key_last($placeOfSupplyAr);\r\n\r\n            //Ireland exception\r\n            if (strpos(mb_strtolower($from), 'northern ireland')) {\r\n                $placeOfSupply = \"UK\";\r\n            } elseif (strpos(mb_strtolower($to), \"northern ireland\")) {\r\n                $placeOfSupply = \"EU\";\r\n            }\r\n            /*get vat rate*/\r\n            $vatRate = 0;\r\n            if ($placeOfSupply == \"UK\") {\r\n                $vatRate = 0.2;\r\n            }\r\n            /*add margin if location includes uk and place of supply EU*/\r\n            $margin = 0;\r\n            if (($from_c_code == 'GB' || $to_c_code == \"GB\") && $placeOfSupply == 'EU') {\r\n                if ($from_c_code == 'GB') {\r\n                    $margin = $muveOnePricingData[$to_c_code][\"eu_country_margin\"]['import_margin'] ?? 0;\r\n                } elseif ($to_c_code == 'GB') {\r\n                    $margin = $muveOnePricingData[$from_c_code][\"eu_country_margin\"]['export_margin'] ?? 0;\r\n                }\r\n            }\r\n\r\n\r\n\r\n            /*calculate final prices*/\r\n            $sizePricing = [];\r\n            foreach ($sizes as $size) {\r\n                $sizeData = $pricingData['size_data'][$size];\r\n                $ppkPercentage = $sizeData['ppk_percentage'];\r\n                $distancePricingData = $sizeData['distance_pricing'][$distanceType];\r\n                $ppkCost = $distancePricingData['ppk_cost'];\r\n                $ferryCostPercentage = $distancePricingData['ferry_cost_percentage'];\r\n                $markUpPercentage = $distancePricingData['markup_percentage'];\r\n                $totalPPKCost = (($ppkCost * $distance) * $ppkPercentage);\r\n                $markUp = ($pricingData['total_markup'] * $markUpPercentage)+100;\r\n                $ferryCost = $totalFerryCost * $ferryCostPercentage;\r\n                $minCost = ($distance < 500) ? $sizeMinPrices[$size] : 0;\r\n                $total = $totalPPKCost + $markUp + $ferryCost + $minCost+$totalZoneCost;\r\n                $total = $total + ($total * $margin);\r\n                $totalWithVat = $total * (1 + $vatRate);\r\n                $sizePricing [$size] = [\r\n                    \"ppk_percentage\"=>$ppkPercentage,\r\n                    \"markup_percentage\"=>$markUpPercentage,\r\n                    \"ferry_cost_percentage\"=>$ferryCostPercentage,\r\n                    \"total_ferry_cost\"=>$totalFerryCost,\r\n                    'size_ppk_cost' => $ppkCost,\r\n                    'size_markUp' => $pricingData['total_markup'],\r\n                    \"zone_cost\"=>$totalZoneCost,\r\n                    \"min_cost\"=>$minCost,\r\n                    'ppk_cost' => $totalPPKCost,\r\n                    'markUp' => $markUp,\r\n                    'ferryCost' => $ferryCost,\r\n                    'margin' => $margin,\r\n                    'total' => $total,\r\n                    'vatRate' => $vatRate,\r\n                    'totalWithVat' => $totalWithVat,\r\n                ];\r\n            }\r\n            /*add 30m3 prices*/\r\n            $sizePricing[30] = [\r\n                \"ppk_percentage\"=>$sizePricing[15][\"ppk_percentage\"],\r\n                \"markup_percentage\"=>$sizePricing[15][\"markup_percentage\"],\r\n                \"ferry_cost_percentage\"=>$sizePricing[15][\"ferry_cost_percentage\"],\r\n                \"total_ferry_cost\"=>$sizePricing[15][\"total_ferry_cost\"],\r\n                'size_ppk_cost' => $sizePricing[15][\"size_ppk_cost\"],\r\n                'size_markUp' =>$sizePricing[15][\"size_markUp\"],\r\n                \"min_cost\"=>$sizePricing[15][\"min_cost\"]*2,\r\n                \"zone_cost\"=>$sizePricing[15][\"zone_cost\"]*2,\r\n                'ppk_cost' => ($sizePricing[15]['ppk_cost']) * 2,\r\n                'markUp' => ($sizePricing[15]['markUp']) * 2,\r\n                'ferryCost' => ($sizePricing[15]['ferryCost']) * 2,\r\n                'total' => ($sizePricing[15]['total']) * 2,\r\n                'totalWithVat' => ($sizePricing[15]['totalWithVat']) * 2,\r\n            ];\r\n\r\n            $markups=[];\r\n            $sizePrices =[];\r\n            $totalSizePpkCosts=[];\r\n            foreach ($volume as $size) {\r\n                $sizeMarkUp =$sizePricing[$size]['markUp'];\r\n                $markups[$size]= $sizeMarkUp;\r\n                $totalCost = $sizePricing[$size]['totalWithVat'];\r\n                $sizePrices[$size] =FormulaService::formatNumber($totalCost);\r\n                $totalSizePpkCosts[$size]= $sizePricing[$size]['ppk_cost'];\r\n            }\r\n            $allSizePrices = collect($sizePricing)->mapWithKeys(function ($value, $key) {\r\n                return [$key =>FormulaService::formatNumber($value['totalWithVat'])];\r\n            })->toArray();\r\n            return [\r\n                'has_null_ferries' => $nullFerries,\r\n                'cost_of_route' => $totalSizePpkCosts,\r\n                'markup' =>  $markups,\r\n                'ferries' => $ferries,\r\n                'mass' => $mass,\r\n                'volume' => $volume,\r\n                'total_distance_through_zones' => collect($zones)->sum('distance'),\r\n                'total_price' => $sizePrices[$volume[0]],\r\n                'prices' => $sizePrices,\r\n                'total_duration' => $trip_duration,\r\n                'from' => $from,\r\n                'to' => $to,\r\n                'distance_by_country' => $distance_by_country,\r\n                'total_distance'=>$distance,\r\n                'place_of_supply' => $placeOfSupply,\r\n                'all_size_prices' => $allSizePrices,\r\n                 'size_prices_10' => $sizePricing[10]\r\n            ];",
        "formula_encode": null,
        "user_id": 2,
        "created_at": "2023-08-22 10:39:52",
        "updated_at": "2025-05-09 04:37:54"
    },
    {
        "id": 14,
        "formula_name": "VanOne",
        "formula": "$withinEuUnsupportedCountries = [\n    'NO' => 'Norway',\n    'CH' => 'Switzerland',\n    'IE' => 'Ireland',\n    'MT' => 'Malta'\n];\n\n$withinEuSupportedCountries = [\n    'AT' => \"Austria\", 'BE' => \"Belgium\", 'BG' => 'Bulgaria', 'HR' => 'Croatia',\n    'CZ' => 'Czech Republic', 'DK' => 'Denmark', 'EE' => 'Estonia', 'FI' => 'Finland',\n    'FR' => 'France', 'DE' => 'Germany', 'GR' => 'Greece', 'HU' => 'Hungary',\n    'IT' => 'Italy', 'LV' => 'Latvia', 'LT' => 'Lithuania', 'LU' => 'Luxembourg',\n    'NL' => 'Netherlands', 'PL' => 'Poland', 'PT' => 'Portugal', 'RO' => 'Romania',\n    'SK' => 'Slovakia', 'SI' => 'Slovenia', 'ES' => 'Spain', 'SE' => 'Sweden',\n    'MC' => 'Monaco', 'GI' => 'Gibraltar'\n];\n\n$sizeMinPrices = [\n    5 => 127.29,\n    10 => 169.72,\n    15 => 169.72,\n    30 => 339.44\n];\n$sizes = ['5', '10', '15'];\n\n/* get pricing data */\n$withInEu = array_key_exists($to_c_code, $withinEuSupportedCountries)\n    && array_key_exists($from_c_code, $withinEuSupportedCountries);\n\nif ($withInEu) {\n    $pricingDataCountryCode = 'EU';\n} elseif ($to_c_code == 'GB') {\n    $pricingDataCountryCode = $from_c_code;\n} elseif ($from_c_code == 'NO') {\n    $pricingDataCountryCode = $from_c_code;\n} else {\n    $pricingDataCountryCode = $to_c_code;\n}\n\nif (!isset($vanOnePricingData[$pricingDataCountryCode])) {\n    /* default pricing data */\n    Log::report(['Message' => 'Vanone pricing data not found for from ' . $from . ' to ' . $to . ' default pricing was used'], 'warning');\n    $pricingDataCountryCode = 'ZZ';\n}\n\n$pricingData = $vanOnePricingData[$pricingDataCountryCode];\n\n/* check null ferries - EARLY RETURN REMOVED TO MATCH SNIPPET 2 */\n$nullFerries = false;\n$totalFerryCost = 0;\nforeach ($ferries as $ferry) {\n    if ($ferry['price'] == 0) {\n        $nullFerries = true;\n        break;\n    } else {\n        $totalFerryCost += $ferry['price'];\n    }\n}\n\n/* calculate customized ferry cost */\n$customFerryCost = 0;\n$totalZoneCost = 0;\nforeach ($zones as $zone) {\n    if ($zone['zone'] == 'dover_ferry') {\n        $customFerryCost = $zone['price'];\n    } else {\n        if ($zone[\"fixed\"]) {\n            $zonePrice = $zone['price'];\n        } else {\n            $zonePrice = $zone['price'] * $zone['distance'];\n        }\n        $totalZoneCost = $totalZoneCost + $zonePrice;\n    }\n}\n$totalFerryCost += $customFerryCost;\n\n/* calculate place of supply */\n$euDistance = 0;\nforeach ($distance_by_country as $code => $countryDistance) {\n    if ($code != 'NO' && $code != 'UK' && $code != 'CH') {\n        $euDistance += $countryDistance;\n    }\n}\n$placeOfSupplyAr = array_filter($distance_by_country, function ($key)",
        "formula_encode": null,
        "user_id": 2,
        "created_at": "2026-02-24 15:07:04",
        "updated_at": "2026-02-24 15:07:04"
    }
]