/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Quote();
     $users = User::getNames();
     $currency = Yii::app()->params->currency;
     $productNames = Product::productNames();
     $productCurrency = Product::productCurrency();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Quote'])) {
         /*
                             foreach($_POST as $key=>$arr){
                                     $pieces=explode("_",$key);
                                     if(isset($pieces[0]) && $pieces[0]=='autoselect'){
                                         $newKey=$pieces[1];
                                         if(isset($_POST[$newKey."_id"]) && $_POST[$newKey."_id"]!=""){
                                             $val=$_POST[$newKey."_id"];
                                         }else{
                                             $field=Fields::model()->findByAttributes(array('fieldName'=>$newKey));
                                             if(isset($field)){
                                                 $type=ucfirst($field->linkType);
                                                 if($type!="Contacts"){
                                                     eval("\$lookupModel=$type::model()->findByAttributes(array('name'=>'$arr'));");
                                                 }else{
                                                     $names=explode(" ",$arr);
                                                     $lookupModel=Contacts::model()->findByAttributes(array('firstName'=>$names[0],'lastName'=>$names[1]));
                                                 }
                                                 if(isset($lookupModel))
                                                     $val=$lookupModel->id;
                                                 else
                                                     $val=$arr;
                                             }
                                         }
                                         $model->$newKey=$val;
                                     }
                                 }
         //			$this->render('test', array('model'=>$_POST));
                     $temp=$model->attributes;
         foreach(array_keys($model->attributes) as $field){
                                     if(isset($_POST['Quote'][$field])){
                                         $model->$field=$_POST['Quote'][$field];
                                         $fieldData=Fields::model()->findByAttributes(array('modelName'=>'Quotes','fieldName'=>$field));
                                         if($fieldData->type=='assignment' && $fieldData->linkType=='multiple'){
                                             $model->$field=Accounts::parseUsers($model->$field);
                                         }elseif($fieldData->type=='date'){
                                             $model->$field=strtotime($model->$field);
                                         }
                                         
                                     }
                                 }
                 	
         			$model->expirationDate = $this->parseDate($model->expirationDate);
         */
         /*
         if(isset($model->associatedContacts)) {
         	$contacts = $model->associatedContacts;
         	$model->associatedContacts = Quote::parseContacts($model->associatedContacts);
         } else {
         	$contacts = array();
         }
         */
         $temp = $model->attributes;
         $model->setX2Fields($_POST['Quote']);
         // get products
         $products = array();
         if (isset($_POST['ExistingProducts'])) {
             $ids = $_POST['ExistingProducts']['id'];
             $prices = $_POST['ExistingProducts']['price'];
             $quantities = $_POST['ExistingProducts']['quantity'];
             $adjustments = $_POST['ExistingProducts']['adjustment'];
             foreach ($ids as $key => $id) {
                 if ($id != 0) {
                     // remove blanks
                     $products[$key]['id'] = $id;
                     $products[$key]['name'] = $productNames[$id];
                     $products[$key]['price'] = $prices[$key];
                     $products[$key]['quantity'] = $quantities[$key];
                     if (strchr($adjustments[$key], '%')) {
                         // percent adjustment
                         $products[$key]['adjustment'] = intval(str_replace("%", "", $adjustments[$key]));
                         $products[$key]['adjustmentType'] = 'percent';
                     } else {
                         $products[$key]['adjustment'] = $adjustments[$key];
                         $products[$key]['adjustmentType'] = 'linear';
                     }
                 }
             }
             if (!empty($products)) {
                 $currency = $productCurrency[$products[0]['id']];
             }
         }
         $model->currency = $currency;
         $this->createQuote($model, $temp, $products);
     }
     $products = Product::activeProducts();
     $this->render('create', array('model' => $model, 'users' => $users, 'products' => $products, 'productNames' => $productNames));
 }