Beispiel #1
0
 public function computeAmount($InvoiceFactory = null, $save = true, $recurring_choice = null)
 {
     if (!empty($InvoiceFactory->metaUser)) {
         $metaUser = $InvoiceFactory->metaUser;
     } else {
         $metaUser = new metaUser($this->userid ? $this->userid : 0);
     }
     $pp = null;
     $madefree = false;
     if (is_null($recurring_choice) && isset($this->params['userselect_recurring'])) {
         $recurring_choice = $this->params['userselect_recurring'];
     }
     if (empty($this->usage)) {
         return null;
     }
     $recurring = 0;
     if (InvoiceFactory::isActualProcessorName($this->method)) {
         if (empty($InvoiceFactory->pp)) {
             $pp = new PaymentProcessor();
             if (!$pp->loadName($this->method)) {
                 $short = 'processor loading failure';
                 $event = 'When computing invoice amount, tried to load processor: ' . $this->method;
                 $tags = 'processor,loading,error';
                 $params = array();
                 $eventlog = new eventLog();
                 $eventlog->issue($short, $tags, $event, 128, $params);
                 return;
             }
             $pp->fullInit();
         } else {
             $pp = $InvoiceFactory->pp;
         }
         if ($pp->is_recurring($recurring_choice)) {
             $recurring = $pp->is_recurring($recurring_choice);
         }
         if (empty($this->currency)) {
             $this->currency = isset($pp->settings['currency']) ? $pp->settings['currency'] : '';
         }
     }
     $usage = explode('.', $this->usage);
     // Update old notation
     if (!isset($usage[1])) {
         $temp = $usage[0];
         $usage[0] = 'p';
         $usage[1] = $temp;
     }
     $allfree = false;
     switch (strtolower($usage[0])) {
         case 'c':
         case 'cart':
             $cart = $this->getObjUsage();
             if ($cart->id) {
                 if (!empty($this->coupons)) {
                     foreach ($this->coupons as $coupon) {
                         if (!$cart->hasCoupon($coupon)) {
                             $cart->addCoupon($coupon);
                         }
                     }
                 }
                 $return = $cart->getAmount($metaUser, $this->counter, $this);
                 $allfree = $cart->checkAllFree($metaUser, $this->counter, $this);
                 $this->amount = $return;
             } elseif (isset($this->params['cart'])) {
                 // Cart has been deleted, use copied data
                 $vars = get_object_vars($this->params['cart']);
                 foreach ($vars as $v => $c) {
                     // Make extra sure we don't put through any _properties
                     if (strpos($v, '_') !== 0) {
                         $cart->{$v} = $c;
                     }
                 }
                 $return = $cart->getAmount($metaUser, $this->counter, $this);
                 $this->amount = $return;
             } else {
                 $this->amount = '0.00';
             }
             break;
         case 'p':
         case 'plan':
         default:
             $plan = $this->getObjUsage();
             if (is_object($pp)) {
                 $pp->exchangeSettingsByPlan($plan);
                 if ($this->currency != $pp->settings['currency'] && !empty($pp->settings['currency'])) {
                     $this->currency = $pp->settings['currency'];
                 }
                 if ($pp->is_recurring($recurring_choice)) {
                     $recurring = $pp->is_recurring($recurring_choice);
                 } else {
                     $recurring = 0;
                 }
             }
             $terms = $plan->getTermsForUser($recurring, $metaUser);
             $terms->incrementPointer($this->counter);
             if ($this->coupons) {
                 $item = array('item' => array('obj' => $plan), 'terms' => $terms);
                 $cpsh = new couponsHandler($metaUser, $InvoiceFactory, $this->coupons);
                 $item = $cpsh->applyAllToItems(0, $item);
                 $terms = $item['terms'];
                 // Coupons might have changed the terms - reset pointer
                 $terms->setPointer($this->counter);
             }
             $allfree = $terms->checkFree();
             if (is_object($terms->nextterm)) {
                 $this->amount = $terms->nextterm->renderTotal();
             } else {
                 $this->amount = '0.00';
             }
             break;
     }
     // TODO: This is just a bandaid. We really need to move stuff from the factory over to the invoice class
     if (!empty($InvoiceFactory->items->grand_total->cost['amount'])) {
         $this->amount = $InvoiceFactory->items->grand_total->cost['amount'];
     }
     $this->amount = AECToolbox::correctAmount($this->amount);
     if (!$recurring || $allfree) {
         if (strcmp($this->amount, '0.00') === 0) {
             $this->method = 'free';
             $madefree = true;
         } elseif (strcmp($this->amount, '0.00') === 0 && strcmp($this->method, 'free') !== 0) {
             $short = 'invoice amount error';
             $event = 'When computing invoice amount: Method error, amount 0.00, but method = ' . $this->method;
             $tags = 'processor,loading,error';
             $params = array();
             $eventlog = new eventLog();
             $eventlog->issue($short, $tags, $event, 128, $params);
             $this->method = 'error';
         }
     }
     if ($save) {
         $this->storeload();
     }
     if ($madefree) {
         $this->made_free = true;
     }
 }