$sql="SELECT f.facnumber, f.rowid as facid, l.fk_product, l.description, l.total_ht, l.rowid, l.fk_code_ventilation,";
$sql.=" p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.fk_product_type as type, p.accountancy_code_sell as code_sell";
// A REVOIR elarifr si vraiment necessaire de rajouter , p.fk_product_type as type. le type produit / service est de facto defini pour chaque ligne de facturedet.product_type
// il est donc plus logique de se servir de l.product_type au lieu de p.fk_product_type
$sql.=" , aa.rowid as aarowid";
// we need f.datef to reorder lines
$sql.=" , f.datef";
// we need to use llx_facturedet l.product_type as used at the time on invoice. if llx_product fk_product_type is changed later it could not change the sell already made !
$sql.=" , l.product_type as type_l";
$sql.=" FROM ".MAIN_DB_PREFIX."facture as f";
$sql.=" INNER JOIN ".MAIN_DB_PREFIX."facturedet as l ON f.rowid = l.fk_facture";
$sql.=" LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = l.fk_product";
...
...
@@ -131,14 +197,27 @@ $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accountingaccount as aa ON p.accountan
$sql.=" LEFT JOIN ".MAIN_DB_PREFIX."accounting_system as accsys ON accsys.pcg_version = aa.fk_pcg_version";
$sql.=" WHERE f.fk_statut > 0 AND fk_code_ventilation <= 0";
$sql.=" AND (accsys.rowid='".$conf->global->CHARTOFACCOUNTS."' OR p.accountancy_code_sell IS NULL OR p.accountancy_code_sell ='')";
// Add search filter like
if(strlen(trim($search_ref))){
$sql.=" AND (p.ref like '%".$search_ref."%')";
}
if(strlen(trim($search_label))){
$sql.=" AND (p.label like '%".$search_label."%')";
}
if(strlen(trim($search_desc))){
$sql.=" AND (l.description like '%".$search_desc."%')";
}
if(!empty($conf->multicompany->enabled)){
$sql.=" AND f.entity IN (".getEntity("facture",1).")";
// C'est le contraire dans les base !!!!!! IT IS INVERTED IN LLX_PRODUCT & LLX_FACTUREDET
// elarifr define account numbercode comptable si pas defini dans la fiche produit a partir des lignes de facturation
// product_type: 1 = service ? 0 = product
// because some modules like subtotal module l.product_type can be other than 0 or 1 ! and we don't put in account lines with product_type=9
// first we check product.fk_product_type as type and l.product_type as type_l
// if product does not exist we use the value of l.product_type provided in facturedet to define if this is a product or service
// issue : if we change product_type value in product DB it should differ from the value stored in facturedet DB ! so we report both and user make choice of accounting account.
$objp->code_sell_l='';
$objp->code_sell_p='';
$objp->aarowid_suggest='';
$code_sell_p_l_differ='';
//check if code_sell defined in product or set default value according p.fk_product_type do not care lines when product_type value not 0 || 1
//and we set suggested accounting account rowid as $objp->aarowid_s