Move common code to get sell price into a method

This commit is contained in:
Laurent Destailleur 2019-01-24 19:45:17 +01:00
parent 2c8557e2e0
commit a5daa25707
4 changed files with 152 additions and 117 deletions

View File

@ -1775,105 +1775,18 @@ if (empty($reshook))
$label = ((GETPOST('product_label') && GETPOST('product_label') != $prod->label) ? GETPOST('product_label') : '');
// Update if prices fields are defined
$tva_tx = get_default_tva($mysoc, $object->thirdparty, $prod->id);
$tva_npr = get_default_npr($mysoc, $object->thirdparty, $prod->id);
if (empty($tva_tx)) $tva_npr=0;
$pu_ht = $prod->price;
$pu_ttc = $prod->price_ttc;
$price_min = $prod->price_min;
$price_base_type = $prod->price_base_type;
// If price per segment
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_min = $prod->multiprices_min[$object->thirdparty->price_level];
$price_base_type = $prod->multiprices_base_type[$object->thirdparty->price_level];
if (! empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) // using this option is a bug. kept for backward compatibility
{
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];
if (empty($tva_tx)) $tva_npr=0;
}
}
// If price per customer
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) {
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;
$tva_tx = $prodcustprice->lines[0]->tva_tx;
if ($prodcustprice->lines[0]->default_vat_code && ! preg_match('/\(.*\)/', $tva_tx)) $tva_tx.= ' ('.$prodcustprice->lines[0]->default_vat_code.')';
$tva_npr = $prodcustprice->lines[0]->recuperableonly;
if (empty($tva_tx)) $tva_npr=0;
}
}
}
// If price per quantity
elseif (! empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY))
{
if ($prod->prices_by_qty[0]) // yes, this product has some prices per quantity
{
// Search the correct price into loaded array product_price_by_qty using id of array retrieved into POST['pqp'].
$pqp = GETPOST('pbq','int');
// Search price into product_price_by_qty from $prod->id
foreach($prod->prices_by_qty_list[0] as $priceforthequantityarray)
{
if ($priceforthequantityarray['rowid'] != $pqp) continue;
// We found the price
if ($priceforthequantityarray['price_base_type'] == 'HT')
{
$pu_ht = $priceforthequantityarray['unitprice'];
}
else
{
$pu_ttc = $priceforthequantityarray['unitprice'];
}
// Note: the remise_percent or price by qty is used to set data on form, so we will use value from POST.
break;
}
}
}
// If price per quantity and customer
elseif (! empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES))
{
if ($prod->prices_by_qty[$object->thirdparty->price_level]) // yes, this product has some prices per quantity
{
// Search the correct price into loaded array product_price_by_qty using id of array retrieved into POST['pqp'].
$pqp = GETPOST('pbq','int');
// Search price into product_price_by_qty from $prod->id
foreach($prod->prices_by_qty_list[$object->thirdparty->price_level] as $priceforthequantityarray)
{
if ($priceforthequantityarray['rowid'] != $pqp) continue;
// We found the price
if ($priceforthequantityarray['price_base_type'] == 'HT')
{
$pu_ht = $priceforthequantityarray['unitprice'];
}
else
{
$pu_ttc = $priceforthequantityarray['unitprice'];
}
// Note: the remise_percent or price by qty is used to set data on form, so we will use value from POST.
break;
}
}
}
// Search the correct price into loaded array product_price_by_qty using id of array retrieved into POST['pqp'].
$pqp = (GETPOST('pbq','int') ? GETPOST('pbq','int') : 0);
$datapriceofproduct = $prod->getSellPrice($mysoc, $object->thirdparty, $pqp);
$pu_ht = $datapriceofproduct['pu_ht'];
$pu_ttc = $datapriceofproduct['pu_ttc'];
$price_min = $datapriceofproduct['price_min'];
$price_base_type = $datapriceofproduct['price_base_type'];
$tva_tx = $datapriceofproduct['tva_tx'];
$tva_npr = $datapriceofproduct['tva_npr'];
$tmpvat = price2num(preg_replace('/\s*\(.*\)/', '', $tva_tx));
$tmpprodvat = price2num(preg_replace('/\s*\(.*\)/', '', $prod->tva_tx));

View File

@ -1526,12 +1526,119 @@ class Product extends CommonObject
}
}
/**
* Return price of sell of a product for a seller/buyer/product.
*
* @param Societe $thirdparty_seller Seller
* @param Societe $thirdparty_buyer Buyer
* @param int $pqp Id of product per price if a selection was done of such a price
* @return array Array of price information
* @see get_buyprice()
*/
function getSellPrice($thirdparty_seller, $thirdparty_buyer, $pqp=0)
{
global $conf, $db;
// Update if prices fields are defined
$tva_tx = get_default_tva($thirdparty_seller, $thirdparty_buyer, $this->id);
$tva_npr = get_default_npr($thirdparty_seller, $thirdparty_buyer, $this->id);
if (empty($tva_tx)) $tva_npr=0;
$pu_ht = $this->price;
$pu_ttc = $this->price_ttc;
$price_min = $this->price_min;
$price_base_type = $this->price_base_type;
// If price per segment
if (! empty($conf->global->PRODUIT_MULTIPRICES) && ! empty($thirdparty_buyer->price_level))
{
$pu_ht = $this->multiprices[$thirdparty_buyer->price_level];
$pu_ttc = $this->multiprices_ttc[$thirdparty_buyer->price_level];
$price_min = $this->multiprices_min[$thirdparty_buyer->price_level];
$price_base_type = $this->multiprices_base_type[$thirdparty_buyer->price_level];
if (! empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) // using this option is a bug. kept for backward compatibility
{
if (isset($this->multiprices_tva_tx[$thirdparty_buyer->price_level])) $tva_tx=$this->multiprices_tva_tx[$thirdparty_buyer->price_level];
if (isset($this->multiprices_recuperableonly[$thirdparty_buyer->price_level])) $tva_npr=$this->multiprices_recuperableonly[$thirdparty_buyer->price_level];
if (empty($tva_tx)) $tva_npr=0;
}
}
// If price per customer
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' => $this->id,'t.fk_soc' => $thirdparty_buyer->id);
$result = $prodcustprice->fetch_all('', '', 0, 0, $filter);
if ($result) {
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;
$tva_tx = $prodcustprice->lines[0]->tva_tx;
if ($prodcustprice->lines[0]->default_vat_code && ! preg_match('/\(.*\)/', $tva_tx)) $tva_tx.= ' ('.$prodcustprice->lines[0]->default_vat_code.')';
$tva_npr = $prodcustprice->lines[0]->recuperableonly;
if (empty($tva_tx)) $tva_npr=0;
}
}
}
// If price per quantity
elseif (! empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY))
{
if ($this->prices_by_qty[0]) // yes, this product has some prices per quantity
{
// Search price into product_price_by_qty from $this->id
foreach($this->prices_by_qty_list[0] as $priceforthequantityarray)
{
if ($priceforthequantityarray['rowid'] != $pqp) continue;
// We found the price
if ($priceforthequantityarray['price_base_type'] == 'HT')
{
$pu_ht = $priceforthequantityarray['unitprice'];
}
else
{
$pu_ttc = $priceforthequantityarray['unitprice'];
}
break;
}
}
}
// If price per quantity and customer
elseif (! empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES))
{
if ($this->prices_by_qty[$thirdparty_buyer->price_level]) // yes, this product has some prices per quantity
{
// Search price into product_price_by_qty from $this->id
foreach($this->prices_by_qty_list[$thirdparty_buyer->price_level] as $priceforthequantityarray)
{
if ($priceforthequantityarray['rowid'] != $pqp) continue;
// We found the price
if ($priceforthequantityarray['price_base_type'] == 'HT')
{
$pu_ht = $priceforthequantityarray['unitprice'];
}
else
{
$pu_ttc = $priceforthequantityarray['unitprice'];
}
break;
}
}
}
return array('pu_ht'=>$pu_ht, 'pu_ttc'=>$pu_ttc, 'price_min'=>$price_min, 'price_base_type'=>$price_base_type, 'tva_tx'=>$tva_tx, 'tva_npr'=>$tva_npr);
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
/**
* Read price used by a provider.
* We enter as input couple prodfournprice/qty or triplet qty/product_id/fourn_ref.
* This also set some properties on product like ->buyprice, ->fourn_pu, ...
* Read price used by a provider.
* We enter as input couple prodfournprice/qty or triplet qty/product_id/fourn_ref.
* This also set some properties on product like ->buyprice, ->fourn_pu, ...
*
* @param int $prodfournprice Id du tarif = rowid table product_fournisseur_price
* @param double $qty Quantity asked or -1 to get first entry found
@ -1539,6 +1646,7 @@ class Product extends CommonObject
* @param string $fourn_ref Filter on a supplier price ref. 'none' to exclude ref in search.
* @param int $fk_soc If of supplier
* @return int <-1 if KO, -1 if qty not enough, 0 if OK but nothing found, id_product if OK and found. May also initialize some properties like (->ref_supplier, buyprice, fourn_pu, vatrate_supplier...)
* @see getSellPrice()
*/
function get_buyprice($prodfournprice, $qty, $product_id=0, $fourn_ref='', $fk_soc=0)
{

View File

@ -1829,7 +1829,7 @@ if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES))
elseif ($action == 'showlog_customer_price')
{
// List of all log of prices by customers
print '<!-- list of all lof of prices per customer -->'."\n";
print '<!-- list of all log of prices per customer -->'."\n";
$filter = array('t.fk_product' => $object->id,'t.fk_soc' => GETPOST('socid', 'int'));
@ -1852,7 +1852,7 @@ if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES))
$title=$langs->trans('PriceByCustomerLog');
$title.=' - '.$staticsoc->getNomUrl(1);
$backbutton='<a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '">' . $langs->trans("Back") . '</a>';
$backbutton='<a class="justalink" href="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '">' . $langs->trans("Back") . '</a>';
print_barre_liste($title, $page, $_SERVEUR['PHP_SELF'], $option, $sortfield, $sortorder, $backbutton, count($prodcustprice->lines), $nbtotalofrecords, 'title_accountancy.png');

View File

@ -313,35 +313,41 @@ if ($massaction == 'generateinvoice')
else
{
include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
include_once DOL_DOCUMENT_ROOT.'/project/class/projet.class.php';
include_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
$tmpinvoice = new Facture($db);
$tmptimespent=new Task();
$tmptimespent=new Task($db);
$tmpproduct=new Product($db);
$fuser = new User($db);
$idprod = 0;
$db->begin();
$idprod = GETPOST('idprod', 'int');
if ($idprod > 0)
{
$tmpproduct->fetch($idprod);
}
$txtva = get_default_tva($mysoc, $projectstatic->thirdparty, $idprod);
$dataforprice = $tmpproduct->getSellPrice($mysoc, $projectstatic->thirdparty, 0);
$pu_ht = $dataforprice['pu_ht'];
$txtva = $dataforprice['tva_tx'];
$tmpinvoice->fk_soc = $projectstatic->thirdparty->id;
$tmpinvoice->create($user);
$arrayoftasks=array();
$totaltimespent = 0;
foreach($toselect as $key => $value)
{
// Get userid, timepent
//$object->fetchTimeSpent(GETPOST('lineid','int'));
$object->fetchTimeSpent($value);
$arrayoftasks[$userid]['timespent']+=$timespent;
$arrayoftasks[$object->timespent_fk_user]['timespent']+=$object->timespent_duration;
}
foreach($arrayoftasks as $userid => $value)
{
$fuser->fetch($userid);
$pu_ht = $value['timespent'] * $fuser->thm;
//$pu_ht = $value['timespent'] * $fuser->thm;
$username = $fuser->getFullName($langs);
// Add lines
@ -349,8 +355,16 @@ if ($massaction == 'generateinvoice')
}
setEventMessages($langs->trans("InvoiceGeneratedFromTimeSpent", $tmpinvoice->ref), null, 'mesgs');
exit;
//var_dump($tmpinvoice);
if (! $error)
{
$db->commit();
}
else
{
$db->rollback();
}
}
}