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; } }
public function getCheckout($metaUser, $counter = 0, $InvoiceFactory = null) { $c = array(); $totalcost = 0; if (empty($this->content)) { return array(); } $return = array(); foreach ($this->content as $cid => $content) { // Cache items if (!isset($c[$content['type']][$content['id']])) { switch ($content['type']) { case 'plan': $obj = new SubscriptionPlan(); $obj->load($content['id']); $o = array(); $o['obj'] = $obj; $o['name'] = $obj->getProperty('name'); $o['desc'] = $obj->getProperty('desc'); $terms = $obj->getTermsForUser(false, $metaUser); if ($counter) { $terms->incrementPointer($counter); } $o['terms'] = $terms; $o['cost'] = $terms->nextterm->renderCost(); $c[$content['type']][$content['id']] = $o; break; } } $entry = array(); $entry['obj'] = $c[$content['type']][$content['id']]['obj']; $entry['fullamount'] = $c[$content['type']][$content['id']]['cost']; $entry['name'] = $c[$content['type']][$content['id']]['name']; $entry['desc'] = $c[$content['type']][$content['id']]['desc']; $entry['terms'] = $c[$content['type']][$content['id']]['terms']; $item = array('item' => array('obj' => $entry['obj']), 'terms' => $entry['terms']); if (!empty($content['coupons'])) { $cpsh = new couponsHandler($metaUser, false, $content['coupons']); $item = $cpsh->applyAllToItems(0, $item); $entry['terms'] = $item['terms']; } $entry['cost'] = $entry['terms']->nextterm->renderTotal(); if ($entry['cost'] > 0) { $total = $content['quantity'] * $entry['cost']; $entry['cost_total'] = AECToolbox::correctAmount($total); } else { $entry['cost_total'] = AECToolbox::correctAmount('0.00'); } if ($entry['cost_total'] == '0.00') { $entry['free'] = true; } else { $entry['free'] = false; } $entry['cost'] = AECToolbox::correctAmount($entry['cost']); $entry['quantity'] = $content['quantity']; $totalcost += $entry['cost_total']; $return[$cid] = $entry; } if (!empty($this->params['overall_coupons'])) { $cpsh = new couponsHandler($metaUser, $InvoiceFactory, $this->params['overall_coupons']); $totalcost_ncp = $totalcost; $totalcost = $cpsh->applyToAmount($totalcost); } else { $totalcost_ncp = $totalcost; } // Append total cost $return[] = array('name' => '', 'count' => '', 'cost' => AECToolbox::correctAmount($totalcost_ncp), 'cost_total' => AECToolbox::correctAmount($totalcost), 'is_total' => true, 'obj' => false); return $return; }