public function update() { require_once 'MB_API.php'; $mb = new MB_API(); //delete it all first, hopefully it works.. This is the only way to ensure we purge old products $this->Product->deleteAll(array(1 => 1)); //first big loop through all Categories, this is the only way as the API won't return the Category ID foreach ($this->CFE_Categories as $cat_id => $cat_name) { $data = $mb->GetProducts(array('SellOnline' => true, 'CategoryIDs' => array($cat_id))); // debug($data); if ($data['GetProductsResult']['ErrorCode'] == 200) { //if only one result it needs to be fixed up if (isset($data['GetProductsResult']['Products']['Product']['ID'])) { $temp_data = array(); $temp_data = $data['GetProductsResult']['Products']['Product']; unset($data['GetProductsResult']['Products']['Product']); $data['GetProductsResult']['Products']['Product'][0] = $temp_data; } //debug($data); foreach ($data['GetProductsResult']['Products']['Product'] as $key => $product) { $product['CategoryID'] = $cat_id; $product['CategoryName'] = $cat_name['name']; $product['prodtype'] = $cat_name['prodtype']; $product['barcodeID'] = $product['ID']; //must deal with Banker's tax rounding if hundreds is even then it rounds down but if odd rounds up //NO I THINK THAT REP WAS WRONG! DAMNIT! /* $tax=number_format(floor(($product['OnlinePrice']*$product['TaxRate'])*100)/100,3)*100; debug($tax); if ( $tax & 1 )$tax++; */ //I think if there is ANY value in thousands place it rounds up, otherwise leaves it alone $rawtax = $product['OnlinePrice'] * $product['TaxRate']; $tax = number_format(floor($product['OnlinePrice'] * $product['TaxRate'] * 100) / 100, 3) * 100; $digit3 = explode('.', $rawtax); if (isset($digit3[1]) && strlen($digit3[1]) > 2) { $tax++; } //debug($tax); $product['ExtendedPrice'] = $product['OnlinePrice'] + $tax / 100; $this->Product->create(); if ($this->Product->save($product)) { $this->Session->setFlash('Products have been updated', 'flash_success'); } } } else { $this->Session->setFlash('Could not get products. Try again then Seek help!', 'flash_danger'); debug($data); } } foreach ($this->CFE_ComboTypeIDs as $ses_id) { $data = $mb->GetServices(array('LocationID' => 1, 'HideRelatedPrograms' => true, 'SellOnline' => true, 'SessionTypeIDs' => array($ses_id))); //there is no description of services sent by the API - DOH! //debug($data); //if only one result it needs to be fixed up if (isset($data['GetServicesResult']['Services']['Service']['ID'])) { $temp_data = array(); $temp_data = $data['GetServicesResult']['Services']['Service']; unset($data['GetServicesResult']['Services']['Service']); $data['GetServicesResult']['Services']['Service'][0] = $temp_data; } //debug($data); foreach ($data['GetServicesResult']['Services']['Service'] as $key => $product) { $product['barcodeID'] = $product['ID']; unset($product['ID']); $product['CategoryID'] = $product['ProductID']; $product['CategoryName'] = 'Service'; $product['prodtype'] = 'Service'; $product['SessionTypeID'] = $ses_id; $product['SessionTypeName'] = 'Service'; $product['DoubleTypeID'] = $this->CFE_DoubleTypeIDs[$ses_id]; $tax = number_format(floor($product['OnlinePrice'] * $product['TaxRate'] * 100) / 100, 2) * 100; //debug($tax); //unless it finished even, in which case no rounding! if ($tax & 1) { $tax++; } $product['ExtendedPrice'] = $product['OnlinePrice'] + $tax / 100; $this->Product->create(); if ($this->Product->save($product)) { $this->Session->setFlash('Products have been updated', 'flash_success'); } else { debug('broke'); break 2; } } } // $this->set('request',$mb->getXMLRequest()); $this->set('products', $this->Product->find('all')); $this->render('update', 'frontend'); }