Esempio n. 1
0
 /**
  * Returns rate information for a given location, optionally ignoring by priority.
  * @param  array $locationInfo
  * @param  array $ignoredPriorities
  * @return object
  */
 protected function getRate($locationInfo, $ignoredPriorities = [])
 {
     $country = Country::find($locationInfo->country_id);
     if (!$country) {
         return null;
     }
     $state = null;
     if (strlen($locationInfo->state_id)) {
         $state = State::find($locationInfo->state_id);
     }
     $countryCode = $country->code;
     $stateCode = $state ? mb_strtoupper($state->code) : '*';
     $zipCode = str_replace(' ', '', trim(strtoupper($locationInfo->zip)));
     if (!strlen($zipCode)) {
         $zipCode = '*';
     }
     $city = str_replace('-', '', str_replace(' ', '', trim(mb_strtoupper($locationInfo->city))));
     if (!strlen($city)) {
         $city = '*';
     }
     $rate = null;
     foreach ($this->rates as $row) {
         $taxPriority = isset($row['priority']) ? $row['priority'] : 1;
         if (in_array($taxPriority, $ignoredPriorities)) {
             continue;
         }
         if ($row['country'] != $countryCode && $row['country'] != '*') {
             continue;
         }
         if (mb_strtoupper($row['state']) != $stateCode && $row['state'] != '*') {
             continue;
         }
         $rowZip = isset($row['zip']) && strlen($row['zip']) ? str_replace(' ', '', $row['zip']) : '*';
         if ($rowZip != $zipCode && $rowZip != '*') {
             continue;
         }
         $rowCity = isset($row['city']) && strlen($row['city']) ? str_replace('-', '', str_replace(' ', '', mb_strtoupper($row['city']))) : '*';
         if ($rowCity != $city && $rowCity != '*') {
             continue;
         }
         $compound = isset($row['compound']) ? $row['compound'] : 0;
         if (preg_match('/^[0-9]+$/', $compound)) {
             $compound = (int) $compound;
         } else {
             $compound = $compound == 'Y' || $compound == 'YES';
         }
         $rateObj = ['rate' => $row['rate'], 'priority' => $taxPriority, 'name' => isset($row['tax_name']) ? $row['tax_name'] : 'TAX', 'compound' => $compound];
         $rate = (object) $rateObj;
         break;
     }
     return $rate;
 }