/**
  *	Update line
  *
  *	@param     	int			$rowid           	Id de la ligne de facture
  *	@param     	string		$desc            	Description de la ligne
  *	@param     	double		$pu              	Prix unitaire
  *	@param     	double		$qty             	Quantity
  *	@param     	double		$remise_percent  	Pourcentage de remise de la ligne
  *	@param     	double		$txtva          	Taux TVA
  *  @param     	double		$txlocaltax1	    Localtax1 tax
  *  @param     	double		$txlocaltax2   		Localtax2 tax
  *  @param     	double		$price_base_type 	Type of price base
  *	@param		int			$info_bits			Miscellaneous informations
  *	@param		int			$type				Type of line (0=product, 1=service)
  *  @param		int			$notrigger			Disable triggers
  *  @param      timestamp   $date_start     	Date start of service
  *  @param      timestamp   $date_end       	Date end of service
  *  @param		array		$array_options		Extrafields array
  * 	@param 		string		$fk_unit 			Code of the unit to use. Null to use the default one
  *	@return    	int         	    			< 0 if error, > 0 if ok
  */
 function updateline($rowid, $desc, $pu, $qty, $remise_percent, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $price_base_type = 'HT', $info_bits = 0, $type = 0, $notrigger = false, $date_start = '', $date_end = '', $array_options = 0, $fk_unit = null)
 {
     global $mysoc;
     dol_syslog(get_class($this) . "::updateline {$rowid}, {$desc}, {$pu}, {$qty}, {$remise_percent}, {$txtva}, {$price_base_type}, {$info_bits}, {$type}, {$fk_unit}");
     include_once DOL_DOCUMENT_ROOT . '/core/lib/price.lib.php';
     if ($this->brouillon) {
         $this->db->begin();
         // Clean parameters
         if (empty($qty)) {
             $qty = 0;
         }
         if (empty($info_bits)) {
             $info_bits = 0;
         }
         if (empty($txtva)) {
             $txtva = 0;
         }
         if (empty($txlocaltax1)) {
             $txlocaltax1 = 0;
         }
         if (empty($txlocaltax2)) {
             $txlocaltax2 = 0;
         }
         if (empty($remise)) {
             $remise = 0;
         }
         if (empty($remise_percent)) {
             $remise_percent = 0;
         }
         $remise_percent = price2num($remise_percent);
         $qty = price2num($qty);
         if (!$qty) {
             $qty = 1;
         }
         $pu = price2num($pu);
         $txtva = price2num($txtva);
         $txlocaltax1 = price2num($txlocaltax1);
         $txlocaltax2 = price2num($txlocaltax2);
         // Check parameters
         if ($type < 0) {
             return -1;
         }
         // Calcul du total TTC et de la TVA pour la ligne a partir de
         // qty, pu, remise_percent et txtva
         // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
         // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
         $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $mysoc, $this->thirdparty);
         $txtva = preg_replace('/\\s*\\(.*\\)/', '', $txtva);
         // Remove code into vatrate.
         $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty, $localtaxes_type);
         $total_ht = $tabprice[0];
         $total_tva = $tabprice[1];
         $total_ttc = $tabprice[2];
         $total_localtax1 = $tabprice[9];
         $total_localtax2 = $tabprice[10];
         $localtax1_type = $localtaxes_type[0];
         $localtax2_type = $localtaxes_type[2];
         $subprice = price2num($pu, 'MU');
         // Mise a jour ligne en base
         $sql = "UPDATE " . MAIN_DB_PREFIX . "commande_fournisseurdet SET";
         $sql .= " description='" . $this->db->escape($desc) . "'";
         $sql .= ",subprice='" . price2num($subprice) . "'";
         //$sql.= ",remise='".price2num($remise)."'";
         $sql .= ",remise_percent='" . price2num($remise_percent) . "'";
         $sql .= ",tva_tx='" . price2num($txtva) . "'";
         $sql .= ",localtax1_tx='" . price2num($txlocaltax1) . "'";
         $sql .= ",localtax2_tx='" . price2num($txlocaltax2) . "'";
         $sql .= ",localtax1_type='" . $localtax1_type . "'";
         $sql .= ",localtax2_type='" . $localtax2_type . "'";
         $sql .= ",qty='" . price2num($qty) . "'";
         $sql .= ",date_start=" . (!empty($date_start) ? "'" . $this->db->idate($date_start) . "'" : "null");
         $sql .= ",date_end=" . (!empty($date_end) ? "'" . $this->db->idate($date_end) . "'" : "null");
         $sql .= ",info_bits='" . $info_bits . "'";
         $sql .= ",total_ht='" . price2num($total_ht) . "'";
         $sql .= ",total_tva='" . price2num($total_tva) . "'";
         $sql .= ",total_localtax1='" . price2num($total_localtax1) . "'";
         $sql .= ",total_localtax2='" . price2num($total_localtax2) . "'";
         $sql .= ",total_ttc='" . price2num($total_ttc) . "'";
         $sql .= ",product_type=" . $type;
         $sql .= $fk_unit ? ",fk_unit='" . $this->db->escape($fk_unit) . "'" : ", fk_unit=null";
         $sql .= " WHERE rowid = " . $rowid;
         dol_syslog(get_class($this) . "::updateline", LOG_DEBUG);
         $result = $this->db->query($sql);
         if ($result > 0) {
             $this->rowid = $rowid;
             if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) {
                 $tmpline = new CommandeFournisseurLigne($this->db);
                 $tmpline->id = $this->rowid;
                 $tmpline->array_options = $array_options;
                 $result = $tmpline->insertExtraFields();
                 if ($result < 0) {
                     $error++;
                 }
             }
             if (!$error && !$notrigger) {
                 global $conf, $langs, $user;
                 // Call trigger
                 $result = $this->call_trigger('LINEORDER_SUPPLIER_UPDATE', $user);
                 if ($result < 0) {
                     $this->db->rollback();
                     return -1;
                 }
                 // End call triggers
             }
             // Mise a jour info denormalisees au niveau facture
             if (!$error) {
                 $this->update_price('', 'auto');
             }
             if (!$error) {
                 $this->db->commit();
                 return $result;
             } else {
                 $this->db->rollback();
                 return -1;
             }
         } else {
             $this->error = $this->db->lasterror();
             $this->db->rollback();
             return -1;
         }
     } else {
         $this->error = "Order status makes operation forbidden";
         dol_syslog(get_class($this) . "::updateline " . $this->error, LOG_ERR);
         return -2;
     }
 }
Esempio n. 2
0
 }
 /*
  *	Updating a line in the order
  */
 if ($action == 'updateline' && $user->rights->fournisseur->commande->creer && !GETPOST('cancel')) {
     $tva_tx = GETPOST('tva_tx');
     if (GETPOST('price_ht') != '') {
         $price_base_type = 'HT';
         $ht = price2num(GETPOST('price_ht'));
     } else {
         $ttc = price2num(GETPOST('price_ttc'));
         $ht = $ttc / (1 + $tva_tx / 100);
         $price_base_type = 'HT';
     }
     if ($lineid) {
         $line = new CommandeFournisseurLigne($db);
         $res = $line->fetch($lineid);
         if (!$res) {
             dol_print_error($db);
         }
     }
     $date_start = dol_mktime(GETPOST('date_starthour'), GETPOST('date_startmin'), GETPOST('date_startsec'), GETPOST('date_startmonth'), GETPOST('date_startday'), GETPOST('date_startyear'));
     $date_end = dol_mktime(GETPOST('date_endhour'), GETPOST('date_endmin'), GETPOST('date_endsec'), GETPOST('date_endmonth'), GETPOST('date_endday'), GETPOST('date_endyear'));
     $localtax1_tx = get_localtax($tva_tx, 1, $mysoc, $object->thirdparty);
     $localtax2_tx = get_localtax($tva_tx, 2, $mysoc, $object->thirdparty);
     // Extrafields Lines
     $extrafieldsline = new ExtraFields($db);
     $extralabelsline = $extrafieldsline->fetch_name_optionals_label($object->table_element_line);
     $array_options = $extrafieldsline->getOptionalsFromPost($extralabelsline);
     // Unset extrafield POST Data
     if (is_array($extralabelsline)) {
         unset($_POST['date_endhour']);
         unset($_POST['date_endmin']);
         unset($_POST['date_endsec']);
         unset($_POST['date_endday']);
         unset($_POST['date_endmonth']);
         unset($_POST['date_endyear']);
         if ($saltar) {
             header("Refresh:0");
         }
     } else {
         setEventMessage($object->error, 'errors');
     }
 } else {
     if ($action == 'update_line' && $user->rights->fournisseur->commande->creer && !GETPOST('cancel')) {
         if ($_POST["elrowid"]) {
             $line = new CommandeFournisseurLigne($db);
             $res = $line->fetch($_POST["elrowid"]);
             if (!$res) {
                 dol_print_error($db);
             }
         }
         $localtax1_tx = get_localtax($_POST['tva_tx'], 1, $mysoc, $object->thirdparty);
         $localtax2_tx = get_localtax($_POST['tva_tx'], 2, $mysoc, $object->thirdparty);
         $result = $object->updateline($_POST['elrowid'], $_POST['eldesc'], $_POST['pu'], $_POST['qty'], $_POST['remise_percent'], $_POST['tva_tx'], $localtax1_tx, $localtax2_tx, 'HT', 0, isset($_POST["type"]) ? $_POST["type"] : $line->product_type);
         unset($_POST['qty']);
         unset($_POST['type']);
         unset($_POST['idprodfournprice']);
         unset($_POST['remmise_percent']);
         unset($_POST['dp_desc']);
         unset($_POST['np_desc']);
         unset($_POST['pu']);
Esempio n. 4
0
/**
 * Mise a jour des totaux lignes de commande fournisseur
 *
 * @param	DoliDB		$db		Database handler
 * @param	Translate	$langs	Object langs
 * @param	Conf		$conf	Object conf
 * @return	void
 */
function migrate_price_commande_fournisseur($db, $langs, $conf)
{
    $db->begin();
    print '<tr><td colspan="4">';
    print '<br>';
    print '<b>' . $langs->trans('MigrationSupplierOrder') . "</b><br>\n";
    // Liste des lignes commande non a jour
    $sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as tva_taux, cd.info_bits,";
    $sql .= " c.rowid as commandeid, c.remise_percent as remise_percent_global";
    $sql .= " FROM " . MAIN_DB_PREFIX . "commande_fournisseurdet as cd, " . MAIN_DB_PREFIX . "commande_fournisseur as c";
    $sql .= " WHERE cd.fk_commande = c.rowid";
    $sql .= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100) or cd.total_ttc IS NULL)";
    dolibarr_install_syslog("upgrade2::migrate_price_commande_fournisseur", LOG_DEBUG);
    $resql = $db->query($sql);
    if ($resql) {
        $num = $db->num_rows($resql);
        $i = 0;
        if ($num) {
            while ($i < $num) {
                $obj = $db->fetch_object($resql);
                $rowid = $obj->rowid;
                $qty = $obj->qty;
                $pu = $obj->subprice;
                $txtva = $obj->tva_taux;
                $remise_percent = $obj->remise_percent;
                $remise_percent_global = $obj->remise_percent_global;
                $info_bits = $obj->info_bits;
                // On met a jour les 3 nouveaux champs
                $commandeligne = new CommandeFournisseurLigne($db);
                $commandeligne->fetch($rowid);
                $result = calcul_price_total($qty, $pu, $remise_percent, $txtva, 0, 0, $remise_percent_global, 'HT', $info_bits, 0);
                $total_ht = $result[0];
                $total_tva = $result[1];
                $total_ttc = $result[2];
                $commandeligne->total_ht = $total_ht;
                $commandeligne->total_tva = $total_tva;
                $commandeligne->total_ttc = $total_ttc;
                dolibarr_install_syslog("upgrade2: Line {$rowid}: commandeid={$obj->rowid} pu={$pu} qty={$qty} tva_taux={$txtva} remise_percent={$remise_percent} remise_global={$remise_percent_global} -> {$total_ht}, {$total_tva}, {$total_ttc}");
                print ". ";
                $commandeligne->update_total();
                /* On touche pas a facture mere
                   $commande = new Commande($db);
                   $commande->id = $obj->rowid;
                   if ( $commande->fetch($commande->id) >= 0 )
                   {
                   if ( $commande->update_price() > 0 )
                   {
                   print ". ";
                   }
                   else
                   {
                   print "Error id=".$commande->id;
                   }
                   }
                   else
                   {
                   print "Error #3";
                   }
                   */
                $i++;
            }
        } else {
            print $langs->trans("AlreadyDone");
        }
        $db->free($resql);
        /*
        $sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseurdet";
        $sql.= " WHERE subprice = 0 and total_ttc = 0 and total_tva = 0 and total_ht = 0";
        $resql=$db->query($sql);
        if (! $resql)
        {
        dol_print_error($db);
        }
        */
        $db->commit();
    } else {
        print "Error #1 " . $db->error();
        $db->rollback();
    }
    print '<br>';
    print '</td></tr>';
}