/**
  * @param string $params
  */
 public function readParams($params, $allow_trial = true)
 {
     // Old params only had trial and full
     if ($allow_trial) {
         $terms = array('trial_', 'full_');
     } else {
         $terms = array('full_');
     }
     $return = false;
     $this->pointer = 0;
     $this->terms = array();
     foreach ($terms as $t) {
         // Make sure this period is actually of substance
         if (!empty($params[$t . 'period']) || !empty($params['lifetime']) && $t == 'full_') {
             $term = new itemTerm();
             // If we have a trial, we need to mark this
             if ($t == 'trial_') {
                 $this->set('hasTrial', true);
                 $term->set('type', 'trial');
             } else {
                 $term->set('type', 'term');
             }
             if ($t != 'trial_' && !empty($params['lifetime'])) {
                 $duration['lifetime'] = true;
             } else {
                 $duration['period'] = $params[$t . 'period'];
                 $duration['unit'] = $params[$t . 'periodunit'];
             }
             $term->set('duration', $duration);
             if ($params[$t . 'free']) {
                 $term->addCost('0.00');
             } else {
                 $term->addCost($params[$t . 'amount']);
             }
             $this->addTerm($term);
             $return = true;
         }
     }
     $free = true;
     foreach ($this->terms as $term) {
         if (empty($term->free)) {
             $free = false;
         }
     }
     $this->free = $free;
     $this->nextterm =& $this->terms[$this->pointer];
     return $return;
 }
Exemple #2
0
 public function invoice_items_total($request)
 {
     if (isset($request->add->tax)) {
         return true;
     } else {
         $request->add->tax = array();
     }
     $taxtypes = array();
     $taxcollections = array();
     // Collect all the taxes from the individual item costs
     foreach ($request->add->itemlist as $item) {
         foreach ($item['terms']->nextterm->cost as $cost) {
             if ($cost->type == 'tax') {
                 if (in_array($cost->cost['details'], $taxtypes)) {
                     $typeid = array_search($cost->cost['details'], $taxtypes);
                 } else {
                     $taxtypes[] = $cost->cost['details'];
                     $typeid = count($taxtypes) - 1;
                 }
                 if (!isset($taxcollections[$typeid])) {
                     $taxcollections[$typeid] = 0;
                 }
                 $taxcollections[$typeid] += $cost->renderCost() * $item['quantity'];
             }
         }
     }
     if (count($taxcollections) == 0) {
         return null;
     }
     $taxamount = 0;
     // Add tax items to total
     foreach ($taxcollections as $tid => $amount) {
         // Create tax
         $term = new itemTerm();
         if (!empty($taxtypes[$tid])) {
             $term->addCost($amount, array('details' => $taxtypes[$tid], 'tax' => true));
         } else {
             $term->addCost($amount, array('tax' => true));
         }
         $terms = new itemTerms();
         $terms->addTerm($term);
         // Add the "Tax" row
         $request->add->tax[] = array('cost' => $amount, 'terms' => $terms);
         $taxamount += $amount;
     }
     $grand_total = $request->add->total->cost['amount'];
     if (!empty($request->add->discount)) {
         foreach ($request->add->discount as $citems) {
             foreach ($citems as $ccitem) {
                 $citem = $ccitem->renderCost();
                 foreach ($citem as $cost) {
                     if ($cost->type == 'discount') {
                         $grand_total += $cost->cost['amount'];
                     }
                 }
             }
         }
     }
     $grand_total = AECToolbox::correctAmount($grand_total + $taxamount);
     // Modify grand total according to tax
     $request->add->grand_total->set('cost', array('amount' => $grand_total));
     // Formatting for total
     $request->add->total->cost['amount'] = AECToolbox::correctAmount($request->add->total->cost['amount']);
     return true;
 }
 public function applyToTotal($items, $cart = false, $fullcart = false)
 {
     $itemcount = 0;
     foreach ($items->itemlist as $item) {
         if (is_object($item)) {
             $itemcount += $item->quantitiy;
         } else {
             $itemcount++;
         }
     }
     if (empty($this->fullcartlist)) {
         return $items;
     }
     foreach ($this->fullcartlist as $coupon_code) {
         if (!$this->loadCoupon($coupon_code)) {
             continue;
         }
         if ($this->cph->discount['amount_use']) {
             $this->cph->discount['amount'] = $this->cph->discount['amount'] / $itemcount;
         }
         $cost = null;
         $costarray = array();
         foreach ($items->itemlist as $cid => $citem) {
             if ($citem['obj'] == false) {
                 continue;
             }
             $citem['terms']->nextterm->computeTotal();
             if (empty($cost)) {
                 $cost = clone $citem['terms']->nextterm->getBaseCostObject();
                 $costarray[$cid] = $cost->cost['amount'];
             } else {
                 $ccost = $citem['terms']->nextterm->getBaseCostObject();
                 $cost->cost['amount'] = $cost->cost['amount'] + $ccost->cost['amount'] * $citem['quantity'];
                 $costarray[$cid] = $ccost->cost['amount'];
             }
             $items->itemlist[$cid]['terms'] = $this->cph->applyToTerms($items->itemlist[$cid]['terms'], true);
         }
     }
     $discounttypes = array();
     $discount_col = array();
     foreach ($items->itemlist as $item) {
         foreach ($item['terms']->nextterm->cost as $cost) {
             if ($cost->type != 'discount') {
                 continue;
             }
             $cc = $cost->cost['coupon'] . ' - ' . $cost->cost['details'];
             if (in_array($cc, $discounttypes)) {
                 $typeid = array_search($cc, $discounttypes);
             } else {
                 $discounttypes[] = $cc;
                 $typeid = count($discounttypes) - 1;
             }
             if (!isset($discount_col[$typeid])) {
                 $discount_col[$typeid] = 0;
             }
             $discount_col[$typeid] += $cost->renderCost();
         }
     }
     if (!empty($discount_col)) {
         // Dummy terms
         $terms = new itemTerms();
         $term = new itemTerm();
         foreach ($discount_col as $cid => $discount) {
             $cce = explode(' - ', $discounttypes[$cid], 2);
             $term->addCost($discount, array('amount' => $discount, 'coupon' => $cce[0], 'details' => $cce[1]));
         }
         $terms->addTerm($term);
         if (empty($items->discount)) {
             $items->discount = array();
         }
         $items->discount[] = $terms->terms;
     }
     return $items;
 }