Esempio n. 1
0
 /**
  * Calculate subscription fees based on input parameter
  *
  * @param JTable  $rowPlan the object which contains information about the plan
  * @param RADForm $form    The form object which is used to calculate extra fee
  * @param array   $data    The post data
  * @param Object  $config
  * @param string  $paymentMethod
  *
  * @return array
  */
 public static function calculateSubscriptionFee($rowPlan, $form, $data, $config, $paymentMethod = null)
 {
     $db = JFactory::getDbo();
     $fees = array();
     $feeAmount = $form->calculateFee();
     $trialAmount = $rowPlan->trial_amount + $feeAmount;
     $trialDiscountAmount = 0;
     $trialTaxAmount = 0;
     $regularAmount = $rowPlan->price + $feeAmount;
     $regularDiscountAmount = 0;
     $regularTaxAmount = 0;
     $discountAmount = 0;
     $taxAmount = 0;
     $action = $data['act'];
     if ($action == 'renew') {
         $renewOptionId = (int) $data['renew_option_id'];
         if ($renewOptionId == OSM_DEFAULT_RENEW_OPTION_ID) {
             $amount = $rowPlan->price;
         } else {
             $sql = 'SELECT price  FROM #__osmembership_renewrates WHERE id=' . (int) $data['renew_option_id'];
             $db->setQuery($sql);
             $amount = $db->loadResult();
         }
     } elseif ($action == 'upgrade') {
         $sql = 'SELECT price FROM #__osmembership_upgraderules WHERE id=' . (int) $data['upgrade_option_id'];
         $db->setQuery($sql);
         $amount = $db->loadResult();
     } else {
         if ($rowPlan->recurring_subscription && $rowPlan->trial_duration) {
             $amount = $rowPlan->trial_amount;
         } else {
             $amount = $rowPlan->price;
         }
     }
     $amount += $feeAmount;
     $couponCode = isset($data['coupon_code']) ? $data['coupon_code'] : '';
     if ($couponCode) {
         $planId = $rowPlan->id;
         $nullDate = $db->getNullDate();
         $query = $db->getQuery(true);
         $query->select('*')->from('#__osmembership_coupons')->where('published=1')->where('code="' . $couponCode . '"')->where('(valid_from="' . $nullDate . '" OR DATE(valid_from) <= CURDATE())')->where('(valid_to="' . $nullDate . '" OR DATE(valid_to) >= CURDATE())')->where('(times = 0 OR times > used)')->where('(plan_id=0 OR plan_id=' . $planId . ')');
         $db->setQuery($query);
         $coupon = $db->loadObject();
         if ($coupon) {
             $couponValid = 1;
             if ($coupon->coupon_type == 0) {
                 $discountAmount = $amount * $coupon->discount / 100;
                 $trialDiscountAmount = $trialAmount * $coupon->discount / 100;
                 $regularDiscountAmount = $regularAmount * $coupon->discount / 100;
             } else {
                 $discountAmount = min($coupon->discount, $amount);
                 $trialDiscountAmount = min($coupon->discount, $trialAmount);
                 $regularDiscountAmount = min($coupon->discount, $regularAmount);
             }
             $discountAmount = round($discountAmount, 2);
             $trialDiscountAmount = round($trialDiscountAmount, 2);
             $regularDiscountAmount = round($regularDiscountAmount, 2);
         } else {
             $couponValid = 0;
         }
     } else {
         $couponValid = 1;
     }
     $country = isset($data['country']) ? $data['country'] : $config->default_country;
     $countryCode = self::getCountryCode($country);
     // Calculate tax
     if (!empty($config->eu_vat_number_field) && isset($data[$config->eu_vat_number_field])) {
         $vatNumber = $data[$config->eu_vat_number_field];
         if ($vatNumber) {
             // If users doesn't enter the country code into the VAT Number, append the code
             $firstTwoCharacters = substr($vatNumber, 0, 2);
             if (strtoupper($firstTwoCharacters) != $countryCode) {
                 $vatNumber = $countryCode . $vatNumber;
             }
         }
     } else {
         $vatNumber = '';
     }
     $vatNumberValid = 1;
     if ($vatNumber) {
         $valid = OSMembershipHelperEuvat::validateEUVATNumber($vatNumber);
         if ($valid) {
             $taxRate = self::calculateTaxRate($rowPlan->id, $country, 1);
         } else {
             $vatNumberValid = 0;
             $taxRate = self::calculateTaxRate($rowPlan->id, $country, 0);
         }
     } else {
         $taxRate = self::calculateTaxRate($rowPlan->id, $country, 0);
     }
     if ($taxRate > 0) {
         $taxAmount = round(($amount - $discountAmount) * $taxRate / 100, 2);
         $trialTaxAmount = round(($trialAmount - $trialDiscountAmount) * $taxRate / 100, 2);
         $regularTaxAmount = round(($regularAmount - $regularDiscountAmount) * $taxRate / 100, 2);
     }
     $trialGrossAmount = $trialAmount - $trialDiscountAmount + $trialTaxAmount;
     $regularGrossAmount = $regularAmount - $regularDiscountAmount + $regularTaxAmount;
     $grossAmount = $amount - $discountAmount + $taxAmount;
     $paymentFeeAmount = 0;
     $paymentFeePercent = 0;
     if ($paymentMethod) {
         $method = os_payments::loadPaymentMethod($paymentMethod);
         $params = new JRegistry($method->params);
         $paymentFeeAmount = (double) $params->get('payment_fee_amount');
         $paymentFeePercent = (double) $params->get('payment_fee_percent');
     }
     if ($paymentFeeAmount > 0 || $paymentFeePercent > 0) {
         $fees['trial_payment_processing_fee'] = round($paymentFeeAmount + $trialAmount * $paymentFeePercent / 100, 2);
         $fees['regular_payment_processing_fee'] = round($paymentFeeAmount + $regularGrossAmount * $paymentFeePercent / 100, 2);
         $fees['payment_processing_fee'] = round($paymentFeeAmount + $grossAmount * $paymentFeePercent / 100, 2);
         $trialGrossAmount += $fees['trial_payment_processing_fee'];
         $regularGrossAmount += $fees['regular_payment_processing_fee'];
         $grossAmount += $fees['payment_processing_fee'];
     } else {
         $fees['trial_payment_processing_fee'] = 0;
         $fees['regular_payment_processing_fee'] = 0;
         $fees['payment_processing_fee'] = 0;
     }
     $fees['trial_amount'] = $trialAmount;
     $fees['trial_discount_amount'] = $trialDiscountAmount;
     $fees['trial_tax_amount'] = $trialTaxAmount;
     $fees['trial_gross_amount'] = $trialGrossAmount;
     $fees['regular_amount'] = $regularAmount;
     $fees['regular_discount_amount'] = $regularDiscountAmount;
     $fees['regular_tax_amount'] = $regularTaxAmount;
     $fees['regular_gross_amount'] = $regularGrossAmount;
     $fees['amount'] = $amount;
     $fees['discount_amount'] = $discountAmount;
     $fees['tax_amount'] = $taxAmount;
     $fees['gross_amount'] = $grossAmount;
     $fees['coupon_valid'] = $couponValid;
     $fees['vatnumber_valid'] = $vatNumberValid;
     $fees['country_code'] = $countryCode;
     if (OSMembershipHelperEuvat::isEUCountry($countryCode)) {
         $fees['show_vat_number_field'] = 1;
     } else {
         $fees['show_vat_number_field'] = 0;
     }
     return $fees;
 }