Ejemplo n.º 1
0
 function mathOperation($options, $supplementItem, $order, $supplement_choice, $calcul)
 {
     $defOpt = array('operator' => '+', 'left' => null, 'right' => null, 'leftMethods' => null, 'rightMethods' => null, 'modifProp' => 'total');
     $opt = array_merge($defOpt, $options);
     if (!is_null($opt['leftMethods'])) {
         $leftOpt = $this->calculFunct(key($opt['leftMethods']), reset($opt['leftMethods']), $supplementItem, $order, $supplement_choice, $calcul);
         $left = $leftOpt[$opt['modifProp']];
     } elseif (!is_null($opt['left'])) {
         $left = $opt['left'];
     } else {
         $left = $supplementItem[$opt['modifProp']];
     }
     if (!is_null($opt['rightMethods'])) {
         $rigthOpt = $this->calculFunct(key($opt['rightMethods']), reset($opt['rightMethods']), $supplementItem, $order, $supplement_choice, $calcul);
         $right = $rigthOpt[$opt['modifProp']];
     } elseif (!is_null($opt['right'])) {
         $right = $opt['right'];
     } else {
         $right = 0;
     }
     App::import('Lib', 'Shop.Operations');
     $supplementItem[$opt['modifProp']] = Operations::simpleOperation($left, $opt['operator'], $right);
     return $supplementItem;
 }
Ejemplo n.º 2
0
 function operations(&$eventOptions, $params)
 {
     if (!empty($params['operations']) && is_array($params['operations'])) {
         //debug($params);
         $defOpt = array('operations' => array());
         $opt = array_merge($defOpt, $params);
         $opt['returnModel'] = true;
         list($model, $conditions) = $this->_selectEntriesCond($opt);
         if (!is_null($conditions)) {
             $findOpt = array('fields' => array_merge(array('id'), Set::extract('/field', $opt['operations'])), 'conditions' => $conditions, 'recursive' => -1);
             //debug($findOpt);
             //$this->Event = ClassRegistry::init('Event');
             //$this->Event->debugEventStack($eventOptions);
             $entries = $model->find('all', $findOpt);
             App::import('Lib', 'Operations');
             $count = 0;
             foreach ($entries as $entry) {
                 foreach ($opt['operations'] as $op) {
                     $fld = $op['field'];
                     $newVal = Operations::simpleOperation($entry[$model->alias][$fld], $op['operator'], $op['value']);
                     if ($newVal != $entry[$model->alias][$fld]) {
                         $entry[$model->alias][$fld] = $newVal;
                     } else {
                         unset($entry[$model->alias][$fld]);
                     }
                 }
                 if (count($entry[$model->alias]) > 1) {
                     //only if there is more than the id in the data
                     //debug($entry);
                     if ($model->save($entry)) {
                         $count++;
                     }
                 }
             }
             return $count;
         }
     }
     return false;
 }
Ejemplo n.º 3
0
 function calculSubItem($products, $order = null)
 {
     App::import('Lib', 'Shop.SetMulti');
     if (SetMulti::isAssoc($products)) {
         $prods = array(&$products);
     } else {
         $prods =& $products;
     }
     foreach ($prods as &$prod) {
         $orderItemMode = isset($prod['item_price']);
         if ($orderItemMode) {
             $p =& $prod;
         } else {
             $p = $this->extractOrderItemData($p2 = $prod, $order);
         }
         $cur_price = $p['item_price'];
         $p['item_alone_price'] = $cur_price;
         $p['overwritten_price'] = false;
         $subItems = $this->extractSubItemData($prod, $order);
         if (!empty($subItems)) {
             //============ calculate ============//
             App::import('Lib', 'Shop.Operations');
             foreach ($subItems as &$subItem) {
                 //debug($subItem);
                 if ($subItem['item_price'] != '' && !is_null($subItem['item_price'])) {
                     $new_price = $cur_price;
                     $subPrice = $subItem['item_price'] * $subItem['nb'];
                     if ($subItem['item_operator'] == '=') {
                         $new_price = $subPrice;
                         $p['overwritten_price'] = true;
                     } else {
                         $new_price = Operations::simpleOperation($cur_price, $subItem['item_operator'], $subPrice);
                     }
                     $subItem['modif'] = $new_price - $cur_price;
                     $cur_price = $new_price;
                 }
             }
             $p['SubItem'] = $subItems;
         }
         $p['subitems_modif'] = $cur_price - $p['item_price'];
         $p['item_price'] = $cur_price;
         if (!$orderItemMode) {
             if (isset($prod['ShopProduct']['DynamicField'])) {
                 $dprod =& $prod['ShopProduct'];
             } else {
                 $dprod =& $prod;
             }
             $dprod['DynamicField']['subitems_modif'] = $p['subitems_modif'];
             $dprod['DynamicField']['alone_price'] = $p['item_alone_price'];
             $dprod['DynamicField']['price'] = $p['item_price'];
             $dprod['DynamicField']['overwritten_price'] = $p['overwritten_price'];
         }
     }
     return $products;
 }
Ejemplo n.º 4
0
 function testApplicableCond(&$model, $cond, $data = null, $alias = null, &$unfiltered = null)
 {
     if (is_null($alias)) {
         $alias = $model->alias;
     }
     if (is_null($data)) {
         $data = $model->data;
     }
     $filtered = $data;
     $unfiltered = $cond;
     //$cond['lol']=3;
     App::import('Lib', 'Operations');
     foreach ($cond as $condkey => $condval) {
         $find = false;
         $matches = array();
         //debug($key);
         if (is_numeric($condkey)) {
         } elseif (preg_match('/^((?:([A-Z][A-Za-z0-9_-]*).)?[A-Za-z0-9_-]*)\\s*(.*)\\s*$/', $condkey, $matches)) {
             //debug($matches);
             list($tmp, $path, $model, $operator) = $matches;
             if (empty($model)) {
                 $path = $alias . '.' . $path;
             }
             $ex = Set::extract('{n}.' . $path, $filtered);
             $ex = array_combine(array_keys($filtered), $ex);
             //debug($condval);
             //debug($ex);
             foreach ($ex as $key => $val) {
                 if (!is_null($val)) {
                     $res = false;
                     if (empty($operator)) {
                         $res = $val == $condval;
                     } else {
                         $res = Operations::simpleOperation($val, $operator, $condval);
                     }
                     if ($res === false) {
                         unset($filtered[$key]);
                     }
                     if (is_bool($res)) {
                         $find = true;
                     }
                 }
             }
         }
         if ($find) {
             unset($unfiltered[$condkey]);
         }
     }
     return $filtered;
 }