/** * Sets object to supplied categories. * * Deletes object from existing categories not supplied. * Adds it to non existing supplied categories. * Existing categories are left untouch. * * @param int[]|int $categories Category or categories IDs */ public function setCategories($categories) { // Handle single category if (!is_array($categories)) { $categories = array($categories); } // Get current categories require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php'; $c = new Categorie($this->db); $existing = $c->containing($this->id, Categorie::TYPE_PRODUCT, 'id'); // Diff if (is_array($existing)) { $to_del = array_diff($existing, $categories); $to_add = array_diff($categories, $existing); } else { $to_del = array(); // Nothing to delete $to_add = $categories; } // Process foreach ($to_del as $del) { $c->fetch($del); $c->del_type($this, 'product'); } foreach ($to_add as $add) { $c->fetch($add); $c->add_type($this, 'product'); } return; }
} if ($user->rights->categorie->supprimer && $action == 'confirm_delete' && $confirm == 'yes') { if ($object->delete($user) >= 0) { header("Location: " . DOL_URL_ROOT . '/categories/index.php?type=' . $type); exit; } else { setEventMessages($object->error, $object->errors, 'errors'); } } if ($type == Categorie::TYPE_PRODUCT && $elemid && $action == 'addintocategory' && ($user->rights->produit->creer || $user->rights->service->creer)) { require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php'; $newobject = new Product($db); $result = $newobject->fetch($elemid); $elementtype = 'product'; // TODO Add into categ $result = $object->add_type($newobject, $elementtype); if ($result >= 0) { setEventMessages($langs->trans("WasAddedSuccessfully", $newobject->ref), null, 'mesgs'); } else { if ($cat->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') { setEventMessages($langs->trans("ObjectAlreadyLinkedToCategory"), null, 'warnings'); } else { setEventMessages($object->error, $object->errors, 'errors'); } } } /* * View */ $form = new Form($db); $formother = new FormOther($db);
/** * Link an object to the category * * @param CommonObject $obj Object to link to category * @param string $type Type of category ('customer', 'supplier', 'contact', 'product', 'member') * * @return int 1 : OK, -1 : erreur SQL, -2 : id not defined, -3 : Already linked */ function add_type($obj, $type) { global $user, $langs, $conf; $error = 0; if ($this->id == -1) { return -2; } // For backward compatibility if ($type == 'societe') { $type = 'customer'; dol_syslog(get_class($this) . "::add_type(): type 'societe' is deprecated, please use 'customer' instead", LOG_WARNING); } elseif ($type == 'fournisseur') { $type = 'supplier'; dol_syslog(get_class($this) . "::add_type(): type 'fournisseur' is deprecated, please use 'supplier' instead", LOG_WARNING); } $this->db->begin(); $sql = "INSERT INTO " . MAIN_DB_PREFIX . "categorie_" . $this->MAP_CAT_TABLE[$type]; $sql .= " (fk_categorie, fk_" . $this->MAP_CAT_FK[$type] . ")"; $sql .= " VALUES (" . $this->id . ", " . $obj->id . ")"; dol_syslog(get_class($this) . '::add_type', LOG_DEBUG); if ($this->db->query($sql)) { if (!empty($conf->global->CATEGORIE_RECURSIV_ADD)) { $sql = 'SELECT fk_parent FROM ' . MAIN_DB_PREFIX . 'categorie'; $sql .= " WHERE rowid = " . $this->id; dol_syslog(get_class($this) . "::add_type", LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { if ($this->db->num_rows($resql) > 0) { $objparent = $this->db->fetch_object($resql); if (!empty($objparent->fk_parent)) { $cat = new Categorie($this->db); $cat->id = $objparent->fk_parent; $result = $cat->add_type($obj, $type); if ($result < 0) { $this->error = $cat->error; $error++; } } } } else { $error++; $this->error = $this->db->lasterror(); } if ($error) { $this->db->rollback(); return -1; } } // Save object we want to link category to into category instance to provide information to trigger $this->linkto = $obj; // Call trigger $result = $this->call_trigger('CATEGORY_LINK', $user); if ($result < 0) { $error++; } // End call triggers if (!$error) { $this->db->commit(); return 1; } else { $this->db->rollback(); return -2; } } else { $this->db->rollback(); if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') { $this->error = $this->db->lasterrno(); return -3; } else { $this->error = $this->db->lasterror(); } return -1; } }
/** * Insert product into database * * @param User $user User making insert * @param int $notrigger Disable triggers * @return int Id of product/service if OK or number of error < 0 */ function create($user,$notrigger=0) { global $conf, $langs; $error=0; // Clean parameters $this->ref = dol_string_nospecial(trim($this->ref)); $this->libelle = trim($this->libelle); $this->price_ttc=price2num($this->price_ttc); $this->price=price2num($this->price); $this->price_min_ttc=price2num($this->price_min_ttc); $this->price_min=price2num($this->price_min); $this->ecotax=price2num($this->ecotax); $this->ecotax_ttc=price2num($this->ecotax_ttc); if (empty($this->tva_tx)) $this->tva_tx = 0; if (empty($this->tva_npr)) $this->tva_npr = 0; //Local taxes if (empty($this->localtax1_tx)) $this->localtax1_tx = 0; if (empty($this->localtax2_tx)) $this->localtax2_tx = 0; if (empty($this->price)) $this->price = 0; if (empty($this->price_min)) $this->price_min = 0; if (empty($this->status)) $this->status = 0; if (empty($this->status_buy)) $this->status_buy = 0; if (empty($this->finished)) $this->finished = 0; if (empty($this->ecotax)) $this->ecotax=0; if (empty($this->ecotax_ttc)) $this->ecotax_ttc=0; $price_ht=0; $price_ttc=0; $price_min_ht=0; $price_min_ttc=0; $ecotax_ht=0; $ecotax_ttc=0; if ($this->price_base_type == 'TTC' && $this->price_ttc > 0) { $price_ttc = price2num($this->price_ttc,'MU'); $price_ht = price2num($this->price_ttc / (1 + ($this->tva_tx / 100)),'MU'); } if ($this->price_base_type != 'TTC' && $this->price > 0) { $price_ht = price2num($this->price,'MU'); $price_ttc = price2num($this->price * (1 + ($this->tva_tx / 100)),'MU'); } if ($this->price_base_type == 'TTC' && $this->ecotax_ttc > 0) { $ecotax_ttc = price2num($this->ecotax_ttc,'MU'); $ecotax_ht = price2num($this->ecotax_ttc / (1 + ($this->tva_tx / 100)),'MU'); } if ($this->price_base_type != 'TTC' && $this->ecotax > 0) { $ecotax_ht = price2num($this->ecotax,'MU'); $ecotax_ttc = price2num($this->ecotax * (1 + ($this->tva_tx / 100)),'MU'); } if (($this->price_min_ttc > 0) && ($this->price_base_type == 'TTC')) { $price_min_ttc = price2num($this->price_min_ttc,'MU'); $price_min_ht = price2num($this->price_min_ttc / (1 + ($this->tva_tx / 100)),'MU'); } if (($this->price_min > 0) && ($this->price_base_type != 'TTC')) { $price_min_ht = price2num($this->price_min,'MU'); $price_min_ttc = price2num($this->price_min * (1 + ($this->tva_tx / 100)),'MU'); } // Check parameters if (empty($this->libelle)) { $this->error='ErrorWrongParameters'; return -1; } if (empty($this->ref)) { $this->error='ErrorWrongParameters'; return -2; } dol_syslog(get_class($this)."::create ref=".$this->ref." price=".$this->price." price_ttc=".$this->price_ttc." tva_tx=".$this->tva_tx." price_base_type=".$this->price_base_type." Category : ".$this->catid, LOG_DEBUG); $now=dol_now(); $this->db->begin(); $sql = "SELECT count(*) as nb"; $sql.= " FROM ".MAIN_DB_PREFIX."product"; $sql.= " WHERE entity IN (".getEntity('product', 1).")"; $sql.= " AND ref = '" .$this->ref."'"; $result = $this->db->query($sql); if ($result) { $obj = $this->db->fetch_object($result); if ($obj->nb == 0) { // Produit non deja existant $sql = "INSERT INTO ".MAIN_DB_PREFIX."product ("; $sql.= "datec"; $sql.= ", entity"; $sql.= ", ref"; $sql.= ", ref_ext"; $sql.= ", price_min"; $sql.= ", price_min_ttc"; $sql.= ", label"; $sql.= ", fk_user_author"; $sql.= ", fk_product_type"; $sql.= ", price"; $sql.= ", price_ttc"; $sql.= ", price_base_type"; $sql.= ", tobuy"; $sql.= ", tosell"; $sql.= ", canvas"; $sql.= ", finished"; $sql.= ", ecotax"; $sql.= ", ecotax_ttc"; $sql.= ") VALUES ("; $sql.= $this->db->idate($now); $sql.= ", ".$conf->entity; $sql.= ", '".$this->db->escape($this->ref)."'"; $sql.= ", ".($this->ref_ext?"'".$this->db->escape($this->ref_ext)."'":"null"); $sql.= ", ".price2num($price_min_ht); $sql.= ", ".price2num($price_min_ttc); $sql.= ", ".($this->libelle?"'".$this->db->escape($this->libelle)."'":"null"); $sql.= ", ".$user->id; $sql.= ", ".$this->type; $sql.= ", ".price2num($price_ht); $sql.= ", ".price2num($price_ttc); $sql.= ", '".$this->price_base_type."'"; $sql.= ", ".$this->status; $sql.= ", ".$this->status_buy; $sql.= ", '".$this->canvas."'"; $sql.= ", ".$this->finished; $sql.= ", ".price2num($ecotax_ht); $sql.= ", ".price2num($ecotax_ttc); $sql.= ")"; dol_syslog(get_class($this)."::Create sql=".$sql); $result = $this->db->query($sql); if ( $result ) { $id = $this->db->last_insert_id(MAIN_DB_PREFIX."product"); if ($id > 0) { $this->id = $id; $this->price = $price_ht; $this->price_ttc = $price_ttc; $this->price_min = $price_min_ht; $this->price_min_ttc = $price_min_ttc; $result = $this->_log_price($user); if ($result > 0) { if ($this->update($id, $user, true) > 0) { if ($this->catid > 0) { require_once(DOL_DOCUMENT_ROOT ."/categories/class/categorie.class.php"); $cat = new Categorie($this->db, $this->catid); $cat->add_type($this,"product"); } } else { $error++; $this->error='ErrorFailedToUpdateRecord'; } } else { $error++; $this->error=$this->db->lasterror(); } } else { $error++; $this->error='ErrorFailedToGetInsertedId'; } } else { $error++; $this->error=$this->db->lasterror(); } } else { // Product already exists with this ref $langs->load("products"); $this->error = "ErrorProductAlreadyExists"; } } else { $error++; $this->error=$this->db->lasterror(); } if (! $error && ! $notrigger) { // Appel des triggers include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php"); $interface=new Interfaces($this->db); $result=$interface->run_triggers('PRODUCT_CREATE',$this,$user,$langs,$conf); if ($result < 0) { $error++; $this->errors=$interface->errors; } // Fin appel triggers } if (! $error) { $this->db->commit(); return $this->id; } else { $this->db->rollback(); return -$error; } }
/** * Sets object to supplied categories. * * Deletes object from existing categories not supplied. * Adds it to non existing supplied categories. * Existing categories are left untouch. * * @param int[]|int $categories Category or categories IDs * @param string $type Category type (customer or supplier) */ public function setCategories($categories, $type) { // Decode type if ($type == 'customer') { $type_id = Categorie::TYPE_CUSTOMER; $type_text = 'customer'; } elseif ($type == 'supplier') { $type_id = Categorie::TYPE_SUPPLIER; $type_text = 'supplier'; } else { dol_syslog(__METHOD__ . ': Type ' . $type . 'is an unknown company category type. Done nothing.', LOG_ERR); return; } // Handle single category if (!is_array($categories)) { $categories = array($categories); } // Get current categories require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php'; $c = new Categorie($this->db); $existing = $c->containing($this->id, $type_id, 'id'); // Diff if (is_array($existing)) { $to_del = array_diff($existing, $categories); $to_add = array_diff($categories, $existing); } else { $to_del = array(); // Nothing to delete $to_add = $categories; } // Process foreach ($to_del as $del) { if ($c->fetch($del) > 0) { $c->del_type($this, $type_text); } } foreach ($to_add as $add) { if ($c->fetch($add) > 0) { $c->add_type($this, $type_text); } } return; }
} if ($type == Categorie::TYPE_MEMBER && $user->rights->adherent->creer) { require_once DOL_DOCUMENT_ROOT . '/adherents/class/adherent.class.php'; $object = new Adherent($db); $result = $object->fetch($objectid); $elementtype = 'member'; } if ($type == Categorie::TYPE_CONTACT && $user->rights->societe->creer) { require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php'; $object = new Contact($db); $result = $object->fetch($objectid); $elementtype = 'contact'; } $cat = new Categorie($db); $result = $cat->fetch($parent); $result = $cat->add_type($object, $elementtype); if ($result >= 0) { setEventMessages($langs->trans("WasAddedSuccessfully", $cat->label), null, 'mesgs'); } else { if ($cat->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') { setEventMessages($langs->trans("ObjectAlreadyLinkedToCategory"), null, 'warnings'); } else { setEventMessages($cat->error, $cat->errors, 'errors'); } } } } /* * View */ $form = new Form($db);
/** * Link an object to the category * * @param Object $obj Object to link to category * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact') * @return int 1 : OK, -1 : erreur SQL, -2 : id not defined, -3 : Already linked */ function add_type($obj, $type) { global $user, $langs, $conf; $error = 0; if ($this->id == -1) { return -2; } // For backward compatibility if ($type == 'company') { $type = 'societe'; } if ($type == 'customer') { $type = 'societe'; } if ($type == 'supplier') { $type = 'fournisseur'; } $column_name = $type; if ($type == 'contact') { $column_name = 'socpeople'; } if ($type == 'fournisseur') { $column_name = 'societe'; } $sql = "INSERT INTO " . MAIN_DB_PREFIX . "categorie_" . $type . " (fk_categorie, fk_" . $column_name . ")"; $sql .= " VALUES (" . $this->id . ", " . $obj->id . ")"; dol_syslog(get_class($this) . '::add_type sql=' . $sql); if ($this->db->query($sql)) { if (!empty($conf->global->CATEGORIE_RECURSIV_ADD)) { $sql = 'SELECT fk_parent FROM ' . MAIN_DB_PREFIX . 'categorie'; $sql .= " WHERE rowid = " . $this->id; dol_syslog(get_class($this) . "::add_type sql=" . $sql); $resql = $this->db->query($sql); if ($resql) { if ($this->db->num_rows($resql) > 0) { $objparent = $this->db->fetch_object($resql); if (!empty($objparent->fk_parent)) { $cat = new Categorie($this->db); $cat->id = $objparent->fk_parent; $result = $cat->add_type($obj, $type); if ($result < 0) { $this->error = $cat->error; $error++; } } } } else { $error++; $this->error = $this->db->lasterror(); } if ($error) { return -1; } } // Save object we want to link category to into category instance to provide information to trigger $this->linkto = $obj; // Appel des triggers include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; $interface = new Interfaces($this->db); $result = $interface->run_triggers('CATEGORY_LINK', $this, $user, $langs, $conf); if ($result < 0) { $error++; $this->errors = $interface->errors; $this->error = $interface->error; } // Fin appel triggers if (!$error) { return 1; } else { return -2; } } else { if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') { $this->error = $this->db->lasterrno(); return -3; } else { $this->error = $this->db->lasterror(); } return -1; } }
/** * testCategorieProduct * * @param int $id Id of category * @return int * * @depends testCategorieCreate * The depends says test is run only if previous is ok */ public function testCategorieProduct($id) { global $conf, $user, $langs, $db; $conf = $this->savconf; $user = $this->savuser; $langs = $this->savlangs; $db = $this->savdb; $localobjecttmp = new Categorie($this->savdb); $localobjecttmp->initAsSpecimen(); $localobjecttmp->label = 'Specimen Category for product'; $localobjecttmp->type = 0; // product category $catid = $localobjecttmp->create($user); print __METHOD__ . " catid=" . $catid . "\n"; $this->assertGreaterThan(0, $catid); // Try to create product linked to category $localobject2 = new Product($this->savdb); $localobject2->initAsSpecimen(); $localobject2->ref .= '-CATEG'; $localobject2->tva_npr = 1; $result = $localobject2->create($user); $cat = new Categorie($this->savdb); $cat->id = $catid; $result = $cat->add_type($localobject2, "product"); print __METHOD__ . " result=" . $result . "\n"; $this->assertGreaterThan(0, $result); // Get list of categories for product $localcateg = new Categorie($this->savdb); $listofcateg = $localcateg->containing($localobject2->id, 'product', 'label'); $this->assertTrue(in_array('Specimen Category for product', $listofcateg), 'Categ not found linked to product when it should'); return $id; }
{ $object = new Societe($db); $result = $object->fetch($objectid); $type = 'societe'; } if ($_REQUEST["type"]==3 && $user->rights->adherent->creer) { require_once(DOL_DOCUMENT_ROOT."/adherents/class/adherent.class.php"); $object = new Adherent($db); $result = $object->fetch($objectid); $type = 'member'; } $cat = new Categorie($db); $result=$cat->fetch($_REQUEST["catMere"]); $result=$cat->add_type($object,$type); if ($result >= 0) { $mesg='<div class="ok">'.$langs->trans("WasAddedSuccessfully",$cat->label).'</div>'; } else { if ($cat->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') $mesg='<div class="warning">'.$langs->trans("ObjectAlreadyLinkedToCategory").'</div>'; else $mesg='<div class="error">'.$langs->trans("Error").' '.$cat->error.'</div>'; } } /* * View