Example #1
0
 /**
  *	Insert line into database
  *
  *	@param      int		$notrigger		1 no triggers
  *	@return		int						<0 if KO, >0 if OK
  */
 function insert($notrigger = 0)
 {
     global $langs, $user, $conf;
     $error = 0;
     $pa_ht_isemptystring = empty($this->pa_ht) && $this->pa_ht == '';
     // If true, we can use a default value. If this->pa_ht = '0', we must use '0'.
     dol_syslog(get_class($this) . "::insert rang=" . $this->rang, LOG_DEBUG);
     // Clean parameters
     $this->desc = trim($this->desc);
     if (empty($this->tva_tx)) {
         $this->tva_tx = 0;
     }
     if (empty($this->localtax1_tx)) {
         $this->localtax1_tx = 0;
     }
     if (empty($this->localtax2_tx)) {
         $this->localtax2_tx = 0;
     }
     if (empty($this->localtax1_type)) {
         $this->localtax1_type = 0;
     }
     if (empty($this->localtax2_type)) {
         $this->localtax2_type = 0;
     }
     if (empty($this->total_localtax1)) {
         $this->total_localtax1 = 0;
     }
     if (empty($this->total_localtax2)) {
         $this->total_localtax2 = 0;
     }
     if (empty($this->rang)) {
         $this->rang = 0;
     }
     if (empty($this->remise_percent)) {
         $this->remise_percent = 0;
     }
     if (empty($this->info_bits)) {
         $this->info_bits = 0;
     }
     if (empty($this->subprice)) {
         $this->subprice = 0;
     }
     if (empty($this->special_code)) {
         $this->special_code = 0;
     }
     if (empty($this->fk_parent_line)) {
         $this->fk_parent_line = 0;
     }
     if (empty($this->fk_prev_id)) {
         $this->fk_prev_id = 'null';
     }
     if (empty($this->situation_percent)) {
         $this->situation_percent = 0;
     }
     if (empty($this->pa_ht)) {
         $this->pa_ht = 0;
     }
     // if buy price not defined, define buyprice as configured in margin admin
     if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
         if (($result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
             return $result;
         } else {
             $this->pa_ht = $result;
         }
     }
     // Check parameters
     if ($this->product_type < 0) {
         $this->error = 'ErrorProductTypeMustBe0orMore';
         return -1;
     }
     if (!empty($this->fk_product)) {
         // Check product exists
         $result = Product::isExistingObject('product', $this->fk_product);
         if ($result <= 0) {
             $this->error = 'ErrorProductIdDoesNotExists';
             return -1;
         }
     }
     $this->db->begin();
     // Insertion dans base de la ligne
     $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'facturedet';
     $sql .= ' (fk_facture, fk_parent_line, label, description, qty,';
     $sql .= ' tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
     $sql .= ' fk_product, product_type, remise_percent, subprice, fk_remise_except,';
     $sql .= ' date_start, date_end, fk_code_ventilation, ';
     $sql .= ' rang, special_code, fk_product_fournisseur_price, buy_price_ht,';
     $sql .= ' info_bits, total_ht, total_tva, total_ttc, total_localtax1, total_localtax2,';
     $sql .= ' situation_percent, fk_prev_id,';
     $sql .= ' fk_unit)';
     $sql .= " VALUES (" . $this->fk_facture . ",";
     $sql .= " " . ($this->fk_parent_line > 0 ? "'" . $this->fk_parent_line . "'" : "null") . ",";
     $sql .= " " . (!empty($this->label) ? "'" . $this->db->escape($this->label) . "'" : "null") . ",";
     $sql .= " '" . $this->db->escape($this->desc) . "',";
     $sql .= " " . price2num($this->qty) . ",";
     $sql .= " " . price2num($this->tva_tx) . ",";
     $sql .= " " . price2num($this->localtax1_tx) . ",";
     $sql .= " " . price2num($this->localtax2_tx) . ",";
     $sql .= " '" . $this->localtax1_type . "',";
     $sql .= " '" . $this->localtax2_type . "',";
     $sql .= ' ' . (!empty($this->fk_product) ? $this->fk_product : "null") . ',';
     $sql .= " " . $this->product_type . ",";
     $sql .= " " . price2num($this->remise_percent) . ",";
     $sql .= " " . price2num($this->subprice) . ",";
     $sql .= ' ' . (!empty($this->fk_remise_except) ? $this->fk_remise_except : "null") . ',';
     $sql .= " " . (!empty($this->date_start) ? "'" . $this->db->idate($this->date_start) . "'" : "null") . ",";
     $sql .= " " . (!empty($this->date_end) ? "'" . $this->db->idate($this->date_end) . "'" : "null") . ",";
     $sql .= ' ' . $this->fk_code_ventilation . ',';
     $sql .= ' ' . $this->rang . ',';
     $sql .= ' ' . $this->special_code . ',';
     $sql .= ' ' . (!empty($this->fk_fournprice) ? $this->fk_fournprice : "null") . ',';
     $sql .= ' ' . price2num($this->pa_ht) . ',';
     $sql .= " '" . $this->info_bits . "',";
     $sql .= " " . price2num($this->total_ht) . ",";
     $sql .= " " . price2num($this->total_tva) . ",";
     $sql .= " " . price2num($this->total_ttc) . ",";
     $sql .= " " . price2num($this->total_localtax1) . ",";
     $sql .= " " . price2num($this->total_localtax2);
     $sql .= ", " . $this->situation_percent;
     $sql .= ", " . $this->fk_prev_id;
     $sql .= ", " . (!$this->fk_unit ? 'NULL' : $this->fk_unit);
     $sql .= ')';
     dol_syslog(get_class($this) . "::insert", LOG_DEBUG);
     $resql = $this->db->query($sql);
     if ($resql) {
         $this->rowid = $this->db->last_insert_id(MAIN_DB_PREFIX . 'facturedet');
         if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) {
             $this->id = $this->rowid;
             $result = $this->insertExtraFields();
             if ($result < 0) {
                 $error++;
             }
         }
         // Si fk_remise_except defini, on lie la remise a la facture
         // ce qui la flague comme "consommee".
         if ($this->fk_remise_except) {
             $discount = new DiscountAbsolute($this->db);
             $result = $discount->fetch($this->fk_remise_except);
             if ($result >= 0) {
                 // Check if discount was found
                 if ($result > 0) {
                     // Check if discount not already affected to another invoice
                     if ($discount->fk_facture) {
                         $this->error = $langs->trans("ErrorDiscountAlreadyUsed", $discount->id);
                         dol_syslog(get_class($this) . "::insert Error " . $this->error, LOG_ERR);
                         $this->db->rollback();
                         return -3;
                     } else {
                         $result = $discount->link_to_invoice($this->rowid, 0);
                         if ($result < 0) {
                             $this->error = $discount->error;
                             dol_syslog(get_class($this) . "::insert Error " . $this->error, LOG_ERR);
                             $this->db->rollback();
                             return -3;
                         }
                     }
                 } else {
                     $this->error = $langs->trans("ErrorADiscountThatHasBeenRemovedIsIncluded");
                     dol_syslog(get_class($this) . "::insert Error " . $this->error, LOG_ERR);
                     $this->db->rollback();
                     return -3;
                 }
             } else {
                 $this->error = $discount->error;
                 dol_syslog(get_class($this) . "::insert Error " . $this->error, LOG_ERR);
                 $this->db->rollback();
                 return -3;
             }
         }
         if (!$notrigger) {
             // Call trigger
             $result = $this->call_trigger('LINEBILL_INSERT', $user);
             if ($result < 0) {
                 $this->db->rollback();
                 return -2;
             }
             // End call triggers
         }
         $this->db->commit();
         return $this->rowid;
     } else {
         $this->error = $this->db->error();
         $this->db->rollback();
         return -2;
     }
 }
Example #2
0
 /**
  *	Insert line into database
  *
  *	@param      int		$notrigger		1 no triggers
  *	@return		int						<0 if KO, >0 if OK
  */
 function insert($notrigger = 0)
 {
     global $langs, $user, $conf;
     $error = 0;
     dol_syslog(get_class($this) . "::insert rang=" . $this->rang, LOG_DEBUG);
     // Clean parameters
     $this->desc = trim($this->desc);
     if (empty($this->tva_tx)) {
         $this->tva_tx = 0;
     }
     if (empty($this->localtax1_tx)) {
         $this->localtax1_tx = 0;
     }
     if (empty($this->localtax2_tx)) {
         $this->localtax2_tx = 0;
     }
     if (empty($this->localtax1_type)) {
         $this->localtax1_type = 0;
     }
     if (empty($this->localtax2_type)) {
         $this->localtax2_type = 0;
     }
     if (empty($this->total_localtax1)) {
         $this->total_localtax1 = 0;
     }
     if (empty($this->total_localtax2)) {
         $this->total_localtax2 = 0;
     }
     if (empty($this->rang)) {
         $this->rang = 0;
     }
     if (empty($this->remise_percent)) {
         $this->remise_percent = 0;
     }
     if (empty($this->info_bits)) {
         $this->info_bits = 0;
     }
     if (empty($this->subprice)) {
         $this->subprice = 0;
     }
     if (empty($this->special_code)) {
         $this->special_code = 0;
     }
     if (empty($this->fk_parent_line)) {
         $this->fk_parent_line = 0;
     }
     if (empty($this->pa_ht)) {
         $this->pa_ht = 0;
     }
     // si prix d'achat non renseigne et utilise pour calcul des marges alors prix achat = prix vente
     if ($this->pa_ht == 0) {
         if ($this->subprice > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)) {
             $this->pa_ht = $this->subprice * (1 - $this->remise_percent / 100);
         }
     }
     // Check parameters
     if ($this->product_type < 0) {
         $this->error = 'ErrorProductTypeMustBe0orMore';
         return -1;
     }
     if (!empty($this->fk_product)) {
         // Check product exists
         $result = Product::isExistingObject('product', $this->fk_product);
         if ($result <= 0) {
             $this->error = 'ErrorProductIdDoesNotExists';
             return -1;
         }
     }
     // POS or by external module, take lowest buying price
     if (!empty($this->fk_product) && empty($this->fk_fournprice) && empty($this->pa_ht)) {
         include_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.product.class.php';
         $productFournisseur = new ProductFournisseur($this->db);
         $productFournisseur->find_min_price_product_fournisseur($this->fk_product);
         $this->fk_fournprice = $productFournisseur->product_fourn_price_id;
     }
     $this->db->begin();
     // Insertion dans base de la ligne
     $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'facturedet';
     $sql .= ' (fk_facture, fk_parent_line, label, description, qty,';
     $sql .= ' tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
     $sql .= ' fk_product, product_type, remise_percent, subprice, fk_remise_except,';
     $sql .= ' date_start, date_end, fk_code_ventilation, ';
     $sql .= ' rang, special_code, fk_product_fournisseur_price, buy_price_ht,';
     $sql .= ' info_bits, total_ht, total_tva, total_ttc, total_localtax1, total_localtax2)';
     $sql .= " VALUES (" . $this->fk_facture . ",";
     $sql .= " " . ($this->fk_parent_line > 0 ? "'" . $this->fk_parent_line . "'" : "null") . ",";
     $sql .= " " . (!empty($this->label) ? "'" . $this->db->escape($this->label) . "'" : "null") . ",";
     $sql .= " '" . $this->db->escape($this->desc) . "',";
     $sql .= " " . price2num($this->qty) . ",";
     $sql .= " " . price2num($this->tva_tx) . ",";
     $sql .= " " . price2num($this->localtax1_tx) . ",";
     $sql .= " " . price2num($this->localtax2_tx) . ",";
     $sql .= " '" . $this->localtax1_type . "',";
     $sql .= " '" . $this->localtax2_type . "',";
     $sql .= ' ' . (!empty($this->fk_product) ? $this->fk_product : "null") . ',';
     $sql .= " " . $this->product_type . ",";
     $sql .= " " . price2num($this->remise_percent) . ",";
     $sql .= " " . price2num($this->subprice) . ",";
     $sql .= ' ' . (!empty($this->fk_remise_except) ? $this->fk_remise_except : "null") . ',';
     $sql .= " " . (!empty($this->date_start) ? "'" . $this->db->idate($this->date_start) . "'" : "null") . ",";
     $sql .= " " . (!empty($this->date_end) ? "'" . $this->db->idate($this->date_end) . "'" : "null") . ",";
     $sql .= ' ' . $this->fk_code_ventilation . ',';
     $sql .= ' ' . $this->rang . ',';
     $sql .= ' ' . $this->special_code . ',';
     $sql .= ' ' . (!empty($this->fk_fournprice) ? $this->fk_fournprice : "null") . ',';
     $sql .= ' ' . price2num($this->pa_ht) . ',';
     $sql .= " '" . $this->info_bits . "',";
     $sql .= " " . price2num($this->total_ht) . ",";
     $sql .= " " . price2num($this->total_tva) . ",";
     $sql .= " " . price2num($this->total_ttc) . ",";
     $sql .= " " . price2num($this->total_localtax1) . ",";
     $sql .= " " . price2num($this->total_localtax2);
     $sql .= ')';
     dol_syslog(get_class($this) . "::insert", LOG_DEBUG);
     $resql = $this->db->query($sql);
     if ($resql) {
         $this->rowid = $this->db->last_insert_id(MAIN_DB_PREFIX . 'facturedet');
         if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) {
             $this->id = $this->rowid;
             $result = $this->insertExtraFields();
             if ($result < 0) {
                 $error++;
             }
         }
         // Si fk_remise_except defini, on lie la remise a la facture
         // ce qui la flague comme "consommee".
         if ($this->fk_remise_except) {
             $discount = new DiscountAbsolute($this->db);
             $result = $discount->fetch($this->fk_remise_except);
             if ($result >= 0) {
                 // Check if discount was found
                 if ($result > 0) {
                     // Check if discount not already affected to another invoice
                     if ($discount->fk_facture) {
                         $this->error = $langs->trans("ErrorDiscountAlreadyUsed", $discount->id);
                         dol_syslog(get_class($this) . "::insert Error " . $this->error, LOG_ERR);
                         $this->db->rollback();
                         return -3;
                     } else {
                         $result = $discount->link_to_invoice($this->rowid, 0);
                         if ($result < 0) {
                             $this->error = $discount->error;
                             dol_syslog(get_class($this) . "::insert Error " . $this->error, LOG_ERR);
                             $this->db->rollback();
                             return -3;
                         }
                     }
                 } else {
                     $this->error = $langs->trans("ErrorADiscountThatHasBeenRemovedIsIncluded");
                     dol_syslog(get_class($this) . "::insert Error " . $this->error, LOG_ERR);
                     $this->db->rollback();
                     return -3;
                 }
             } else {
                 $this->error = $discount->error;
                 dol_syslog(get_class($this) . "::insert Error " . $this->error, LOG_ERR);
                 $this->db->rollback();
                 return -3;
             }
         }
         if (!$notrigger) {
             // Call trigger
             $result = $this->call_trigger('LINEBILL_INSERT', $user);
             if ($result < 0) {
                 $this->db->rollback();
                 return -2;
             }
             // End call triggers
         }
         $this->db->commit();
         return $this->rowid;
     } else {
         $this->error = $this->db->error();
         $this->db->rollback();
         return -2;
     }
 }