/**
  *	Modify price of a product/Service
  *
  *	@param  	double	$newprice		New price
  *	@param  	string	$newpricebase	HT or TTC
  *	@param  	User	$user        	Object user that make change
  *	@param  	double	$newvat			New VAT Rate
  *  @param		double	$newminprice	New price min
  *  @param		int		$level			0=standard, >0 = level if multilevel prices
  *  @param     	int		$newnpr         0=Standard vat rate, 1=Special vat rate for French NPR VAT
  *  @param     	int		$newpsq         1 if it has price by quantity
  * 	@return		int						<0 if KO, >0 if OK
  */
 function updatePrice($newprice, $newpricebase, $user, $newvat = '', $newminprice = '', $level = 0, $newnpr = 0, $newpsq = 0)
 {
     global $conf, $langs;
     $id = $this->id;
     dol_syslog(get_class($this) . "::update_price id=" . $id . " newprice=" . $newprice . " newpricebase=" . $newpricebase . " newminprice=" . $newminprice . " level=" . $level . " npr=" . $newnpr);
     // Clean parameters
     if (empty($this->tva_tx)) {
         $this->tva_tx = 0;
     }
     if (empty($newnpr)) {
         $newnpr = 0;
     }
     // Check parameters
     if ($newvat == '') {
         $newvat = $this->tva_tx;
     }
     if (!empty($newminprice) && $newminprice > $newprice) {
         $this->error = 'ErrorPriceCantBeLowerThanMinPrice';
         return -1;
     }
     if ($newprice != '' || $newprice == 0) {
         if ($newpricebase == 'TTC') {
             $price_ttc = price2num($newprice, 'MU');
             $price = price2num($newprice) / (1 + $newvat / 100);
             $price = price2num($price, 'MU');
             if ($newminprice != '' || $newminprice == 0) {
                 $price_min_ttc = price2num($newminprice, 'MU');
                 $price_min = price2num($newminprice) / (1 + $newvat / 100);
                 $price_min = price2num($price_min, 'MU');
             } else {
                 $price_min = 0;
                 $price_min_ttc = 0;
             }
         } else {
             $price = price2num($newprice, 'MU');
             $price_ttc = $newnpr != 1 ? price2num($newprice) * (1 + $newvat / 100) : $price;
             $price_ttc = price2num($price_ttc, 'MU');
             if ($newminprice != '' || $newminprice == 0) {
                 $price_min = price2num($newminprice, 'MU');
                 $price_min_ttc = price2num($newminprice) * (1 + $newvat / 100);
                 $price_min_ttc = price2num($price_min_ttc, 'MU');
                 //print 'X'.$newminprice.'-'.$price_min;
             } else {
                 $price_min = 0;
                 $price_min_ttc = 0;
             }
         }
         //print 'x'.$id.'-'.$newprice.'-'.$newpricebase.'-'.$price.'-'.$price_ttc.'-'.$price_min.'-'.$price_min_ttc;
         //Local taxes
         $localtax1 = get_localtax($newvat, 1);
         $localtax2 = get_localtax($newvat, 2);
         if (empty($localtax1)) {
             $localtax1 = 0;
         }
         // If = '' then = 0
         if (empty($localtax2)) {
             $localtax2 = 0;
         }
         // If = '' then = 0
         $this->db->begin();
         // Ne pas mettre de quote sur les numeriques decimaux.
         // Ceci provoque des stockages avec arrondis en base au lieu des valeurs exactes.
         $sql = "UPDATE " . MAIN_DB_PREFIX . "product SET";
         $sql .= " price_base_type='" . $newpricebase . "',";
         $sql .= " price=" . $price . ",";
         $sql .= " price_ttc=" . $price_ttc . ",";
         $sql .= " price_min=" . $price_min . ",";
         $sql .= " price_min_ttc=" . $price_min_ttc . ",";
         $sql .= " localtax1_tx=" . ($localtax1 >= 0 ? $localtax1 : 'NULL') . ",";
         $sql .= " localtax2_tx=" . ($localtax2 >= 0 ? $localtax2 : 'NULL') . ",";
         $sql .= " tva_tx='" . price2num($newvat) . "',";
         $sql .= " recuperableonly='" . $newnpr . "'";
         $sql .= " WHERE rowid = " . $id;
         dol_syslog(get_class($this) . "::update_price", LOG_DEBUG);
         $resql = $this->db->query($sql);
         if ($resql) {
             $this->price = $price;
             $this->price_ttc = $price_ttc;
             $this->price_min = $price_min;
             $this->price_min_ttc = $price_min_ttc;
             $this->price_base_type = $newpricebase;
             $this->tva_tx = $newvat;
             $this->tva_npr = $newnpr;
             //Local taxes
             $this->localtax1_tx = $localtax1;
             $this->localtax2_tx = $localtax2;
             // Price by quantity
             $this->price_by_qty = $newpsq;
             $this->_log_price($user, $level);
             // Save price for level into table product_price
             $this->level = $level;
             // Store level of price edited for trigger
             // Call trigger
             $result = $this->call_trigger('PRODUCT_PRICE_MODIFY', $user);
             if ($result < 0) {
                 $this->db->rollback();
                 return -1;
             }
             // End call triggers
             $this->db->commit();
         } else {
             $this->db->rollback();
             dol_print_error($this->db);
         }
     }
     return 1;
 }
Exemple #2
0
		exit;
	}

	$prod = new Product($db);
	$result = $prod->fetch($id);
	if ($result <= 0)
	{
		dol_print_error($db,$prod->error);
		exit;
	}

	$desc = $prod->description;

	$tva_tx = get_default_tva($mysoc, $soc, $prod->id);
	$localtax1_tx= get_localtax($tva_tx, 1, $soc);
	$localtax2_tx= get_localtax($tva_tx, 2, $soc);

    $pu_ht = $prod->price;
    $pu_ttc = $prod->price_ttc;
    $price_base_type = $prod->price_base_type;

    // If multiprice
    if ($conf->global->PRODUIT_MULTIPRICES && $soc->price_level)
    {
        $pu_ht = $prod->multiprices[$soc->price_level];
        $pu_ttc = $prod->multiprices_ttc[$soc->price_level];
        $price_base_type = $prod->multiprices_base_type[$soc->price_level];
    }

	// On reevalue prix selon taux tva car taux tva transaction peut etre different
	// de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur).
Exemple #3
0
	/**
	 *	Modify price of a product/Service
	 *
	 *	@param  	int		$id          	Id of product/service to change
	 *	@param  	double	$newprice		New price
	 *	@param  	string	$newpricebase	HT or TTC
	 *	@param  	User	$user        	Object user that make change
	 *	@param  	double	$newvat			New VAT Rate
	 *  @param		double	$newminprice	New price min
	 *  @param		int		$level			0=standard, >0 = level if multilevel prices
	 *  @param     	int		$newnpr         0=Standard vat rate, 1=Special vat rate for French NPR VAT
	 * 	@return		int						<0 if KO, >0 if OK
	 */
	function updatePrice($id, $newprice, $newpricebase, $user, $newvat='',$newminprice='',$newecotax=0, $level=0, $newnpr=0)
	{
		global $conf,$langs;

		dol_syslog(get_class($this)."update_price id=".$id." newprice=".$newprice." newpricebase=".$newpricebase." newminprice=".$newminprice." level=".$level." npr=".$newnpr);

		// Clean parameters
		if (empty($this->tva_tx))  $this->tva_tx=0;
                if (empty($newnpr)) $newnpr=0;

		// Check parameters
		if ($newvat == '') $newvat=$this->tva_tx;

		if ($newprice!='' || $newprice==0)
		{
			if ($newpricebase == 'TTC')
			{
				$price_ttc = price2num($newprice,'MU');
				$price = price2num($newprice) / (1 + ($newvat / 100));
				$price = price2num($price,'MU');

				if ($newminprice!='' || $newminprice==0)
				{
					$price_min_ttc = price2num($newminprice,'MU');
					$price_min = price2num($newminprice) / (1 + ($newvat / 100));
					$price_min = price2num($price_min,'MU');
				}
				else
				{
					$price_min=0;
					$price_min_ttc=0;
				}
                                
                                $ecotax_ttc = price2num($newecotax,'MU');
				$ecotax = price2num($newecotax) / (1 + ($newvat / 100));
				$ecotax = price2num($ecotax,'MU');
			}
			else
			{
				$price = price2num($newprice,'MU');
				$price_ttc = price2num($newprice) * (1 + ($newvat / 100));
				$price_ttc = price2num($price_ttc,'MU');

				if ($newminprice!='' || $newminprice==0)
				{
					$price_min = price2num($newminprice,'MU');
					$price_min_ttc = price2num($newminprice) * (1 + ($newvat / 100));
					$price_min_ttc = price2num($price_min_ttc,'MU');
					//print 'X'.$newminprice.'-'.$price_min;
				}
				else
				{
					$price_min=0;
					$price_min_ttc=0;
				}
                                
                                $ecotax = price2num($newecotax,'MU');
				$ecotax_ttc = price2num($newecotax) * (1 + ($newvat / 100));
				$ecotax_ttc = price2num($ecotax_ttc,'MU');
			}
			//print 'x'.$id.'-'.$newprice.'-'.$newpricebase.'-'.$price.'-'.$price_ttc.'-'.$price_min.'-'.$price_min_ttc;

			//Local taxes
			$localtax1=get_localtax($newvat,1);
			$localtax2=get_localtax($newvat,2);
			if (empty($localtax1)) $localtax1=0;	// If = '' then = 0
			if (empty($localtax2)) $localtax2=0;	// If = '' then = 0

			// Ne pas mettre de quote sur les numeriques decimaux.
			// Ceci provoque des stockages avec arrondis en base au lieu des valeurs exactes.
			$sql = "UPDATE ".MAIN_DB_PREFIX."product SET";
			$sql.= " price_base_type='".$newpricebase."',";
			$sql.= " price=".$price.",";
			$sql.= " price_ttc=".$price_ttc.",";
			$sql.= " price_min=".$price_min.",";
			$sql.= " price_min_ttc=".$price_min_ttc.",";
                        $sql.= " ecotax=".$ecotax.",";
                        $sql.= " ecotax_ttc=".$ecotax_ttc.",";
			$sql.= " localtax1_tx=".($localtax1>=0?$localtax1:'NULL').",";
			$sql.= " localtax2_tx=".($localtax2>=0?$localtax2:'NULL').",";
			$sql.= " tva_tx='".price2num($newvat)."',";
            $sql.= " recuperableonly='".$newnpr."'";
			$sql.= " WHERE rowid = ".$id;

			dol_syslog(get_class($this)."update_price sql=".$sql, LOG_DEBUG);
			$resql=$this->db->query($sql);
			if ($resql)
			{
				$this->price = $price;
				$this->price_ttc = $price_ttc;
				$this->price_min = $price_min;
				$this->price_min_ttc = $price_min_ttc;
				$this->price_base_type = $newpricebase;
				$this->tva_tx = $newvat;
				$this->tva_npr = $newnpr;
				//Local taxes
				$this->localtax1_tx = $localtax1;
				$this->localtax2_tx = $localtax2;
                                $this->ecotax   = $ecotax;
                                $this->ecotax_ttc   = $ecotax_ttc;

				$this->_log_price($user,$level);
			}
			else
			{
				dol_print_error($this->db);
			}
		}

		return 1;
	}
Exemple #4
0
 /**
  *	Create invoice in database
  *  Note: this->ref can be set or empty. If empty, we will use "(PROV)"
  *
  *	@param	User	$user      		Object user that create
  *	@param  int		$notrigger		1=Does not execute triggers, 0 otherwise
  * 	@param	int		$forceduedate	1=Do not recalculate due date from payment condition but force it with value
  *	@return	int						<0 if KO, >0 if OK
  */
 function create($user, $notrigger = 0, $forceduedate = 0)
 {
     global $langs, $conf, $mysoc, $hookmanager;
     $error = 0;
     // Clean parameters
     if (empty($this->type)) {
         $this->type = self::TYPE_STANDARD;
     }
     $this->ref_client = trim($this->ref_client);
     $this->note = isset($this->note) ? trim($this->note) : trim($this->note_private);
     // deprecated
     $this->note_private = isset($this->note_private) ? trim($this->note_private) : trim($this->note_private);
     $this->note_public = trim($this->note_public);
     if (!$this->cond_reglement_id) {
         $this->cond_reglement_id = 0;
     }
     if (!$this->mode_reglement_id) {
         $this->mode_reglement_id = 0;
     }
     $this->brouillon = 1;
     dol_syslog(get_class($this) . "::create user="******"ErrorBadParameter";
         dol_syslog(get_class($this) . "::create Try to create an invoice with an empty parameter (user, date, ...)", LOG_ERR);
         return -3;
     }
     $soc = new Societe($this->db);
     $result = $soc->fetch($this->socid);
     if ($result < 0) {
         $this->error = "Failed to fetch company";
         dol_syslog(get_class($this) . "::create " . $this->error, LOG_ERR);
         return -2;
     }
     $now = dol_now();
     $this->db->begin();
     // Create invoice from a predefined invoice
     if ($this->fac_rec > 0) {
         require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture-rec.class.php';
         $_facrec = new FactureRec($this->db);
         $result = $_facrec->fetch($this->fac_rec);
         $this->fk_project = $_facrec->fk_project;
         $this->cond_reglement_id = $_facrec->cond_reglement_id;
         $this->mode_reglement_id = $_facrec->mode_reglement_id;
         $this->remise_absolue = $_facrec->remise_absolue;
         $this->remise_percent = $_facrec->remise_percent;
         $this->fk_incoterms = $_facrec->fk_incoterms;
         $this->location_incoterms = $_facrec->location_incoterms;
         // Clean parameters
         if (!$this->type) {
             $this->type = self::TYPE_STANDARD;
         }
         $this->ref_client = trim($this->ref_client);
         $this->note_private = trim($this->note_private);
         $this->note_public = trim($this->note_public);
         //if (! $this->remise) $this->remise = 0;
         if (!$this->mode_reglement_id) {
             $this->mode_reglement_id = 0;
         }
         $this->brouillon = 1;
     }
     // Define due date if not already defined
     $datelim = empty($forceduedate) ? $this->calculate_date_lim_reglement() : $forceduedate;
     // Insert into database
     $socid = $this->socid;
     $sql = "INSERT INTO " . MAIN_DB_PREFIX . "facture (";
     $sql .= " facnumber";
     $sql .= ", entity";
     $sql .= ", ref_ext";
     $sql .= ", type";
     $sql .= ", fk_soc";
     $sql .= ", datec";
     $sql .= ", remise_absolue";
     $sql .= ", remise_percent";
     $sql .= ", datef";
     $sql .= ", note_private";
     $sql .= ", note_public";
     $sql .= ", ref_client, ref_int";
     $sql .= ", fk_account";
     $sql .= ", fk_facture_source, fk_user_author, fk_projet";
     $sql .= ", fk_cond_reglement, fk_mode_reglement, date_lim_reglement, model_pdf";
     $sql .= ", situation_cycle_ref, situation_counter, situation_final";
     $sql .= ", fk_incoterms, location_incoterms";
     $sql .= ")";
     $sql .= " VALUES (";
     $sql .= "'(PROV)'";
     $sql .= ", " . $conf->entity;
     $sql .= ", " . ($this->ref_ext ? "'" . $this->db->escape($this->ref_ext) . "'" : "null");
     $sql .= ", '" . $this->db->escape($this->type) . "'";
     $sql .= ", '" . $socid . "'";
     $sql .= ", '" . $this->db->idate($now) . "'";
     $sql .= ", " . ($this->remise_absolue > 0 ? $this->remise_absolue : 'NULL');
     $sql .= ", " . ($this->remise_percent > 0 ? $this->remise_percent : 'NULL');
     $sql .= ", '" . $this->db->idate($this->date) . "'";
     $sql .= ", " . ($this->note_private ? "'" . $this->db->escape($this->note_private) . "'" : "null");
     $sql .= ", " . ($this->note_public ? "'" . $this->db->escape($this->note_public) . "'" : "null");
     $sql .= ", " . ($this->ref_client ? "'" . $this->db->escape($this->ref_client) . "'" : "null");
     $sql .= ", " . ($this->ref_int ? "'" . $this->db->escape($this->ref_int) . "'" : "null");
     $sql .= ", " . ($this->fk_account > 0 ? $this->fk_account : 'NULL');
     $sql .= ", " . ($this->fk_facture_source ? "'" . $this->db->escape($this->fk_facture_source) . "'" : "null");
     $sql .= ", " . ($user->id > 0 ? "'" . $user->id . "'" : "null");
     $sql .= ", " . ($this->fk_project ? $this->fk_project : "null");
     $sql .= ", " . $this->cond_reglement_id;
     $sql .= ", " . $this->mode_reglement_id;
     $sql .= ", '" . $this->db->idate($datelim) . "', '" . $this->db->escape($this->modelpdf) . "'";
     $sql .= ", " . ($this->situation_cycle_ref ? "'" . $this->db->escape($this->situation_cycle_ref) . "'" : "null");
     $sql .= ", " . ($this->situation_counter ? "'" . $this->db->escape($this->situation_counter) . "'" : "null");
     $sql .= ", " . ($this->situation_final ? $this->situation_final : 0);
     $sql .= ", " . (int) $this->fk_incoterms;
     $sql .= ", '" . $this->db->escape($this->location_incoterms) . "'";
     $sql .= ")";
     dol_syslog(get_class($this) . "::create", LOG_DEBUG);
     $resql = $this->db->query($sql);
     if ($resql) {
         $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'facture');
         // Update ref with new one
         $this->ref = '(PROV' . $this->id . ')';
         $sql = 'UPDATE ' . MAIN_DB_PREFIX . "facture SET facnumber='" . $this->ref . "' WHERE rowid=" . $this->id;
         dol_syslog(get_class($this) . "::create", LOG_DEBUG);
         $resql = $this->db->query($sql);
         if (!$resql) {
             $error++;
         }
         // Add object linked
         if (!$error && $this->id && is_array($this->linked_objects) && !empty($this->linked_objects)) {
             foreach ($this->linked_objects as $origin => $origin_id) {
                 $ret = $this->add_object_linked($origin, $origin_id);
                 if (!$ret) {
                     dol_print_error($this->db);
                     $error++;
                 }
                 // TODO mutualiser
                 if ($origin == 'commande') {
                     // On recupere les differents contact interne et externe
                     $order = new Commande($this->db);
                     $order->id = $origin_id;
                     // On recupere le commercial suivi propale
                     $this->userid = $order->getIdcontact('internal', 'SALESREPFOLL');
                     if ($this->userid) {
                         //On passe le commercial suivi commande en commercial suivi paiement
                         $this->add_contact($this->userid[0], 'SALESREPFOLL', 'internal');
                     }
                     // On recupere le contact client facturation commande
                     $this->contactid = $order->getIdcontact('external', 'BILLING');
                     if ($this->contactid) {
                         //On passe le contact client facturation commande en contact client facturation
                         $this->add_contact($this->contactid[0], 'BILLING', 'external');
                     }
                 }
             }
         }
         /*
          *  Insert lines of invoices into database
          */
         if (count($this->lines) && is_object($this->lines[0])) {
             $fk_parent_line = 0;
             dol_syslog("There is " . count($this->lines) . " lines that are invoice lines objects");
             foreach ($this->lines as $i => $val) {
                 $newinvoiceline = $this->lines[$i];
                 $newinvoiceline->fk_facture = $this->id;
                 $newinvoiceline->origin = $this->element;
                 $newinvoiceline->origin_id = $this->lines[$i]->id;
                 if ($result >= 0 && ($newinvoiceline->info_bits & 0x1) == 0) {
                     // Reset fk_parent_line for no child products and special product
                     if ($newinvoiceline->product_type != 9 && empty($newinvoiceline->fk_parent_line) || $newinvoiceline->product_type == 9) {
                         $fk_parent_line = 0;
                     }
                     $newinvoiceline->fk_parent_line = $fk_parent_line;
                     $result = $newinvoiceline->insert();
                     // Defined the new fk_parent_line
                     if ($result > 0 && $newinvoiceline->product_type == 9) {
                         $fk_parent_line = $result;
                     }
                 }
                 if ($result < 0) {
                     $this->error = $newinvoiceline->error;
                     $error++;
                     break;
                 }
             }
         } else {
             $fk_parent_line = 0;
             dol_syslog("There is " . count($this->lines) . " lines that are array lines");
             foreach ($this->lines as $i => $val) {
                 if (($this->lines[$i]->info_bits & 0x1) == 0) {
                     // Reset fk_parent_line for no child products and special product
                     if ($this->lines[$i]->product_type != 9 && empty($this->lines[$i]->fk_parent_line) || $this->lines[$i]->product_type == 9) {
                         $fk_parent_line = 0;
                     }
                     $result = $this->addline($this->lines[$i]->desc, $this->lines[$i]->subprice, $this->lines[$i]->qty, $this->lines[$i]->tva_tx, $this->lines[$i]->localtax1_tx, $this->lines[$i]->localtax2_tx, $this->lines[$i]->fk_product, $this->lines[$i]->remise_percent, $this->lines[$i]->date_start, $this->lines[$i]->date_end, $this->lines[$i]->fk_code_ventilation, $this->lines[$i]->info_bits, $this->lines[$i]->fk_remise_except, 'HT', 0, $this->lines[$i]->product_type, $this->lines[$i]->rang, $this->lines[$i]->special_code, $this->element, $this->lines[$i]->id, $fk_parent_line, $this->lines[$i]->fk_fournprice, $this->lines[$i]->pa_ht, $this->lines[$i]->label, $this->lines[$i]->array_options, $this->lines[$i]->situation_percent, $this->lines[$i]->fk_prev_id, $this->lines[$i]->fk_unit);
                     if ($result < 0) {
                         $this->error = $this->db->lasterror();
                         dol_print_error($this->db);
                         $this->db->rollback();
                         return -1;
                     }
                     // Defined the new fk_parent_line
                     if ($result > 0 && $this->lines[$i]->product_type == 9) {
                         $fk_parent_line = $result;
                     }
                 }
             }
         }
         /*
          * Insert lines of predefined invoices
          */
         if (!$error && $this->fac_rec > 0) {
             foreach ($_facrec->lines as $i => $val) {
                 if ($_facrec->lines[$i]->fk_product) {
                     $prod = new Product($this->db);
                     $res = $prod->fetch($_facrec->lines[$i]->fk_product);
                 }
                 $tva_tx = get_default_tva($mysoc, $soc, $prod->id);
                 $localtax1_tx = get_localtax($tva_tx, 1, $soc);
                 $localtax2_tx = get_localtax($tva_tx, 2, $soc);
                 $result_insert = $this->addline($_facrec->lines[$i]->desc, $_facrec->lines[$i]->subprice, $_facrec->lines[$i]->qty, $tva_tx, $localtax1_tx, $localtax2_tx, $_facrec->lines[$i]->fk_product, $_facrec->lines[$i]->remise_percent, '', '', 0, 0, '', 'HT', 0, $_facrec->lines[$i]->product_type, $_facrec->lines[$i]->rang, $_facrec->lines[$i]->special_code, '', 0, 0, null, 0, $_facrec->lines[$i]->label, null, $_facrec->lines[$i]->situation_percent, '', $_facrec->lines[$i]->fk_unit);
                 if ($result_insert < 0) {
                     $error++;
                     $this->error = $this->db->error();
                     break;
                 }
             }
         }
         if (!$error) {
             $result = $this->update_price(1);
             if ($result > 0) {
                 $action = 'create';
                 // Actions on extra fields (by external module or standard code)
                 // TODO le hook fait double emploi avec le trigger !!
                 $hookmanager->initHooks(array('invoicedao'));
                 $parameters = array('invoiceid' => $this->id);
                 $reshook = $hookmanager->executeHooks('insertExtraFields', $parameters, $this, $action);
                 // Note that $action and $object may have been modified by some hooks
                 if (empty($reshook)) {
                     if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) {
                         $result = $this->insertExtraFields();
                         if ($result < 0) {
                             $error++;
                         }
                     }
                 } else {
                     if ($reshook < 0) {
                         $error++;
                     }
                 }
                 // Call trigger
                 $result = $this->call_trigger('BILL_CREATE', $user);
                 if ($result < 0) {
                     $error++;
                 }
                 // End call triggers
                 if (!$error) {
                     $this->db->commit();
                     return $this->id;
                 } else {
                     $this->db->rollback();
                     return -4;
                 }
             } else {
                 $this->error = $langs->trans('FailedToUpdatePrice');
                 $this->db->rollback();
                 return -3;
             }
         } else {
             dol_syslog(get_class($this) . "::create error " . $this->error, LOG_ERR);
             $this->db->rollback();
             return -2;
         }
     } else {
         $this->error = $this->db->error();
         $this->db->rollback();
         return -1;
     }
 }
/**
 *  Get type and rate of localtaxes for a particular vat rate/country fo thirdparty
 *  TODO
 *  This function is also called to retrieve type for building PDF. Such call of function must be removed.
 *  Instead this function must be called when adding a line to get the array of localtax and type, and then
 *  provide it to the function calcul_price_total.
 *
 *  @param	float	$vatrate			VAT Rate
 *  @param	int		$local              Number of localtax (1 or 2, or 0 to return 1 & 2)
 *  @param	Societe	$buyer         		Company object
 *  @param	Societe	$seller        		Company object
 *  @return	array    	  				array(localtax_type1(1-6 / 0 if not found), rate of localtax1, ...)
 */
function getLocalTaxesFromRate($vatrate, $local, $buyer, $seller)
{
    global $db, $mysoc;
    dol_syslog("getLocalTaxesFromRate vatrate=" . $vatrate . " local=" . $local);
    $vatratecleaned = $vatrate;
    if (preg_match('/^(.*)\\s*\\((.*)\\)$/', $vatrate, $reg)) {
        $vatratecleaned = $reg[1];
        $vatratecode = $reg[2];
    }
    // Search local taxes
    $sql = "SELECT t.localtax1, t.localtax1_type, t.localtax2, t.localtax2_type, t.accountancy_code_sell, t.accountancy_code_buy";
    $sql .= " FROM " . MAIN_DB_PREFIX . "c_tva as t, " . MAIN_DB_PREFIX . "c_country as c";
    if ($mysoc->country_code == 'ES') {
        $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '" . $buyer->country_code . "'";
    } else {
        $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '" . $seller->country_code . "'";
    }
    $sql .= " AND t.taux = " . (double) $vatratecleaned . " AND t.active = 1";
    if ($vatratecode) {
        $sql .= " AND t.code ='" . $vatratecode . "'";
    }
    $resql = $db->query($sql);
    if ($resql) {
        $obj = $db->fetch_object($resql);
        if ($local == 1) {
            if (!isOnlyOneLocalTax(1)) {
                return array($obj->localtax1_type, get_localtax($vatrate, $local, $buyer, $seller), $obj->accountancy_code_sell, $obj->accountancy_code_buy);
            } else {
                return array($obj->localtax1_type, $obj->localtax1, $obj->accountancy_code_sell, $obj->accountancy_code_buy);
            }
        } elseif ($local == 2) {
            if (!isOnlyOneLocalTax(2)) {
                return array($obj->localtax2_type, get_localtax($vatrate, $local, $buyer, $seller), $obj->accountancy_code_sell, $obj->accountancy_code_buy);
            } else {
                return array($obj->localtax2_type, $obj->localtax2, $obj->accountancy_code_sell, $obj->accountancy_code_buy);
            }
        } else {
            if (!isOnlyOneLocalTax(1)) {
                if (!isOnlyOneLocalTax(2)) {
                    return array($obj->localtax1_type, get_localtax($vatrate, 1, $buyer, $seller), $obj->localtax2_type, get_localtax($vatrate, 2, $buyer, $seller), $obj->accountancy_code_sell, $obj->accountancy_code_buy);
                } else {
                    return array($obj->localtax1_type, get_localtax($vatrate, 1, $buyer, $seller), $obj->localtax2_type, $obj->localtax2, $obj->accountancy_code_sell, $obj->accountancy_code_buy);
                }
            } else {
                if (!isOnlyOneLocalTax(2)) {
                    return array($obj->localtax1_type, $obj->localtax1, $obj->localtax2_type, get_localtax($vatrate, 2, $buyer, $seller), $obj->accountancy_code_sell, $obj->accountancy_code_buy);
                } else {
                    return array($obj->localtax1_type, $obj->localtax1, $obj->localtax2_type, $obj->localtax2, $obj->accountancy_code_sell, $obj->accountancy_code_buy);
                }
            }
        }
    }
    return 0;
}
Exemple #6
0
 $date_start = '';
 $date_end = '';
 $date_start = dol_mktime(0, 0, 0, $_POST['date_start' . $suffixe . 'month'], $_POST['date_start' . $suffixe . 'day'], $_POST['date_start' . $suffixe . 'year']);
 $date_end = dol_mktime(0, 0, 0, $_POST['date_end' . $suffixe . 'month'], $_POST['date_end' . $suffixe . 'day'], $_POST['date_end' . $suffixe . 'year']);
 $description = dol_htmlcleanlastbr($_POST['desc']);
 $up_ht = GETPOST('pu') ? GETPOST('pu') : GETPOST('subprice');
 // Define info_bits
 $info_bits = 0;
 if (preg_match('/\\*/', $_POST['tva_tx'])) {
     $info_bits |= 0x1;
 }
 // Define vat_rate
 $vat_rate = $_POST['tva_tx'];
 $vat_rate = str_replace('*', '', $vat_rate);
 $localtax1_rate = get_localtax($vat_rate, 1, $object->client);
 $localtax2_rate = get_localtax($vat_rate, 2, $object->client);
 // Check parameters
 if (empty($_POST['productid']) && $_POST["type"] < 0) {
     $mesg = '<div class="error">' . $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")) . '</div>';
     $result = -1;
 }
 // Check minimum price
 if (!empty($_POST['productid'])) {
     $productid = $_POST['productid'];
     $product = new Product($db);
     $product->fetch($productid);
     $type = $product->type;
     $price_min = $product->price_min;
     if ($conf->global->PRODUIT_MULTIPRICES && $object->client->price_level) {
         $price_min = $product->multiprices_min[$object->client->price_level];
     }
Exemple #7
0
 /**
  * 		Add line into array
  *		$this->client doit etre charge
  *		@param		idproduct			Id du produit a ajouter
  *		@param		qty					Quantite
  *		@param		remise_percent		Remise relative effectuee sur le produit
  * 		@param    	date_start          Start date of the line - Added by Matelli (See http://matelli.fr/showcases/patchs-dolibarr/add-dates-in-order-lines.html)
  * 		@param    	date_end            End date of the line - Added by Matelli (See http://matelli.fr/showcases/patchs-dolibarr/add-dates-in-order-lines.html)
  * 		@return    	void
  *		TODO	Remplacer les appels a cette fonction par generation objet Ligne
  *				insere dans tableau $this->products
  */
 function add_product($idproduct, $qty, $remise_percent = 0, $date_start = '', $date_end = '')
 {
     global $conf, $mysoc;
     if (!$qty) {
         $qty = 1;
     }
     if ($idproduct > 0) {
         $prod = new Product($this->db);
         $prod->fetch($idproduct);
         $tva_tx = get_default_tva($mysoc, $this->client, $prod->id);
         $localtax1_tx = get_localtax($tva_tx, 1, $this->client);
         $localtax2_tx = get_localtax($tva_tx, 2, $this->client);
         // multiprix
         if ($conf->global->PRODUIT_MULTIPRICES && $this->client->price_level) {
             $price = $prod->multiprices[$this->client->price_level];
         } else {
             $price = $prod->price;
         }
         $line = new OrderLine($this->db);
         $line->fk_product = $idproduct;
         $line->desc = $prod->description;
         $line->qty = $qty;
         $line->subprice = $price;
         $line->remise_percent = $remise_percent;
         $line->tva_tx = $tva_tx;
         $line->localtax1_tx = $localtax1_tx;
         $line->localtax2_tx = $localtax2_tx;
         $line->ref = $prod->ref;
         $line->libelle = $prod->libelle;
         $line->product_desc = $prod->description;
         // Added by Matelli (See http://matelli.fr/showcases/patchs-dolibarr/add-dates-in-order-lines.html)
         // Save the start and end date of the line in the object
         if ($date_start) {
             $line->date_start = $date_start;
         }
         if ($date_end) {
             $line->date_end = $date_end;
         }
         $this->lines[] = $line;
         /** POUR AJOUTER AUTOMATIQUEMENT LES SOUSPRODUITS a LA COMMANDE
         			 if (! empty($conf->global->PRODUIT_SOUSPRODUITS))
         			 {
         			 $prod = new Product($this->db, $idproduct);
         			 $prod -> get_sousproduits_arbo ();
         			 $prods_arbo = $prod->get_each_prod();
         			 if(sizeof($prods_arbo) > 0)
         			 {
         			 foreach($prods_arbo as $key => $value)
         			 {
         			 // print "id : ".$value[1].' :qty: '.$value[0].'<br>';
         			 if(! in_array($value[1],$this->products))
         			 $this->add_product($value[1], $value[0]);
         
         			 }
         			 }
         
         			 }
         			 **/
     }
 }
 static function createFactureFromObject(&$object)
 {
     global $db, $conf, $user, $langs;
     dol_include_once('/compta/facture/class/facture.class.php');
     $langs->load('grapefruit@grapefruit');
     $dateinvoice = dol_mktime(0, 0, 0, date('m'), date('d'), date('Y'));
     $f = new Facture($db);
     $f->socid = $object->socid;
     $f->type = Facture::TYPE_STANDARD;
     $f->number = $_POST['facnumber'];
     $f->date = $dateinvoice;
     $f->note_public = $object->note_public;
     $f->note_private = $object->note_private;
     $f->ref_client = $object->ref_client;
     $f->fk_project = $object->fk_project;
     $f->cond_reglement_id = $object->cond_reglement_id;
     $f->mode_reglement_id = $object->mode_reglement_id;
     $origin = 'commande';
     $originid = $object->id;
     $f->linked_objects[$origin] = $originid;
     $id = $f->create($user);
     $lines = $object->lines;
     if (empty($lines) && method_exists($object, 'fetch_lines')) {
         $object->fetch_lines();
         $lines = $object->lines;
     }
     $fk_parent_line = 0;
     $num = count($lines);
     for ($i = 0; $i < $num; $i++) {
         $label = !empty($lines[$i]->label) ? $lines[$i]->label : '';
         $desc = !empty($lines[$i]->desc) ? $lines[$i]->desc : $lines[$i]->libelle;
         if ($f->situation_counter == 1) {
             $lines[$i]->situation_percent = 0;
         }
         if ($lines[$i]->subprice < 0) {
             // Negative line, we create a discount line
             $discount = new DiscountAbsolute($db);
             $discount->fk_soc = $f->socid;
             $discount->amount_ht = abs($lines[$i]->total_ht);
             $discount->amount_tva = abs($lines[$i]->total_tva);
             $discount->amount_ttc = abs($lines[$i]->total_ttc);
             $discount->tva_tx = $lines[$i]->tva_tx;
             $discount->fk_user = $user->id;
             $discount->description = $desc;
             $discountid = $discount->create($user);
             if ($discountid > 0) {
                 $result = $f->insert_discount($discountid);
                 // This include link_to_invoice
             } else {
                 setEventMessages($discount->error, $discount->errors, 'errors');
                 $error++;
                 break;
             }
         } else {
             // Positive line
             $product_type = $lines[$i]->product_type ? $lines[$i]->product_type : 0;
             // Date start
             $date_start = false;
             if ($lines[$i]->date_debut_prevue) {
                 $date_start = $lines[$i]->date_debut_prevue;
             }
             if ($lines[$i]->date_debut_reel) {
                 $date_start = $lines[$i]->date_debut_reel;
             }
             if ($lines[$i]->date_start) {
                 $date_start = $lines[$i]->date_start;
             }
             // Date end
             $date_end = false;
             if ($lines[$i]->date_fin_prevue) {
                 $date_end = $lines[$i]->date_fin_prevue;
             }
             if ($lines[$i]->date_fin_reel) {
                 $date_end = $lines[$i]->date_fin_reel;
             }
             if ($lines[$i]->date_end) {
                 $date_end = $lines[$i]->date_end;
             }
             // Reset fk_parent_line for no child products and special product
             if ($lines[$i]->product_type != 9 && empty($lines[$i]->fk_parent_line) || $lines[$i]->product_type == 9) {
                 $fk_parent_line = 0;
             }
             // Extrafields
             if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && method_exists($lines[$i], 'fetch_optionals')) {
                 $lines[$i]->fetch_optionals($lines[$i]->rowid);
                 $array_options = $lines[$i]->array_options;
             }
             // View third's localtaxes for now
             $localtax1_tx = get_localtax($lines[$i]->tva_tx, 1, $f->client);
             $localtax2_tx = get_localtax($lines[$i]->tva_tx, 2, $f->client);
             $result = $f->addline($desc, $lines[$i]->subprice, $lines[$i]->qty, $lines[$i]->tva_tx, $localtax1_tx, $localtax2_tx, $lines[$i]->fk_product, $lines[$i]->remise_percent, $date_start, $date_end, 0, $lines[$i]->info_bits, $lines[$i]->fk_remise_except, 'HT', 0, $product_type, $lines[$i]->rang, $lines[$i]->special_code, $f->origin, $lines[$i]->rowid, $fk_parent_line, $lines[$i]->fk_fournprice, $lines[$i]->pa_ht, $label, $array_options, $lines[$i]->situation_percent, $lines[$i]->fk_prev_id, $lines[$i]->fk_unit);
             if ($result > 0) {
                 $lineid = $result;
             } else {
                 $lineid = 0;
                 $error++;
                 break;
             }
             // Defined the new fk_parent_line
             if ($result > 0 && $lines[$i]->product_type == 9) {
                 $fk_parent_line = $result;
             }
         }
     }
     if (empty($error)) {
         if ($f->validate($user) > 0) {
             $object->classifyBilled();
             // Redirection vers écrand de paiement
             setEventMessage($langs->trans('BillCreated'));
             header('Location: ' . dol_buildpath('/compta/paiement.php?action=create&facid=' . $f->id, 1));
         }
     }
 }
 /**
  * 	Add line into array products
  *	$this->client doit etre charge
  *
  * 	@param  int		$idproduct       	Product Id to add
  * 	@param  int		$qty             	Quantity
  * 	@param  int		$remise_percent  	Discount effected on Product
  *  @return	int							<0 if KO, >0 if OK
  *
  *	TODO	Remplacer les appels a cette fonction par generation objet Ligne
  *			insere dans tableau $this->products
  */
 function add_product($idproduct, $qty, $remise_percent = 0)
 {
     global $conf, $mysoc;
     if (!$qty) {
         $qty = 1;
     }
     dol_syslog(get_class($this) . "::add_product {$idproduct}, {$qty}, {$remise_percent}");
     if ($idproduct > 0) {
         $prod = new Product($this->db);
         $prod->fetch($idproduct);
         $productdesc = $prod->description;
         $tva_tx = get_default_tva($mysoc, $this->client, $prod->id);
         $tva_npr = get_default_npr($mysoc, $this->client, $prod->id);
         if (empty($tva_tx)) {
             $tva_npr = 0;
         }
         $localtax1_tx = get_localtax($tva_tx, 1, $mysoc, $this->client, $tva_npr);
         $localtax2_tx = get_localtax($tva_tx, 2, $mysoc, $this->client, $tva_npr);
         // multiprix
         if ($conf->global->PRODUIT_MULTIPRICES && $this->client->price_level) {
             $price = $prod->multiprices[$this->client->price_level];
         } else {
             $price = $prod->price;
         }
         $line = new SupplierProposalLine($this->db);
         $line->fk_product = $idproduct;
         $line->desc = $productdesc;
         $line->qty = $qty;
         $line->subprice = $price;
         $line->remise_percent = $remise_percent;
         $line->tva_tx = $tva_tx;
         $this->lines[] = $line;
     }
 }
Exemple #10
0
		}
    }
}

if ($action == 'updateligne' && $user->rights->contrat->creer && ! $_POST["cancel"])
{
    $objectline = new ContratLigne($db);
    if ($objectline->fetch($_POST["elrowid"]))
    {
		$db->begin();

		if ($date_start_real_update == '') $date_start_real_update=$objectline->date_ouverture;
		if ($date_end_real_update == '')   $date_end_real_update=$objectline->date_cloture;

		$localtax1_tx=get_localtax($_POST["eltva_tx"],1,$object->client);
        $localtax2_tx=get_localtax($_POST["eltva_tx"],2,$object->client);

		$objectline->description=$_POST["eldesc"];
		$objectline->price_ht=$_POST["elprice"];
		$objectline->subprice=$_POST["elprice"];
        $objectline->qty=$_POST["elqty"];
        $objectline->remise_percent=$_POST["elremise_percent"];
		$objectline->tva_tx=$_POST["eltva_tx"];
		$objectline->localtax1_tx=$localtax1_tx;
		$objectline->localtax2_tx=$localtax2_tx;
		$objectline->date_ouverture_prevue=$date_start_update;
		$objectline->date_ouverture=$date_start_real_update;
		$objectline->date_fin_validite=$date_end_update;
        $objectline->date_cloture=$date_end_real_update;
		$objectline->fk_user_cloture=$user->id;
Exemple #11
0
 /**
  *	Modify price of a product/Service
  *
  *	@param  	double	$newprice		New price
  *	@param  	string	$newpricebase	HT or TTC
  *	@param  	User	$user        	Object user that make change
  *	@param  	double	$newvat			New VAT Rate (For example 8.5. Should not be a string)
  *  @param		double	$newminprice	New price min
  *  @param		int		$level			0=standard, >0 = level if multilevel prices
  *  @param     	int		$newnpr         0=Standard vat rate, 1=Special vat rate for French NPR VAT
  *  @param     	int		$newpsq         1 if it has price by quantity
  *  @param 		int 	$ignore_autogen Used to avoid infinite loops
  *	@param      array	$localtaxes_array	Array with localtaxes info array('0'=>type1,'1'=>rate1,'2'=>type2,'3'=>rate2) (loaded by getLocalTaxesFromRate(vatrate, 0, ...) function).
  * 	@return		int						    <0 if KO, >0 if OK
  */
 function updatePrice($newprice, $newpricebase, $user, $newvat = '', $newminprice = '', $level = 0, $newnpr = 0, $newpsq = 0, $ignore_autogen = 0, $localtaxes_array = array())
 {
     global $conf, $langs;
     $id = $this->id;
     dol_syslog(get_class($this) . "::update_price id=" . $id . " newprice=" . $newprice . " newpricebase=" . $newpricebase . " newminprice=" . $newminprice . " level=" . $level . " npr=" . $newnpr);
     // Clean parameters
     if (empty($this->tva_tx)) {
         $this->tva_tx = 0;
     }
     if (empty($newnpr)) {
         $newnpr = 0;
     }
     // Check parameters
     if ($newvat == '') {
         $newvat = $this->tva_tx;
     }
     // If multiprices are enabled, then we check if the current product is subject to price autogeneration
     // Price will be modified ONLY when the first one is the one that is being modified
     if (!empty($conf->global->PRODUIT_MULTIPRICES) && !$ignore_autogen && $this->price_autogen && $level == 1) {
         return $this->generateMultiprices($user, $newprice, $newpricebase, $newvat, $newnpr, $newpsq);
     }
     if (!empty($newminprice) && $newminprice > $newprice) {
         $this->error = 'ErrorPriceCantBeLowerThanMinPrice';
         return -1;
     }
     if ($newprice != '' || $newprice == 0) {
         if ($newpricebase == 'TTC') {
             $price_ttc = price2num($newprice, 'MU');
             $price = price2num($newprice) / (1 + $newvat / 100);
             $price = price2num($price, 'MU');
             if ($newminprice != '' || $newminprice == 0) {
                 $price_min_ttc = price2num($newminprice, 'MU');
                 $price_min = price2num($newminprice) / (1 + $newvat / 100);
                 $price_min = price2num($price_min, 'MU');
             } else {
                 $price_min = 0;
                 $price_min_ttc = 0;
             }
         } else {
             $price = price2num($newprice, 'MU');
             $price_ttc = $newnpr != 1 ? price2num($newprice) * (1 + $newvat / 100) : $price;
             $price_ttc = price2num($price_ttc, 'MU');
             if ($newminprice != '' || $newminprice == 0) {
                 $price_min = price2num($newminprice, 'MU');
                 $price_min_ttc = price2num($newminprice) * (1 + $newvat / 100);
                 $price_min_ttc = price2num($price_min_ttc, 'MU');
                 //print 'X'.$newminprice.'-'.$price_min;
             } else {
                 $price_min = 0;
                 $price_min_ttc = 0;
             }
         }
         //print 'x'.$id.'-'.$newprice.'-'.$newpricebase.'-'.$price.'-'.$price_ttc.'-'.$price_min.'-'.$price_min_ttc;
         if (count($localtaxes_array) > 0) {
             $localtaxtype1 = $localtaxes_array['0'];
             $localtax1 = $localtaxes_array['1'];
             $localtaxtype2 = $localtaxes_array['2'];
             $localtax2 = $localtaxes_array['3'];
         } else {
             $localtaxtype1 = '0';
             $localtax1 = get_localtax($newvat, 1);
             $localtaxtype2 = '0';
             $localtax2 = get_localtax($newvat, 2);
         }
         if (empty($localtax1)) {
             $localtax1 = 0;
         }
         // If = '' then = 0
         if (empty($localtax2)) {
             $localtax2 = 0;
         }
         // If = '' then = 0
         $this->db->begin();
         // Ne pas mettre de quote sur les numeriques decimaux.
         // Ceci provoque des stockages avec arrondis en base au lieu des valeurs exactes.
         $sql = "UPDATE " . MAIN_DB_PREFIX . "product SET";
         $sql .= " price_base_type='" . $newpricebase . "',";
         $sql .= " price=" . $price . ",";
         $sql .= " price_ttc=" . $price_ttc . ",";
         $sql .= " price_min=" . $price_min . ",";
         $sql .= " price_min_ttc=" . $price_min_ttc . ",";
         $sql .= " localtax1_tx=" . ($localtax1 >= 0 ? $localtax1 : 'NULL') . ",";
         $sql .= " localtax2_tx=" . ($localtax2 >= 0 ? $localtax2 : 'NULL') . ",";
         $sql .= " localtax1_type=" . ($localtaxtype1 != '' ? "'" . $localtaxtype1 . "'" : "'0'") . ",";
         $sql .= " localtax2_type=" . ($localtaxtype2 != '' ? "'" . $localtaxtype2 . "'" : "'0'") . ",";
         $sql .= " tva_tx='" . price2num($newvat) . "',";
         $sql .= " recuperableonly='" . $newnpr . "'";
         $sql .= " WHERE rowid = " . $id;
         dol_syslog(get_class($this) . "::update_price", LOG_DEBUG);
         $resql = $this->db->query($sql);
         if ($resql) {
             $this->multiprices[$level] = $price;
             $this->multiprices_ttc[$level] = $price_ttc;
             $this->multiprices_min[$level] = $price_min;
             $this->multiprices_min_ttc[$level] = $price_min_ttc;
             $this->multiprices_base_type[$level] = $newpricebase;
             $this->multiprices_tva_tx[$level] = $newvat;
             $this->multiprices_recuperableonly[$level] = $newnpr;
             $this->price = $price;
             $this->price_ttc = $price_ttc;
             $this->price_min = $price_min;
             $this->price_min_ttc = $price_min_ttc;
             $this->price_base_type = $newpricebase;
             $this->tva_tx = $newvat;
             $this->tva_npr = $newnpr;
             //Local taxes
             $this->localtax1_tx = $localtax1;
             $this->localtax2_tx = $localtax2;
             $this->localtax1_type = $localtaxtype1;
             $this->localtax2_type = $localtaxtype2;
             // Price by quantity
             $this->price_by_qty = $newpsq;
             $this->_log_price($user, $level);
             // Save price for level into table product_price
             $this->level = $level;
             // Store level of price edited for trigger
             // Call trigger
             $result = $this->call_trigger('PRODUCT_PRICE_MODIFY', $user);
             if ($result < 0) {
                 $this->db->rollback();
                 return -1;
             }
             // End call triggers
             $this->db->commit();
         } else {
             $this->db->rollback();
             dol_print_error($this->db);
         }
     }
     return 1;
 }
Exemple #12
0
            $type = $product->type;

            $result=$facfou->addline($label, $product->fourn_pu, $tvatx, $localtax2tx, $localtax2tx ,$_POST['qty'], $idprod);
        }
        if ($idprod == -1)
        {
            // Quantity too low
            $langs->load("errors");
            $mesg='<div class="error">'.$langs->trans("ErrorQtyTooLowForThisSupplier").'</div>';
        }
    }
    else
    {
        $tauxtva = price2num($_POST['tauxtva']);
		$localtax1tx= get_localtax($tauxtva, 1, $societe);
		$localtax2tx= get_localtax($tauxtva, 2, $societe);

        if (! $_POST['label'])
        {
            $mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Label")).'</div>';
        }
        else
        {
            $type = $_POST["type"];
            if (! empty($_POST['amount']))
            {
                $ht = price2num($_POST['amount']);
                $price_base_type = 'HT';

                //$desc, $pu, $txtva, $qty, $fk_product=0, $remise_percent=0, $date_start='', $date_end='', $ventil=0, $info_bits='', $price_base_type='HT', $type=0)
                $result=$facfou->addline($_POST['label'], $ht, $tauxtva, $localtax1tx, $localtax2tx, $_POST['qty'], 0, 0, $datestart, $dateend, 0, 0, $price_base_type, $type);
Exemple #13
0
/**
 *  Get type and rate of localtaxes for a particular vat rate/country fo thirdparty
 *  TODO
 *  This function is also called to retrieve type for building PDF. Such call of function must be removed.
 *  Instead this function must be called when adding a line to get (array of localtax and type) and
 *  provide it to the function calcul_price_total.
 *
 *  @param	float	$vatrate			VAT Rate
 *  @param	int		$local              Number of localtax (1 or 2, or 0 to return 1 & 2)
 *  @param	Societe	$buyer         		Company object
 *  @param	Societe	$seller        		Company object
 *  @return	array    	  				array(localtax_type1(1-6 / 0 if not found), rate of localtax1, ...)
 */
function getLocalTaxesFromRate($vatrate, $local, $buyer, $seller)
{
    global $db;
    dol_syslog("getLocalTaxesFromRate vatrate=" . $vatrate . " local=" . $local);
    // Search local taxes
    $sql = "SELECT t.localtax1, t.localtax1_type, t.localtax2, t.localtax2_type, t.accountancy_code_sell, t.accountancy_code_buy";
    $sql .= " FROM " . MAIN_DB_PREFIX . "c_tva as t, " . MAIN_DB_PREFIX . "c_country as c";
    $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '" . $buyer->country_code . "'";
    $sql .= " AND t.taux = " . (double) $vatrate . " AND t.active = 1";
    $resql = $db->query($sql);
    if ($resql) {
        $obj = $db->fetch_object($resql);
        if ($local == 1) {
            if (!isOnlyOneLocalTax(1)) {
                return array($obj->localtax1_type, get_localtax($vatrate, $local, $buyer, $seller), $obj->accountancy_code_sell, $obj->accountancy_code_buy);
            } else {
                return array($obj->localtax1_type, $obj->localtax1, $obj->accountancy_code_sell, $obj->accountancy_code_buy);
            }
        } elseif ($local == 2) {
            if (!isOnlyOneLocalTax(2)) {
                return array($obj->localtax2_type, get_localtax($vatrate, $local, $buyer, $seller), $obj->accountancy_code_sell, $obj->accountancy_code_buy);
            } else {
                return array($obj->localtax2_type, $obj->localtax2, $obj->accountancy_code_sell, $obj->accountancy_code_buy);
            }
        } else {
            if (!isOnlyOneLocalTax(1)) {
                if (!isOnlyOneLocalTax(2)) {
                    return array($obj->localtax1_type, get_localtax($vatrate, 1, $buyer, $seller), $obj->localtax2_type, get_localtax($vatrate, 2, $buyer, $seller), $obj->accountancy_code_sell, $obj->accountancy_code_buy);
                } else {
                    return array($obj->localtax1_type, get_localtax($vatrate, 1, $buyer, $seller), $obj->localtax2_type, $obj->localtax2, $obj->accountancy_code_sell, $obj->accountancy_code_buy);
                }
            } else {
                if (!isOnlyOneLocalTax(2)) {
                    return array($obj->localtax1_type, $obj->localtax1, $obj->localtax2_type, get_localtax($vatrate, 2, $buyer, $seller), $obj->accountancy_code_sell, $obj->accountancy_code_buy);
                } else {
                    return array($obj->localtax1_type, $obj->localtax1, $obj->localtax2_type, $obj->localtax2, $obj->accountancy_code_sell, $obj->accountancy_code_buy);
                }
            }
        }
    }
    return 0;
}
Exemple #14
0
         $localtax2tx = get_localtax($tvatx, 2, $mysoc, $object->thirdparty);
         $remise_percent = GETPOST('remise_percent');
         $type = $product->type;
         $result = $object->addline($label, $product->fourn_pu, $tvatx, $localtax1tx, $localtax2tx, $_POST['qty'], $idprod, $remise_percent, '', '', 0, $npr);
     }
     if ($idprod == -1) {
         // Quantity too low
         $langs->load("errors");
         $mesg = '<div class="error">' . $langs->trans("ErrorQtyTooLowForThisSupplier") . '</div>';
     }
 } else {
     $npr = preg_match('/\\*/', $_POST['tauxtva']) ? 1 : 0;
     $tauxtva = str_replace('*', '', $_POST["tauxtva"]);
     $tauxtva = price2num($tauxtva);
     $localtax1tx = get_localtax($tauxtva, 1, $mysoc, $object->thirdparty);
     $localtax2tx = get_localtax($tauxtva, 2, $mysoc, $object->thirdparty);
     $remise_percent = GETPOST('remise_percent');
     if (!$_POST['dp_desc']) {
         $mesg = '<div class="error">' . $langs->trans("ErrorFieldRequired", $langs->transnoentities("Description")) . '</div>';
     } else {
         $type = $_POST["type"];
         if (!empty($_POST['amount'])) {
             $ht = price2num($_POST['amount']);
             $price_base_type = 'HT';
             //$desc, $pu, $txtva, $qty, $fk_product=0, $remise_percent=0, $date_start='', $date_end='', $ventil=0, $info_bits='', $price_base_type='HT', $type=0)
             $result = $object->addline($_POST['dp_desc'], $ht, $tauxtva, $localtax1tx, $localtax2tx, $_POST['qty'], 0, $remise_percent, $datestart, $dateend, 0, $npr, $price_base_type, $type);
         } else {
             $ttc = price2num($_POST['amountttc']);
             $ht = $ttc / (1 + $tauxtva / 100);
             $price_base_type = 'HT';
             $result = $object->addline($_POST['dp_desc'], $ht, $tauxtva, $localtax1tx, $localtax2tx, $_POST['qty'], 0, $remise_percent, $datestart, $dateend, 0, $npr, $price_base_type, $type);
Exemple #15
0
 }
 if ($thirpdartyid > 0) {
     $soc = new Societe($db);
     $result = $soc->fetch($thirpdartyid);
     if ($result <= 0) {
         dol_print_error($db, $soc->error);
         exit;
     }
     $desc = $object->description;
     $tva_tx = get_default_tva($mysoc, $soc, $object->id);
     $tva_npr = get_default_npr($mysoc, $soc, $object->id);
     if (empty($tva_tx)) {
         $tva_npr = 0;
     }
     $localtax1_tx = get_localtax($tva_tx, 1, $soc, $mysoc, $tva_npr);
     $localtax2_tx = get_localtax($tva_tx, 2, $soc, $mysoc, $tva_npr);
     $pu_ht = $object->price;
     $pu_ttc = $object->price_ttc;
     $price_base_type = $object->price_base_type;
     // If multiprice
     if ($conf->global->PRODUIT_MULTIPRICES && $soc->price_level) {
         $pu_ht = $object->multiprices[$soc->price_level];
         $pu_ttc = $object->multiprices_ttc[$soc->price_level];
         $price_base_type = $object->multiprices_base_type[$soc->price_level];
     } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
         require_once DOL_DOCUMENT_ROOT . '/product/class/productcustomerprice.class.php';
         $prodcustprice = new Productcustomerprice($db);
         $filter = array('t.fk_product' => $object->id, 't.fk_soc' => $soc->id);
         $result = $prodcustprice->fetch_all('', '', 0, 0, $filter);
         if ($result) {
             if (count($prodcustprice->lines) > 0) {
 /**
  * Adds a product to the order
  *
  * @param Commande $object Order object
  * @param Product $prod Product to add
  * @param int $qty Quantity of the product
  * @throws Exception
  */
 public static function addOrderLine(Commande $object, Product $prod, $qty)
 {
     global $db, $conf, $mysoc, $langs;
     require_once DOL_DOCUMENT_ROOT . '/core/lib/admin.lib.php';
     $tva_tx = get_default_tva($mysoc, $object->thirdparty, $prod->id);
     $tva_npr = get_default_npr($mysoc, $object->thirdparty, $prod->id);
     if (!empty($conf->global->PRODUIT_MULTIPRICES) && !empty($object->thirdparty->price_level)) {
         $pu_ht = $prod->multiprices[$object->thirdparty->price_level];
         $pu_ttc = $prod->multiprices_ttc[$object->thirdparty->price_level];
         $price_base_type = $prod->multiprices_base_type[$object->thirdparty->price_level];
         if (isset($prod->multiprices_tva_tx[$object->thirdparty->price_level])) {
             $tva_tx = $prod->multiprices_tva_tx[$object->thirdparty->price_level];
         }
         if (isset($prod->multiprices_recuperableonly[$object->thirdparty->price_level])) {
             $tva_npr = $prod->multiprices_recuperableonly[$object->thirdparty->price_level];
         }
     } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
         require_once DOL_DOCUMENT_ROOT . '/product/class/productcustomerprice.class.php';
         $prodcustprice = new Productcustomerprice($db);
         $filter = array('t.fk_product' => $prod->id, 't.fk_soc' => $object->thirdparty->id);
         $result = $prodcustprice->fetch_all('', '', 0, 0, $filter);
         if ($result >= 0) {
             if (count($prodcustprice->lines) > 0) {
                 $pu_ht = price($prodcustprice->lines[0]->price);
                 $pu_ttc = price($prodcustprice->lines[0]->price_ttc);
                 $price_base_type = $prodcustprice->lines[0]->price_base_type;
                 $prod->tva_tx = $prodcustprice->lines[0]->tva_tx;
             } else {
                 $pu_ht = $prod->price;
                 $pu_ttc = $prod->price_ttc;
                 $price_base_type = $prod->price_base_type;
             }
         } else {
             throw new Exception($prodcustprice->error);
         }
     } else {
         $pu_ht = $prod->price;
         $pu_ttc = $prod->price_ttc;
         $price_base_type = $prod->price_base_type;
     }
     // if price ht is forced (ie: calculated by margin rate and cost price)
     if (!empty($price_ht)) {
         $pu_ht = price2num($price_ht, 'MU');
         $pu_ttc = price2num($pu_ht * (1 + $tva_tx / 100), 'MU');
     } elseif ($tva_tx != $prod->tva_tx) {
         if ($price_base_type != 'HT') {
             $pu_ht = price2num($pu_ttc / (1 + $tva_tx / 100), 'MU');
         } else {
             $pu_ttc = price2num($pu_ht * (1 + $tva_tx / 100), 'MU');
         }
     }
     // Define output language
     if (!empty($conf->global->MAIN_MULTILANGS) && !empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE)) {
         $outputlangs = $langs;
         $newlang = '';
         if (empty($newlang) && GETPOST('lang_id')) {
             $newlang = GETPOST('lang_id');
         }
         if (empty($newlang)) {
             $newlang = $object->thirdparty->default_lang;
         }
         if (!empty($newlang)) {
             $outputlangs = new Translate("", $conf);
             $outputlangs->setDefaultLang($newlang);
         }
         $desc = !empty($prod->multilangs[$outputlangs->defaultlang]["description"]) ? $prod->multilangs[$outputlangs->defaultlang]["description"] : $prod->description;
     } else {
         $desc = $prod->description;
     }
     // Add custom code and origin country into description
     if (empty($conf->global->MAIN_PRODUCT_DISABLE_CUSTOMCOUNTRYCODE) && (!empty($prod->customcode) || !empty($prod->country_code))) {
         $tmptxt = '(';
         if (!empty($prod->customcode)) {
             $tmptxt .= $langs->transnoentitiesnoconv("CustomCode") . ': ' . $prod->customcode;
         }
         if (!empty($prod->customcode) && !empty($prod->country_code)) {
             $tmptxt .= ' - ';
         }
         if (!empty($prod->country_code)) {
             $tmptxt .= $langs->transnoentitiesnoconv("CountryOrigin") . ': ' . getCountry($prod->country_code, 0, $db, $langs, 0);
         }
         $tmptxt .= ')';
         $desc = dol_concatdesc($desc, $tmptxt);
     }
     //3.9.0 version added support for price units
     if (versioncompare(versiondolibarrarray(), array(3, 9, 0)) >= 0) {
         $fk_unit = $prod->fk_unit;
     } else {
         $fk_unit = null;
     }
     // Local Taxes
     $localtax1_tx = get_localtax($tva_tx, 1, $object->thirdparty);
     $localtax2_tx = get_localtax($tva_tx, 2, $object->thirdparty);
     $info_bits = 0;
     if ($tva_npr) {
         $info_bits |= 0x1;
     }
     //Percent remise
     if (!empty($object->thirdparty->remise_percent)) {
         $percent_remise = $object->thirdparty->remise_percent;
     } else {
         $percent_remise = 0;
     }
     // Insert line
     $result = $object->addline($desc, $pu_ht, $qty, $tva_tx, $localtax1_tx, $localtax2_tx, $prod->id, $percent_remise, $info_bits, 0, $price_base_type, $pu_ttc, '', '', $prod->type, -1, 0, 0, null, 0, '', 0, $fk_unit);
     if ($result < 0) {
         throw new Exception($langs->trans('ErrorAddOrderLine', $prod->ref));
     }
 }
 public static function getLocalTax($data)
 {
     global $db;
     require_once DOL_DOCUMENT_ROOT . "/core/lib/functions.lib.php";
     $customer = new Societe($db);
     $customer->fetch($data["customer"]);
     $localtax['1'] = get_localtax($data["tva"], 1, $customer);
     $localtax['2'] = get_localtax($data["tva"], 2, $customer);
     return $localtax;
 }
Exemple #18
0
 *	Mise a jour	d'une ligne	dans la	commande
 */
if ($action == 'updateligne' && $user->rights->fournisseur->commande->creer && $_POST['save'] == $langs->trans('Save')) {
    $product = new Product($db);
    if ($_POST["elrowid"]) {
        if ($product->fetch($_POST["elrowid"]) < 0) {
            dol_print_error($db);
        }
    }
    if ($object->fetch($id) < 0) {
        dol_print_error($db);
    }
    $societe = new Societe($db);
    $societe->fetch($object->socid);
    $localtax1_tx = get_localtax($_POST['tva_tx'], 1, $societe);
    $localtax2_tx = get_localtax($_POST['tva_tx'], 2, $societe);
    $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"] : $product->type);
    if ($result >= 0) {
        $outputlangs = $langs;
        if (!empty($_REQUEST['lang_id'])) {
            $outputlangs = new Translate("", $conf);
            $outputlangs->setDefaultLang($_REQUEST['lang_id']);
        }
        supplier_order_pdf_create($db, $object, $object->modelpdf, $outputlangs, GETPOST('hidedetails'), GETPOST('hidedesc'), GETPOST('hideref'));
    } else {
        dol_print_error($db, $object->error);
        exit;
    }
}
if ($action == 'confirm_deleteproductline' && $confirm == 'yes' && $user->rights->fournisseur->commande->creer) {
    $object->fetch($id);
Exemple #19
0
     }
 } else {
     if (GETPOST('price_ht') !== '' || GETPOST('price_ttc') !== '') {
         $pu_ht = price2num($price_ht, 'MU');
         $pu_ttc = price2num(GETPOST('price_ttc'), 'MU');
         $tva_npr = preg_match('/\\*/', $tva_tx) ? 1 : 0;
         $tva_tx = str_replace('*', '', $tva_tx);
         $label = GETPOST('product_label') ? GETPOST('product_label') : '';
         $desc = $product_desc;
         $type = GETPOST('type');
         $fk_unit = GETPOST('units', 'alpha');
         $tva_tx = price2num($tva_tx);
         // When vat is text input field
         // Local Taxes
         $localtax1_tx = get_localtax($tva_tx, 1, $mysoc, $object->thirdparty);
         $localtax2_tx = get_localtax($tva_tx, 2, $mysoc, $object->thirdparty);
         if (!empty($_POST['price_ht'])) {
             $ht = price2num($_POST['price_ht']);
             $price_base_type = 'HT';
         } else {
             $ttc = price2num($_POST['price_ttc']);
             $ht = $ttc / (1 + $tva_tx / 100);
             $price_base_type = 'HT';
         }
         $result = $object->addline($product_desc, $ht, $tva_tx, $localtax1_tx, $localtax2_tx, $qty, 0, $remise_percent, $date_start, $date_end, 0, $npr, $price_base_type, $type, -1, 0, $array_options, $fk_unit);
     }
 }
 //print "xx".$tva_tx; exit;
 if (!$error && $result > 0) {
     $db->commit();
     // Define output language
Exemple #20
0
 /**
  *	Create invoice in database
  *  Note: this->ref can be set or empty. If empty, we will use "(PROV)"
  *	@param     	user       		Object user that create
  *	@param      notrigger		1=Does not execute triggers, 0 otherwise
  * 	@param		forceduedate	1=Do not recalculate due date from payment condition but force it with value
  *	@return		int				<0 if KO, >0 if OK
  */
 function create($user, $notrigger = 0, $forceduedate = 0)
 {
     global $langs, $conf, $mysoc;
     $error = 0;
     // Clean parameters
     if (!$this->type) {
         $this->type = 0;
     }
     $this->ref_client = trim($this->ref_client);
     $this->note = trim($this->note);
     $this->note_public = trim($this->note_public);
     if (!$this->remise) {
         $this->remise = 0;
     }
     if (!$this->cond_reglement_id) {
         $this->cond_reglement_id = 0;
     }
     if (!$this->mode_reglement_id) {
         $this->mode_reglement_id = 0;
     }
     $this->brouillon = 1;
     dol_syslog("Facture::Create user="******"ErrorBadParameter";
         dol_syslog("Facture::create Try to create an invoice with an empty parameter (user, date, ...)", LOG_ERR);
         return -3;
     }
     $soc = new Societe($this->db);
     $result = $soc->fetch($this->socid);
     if ($result < 0) {
         $this->error = "Failed to fetch company";
         dol_syslog("Facture::create " . $this->error, LOG_ERR);
         return -2;
     }
     $now = dol_now();
     $this->db->begin();
     // Create invoice from a predefined invoice
     if ($this->fac_rec > 0) {
         require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture-rec.class.php';
         $_facrec = new FactureRec($this->db);
         $result = $_facrec->fetch($this->fac_rec);
         $this->fk_project = $_facrec->fk_project;
         $this->cond_reglement = $_facrec->cond_reglement_id;
         $this->cond_reglement_id = $_facrec->cond_reglement_id;
         $this->mode_reglement = $_facrec->mode_reglement_id;
         $this->mode_reglement_id = $_facrec->mode_reglement_id;
         $this->amount = $_facrec->amount;
         $this->remise_absolue = $_facrec->remise_absolue;
         $this->remise_percent = $_facrec->remise_percent;
         $this->remise = $_facrec->remise;
         // Clean parametres
         if (!$this->type) {
             $this->type = 0;
         }
         $this->ref_client = trim($this->ref_client);
         $this->note = trim($this->note);
         $this->note_public = trim($this->note_public);
         if (!$this->remise) {
             $this->remise = 0;
         }
         if (!$this->mode_reglement_id) {
             $this->mode_reglement_id = 0;
         }
         $this->brouillon = 1;
     }
     // Define due date if not already defined
     $datelim = empty($forceduedate) ? $this->calculate_date_lim_reglement() : $forceduedate;
     // Insert into database
     $socid = $this->socid;
     $amount = $this->amount;
     $remise = $this->remise;
     $totalht = $amount - $remise;
     $sql = "INSERT INTO " . MAIN_DB_PREFIX . "facture (";
     $sql .= " facnumber";
     $sql .= ", entity";
     $sql .= ", type";
     $sql .= ", fk_soc";
     $sql .= ", datec";
     $sql .= ", amount";
     $sql .= ", remise_absolue";
     $sql .= ", remise_percent";
     $sql .= ", datef";
     $sql .= ", note";
     $sql .= ", note_public";
     $sql .= ", ref_client, ref_int";
     $sql .= ", fk_facture_source, fk_user_author, fk_projet";
     $sql .= ", fk_cond_reglement, fk_mode_reglement, date_lim_reglement, model_pdf";
     $sql .= ")";
     $sql .= " VALUES (";
     $sql .= "'(PROV)'";
     $sql .= ", " . $conf->entity;
     $sql .= ", '" . $this->type . "'";
     $sql .= ", '" . $socid . "'";
     $sql .= ", '" . $this->db->idate($now) . "'";
     $sql .= ", '" . $totalht . "'";
     $sql .= "," . ($this->remise_absolue > 0 ? $this->remise_absolue : 'NULL');
     $sql .= "," . ($this->remise_percent > 0 ? $this->remise_percent : 'NULL');
     $sql .= ", '" . $this->db->idate($this->date) . "'";
     $sql .= "," . ($this->note ? "'" . $this->db->escape($this->note) . "'" : "null");
     $sql .= "," . ($this->note_public ? "'" . $this->db->escape($this->note_public) . "'" : "null");
     $sql .= "," . ($this->ref_client ? "'" . $this->db->escape($this->ref_client) . "'" : "null");
     $sql .= "," . ($this->ref_int ? "'" . $this->db->escape($this->ref_int) . "'" : "null");
     $sql .= "," . ($this->fk_facture_source ? "'" . $this->db->escape($this->fk_facture_source) . "'" : "null");
     $sql .= "," . ($user->id > 0 ? "'" . $user->id . "'" : "null");
     $sql .= "," . ($this->fk_project ? $this->fk_project : "null");
     $sql .= ',' . $this->cond_reglement_id;
     $sql .= "," . $this->mode_reglement_id;
     $sql .= ", '" . $this->db->idate($datelim) . "', '" . $this->modelpdf . "')";
     dol_syslog("Facture::Create sql=" . $sql);
     $resql = $this->db->query($sql);
     if ($resql) {
         $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'facture');
         // Update ref with new one
         $this->ref = '(PROV' . $this->id . ')';
         $sql = 'UPDATE ' . MAIN_DB_PREFIX . "facture SET facnumber='" . $this->ref . "' WHERE rowid=" . $this->id;
         dol_syslog("Facture::create sql=" . $sql);
         $resql = $this->db->query($sql);
         if (!$resql) {
             $error++;
         }
         // Add object linked
         if (!$error && $this->id && $this->origin && $this->origin_id) {
             $ret = $this->add_object_linked();
             if (!$ret) {
                 dol_print_error($this->db);
                 $error++;
             }
         }
         /*
          *  Insert lines of invoices into database
          */
         if (sizeof($this->lines) && is_object($this->lines[0])) {
             $fk_parent_line = 0;
             dol_syslog("There is " . sizeof($this->lines) . " lines that are invoice lines objects");
             foreach ($this->lines as $i => $val) {
                 $newinvoiceline = new FactureLigne($this->db);
                 $newinvoiceline = $this->lines[$i];
                 $newinvoiceline->fk_facture = $this->id;
                 if ($result >= 0 && ($newinvoiceline->info_bits & 0x1) == 0) {
                     // Reset fk_parent_line for no child products and special product
                     if ($newinvoiceline->product_type != 9 && empty($newinvoiceline->fk_parent_line) || $newinvoiceline->product_type == 9) {
                         $fk_parent_line = 0;
                     }
                     $newinvoiceline->fk_parent_line = $fk_parent_line;
                     $result = $newinvoiceline->insert();
                     // Defined the new fk_parent_line
                     if ($result > 0 && $newinvoiceline->product_type == 9) {
                         $fk_parent_line = $result;
                     }
                 }
                 if ($result < 0) {
                     $this->error = $newinvoiceline->error;
                     $error++;
                     break;
                 }
             }
         } else {
             $fk_parent_line = 0;
             dol_syslog("There is " . sizeof($this->lines) . " lines that are array lines");
             foreach ($this->lines as $i => $val) {
                 if (($this->lines[$i]->info_bits & 0x1) == 0) {
                     // Reset fk_parent_line for no child products and special product
                     if ($this->lines[$i]->product_type != 9 && empty($this->lines[$i]->fk_parent_line) || $this->lines[$i]->product_type == 9) {
                         $fk_parent_line = 0;
                     }
                     $result = $this->addline($this->id, $this->lines[$i]->desc, $this->lines[$i]->subprice, $this->lines[$i]->qty, $this->lines[$i]->tva_tx, $this->lines[$i]->localtax1_tx, $this->lines[$i]->localtax2_tx, $this->lines[$i]->fk_product, $this->lines[$i]->remise_percent, $this->lines[$i]->date_start, $this->lines[$i]->date_end, $this->lines[$i]->fk_code_ventilation, $this->lines[$i]->info_bits, $this->lines[$i]->fk_remise_except, 'HT', 0, $this->lines[$i]->product_type, $this->lines[$i]->rang, $this->lines[$i]->special_code, '', 0, $fk_parent_line);
                     if ($result < 0) {
                         $this->error = $this->db->lasterror();
                         dol_print_error($this->db);
                         $this->db->rollback();
                         return -1;
                     }
                     // Defined the new fk_parent_line
                     if ($result > 0 && $this->lines[$i]->product_type == 9) {
                         $fk_parent_line = $result;
                     }
                 }
             }
         }
         /*
          * Insert lines of predefined invoices
          */
         if (!$error && $this->fac_rec > 0) {
             foreach ($_facrec->lines as $i => $val) {
                 if ($_facrec->lines[$i]->fk_product) {
                     $prod = new Product($this->db, $_facrec->lines[$i]->fk_product);
                     $res = $prod->fetch($_facrec->lines[$i]->fk_product);
                 }
                 $tva_tx = get_default_tva($mysoc, $soc, $prod->id);
                 $localtax1_tx = get_localtax($tva_tx, 1, $soc);
                 $localtax2_tx = get_localtax($tva_tx, 2, $soc);
                 $result_insert = $this->addline($this->id, $_facrec->lines[$i]->desc, $_facrec->lines[$i]->subprice, $_facrec->lines[$i]->qty, $tva_tx, $localtax1_tx, $localtax2_tx, $_facrec->lines[$i]->fk_product, $_facrec->lines[$i]->remise_percent, '', '', 0, 0, '', 'HT', 0, $_facrec->lines[$i]->product_type, $_facrec->lines[$i]->rang, $_facrec->lines[$i]->special_code);
                 if ($result_insert < 0) {
                     $error++;
                     $this->error = $this->db->error();
                     break;
                 }
             }
         }
         if (!$error) {
             $result = $this->update_price(1);
             if ($result > 0) {
                 // Appel des triggers
                 include_once DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php";
                 $interface = new Interfaces($this->db);
                 $result = $interface->run_triggers('BILL_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 -4;
                 }
             } else {
                 $this->error = $langs->trans('FailedToUpdatePrice');
                 $this->db->rollback();
                 return -3;
             }
         } else {
             dol_syslog("Facture::create error " . $this->error, LOG_ERR);
             $this->db->rollback();
             return -2;
         }
     } else {
         $this->error = $this->db->error();
         dol_syslog("Facture::create error " . $this->error . " sql=" . $sql, LOG_ERR);
         $this->db->rollback();
         return -1;
     }
 }
Exemple #21
0
             setEventMessage($object->error, 'errors');
         }
     }
 } else {
     if ($action == 'updateligne' && $user->rights->contrat->creer && !GETPOST('cancel')) {
         $objectline = new ContratLigne($db);
         if ($objectline->fetch(GETPOST('elrowid'))) {
             $db->begin();
             if ($date_start_real_update == '') {
                 $date_start_real_update = $objectline->date_ouverture;
             }
             if ($date_end_real_update == '') {
                 $date_end_real_update = $objectline->date_cloture;
             }
             $localtax1_tx = get_localtax(GETPOST('eltva_tx'), 1, $object->thirdparty);
             $localtax2_tx = get_localtax(GETPOST('eltva_tx'), 2, $object->thirdparty);
             // ajout prix d'achat
             $fk_fournprice = $_POST['fournprice'];
             if (!empty($_POST['buying_price'])) {
                 $pa_ht = $_POST['buying_price'];
             } else {
                 $pa_ht = null;
             }
             $fk_unit = GETPOST('unit', 'alpha');
             $objectline->description = GETPOST('product_desc');
             $objectline->price_ht = GETPOST('elprice');
             $objectline->subprice = GETPOST('elprice');
             $objectline->qty = GETPOST('elqty');
             $objectline->remise_percent = GETPOST('elremise_percent');
             $objectline->tva_tx = GETPOST('eltva_tx') ? GETPOST('eltva_tx') : 0;
             // Field may be disabled, so we use vat rate 0
             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']);
         unset($_POST['tva_tx']);
         unset($localtax1_tx);
         unset($localtax2_tx);
         if ($result >= 0) {
             $outputlangs = $langs;
             if (GETPOST('lang_id')) {
                 $outputlangs = new Translate("", $conf);
Exemple #23
0
 $date_end = '';
 $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'));
 $description = dol_htmlcleanlastbr(GETPOST('product_desc'));
 $pu_ht = GETPOST('price_ht');
 $vat_rate = GETPOST('tva_tx') ? GETPOST('tva_tx') : 0;
 $qty = GETPOST('qty');
 // Define info_bits
 $info_bits = 0;
 if (preg_match('/\\*/', $vat_rate)) {
     $info_bits |= 0x1;
 }
 // Define vat_rate
 $vat_rate = str_replace('*', '', $vat_rate);
 $localtax1_rate = get_localtax($vat_rate, 1, $object->thirdparty);
 $localtax2_rate = get_localtax($vat_rate, 2, $object->thirdparty);
 // Add buying price
 $fournprice = price2num(GETPOST('fournprice') ? GETPOST('fournprice') : '');
 $buyingprice = price2num(GETPOST('buying_price') != '' ? GETPOST('buying_price') : '');
 // If buying_price is '0', we muste keep this value
 // Extrafields
 $extrafieldsline = new ExtraFields($db);
 $extralabelsline = $extrafieldsline->fetch_name_optionals_label($object->table_element_line);
 $array_options = $extrafieldsline->getOptionalsFromPost($extralabelsline);
 // Unset extrafield
 if (is_array($extralabelsline)) {
     // Get extra fields
     foreach ($extralabelsline as $key => $value) {
         unset($_POST["options_" . $key]);
     }
 }
Exemple #24
0
         $label = $product->description;
         $tvatx = get_default_tva($facfou->thirdparty, $mysoc, $product->id);
         $localtax1tx = get_localtax($tvatx, 1, $mysoc);
         $localtax2tx = get_localtax($tvatx, 2, $mysoc);
         $type = $product->type;
         $result = $facfou->addline($label, $product->fourn_pu, $tvatx, $localtax1tx, $localtax2tx, $_POST['qty'], $idprod);
     }
     if ($idprod == -1) {
         // Quantity too low
         $langs->load("errors");
         $mesg = '<div class="error">' . $langs->trans("ErrorQtyTooLowForThisSupplier") . '</div>';
     }
 } else {
     $tauxtva = price2num($_POST['tauxtva']);
     $localtax1tx = get_localtax($tauxtva, 1, $facfou->thirdparty);
     $localtax2tx = get_localtax($tauxtva, 2, $facfou->thirdparty);
     if (!$_POST['label']) {
         $mesg = '<div class="error">' . $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label")) . '</div>';
     } else {
         $type = $_POST["type"];
         if (!empty($_POST['amount'])) {
             $ht = price2num($_POST['amount']);
             $price_base_type = 'HT';
             //$desc, $pu, $txtva, $qty, $fk_product=0, $remise_percent=0, $date_start='', $date_end='', $ventil=0, $info_bits='', $price_base_type='HT', $type=0)
             $result = $facfou->addline($_POST['label'], $ht, $tauxtva, $localtax1tx, $localtax2tx, $_POST['qty'], 0, 0, $datestart, $dateend, 0, 0, $price_base_type, $type);
         } else {
             $ttc = price2num($_POST['amountttc']);
             $ht = $ttc / (1 + $tauxtva / 100);
             $price_base_type = 'HT';
             $result = $facfou->addline($_POST['label'], $ht, $tauxtva, $localtax1tx, $localtax2tx, $_POST['qty'], 0, 0, $datestart, $dateend, 0, 0, $price_base_type, $type);
         }