簡體   English   中英

在另一個函數中將重復的代碼分開(代碼優化)

[英]Separate duplicated code in another function (code optimisation)

我有此功能,用於計算產品稅率,並且必須考慮不同的征稅規則...問題是此代碼有效,但是由於一部分重復,我現在不願進行代碼優化。 如果仔細檢查,將會發現foreach ($taxationRules as $taxRule) {重復了兩次,但是它必須應用不同的規則(添加或刪除稅率)

大家能否幫助我,如果可能的話,如何正確分離重復的代碼。

這里還有一件事很重要! 由於沒有稅收規則優先級,所有稅收規則必須首先添加稅收,然后(在另一個foreach中)清除稅收。 這就是為什么我重復了代碼,而不是在第一個foreach中僅包含$taxRule['TaxationRule']['Taxation_rule_disabled_tax_rate']

private static function getProductTaxRatesByTaxationRules($cartItem) {
    $shippingAddressData = self::$cart['ShippingAddress']['UserAddress'];
    $taxationRules = self::$cart['TaxationRules'];
    $currentUser = LoggedInUser::get();

    // Prepare data to compare with taxation rules
    $regularUser = false;
    if (!$currentUser['is_company']) {
        $regularUser = true;
    }
    $compareData = [
        'regular_user' => $regularUser,
        'company' => $currentUser['is_company'],
        'product_id' => $cartItem['product_id'],
        'subtype_id' => $cartItem['productIndex']['subtype_id'],
        'country_id' => $shippingAddressData['country_id'],
        'country_state_id' => $shippingAddressData['country_state_id']
    ];

    $taxRates = $cartItem['productIndex']['tax_rate'];

    foreach ($taxationRules as $taxRule) {

        if ($taxRule['TaxationRule']['user_type'] == 'regular' && !$compareData['regular_user']) {
            continue;
        }
        if ($taxRule['TaxationRule']['user_type'] == 'companies' && !$compareData['company']) {
            continue;
        }
        if (!empty($taxRule['TaxationRule']['Taxation_rule_product'])) {
            if (!in_array($compareData['product_id'], $taxRule['TaxationRule']['Taxation_rule_product'])) {
                continue;
            }
        }
        if (!empty($taxRule['TaxationRule']['Taxation_rule_product_subtype'])) {
            if (!in_array($compareData['subtype_id'], $taxRule['TaxationRule']['Taxation_rule_product_subtype'])) {
                continue;
            }
        }
        if (!empty($taxRule['TaxationRule']['TaxationRuleCountryState'])) {
            $found = false;
            foreach ($taxRule['TaxationRule']['TaxationRuleCountryState'] as $countryStates) {
                if (isset($countryStates['country_state_id']) && !is_null($countryStates['country_state_id'])) {
                    if ($countryStates['country_state_id'] == $compareData['country_state_id']) {
                        $found = true;
                    }
                } else {
                    if ($countryStates['country_id'] == $compareData['country_id']) {
                        $found = true;
                    }
                }
            }
            if (!$found) {
                continue;
            }
        }
        // If any of flags aren't true, then add Taxation_rule_enabled_tax_rate and
        // check if there are enabled tax rates that needs to be added
        if (!empty($taxRule['TaxationRule']['Taxation_rule_enabled_tax_rate'])) {
            foreach ($taxRule['TaxationRule']['Taxation_rule_enabled_tax_rate'] as $includeTax) {
                foreach ($taxRule['TaxRate'] as $taxData) {
                    if ($taxData['id'] == $includeTax) {
                        $taxRates[] = array(
                            'id' => $taxData['id'],
                            'value' => $taxData['taxrate']
                        );
                    }
                }
            }
        }
    }

    foreach ($taxationRules as $taxRule) {
        if ($taxRule['TaxationRule']['user_type'] == 'regular' && !$compareData['regular_user']) {
            continue;
        }
        if ($taxRule['TaxationRule']['user_type'] == 'companies' && !$compareData['company']) {
            continue;
        }
        if (!empty($taxRule['TaxationRule']['Taxation_rule_product'])) {
            if (!in_array($compareData['product_id'], $taxRule['TaxationRule']['Taxation_rule_product'])) {
                continue;
            }
        }
        if (!empty($taxRule['TaxationRule']['Taxation_rule_product_subtype'])) {
            if (!in_array($compareData['subtype_id'], $taxRule['TaxationRule']['Taxation_rule_product_subtype'])) {
                continue;
            }
        }
        if (!empty($taxRule['TaxationRule']['TaxationRuleCountryState'])) {
            $found = false;
            foreach ($taxRule['TaxationRule']['TaxationRuleCountryState'] as $countryStates) {
                if (isset($countryStates['country_state_id']) && !is_null($countryStates['country_state_id'])) {
                    if ($countryStates['country_state_id'] == $compareData['country_state_id']) {
                        $found = true;
                    }
                } else {
                    if ($countryStates['country_id'] == $compareData['country_id']) {
                        $found = true;
                    }
                }
            }
            if (!$found) {
                continue;
            }
        }

        // If any of flags aren't true, then remove Taxation_rule_disabled_tax_rate
        // Check if there are disabled tax rates that needed to be removed
        if (!empty($taxRule['TaxationRule']['Taxation_rule_disabled_tax_rate'])) {
            foreach ($taxRule['TaxationRule']['Taxation_rule_disabled_tax_rate'] as $excludeTaxId) {
                foreach ($taxRates as $key => $value) {
                    if ($value['id'] == $excludeTaxId) {
                        unset($taxRates[$key]);
                        break;
                    }
                }
            }
        }
    }

    return $taxRates;
}

如果您需要任何其他信息,請告訴我,我會提供。 謝謝!

對於像foreach這樣的比較小部件:

foreach ($taxRule['TaxationRule']['Taxation_rule_product'] as $taxProductId) {
        if ($compareData['product_id'] == $taxProductId) {
            $found = true;
        }
}

您可以使用in_array函數,更多信息可以在in_array函數中找到

    private static function getProductTaxRatesByTaxationRules($cartItem) {
        $shippingAddressData = self::$cart['ShippingAddress']['UserAddress'];
        $taxationRules = self::$cart['TaxationRules'];
        $currentUser = LoggedInUser::get();

        // Prepare data to compare with taxation rules
        $regularUser = false;
        if (!$currentUser['is_company']) {
            $regularUser = true;
        }
        $compareData = [
            'regular_user' => $regularUser,
            'company' => $currentUser['is_company'],
            'product_id' => $cartItem['product_id'],
            'subtype_id' => $cartItem['productIndex']['subtype_id'],
            'country_id' => $shippingAddressData['country_id'],
            'country_state_id' => $shippingAddressData['country_state_id']
        ];

        $taxRates = $cartItem['productIndex']['tax_rate'];

        foreach ($taxationRules as $taxRule) {
            $result = validateData($taxRule, $compareData);
            if ($result && !empty($taxRule['TaxationRule']['TaxationRuleCountryState'])) {
                $found = false;
                foreach ($taxRule['TaxationRule']['TaxationRuleCountryState'] as $countryStates) {
                    if (isset($countryStates['country_state_id']) && !is_null($countryStates['country_state_id'])) {
                        if ($countryStates['country_state_id'] == $compareData['country_state_id']) {
                            $found = true;
                        }
                    } else {
                        if ($countryStates['country_id'] == $compareData['country_id']) {
                            $found = true;
                        }
                    }
                }
                if (!$found) {
                    continue;
                }
            }
            // If any of flags aren't true, then add Taxation_rule_enabled_tax_rate and
            // check if there are enabled tax rates that needs to be added
            if (!empty($taxRule['TaxationRule']['Taxation_rule_enabled_tax_rate'])) {
                foreach ($taxRule['TaxationRule']['Taxation_rule_enabled_tax_rate'] as $includeTax) {
                    foreach ($taxRule['TaxRate'] as $taxData) {
                        if ($taxData['id'] == $includeTax) {
                            $taxRates[] = array(
                                'id' => $taxData['id'],
                                'value' => $taxData['taxrate']
                            );
                        }
                    }
                }
            }
        }

        foreach ($taxationRules as $taxRule) {
            $result = validateData($taxRule, $compareData);
            if ($result && !empty($taxRule['TaxationRule']['TaxationRuleCountryState'])) {
                $found = false;
                foreach ($taxRule['TaxationRule']['TaxationRuleCountryState'] as $countryStates) {
                    if (isset($countryStates['country_state_id']) && !is_null($countryStates['country_state_id'])) {
                        if ($countryStates['country_state_id'] == $compareData['country_state_id']) {
                            $found = true;
                        }
                    } else {
                        if ($countryStates['country_id'] == $compareData['country_id']) {
                            $found = true;
                        }
                    }
                }
                if (!$found) {
                    continue;
                }
            }

            // If any of flags aren't true, then remove Taxation_rule_disabled_tax_rate
            // Check if there are disabled tax rates that needed to be removed
            if (!empty($taxRule['TaxationRule']['Taxation_rule_disabled_tax_rate'])) {
                foreach ($taxRule['TaxationRule']['Taxation_rule_disabled_tax_rate'] as $excludeTaxId) {
                    foreach ($taxRates as $key => $value) {
                        if ($value['id'] == $excludeTaxId) {
                            unset($taxRates[$key]);
                            break;
                        }
                    }
                }
            }
        }

        return $taxRates;
    }

    private function validateData($taxRule, $compareData) {
        $result = false;
        if ($taxRule['TaxationRule']['user_type'] == 'regular' && !$compareData['regular_user']) {
            $result = true;
        }
        if ($taxRule['TaxationRule']['user_type'] == 'companies' && !$compareData['company']) {
            $result = true;
        }
        if (!empty($taxRule['TaxationRule']['Taxation_rule_product'])) {
            if (!in_array($compareData['product_id'], $taxRule['TaxationRule']['Taxation_rule_product'])) {
                $result = true;
            }
        }
        if (!empty($taxRule['TaxationRule']['Taxation_rule_product_subtype'])) {
            if (!in_array($compareData['subtype_id'], $taxRule['TaxationRule']['Taxation_rule_product_subtype'])) {
                $result = true;
            }
        }
        return $result;
    }
    private static function getProductTaxRatesByTaxationRules($cartItem) {
        $shippingAddressData = self::$cart['ShippingAddress']['UserAddress'];
        $taxationRules = self::$cart['TaxationRules'];
        $currentUser = LoggedInUser::get();

        // Prepare data to compare with taxation rules
        $regularUser = false;
        if (!$currentUser['is_company']) {
            $regularUser = true;
        }
        $compareData = [
            'regular_user' => $regularUser,
            'company' => $currentUser['is_company'],
            'product_id' => $cartItem['product_id'],
            'subtype_id' => $cartItem['productIndex']['subtype_id'],
            'country_id' => $shippingAddressData['country_id'],
            'country_state_id' => $shippingAddressData['country_state_id']
        ];

        $taxRates = $cartItem['productIndex']['tax_rate'];

        foreach ($taxationRules as $taxRule) {
            $result = validateData($taxRule, $compareData);

            // If any of flags aren't true, then remove Taxation_rule_disabled_tax_rate
            // Check if there are disabled tax rates that needed to be removed
            if ($result && !empty($taxRule['TaxationRule']['Taxation_rule_disabled_tax_rate'])) {
                foreach ($taxRule['TaxationRule']['Taxation_rule_disabled_tax_rate'] as $excludeTaxId) {
                    foreach ($taxRates as $key => $value) {
                        if ($value['id'] == $excludeTaxId) {
                            unset($taxRates[$key]);
                            break;
                        }
                    }
                }
            }

            // If any of flags aren't true, then add Taxation_rule_enabled_tax_rate and
            // check if there are enabled tax rates that needs to be added
            if ($result && !empty($taxRule['TaxationRule']['Taxation_rule_enabled_tax_rate'])) {
                foreach ($taxRule['TaxationRule']['Taxation_rule_enabled_tax_rate'] as $includeTax) {
                    foreach ($taxRule['TaxRate'] as $taxData) {
                        if ($taxData['id'] == $includeTax) {
                            $taxRates[] = array(
                                'id' => $taxData['id'],
                                'value' => $taxData['taxrate']
                            );
                        }
                    }
                }
            }
        }

        return $taxRates;
    }

    private function validateData($taxRule, $compareData) {
        $result = false;
        if ($taxRule['TaxationRule']['user_type'] == 'regular' && !$compareData['regular_user']) {
            $result = true;
        }
        if ($taxRule['TaxationRule']['user_type'] == 'companies' && !$compareData['company']) {
            $result = true;
        }
        if (!empty($taxRule['TaxationRule']['Taxation_rule_product'])) {
            if (!in_array($compareData['product_id'], $taxRule['TaxationRule']['Taxation_rule_product'])) {
                $result = true;
            }
        }
        if (!empty($taxRule['TaxationRule']['Taxation_rule_product_subtype'])) {
            if (!in_array($compareData['subtype_id'], $taxRule['TaxationRule']['Taxation_rule_product_subtype'])) {
                $result = true;
            }
        }
        if (!empty($taxRule['TaxationRule']['TaxationRuleCountryState'])) {
            foreach ($taxRule['TaxationRule']['TaxationRuleCountryState'] as $countryStates) {
                if (isset($countryStates['country_state_id']) && !is_null($countryStates['country_state_id'])) {
                    if ($countryStates['country_state_id'] == $compareData['country_state_id']) {
                        $result = true;
                    }
                } else {
                    if ($countryStates['country_id'] == $compareData['country_id']) {
                        $result = true;
                    }
                }
            }
        }
        return $result;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM