public function getAllCategories()
 {
     $categories = array();
     $ids = explode(" ", trim($this->category_ids));
     foreach ($ids as $id) {
         $c = new oqc_Category();
         if ($c->retrieve($id)) {
             $categories[] = array('category' => $c, 'subcategories' => $c->getSubCategories());
         }
     }
     return $categories;
 }
function getCategoryNameHtml($focus, $name, $value, $view)
{
    if ('DetailView' != $view) {
        return "";
        // skip the rest of the method if another view calls this method
    }
    $category = new oqc_Category();
    if ($category->retrieve($focus->relatedcategory_id)) {
        return $category->name;
    } else {
        return "";
    }
}
function getProductUsage($id)
{
    $productCatalog = new oqc_ProductCatalog();
    if ($productCatalog->retrieve($id)) {
        $frequency = array();
        $json = getJSONobj();
        $c = new oqc_Contract();
        $result = $c->get_list('', 'deleted=0');
        $allContracts = $result['list'];
        foreach ($allContracts as $contract) {
            // services of this contract
            $services = $contract->get_linked_beans('oqc_service', 'oqc_Service');
            foreach ($services as $service) {
                $product = new oqc_Product();
                // if the service refers to an existing product that is defined in this product catalog ...
                if ($product->retrieve($service->product_id) && $product->catalog_id == $id) {
                    // increase the frequency of the appearance of this product
                    $frequency[$product->name]['rate'] += $service->quantity;
                    if (!array_key_exists('category', $frequency[$product->name])) {
                        $category = new oqc_Category();
                        if ($category->retrieve($product->relatedcategory_id)) {
                            $frequency[$product->name]['category'] = $category->name;
                        }
                    }
                }
            }
        }
        $chartData = array();
        foreach ($frequency as $name => $frequencyArray) {
            $chartData[] = array('name' => $name, 'frequency' => $frequencyArray['rate'], 'category' => $frequencyArray['category']);
        }
        $encoded = $json->encode($chartData);
        echo $encoded;
    } else {
        echo "No Product Catalog with id='" + $id + "' found.";
    }
}
 function action_save()
 {
     $isDuplicate = empty($_REQUEST['record']) && empty($_REQUEST['return_id']);
     // check if there are any modifications
     $modified = hasBeenModified($this->bean, array());
     if (!$isDuplicate && !$modified) {
         return;
         // skip save if this is not a duplicate and nothing been modified
     }
     //2.0 We determine to what catalog category belongs
     if (!empty($this->bean->relatedcategory_id)) {
         $category = new oqc_Category();
         if ($category->retrieve($this->bean->relatedcategory_id)) {
             $this->bean->catalog_id = $category->catalog_id;
         }
     }
     // save id of user that created the old version
     global $timedate;
     $dateCreated = $timedate->to_db($this->bean->date_entered);
     $createdById = $this->bean->created_by;
     $old_id = $this->begin_new_version();
     parent::action_save();
     $this->end_new_version($old_id);
     $this->initializeUniqueIdentifier();
     $this->save_packaged_products();
     $this->saveAttachedDocuments();
     $this->saveProductOptions();
     $this->saveImageWithResize();
     $this->updateRelatedProducts($old_id);
     //$GLOBALS['log']->error("Dates are: ". $this->bean->date_entered );
     // the new contract should have the same creator and creation date as the previous version, fix for #486
     if ($dateCreated) {
         $this->bean->date_entered = $dateCreated;
     }
     if ($createdById) {
         $this->bean->created_by = $createdById;
     }
     if (!isset($_POST['assigned_user_id'])) {
         $this->bean->assigned_user_id = $this->bean->created_by;
     }
     //2.1 set this only if it is not in $_POST
     parent::action_save();
 }
 function fill_in_additional_list_fields()
 {
     parent::fill_in_additional_list_fields();
     // fillin the category number field if possible
     if (!empty($this->relatedcategory_id)) {
         $category = new oqc_Category();
         if ($category->retrieve($this->relatedcategory_id)) {
             $this->category_number = $category->number;
         }
     }
     $catalog = new oqc_ProductCatalog();
     if ($catalog->retrieve($this->catalog_id)) {
         $this->catalog_name = $catalog->name;
     }
     if (isset($this->currency_id) && $this->currency_id != '') {
         $currency = new Currency();
         $currency->retrieve($this->currency_id);
         if ($currency->id != $this->currency_id || $currency->deleted == 1) {
             //			$this->amount = $this->amount_usdollar;
             $this->currency_id = $currency->id;
         }
     } else {
         $this->currency_id = '-99';
     }
     if ($this->force_load_details == true) {
         $this->fill_in_additional_detail_fields();
     }
 }
 public static function getFromId($id)
 {
     $category = new oqc_Category();
     return $category->retrieve($id);
 }
 private function saveCategories($subCategoryArray, $prefix = '', $masterCategory = '')
 {
     if (empty($subCategoryArray)) {
         return array();
     } else {
         //decode tree array into actions that we need to do after tree save
         $subCategoryIds = array();
         $productIds = array();
         $optionSequenceIds = array();
         $updateDescriptionsIds = array();
         foreach ($subCategoryArray as $number => $subCategoryItem) {
             if ($subCategoryItem['isProduct'] && $masterCategory) {
                 $productIds[] = array($subCategoryItem['key'], $subCategoryItem['title'], isset($subCategotyItem['wasActive']) ? true : false);
                 if ($subCategoryItem['children'] != '') {
                     $optionSequenceIds = array_merge($this->saveOptions($subCategoryItem['children'], $subCategoryItem['key'], $updateDescriptionsIds), $optionSequenceIds);
                 }
                 continue;
             }
             // initialize the category correctly if it does not exist yet
             if (($subCategory = oqc_Category::getFromId($subCategoryItem['key'])) == null) {
                 $subCategory = new oqc_Category();
             }
             $subCategory->number = $prefix . ($number + 1);
             $subCategory->name = $subCategoryItem['title'];
             $subCategory->description = isset($_POST['categoryDescription_' . $subCategoryItem['key']]) ? $_POST['categoryDescription_' . $subCategoryItem['key']] : "";
             if ($subCategoryItem['children'] != '') {
                 $subCategory->subcategories = implode(" ", $this->saveCategories($subCategoryItem['children'], $prefix . ($number + 1) . '.', $subCategoryItem['key']));
             } else {
                 $subCategory->subcategories = "";
             }
             $subCategory->catalog_id = $this->bean->id;
             $subCategory->save();
             // put id of this subcategory into the array $subCategoryIds
             if (array_search($subCategory->id, $subCategoryIds) === FALSE) {
                 $subCategoryIds[] = $subCategory->id;
             }
         }
         //2.2RC1 now process Products and options that are in this particular tree level
         //1. update Product descriptions if was Active, update ordering of Products, update title of products is was Active, update Product category (it might be changed is was Active
         //2. For options update ordering string and description if wasActive
         //$GLOBALS['log']->error("product catalog subcategories: ". var_export($subCategoryIds,true));
         //$GLOBALS['log']->error("product catalog products: ". var_export($productIds,true));
         //$GLOBALS['log']->error("product catalog options sequences: ". var_export($optionSequenceIds,true));
         //$GLOBALS['log']->error("product catalog descriptions: ". var_export($updateDescriptionsIds,true));
         if (!empty($productIds)) {
             foreach ($productIds as $number => $productId) {
                 $product = new oqc_Product();
                 if ($product->retrieve($productId[0])) {
                     $product->name = $productId[1];
                     $product->description = isset($_POST['categoryDescription_' . $productId[0]]) ? $_POST['categoryDescription_' . $productId[0]] : "";
                     if (array_key_exists($productId[0], $optionSequenceIds)) {
                         $product->optionssequence = $optionSequenceIds[$productId[0]];
                         unset($optionSequenceIds[$productId[0]]);
                     }
                     $product->catalog_position = $number + 1;
                     $product->relatedcategory_id = $masterCategory;
                     $product->save();
                 }
             }
         }
         if (!empty($optionSequenceIds)) {
             foreach ($optionSequenceIds as $key => $optionSequenceId) {
                 $option = new oqc_Product();
                 if ($option->retrieve($key)) {
                     if (isset($_POST['categoryDescription_' . $key]) && array_search($key, $updateDescriptionsIds)) {
                         $option->description = $_POST['categoryDescription_' . $key];
                         unset($updateDescriptionsIds[array_search($key, $updateDescriptionsIds)]);
                     }
                     if (array_key_exists($key, $optionSequenceIds)) {
                         $option->optionssequence = $optionSequenceIds[$key];
                         unset($optionSequenceIds[$key]);
                     }
                     //	$option->catalog_position = $number+1;
                     //	$option->relatedcategory_id = $key;
                     $option->save();
                 }
             }
         }
         if (!empty($updateDescriptionsIds)) {
             foreach ($updateDescriptionsIds as $updateDescriptionsId) {
                 if (isset($_POST['categoryDescription_' . $updateDescriptionsId])) {
                     $option = new oqc_Product();
                     if ($option->retrieve($updateDescriptionsId)) {
                         $option->description = $_POST['categoryDescription_' . $updateDescriptionsId];
                         //			unset($updateDescriptionsIds[array_search($key ,$updateDescriptionsIds)];
                         $option->save();
                     }
                 }
             }
         }
         return $subCategoryIds;
     }
 }