Skip to content

Instantly share code, notes, and snippets.

@DavidOliver
Created February 10, 2012 14:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidOliver/1789929 to your computer and use it in GitHub Desktop.
Save DavidOliver/1789929 to your computer and use it in GitHub Desktop.
Calculating parts prices - Symphony CMS
<?php
public function grab(&$param_pool=NULL){
$result = new XMLElement($this->dsParamROOTELEMENT);
try{
include(TOOLKIT . '/data-sources/datasource.section.php');
}
catch(FrontendPageNotFoundException $e){
// Work around. This ensures the 404 page is displayed and
// is not picked up by the default catch() statement below
FrontendPageNotFoundExceptionHandler::render($e);
}
catch(Exception $e){
$result->appendChild(new XMLElement('error', $e->getMessage()));
return $result;
}
if($this->_force_empty_result) $result = $this->emptyXMLSet();
// Get and set default profit margin
$dpm_query = 'SELECT value FROM sym_entries_data_1 LIMIT 1';
$dpm_result = Symphony::Database()->fetch($dpm_query);
$default_profit_margin = $dpm_result[0]['value'];
// Add profit margin
function addProfitMargin($profit_margin, $purchase_price) {
return (100 + $profit_margin) * $purchase_price / 100;
}
// Loop through entries
foreach($result->getChildren('entry') as $entry) {
if($entry->getName() == 'entry') {
// Loop through entry fields
$entry_children = $entry->getChildren();
foreach($entry_children as $field) {
// Grab entry information required for calculation
switch($field->getName()) {
case 'purchase-price':
$purchase_price = $field->getValue();
break;
case 'fixed-sales-price':
$fixed_sales_price = $field->getValue();
break;
case 'custom-profit-margin':
$custom_profit_margin = $field->getValue();
break;
case 'sales-price':
$field_child = $field->getChildrenByName('item');
$sales_price = $field_child[0]->getValue();
break;
}
}
// Calculate actual price
switch($sales_price) {
case 'Fixed':
$actual_price = $fixed_sales_price;
break;
case 'Custom profit margin':
$actual_price = addProfitMargin($custom_profit_margin, $purchase_price);
break;
case 'Default profit margin':
$actual_price = addProfitMargin($default_profit_margin, $purchase_price);
break;
}
// Remove unwanted XML from result
$entry->removeChildAt(3);
$entry->removeChildAt(4);
$entry->removeChildAt(5);
// Add actual price to result
$entry->appendChild(new XMLElement('price', $actual_price));
}
}
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment