/**
  * Template for managing override elements used
  */
 function templateOverrideElementsUsed()
 {
     /*===========================================================
       * ADD.INFO  :   o   >> override
       *               or  >> override reset
       *               rd  >> replace default
       =============================================================*/
     use_class("element");
     use_class("products_articles");
     $class_pa = new products_articles();
     $ovr_data = array();
     $ovr_def_data = array();
     $ovr_type = isset($_POST['ovr_type']) && $_POST['ovr_type'] != '' ? $_POST['ovr_type'] : 'o';
     $pid = $_POST['products_id'];
     $pean = $_POST['ovr_pean'];
     $paid = $_POST['ovr_paid'];
     $pueid = $_POST['ovr_pueid'];
     $elid_def = $_POST['ovr_elid_def'];
     $qty_def = $_POST['ovr_qty_def'];
     $elid_old = $_POST['ovr_elid_old'];
     $qty_old = $_POST['ovr_qty_old'];
     $elid_ovr = $_POST['ovr_id'];
     $qty_ovr = $_POST['ovr_qty'];
     $ovr_el_article = $_POST['ovr_el_article'];
     //PREPARE OVERRIDE DATA
     $ovr_data['products_articles_id'] = $paid;
     $ovr_data['products_use_elements_id'] = $pueid;
     $ovr_data['elements_id'] = '0';
     $ovr_data['quantity'] = '';
     $changes = 'elid';
     $ovr_def_data['elements_id'] = '0';
     $ovr_def_data['quantity'] = '';
     $is_override = true;
     $el_ovr_found = false;
     if ($ovr_type == 'o') {
         if ($elid_ovr == $elid_def) {
             //NO ELEMENT CHANGES
             $el_ovr_found = true;
             if ($qty_ovr == $qty_def) {
                 //NO QTY CHANGES
                 $is_override = false;
             } else {
                 $changes = 'qty';
                 $ovr_data['quantity'] = $qty_ovr;
                 $ovr_msg = "Quantity of Article {$pean} Element ID <strong>{$elid_def}</strong> is overrided become {$qty_ovr}.";
             }
         } else {
             //ELEMENT CHANGES
             $e = new element($elid_ovr);
             if (!is_null($e->id)) {
                 $el_ovr_found = true;
                 $ovr_data['elements_id'] = $elid_ovr;
                 $ovr_data['quantity'] = $qty_ovr;
                 $ovr_msg = "Article {$pean} Element ID Default <strong>{$elid_def}</strong> is overrided become {$elid_ovr} with quantity {$qty_ovr}.";
             } else {
                 $is_override = false;
             }
         }
     }
     if ($ovr_type == 'rd') {
         if ($elid_ovr == $elid_def) {
             //NO ELEMENT CHANGES
             $el_ovr_found = true;
             if ($qty_ovr == $qty_def) {
                 //NO QTY CHANGES
                 $is_override = false;
             } else {
                 $changes = 'qty';
                 $ovr_def_data['quantity'] = $qty_ovr;
                 $ovr_msg = "Quantity Element ID <strong>{$elid_def}</strong> is overrided become {$qty_ovr}.";
             }
         } else {
             //ELEMENT CHANGES
             $e = new element($elid_ovr);
             if (!is_null($e->id)) {
                 $el_ovr_found = true;
                 $ovr_def_data['elements_id'] = $elid_ovr;
                 $ovr_def_data['quantity'] = $qty_ovr;
                 $ovr_msg = "Element ID Default <strong>{$elid_def}</strong> is overrided become {$elid_ovr} with quantity {$qty_ovr}.";
             } else {
                 $is_override = false;
             }
         }
     }
     if ($is_override) {
         if ($ovr_type == 'or') {
             $class_pa->overrideReset($paid, $pueid);
             $changes = 'reset';
             $elid = $elid_def;
             $qty = $qty_def;
         } else {
             if ($ovr_type == 'rd') {
                 $ovr_def_data['elements_id'] = $ovr_def_data['elements_id'] == '0' ? $elid_def : $ovr_def_data['elements_id'];
                 if (!is_null($ovr_def_data)) {
                     tep_db_perform('products_use_elements', $ovr_def_data, 'update', "products_use_elements_id={$pueid}");
                     $dbc = tep_db_query("SELECT * FROM products_articles_use_elements WHERE products_use_elements_id = {$pueid}");
                     if (tep_db_num_rows($dbc) > 0 && $ovr_el_article == "true") {
                         while ($row = tep_db_fetch_array($dbc)) {
                             if ($row['quantity'] != $qty_def) {
                                 $ovr_def_data['quantity'] = $row['quantity'];
                             } else {
                                 $ovr_def_data['quantity'] = $ovr_def_data['quantity'];
                             }
                             $id = $row['id'];
                             tep_db_perform('products_articles_use_elements', $ovr_def_data, 'update', "id={$id}");
                         }
                     }
                     $this->setProductDescription2($pid, 'c,s,ch');
                     $e_def = new element($elid_def);
                     //removed replaced element default from product materials
                     $this->unsetProductMaterials($pid, $e_def->attributes['material']['id']);
                     $e_rd = new element($ovr_def_data['elements_id']);
                     //add to product materials for new element replacing the default ones
                     $this->setProductMaterials($pid, $e_rd->attributes['material']['id']);
                     $contain_stones = $e_def->attributes['category']['id'] == 5 || $e_rd->attributes['category']['id'] == 5 ? true : false;
                     if ($contain_stones) {
                         $this->setProductDescription2($pid, 'st');
                     }
                     //Manage this as a function, there some functions need to called this
                     $this->refreshProductDataSupport(array('pid' => $pid, 'contain_stones' => $contain_stones));
                 }
             } else {
                 $elid = $ovr_data['elements_id'] == '0' ? $elid_def : $ovr_data['elements_id'];
                 $qty = $ovr_data['quantity'];
                 $id = $class_pa->overrideElement($paid, $pueid, $ovr_data);
                 //                if(!is_null($id)&&$id>0) $messagebox->add($ovr_msg, 'green');
                 //                else $messagebox->add("Failed overriding Article $pean !", 'red');
             }
         }
         $e = new element($elid);
         $result = array();
         $result['rowid'] = $paid . $elid_def;
         $result['paid'] = $paid;
         $result['pueid'] = $pueid;
         $result['imgdef'] = webImage($e->image, '80', '80');
         $result['elid'] = $changes == 'elid' ? '<a href="?open=element&amp;id=' . $elid . '&amp;hidemenu=true" class="view_webpage">' . $elid . '</a>' : $elid;
         $result['qty'] = $qty;
         $result['changes'] = $changes;
         $result['type'] = $ovr_type;
         $result['ovr_el_article'] = $ovr_el_article;
         ajaxReturn($result);
     } else {
         $result = array();
         $e = new element($elid_old);
         $result['imgdef'] = webImage($e->image, '80', '80');
         $result['rowid'] = $paid . $elid_def;
         $result['paid'] = $paid;
         $result['pueid'] = $pueid;
         $result['elid'] = $elid_old;
         $result['qty'] = $qty_old;
         $result['changes'] = '';
         $result['type'] = $ovr_type;
         if (!$el_ovr_found) {
             $result['elidnotfound'] = $elid_ovr;
             $result['msg'] = 'elementnotfound';
             $result['changes'] = 'nochanges';
         }
         ajaxReturn($result);
     }
 }