--- /tmp/dsg/dolibarr/htdocs/margin/github_19.0.3_agentMargins.php +++ /tmp/dsg/dolibarr/htdocs/margin/client_agentMargins.php @@ -17 +17 @@ - * along with this program. If not, see . + * along with this program. If not, see . @@ -26 +25,0 @@ -// Load Dolibarr environment @@ -33,20 +32,8 @@ -// Load translation files required by the page -$langs->loadLangs(array('companies', 'bills', 'products', 'margins')); - -$mesg = ''; - -// Load variable for pagination -$limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit; -$sortfield = GETPOST('sortfield', 'aZ09comma'); -$sortorder = GETPOST('sortorder', 'aZ09comma'); -$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int'); -if (empty($page) || $page == -1) { - $page = 0; -} // If $page is not defined, or '' or -1 -$offset = $limit * $page; -$pageprev = $page - 1; -$pagenext = $page + 1; -if (!$sortorder) { - $sortorder = "ASC"; -} -if ($user->hasRight('margins', 'read', 'all')) { +$langs->load("companies"); +$langs->load("bills"); +$langs->load("products"); +$langs->load("margins"); + +// Security check + +if ($user->rights->margins->read->all) { @@ -57,38 +44,25 @@ -if (!$sortfield) { - if ($agentid > 0) { - $sortfield = "s.nom"; - } else { - $sortfield = "u.lastname"; - } -} - -$startdate = $enddate = ''; - -$startdateday = GETPOST('startdateday', 'int'); -$startdatemonth = GETPOST('startdatemonth', 'int'); -$startdateyear = GETPOST('startdateyear', 'int'); -$enddateday = GETPOST('enddateday', 'int'); -$enddatemonth = GETPOST('enddatemonth', 'int'); -$enddateyear = GETPOST('enddateyear', 'int'); - -if (!empty($startdatemonth)) { - $startdate = dol_mktime(0, 0, 0, $startdatemonth, $startdateday, $startdateyear); -} -if (!empty($enddatemonth)) { - $enddate = dol_mktime(23, 59, 59, $enddatemonth, $enddateday, $enddateyear); -} - -// Security check -$result = restrictedArea($user, 'margins'); - -// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context -$object = new User($db); -$hookmanager->initHooks(array('marginagentlist')); - -/* - * Actions - */ - -// None - - + +$mesg = ''; + +$sortfield = GETPOST("sortfield",'alpha'); +$sortorder = GETPOST("sortorder",'alpha'); +if (! $sortorder) $sortorder="ASC"; +if (! $sortfield) +{ + if ($agentid > 0) + $sortfield="s.nom"; + else + $sortfield="u.lastname"; +} +$page = GETPOST("page",'int'); +if ($page == -1) { $page = 0; } +$offset = $conf->liste_limit * $page; +$pageprev = $page - 1; +$pagenext = $page + 1; + +$startdate=$enddate=''; + +if (!empty($_POST['startdatemonth'])) + $startdate = dol_mktime(0, 0, 0, $_POST['startdatemonth'], $_POST['startdateday'], $_POST['startdateyear']); +if (!empty($_POST['enddatemonth'])) + $enddate = dol_mktime(23, 59, 59, $_POST['enddatemonth'], $_POST['enddateday'], $_POST['enddateyear']); @@ -102 +76 @@ -$invoicestatic = new Facture($db); +$invoicestatic=new Facture($db); @@ -106,4 +80,4 @@ -llxHeader('', $langs->trans("Margins").' - '.$langs->trans("Agents")); - -$text = $langs->trans("Margins"); -//print load_fiche_titre($text); +llxHeader('',$langs->trans("Margins").' - '.$langs->trans("Agents")); + +$text=$langs->trans("Margins"); +//print_fiche_titre($text); @@ -112,4 +86,4 @@ -$head = marges_prepare_head(); - -$titre = $langs->trans("Margins"); -$picto = 'margin'; +$head=marges_prepare_head($user); +$titre=$langs->trans("Margins"); +$picto='margin'; +dol_fiche_head($head, 'agentMargins', $titre, 0, $picto); @@ -118,10 +92,8 @@ -print ''; - -print dol_get_fiche_head($head, 'agentMargins', $titre, 0, $picto); - -print ''; - -print ''; -print ''; +print '
'.$langs->trans('ContactOfInvoice').''; -print img_picto('', 'user').$form->select_dolusers($agentid, 'agentid', 1, '', $user->hasRight('margins', 'read', 'all') ? 0 : 1, '', '', 0, 0, 0, '', 0, '', 'maxwidth300'); -print '
'; + +if ($user->rights->margins->read->all) { + print ''; + print ''; +} @@ -130,3 +102,3 @@ -print ''; -print ''; +print ''; -print ''; +print '
'.$langs->trans('SalesRepresentative').''; + print $form->select_dolusers($agentid, 'agentid', 1); + print '
'.$langs->trans('DateStart').' ('.$langs->trans("DateValidation").')'; -print $form->selectDate($startdate, 'startdate', '', '', 1, "sel", 1, 1); +print ''.$langs->trans('StartDate').' ('.$langs->trans("DateValidation").')'; +$form->select_date($startdate,'startdate','','',1,"sel",1,1); @@ -134,3 +106,3 @@ -print ''.$langs->trans('DateEnd').' ('.$langs->trans("DateValidation").')'; -print $form->selectDate($enddate, 'enddate', '', '', 1, "sel", 1, 1); +print ''.$langs->trans('EndDate').' ('.$langs->trans("DateValidation").')'; +$form->select_date($enddate,'enddate','','',1,"sel",1,1); @@ -139 +111 @@ -print ''; +print ''; @@ -142,3 +113,0 @@ - -print dol_get_fiche_end(); - @@ -147,2 +115,0 @@ -$invoice_status_except_list = array(Facture::STATUS_DRAFT, Facture::STATUS_ABANDONED); - @@ -150,21 +117,17 @@ -$sql .= " s.rowid as socid, s.nom as name, s.code_client, s.client,"; -$sql .= " u.rowid as agent, u.login, u.lastname, u.firstname,"; -$sql .= " sum(d.total_ht) as selling_price,"; -// Note: qty and buy_price_ht is always positive (if not your database may be corrupted, you can update this) - -$sql .= " sum(".$db->ifsql('(d.total_ht < 0 OR (d.total_ht = 0 AND f.type = 2))', '-1 * d.qty * d.buy_price_ht * (d.situation_percent / 100)', 'd.qty * d.buy_price_ht * (d.situation_percent / 100)').") as buying_price,"; -$sql .= " sum(".$db->ifsql('(d.total_ht < 0 OR (d.total_ht = 0 AND f.type = 2))', '-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty * (d.situation_percent / 100)))', 'd.total_ht - (d.buy_price_ht * d.qty * (d.situation_percent / 100))').") as marge"; - -$sql .= " FROM ".MAIN_DB_PREFIX."societe as s"; -$sql .= ", ".MAIN_DB_PREFIX."facture as f"; -$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_contact e ON e.element_id = f.rowid and e.statut = 4 and e.fk_c_type_contact = ".(!getDolGlobalString('AGENT_CONTACT_TYPE') ? -1 : $conf->global->AGENT_CONTACT_TYPE); -$sql .= ", ".MAIN_DB_PREFIX."facturedet as d"; -$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; -$sql .= ", ".MAIN_DB_PREFIX."user as u"; -$sql .= " WHERE f.fk_soc = s.rowid"; -$sql .= ' AND f.entity IN ('.getEntity('invoice').')'; -$sql .= " AND sc.fk_soc = f.fk_soc"; -$sql .= " AND (d.product_type = 0 OR d.product_type = 1)"; -if (getDolGlobalString('AGENT_CONTACT_TYPE')) { - $sql .= " AND ((e.fk_socpeople IS NULL AND sc.fk_user = u.rowid) OR (e.fk_socpeople IS NOT NULL AND e.fk_socpeople = u.rowid))"; -} else { +if ($agentid > 0) $sql.= " s.rowid as socid, s.nom as name, s.code_client, s.client,"; +$sql.= " u.rowid as agent, u.login, u.lastname, u.firstname,"; +$sql.= " sum(d.total_ht) as selling_price,"; +$sql.= " sum(".$db->ifsql('d.total_ht < 0','d.qty * d.buy_price_ht * -1','d.qty * d.buy_price_ht').") as buying_price,"; +$sql.= " sum(".$db->ifsql('d.total_ht < 0','-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty))','d.total_ht - (d.buy_price_ht * d.qty)').") as marge" ; +$sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; +$sql.= ", ".MAIN_DB_PREFIX."facture as f"; +$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_contact e ON e.element_id = f.rowid and e.statut = 4 and e.fk_c_type_contact = ".(empty($conf->global->AGENT_CONTACT_TYPE)?-1:$conf->global->AGENT_CONTACT_TYPE); +$sql.= ", ".MAIN_DB_PREFIX."facturedet as d"; +$sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; +$sql.= ", ".MAIN_DB_PREFIX."user as u"; +$sql.= " WHERE f.fk_soc = s.rowid"; +$sql.= " AND sc.fk_soc = f.fk_soc"; +$sql.= " AND (d.product_type = 0 OR d.product_type = 1)"; +if (! empty($conf->global->AGENT_CONTACT_TYPE)) + $sql.= " AND ((e.fk_socpeople IS NULL AND sc.fk_user = u.rowid) OR (e.fk_socpeople IS NOT NULL AND e.fk_socpeople = u.rowid))"; +else @@ -172,4 +135,3 @@ -} -$sql .= " AND f.fk_statut NOT IN (".$db->sanitize(implode(', ', $invoice_status_except_list)).")"; -$sql .= ' AND s.entity IN ('.getEntity('societe').')'; -$sql .= " AND d.fk_facture = f.rowid"; +$sql.= " AND f.fk_statut > 0"; +$sql.= " AND s.entity = ".$conf->entity; +$sql.= " AND d.fk_facture = f.rowid"; @@ -177,12 +139,9 @@ - if (getDolGlobalString('AGENT_CONTACT_TYPE')) { - $sql .= " AND ((e.fk_socpeople IS NULL AND sc.fk_user = ".((int) $agentid).") OR (e.fk_socpeople IS NOT NULL AND e.fk_socpeople = ".((int) $agentid)."))"; - } else { - $sql .= " AND sc.fk_user = ".((int) $agentid); - } -} -if (!empty($startdate)) { - $sql .= " AND f.datef >= '".$db->idate($startdate)."'"; -} -if (!empty($enddate)) { - $sql .= " AND f.datef <= '".$db->idate($enddate)."'"; -} + if (! empty($conf->global->AGENT_CONTACT_TYPE)) + $sql.= " AND ((e.fk_socpeople IS NULL AND sc.fk_user = ".$agentid.") OR (e.fk_socpeople IS NOT NULL AND e.fk_socpeople = ".$agentid."))"; + else + $sql .= " AND sc.fk_user = ".$agentid; +} +if (!empty($startdate)) + $sql.= " AND f.datef >= '".$db->idate($startdate)."'"; +if (!empty($enddate)) + $sql.= " AND f.datef <= '".$db->idate($enddate)."'"; @@ -190,9 +149,4 @@ -// We should not use this here. Option ForceBuyingPriceIfNull should have effect only when inserting data. Once data is recorded, it must be used as it is for report. -// We keep it with value ForceBuyingPriceIfNull = 2 for retroactive effect but results are unpredicable. -if (getDolGlobalInt('ForceBuyingPriceIfNull') == 2) { - $sql .= " AND d.buy_price_ht <> 0"; -} -//if ($agentid > 0) $sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client, u.rowid, u.login, u.lastname, u.firstname"; -//else $sql.= " GROUP BY u.rowid, u.login, u.lastname, u.firstname"; -$sql .= " GROUP BY s.rowid, s.nom, s.code_client, s.client, u.rowid, u.login, u.lastname, u.firstname"; -$sql .= $db->order($sortfield, $sortorder); +if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) $sql .= " AND d.buy_price_ht <> 0"; +if ($agentid > 0) $sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client, u.rowid, u.login, u.lastname, u.firstname"; +else $sql.= " GROUP BY u.rowid, u.login, u.lastname, u.firstname"; +$sql.=$db->order($sortfield,$sortorder); @@ -202,30 +155,0 @@ - -print '
'; -print ''.$langs->trans("MarginPerSaleRepresentativeWarning").'
'; - -$param = ''; -if (!empty($agentid)) { - $param .= "&agentid=".urlencode($agentid); -} -if (!empty($startdateday)) { - $param .= "&startdateday=".urlencode($startdateday); -} -if (!empty($startdatemonth)) { - $param .= "&startdatemonth=".urlencode($startdatemonth); -} -if (!empty($startdateyear)) { - $param .= "&startdateyear=".urlencode($startdateyear); -} -if (!empty($enddateday)) { - $param .= "&enddateday=".urlencode($enddateday); -} -if (!empty($enddatemonth)) { - $param .= "&enddatemonth=".urlencode($enddatemonth); -} -if (!empty($enddateyear)) { - $param .= "&enddateyear=".urlencode($enddateyear); -} - -$totalMargin = 0; -$marginRate = ''; -$markRate = ''; @@ -234 +158,2 @@ -if ($result) { +if ($result) +{ @@ -238,9 +163 @@ - print_barre_liste($langs->trans("MarginDetails"), $page, $_SERVER["PHP_SELF"], "", $sortfield, $sortorder, '', $num, $num, '', 0, '', '', 0, 1); - - if (getDolGlobalString('MARGIN_TYPE') == "1") { - $labelcostprice = 'BuyingPrice'; - } else { // value is 'costprice' or 'pmp' - $labelcostprice = 'CostPrice'; - } - - $moreforfilter = ''; + print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"",$sortfield,$sortorder,'',0,0,''); @@ -249,2 +166 @@ - print '
'; - print ''."\n"; + print "
"; @@ -253,15 +169,12 @@ - if ($agentid > 0) { - print_liste_field_titre("Customer", $_SERVER["PHP_SELF"], "s.nom", "", $param, '', $sortfield, $sortorder); - } else { - print_liste_field_titre("SalesRepresentative", $_SERVER["PHP_SELF"], "u.lastname", "", $param, '', $sortfield, $sortorder); - } - - print_liste_field_titre("SellingPrice", $_SERVER["PHP_SELF"], "selling_price", "", $param, '', $sortfield, $sortorder, 'right '); - print_liste_field_titre($labelcostprice, $_SERVER["PHP_SELF"], "buying_price", "", $param, '', $sortfield, $sortorder, 'right '); - print_liste_field_titre("Margin", $_SERVER["PHP_SELF"], "marge", "", $param, '', $sortfield, $sortorder, 'right '); - if (getDolGlobalString('DISPLAY_MARGIN_RATES')) { - print_liste_field_titre("MarginRate", $_SERVER["PHP_SELF"], "", "", $param, '', $sortfield, $sortorder, 'right '); - } - if (getDolGlobalString('DISPLAY_MARK_RATES')) { - print_liste_field_titre("MarkRate", $_SERVER["PHP_SELF"], "", "", $param, '', $sortfield, $sortorder, 'right '); - } + if ($agentid > 0) + print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&agentid=".$agentid,'',$sortfield,$sortorder); + else + print_liste_field_titre($langs->trans("SalesRepresentative"),$_SERVER["PHP_SELF"],"u.lastname","","&agentid=".$agentid,'',$sortfield,$sortorder); + + print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&agentid=".$agentid,'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&agentid=".$agentid,'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&agentid=".$agentid,'align="right"',$sortfield,$sortorder); + if (! empty($conf->global->DISPLAY_MARGIN_RATES)) + print_liste_field_titre($langs->trans("MarginRate"),$_SERVER["PHP_SELF"],"","","&agentid=".$agentid,'align="right"',$sortfield,$sortorder); + if (! empty($conf->global->DISPLAY_MARK_RATES)) + print_liste_field_titre($langs->trans("MarkRate"),$_SERVER["PHP_SELF"],"","","&agentid=".$agentid,'align="right"',$sortfield,$sortorder); @@ -270,3 +183,28 @@ - if ($num > 0) { - $group_list = array(); - while ($objp = $db->fetch_object($result)) { + $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT); + + if ($num > 0) + { + $var=true; + + while ($i < $num /*&& $i < $conf->liste_limit*/) + { + $objp = $db->fetch_object($result); + + $pa = $objp->buying_price; + $pv = $objp->selling_price; + $marge = $objp->marge; + + if ($marge < 0) + { + $marginRate = ($pa != 0)?-1*(100 * $marge / $pa):'' ; + $markRate = ($pv != 0)?-1*(100 * $marge / $pv):'' ; + } + else + { + $marginRate = ($pa != 0)?(100 * $marge / $pa):'' ; + $markRate = ($pv != 0)?(100 * $marge / $pv):'' ; + } + + $var=!$var; + + print ""; @@ -274,71 +212,17 @@ - $group_id = $objp->socid; - } else { - $group_id = $objp->agent; - } - - if (!isset($group_list[$group_id])) { - if ($agentid > 0) { - $group_name = $objp->name; - $companystatic->id = $objp->socid; - $companystatic->name = $objp->name; - $companystatic->client = $objp->client; - $group_htmlname = $companystatic->getNomUrl(1, 'customer'); - } else { - $group_name = $objp->lastname; - $userstatic->fetch($objp->agent); - $group_htmlname = $userstatic->getFullName($langs, 0, 0, 0); - } - $group_list[$group_id] = array('name' => $group_name, 'htmlname' => $group_htmlname, 'selling_price' => 0, 'buying_price' => 0, 'marge' => 0); - } - - $seller_nb = 1; - if ($objp->socid > 0) { - // sql nb sellers - $sql_seller = "SELECT COUNT(sc.rowid) as nb"; - $sql_seller .= " FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - $sql_seller .= " WHERE sc.fk_soc = ".((int) $objp->socid); - $sql_seller .= " LIMIT 1"; - - $resql_seller = $db->query($sql_seller); - if (!$resql_seller) { - dol_print_error($db); - } else { - if ($obj_seller = $db->fetch_object($resql_seller)) { - if ($obj_seller->nb > 0) { - $seller_nb = $obj_seller->nb; - } - } - } - } - - $group_list[$group_id]['selling_price'] += $objp->selling_price / $seller_nb; - $group_list[$group_id]['buying_price'] += $objp->buying_price / $seller_nb; - $group_list[$group_id]['marge'] += $objp->marge / $seller_nb; - } - - // sort group array by sortfield - if ($sortfield == 'u.lastname' || $sortfield == 's.nom') { - $sortfield = 'name'; - } - $group_list = dol_sort_array($group_list, $sortfield, $sortorder); - $cumul_achat = 0; - $cumul_vente = 0; - foreach ($group_list as $group_id => $group_array) { - $pa = $group_array['buying_price']; - $pv = $group_array['selling_price']; - $marge = $group_array['marge']; - - $marginRate = ($pa != 0) ? (100 * $marge / $pa) : ''; - $markRate = ($pv != 0) ? (100 * $marge / $pv) : ''; - - print ''; - print "\n"; - print ''; - print ''; - print ''; - if (getDolGlobalString('DISPLAY_MARGIN_RATES')) { - print ''; - } - if (getDolGlobalString('DISPLAY_MARK_RATES')) { - print ''; - } + $companystatic->id=$objp->socid; + $companystatic->name=$objp->name; + $companystatic->client=$objp->client; + print "\n"; + } + else { + $userstatic->fetch($objp->agent); + print "\n"; + } + + print "\n"; + print "\n"; + print "\n"; + if (! empty($conf->global->DISPLAY_MARGIN_RATES)) + print "\n"; + if (! empty($conf->global->DISPLAY_MARK_RATES)) + print "\n"; @@ -348,2 +231,0 @@ - $cumul_achat += $pa; - $cumul_vente += $pv; @@ -352,30 +234,4 @@ - - // Show total margin - if (!isset($cumul_achat)) { - $cumul_achat = 0; - } - if (!isset($cumul_vente)) { - $cumul_vente = 0; - } - $totalMargin = $cumul_vente - $cumul_achat; - - $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) : ''; - $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) : ''; - - print ''; - print '"; - print ''; - print ''; - print ''; - if (getDolGlobalString('DISPLAY_MARGIN_RATES')) { - print ''; - } - if (getDolGlobalString('DISPLAY_MARK_RATES')) { - print ''; - } - print ''; - - print '
".$group_array['htmlname']."'.price(price2num($pv, 'MT')).''.price(price2num($pa, 'MT')).''.price(price2num($marge, 'MT')).''.(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%").''.(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%").'".$companystatic->getNomUrl(1,'customer')."".$userstatic->getFullName($langs,0,0,0)."".price($pv, null, null, null, null, $rounding)."".price($pa, null, null, null, null, $rounding)."".price($marge, null, null, null, null, $rounding)."".(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")."".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")."
'; - print $langs->trans('TotalMargin')."'.price(price2num($cumul_vente, 'MT')).''.price(price2num($cumul_achat, 'MT')).''.price(price2num($totalMargin, 'MT')).''.(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%").''.(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%").'
'; - print '
'; -} else { + print "
"; +} +else +{ @@ -386,10 +242 @@ -print "\n".''."\n"; - -// End of page + @@ -397,0 +245,11 @@ + +?> + + --- /tmp/dsg/dolibarr/htdocs/margin/github_19.0.3_customerMargins.php +++ /tmp/dsg/dolibarr/htdocs/margin/client_customerMargins.php @@ -16 +16 @@ - * along with this program. If not, see . + * along with this program. If not, see . @@ -25 +24,0 @@ -// Load Dolibarr environment @@ -32,12 +31,16 @@ -// Load translation files required by the page -$langs->loadLangs(array('companies', 'bills', 'products', 'margins')); - -// Load variable for pagination -$limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit; -$sortfield = GETPOST('sortfield', 'aZ09comma'); -$sortorder = GETPOST('sortorder', 'aZ09comma'); -$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int'); -if (empty($page) || $page == -1) { - $page = 0; -} // If $page is not defined, or '' or -1 -$offset = $limit * $page; +$langs->load("companies"); +$langs->load("bills"); +$langs->load("products"); +$langs->load("margins"); + +// Security check +$socid = GETPOST('socid','int'); +if (! empty($user->societe_id)) $socid=$user->societe_id; +$result = restrictedArea($user, 'societe','',''); + + +$mesg = ''; + +$page = GETPOST("page",'int'); +if ($page == -1) { $page = 0; } +$offset = $conf->liste_limit * $page; @@ -46,30 +49,7 @@ -if (!$sortfield) { - $sortfield = "s.nom"; // Set here default search field -} -if (!$sortorder) { - $sortorder = "ASC"; -} - -$startdate = $enddate = ''; -if (GETPOST('startdatemonth')) { - $startdate = dol_mktime(0, 0, 0, GETPOST('startdatemonth', 'int'), GETPOST('startdateday', 'int'), GETPOST('startdateyear', 'int')); -} -if (GETPOST('enddatemonth')) { - $enddate = dol_mktime(23, 59, 59, GETPOST('enddatemonth', 'int'), GETPOST('enddateday', 'int'), GETPOST('enddateyear')); -} - -// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context -$object = new Societe($db); -$hookmanager->initHooks(array('margincustomerlist')); - -// Security check -$socid = GETPOST('socid', 'int'); -$TSelectedProducts = GETPOST('products', 'array'); -$TSelectedCats = GETPOST('categories', 'array'); - -if (!empty($user->socid)) { - $socid = $user->socid; -} -$result = restrictedArea($user, 'societe', '', ''); -$result = restrictedArea($user, 'margins'); - + +$startdate=$enddate=''; + +if (!empty($_POST['startdatemonth'])) + $startdate = dol_mktime(0, 0, 0, $_POST['startdatemonth'], $_POST['startdateday'], $_POST['startdateyear']); +if (!empty($_POST['enddatemonth'])) + $enddate = dol_mktime(23, 59, 59, $_POST['enddatemonth'], $_POST['enddateday'], $_POST['enddateyear']); @@ -82 +62 @@ -$invoicestatic = new Facture($db); +$invoicestatic=new Facture($db); @@ -86,4 +66,4 @@ -llxHeader('', $langs->trans("Margins").' - '.$langs->trans("Clients")); - -$text = $langs->trans("Margins"); -//print load_fiche_titre($text); +llxHeader('',$langs->trans("Margins").' - '.$langs->trans("Clients")); + +$text=$langs->trans("Margins"); +//print_fiche_titre($text); @@ -92,5 +72,4 @@ -$head = marges_prepare_head(); - -$titre = $langs->trans("Margins"); -$picto = 'margin'; - +$head=marges_prepare_head($user); +$titre=$langs->trans("Margins"); +$picto='margin'; +dol_fiche_head($head, 'customerMargins', $titre, 0, $picto); @@ -99,5 +78 @@ -print ''; - -print dol_get_fiche_head($head, 'customerMargins', $titre, 0, $picto); - -print ''; +print '
'; @@ -106,0 +82 @@ + @@ -110,5 +86,5 @@ - if ($soc->client) { - print ''; - print ''; + print ''; - print ''; + print ''; -print ''; - -// Categories -$TCats = $form->select_all_categories('product', array(), '', 64, 0, 1); - -print ''; -print ''; -print ''; +print ''; - -// Start date -print ''; -print ''; -print ''; -print ''; +print '
'.$langs->trans('ThirdPartyName').''; - $filter = '(client:IN:1,3)'; - print img_picto('', 'company').$form->select_company($socid, 'socid', $filter, 1, 0, 0); + if ($soc->client) + { + print '
'.$langs->trans('ThirdPartyName').''; + $form->form_thirdparty($_SERVER['PHP_SELF'].'?socid='.$socid,$socid,'socid','client=1 OR client=3',1,0,1); @@ -118,11 +94,8 @@ - if (!$sortorder) { - $sortorder = "DESC"; - } - if (!$sortfield) { - $sortfield = "f.datef"; - } - } -} else { - print '
'.$langs->trans('ThirdPartyName').''; - print img_picto('', 'company').$form->select_company(null, 'socid', '((client:=:1) OR (client:=:3))', 1, 0, 0); + if (! $sortorder) $sortorder="DESC"; + if (! $sortfield) $sortfield="f.datef"; + } +} +else { + print '
'.$langs->trans('ThirdPartyName').''; + $form->form_thirdparty($_SERVER['PHP_SELF'],null,'socid','client=1 OR client=3',1,0,1); @@ -132,35 +105,21 @@ -$sortfield = GETPOST('sortfield', 'aZ09comma'); -$sortorder = GETPOST('sortorder', 'aZ09comma'); -if (!$sortorder) { - $sortorder = "ASC"; -} -if (!$sortfield) { - if ($client) { - $sortfield = "f.datef"; - $sortorder = "DESC"; - } else { - $sortfield = "s.nom"; - $sortorder = "ASC"; - } -} - -// Products -$TRes = $form->select_produits_list('', '', '', '', 0, '', 1, 2, 1, 0, '', 1); - -$TProducts = array(); -foreach ($TRes as $prod) { - $TProducts[$prod['key']] = $prod['label']; -} - -print '
'.$langs->trans('ProductOrService').''; -print img_picto('', 'product').$form->multiselectarray('products', $TProducts, $TSelectedProducts, 0, 0, 'minwidth500'); -print '
'.$langs->trans('Category').''; -print img_picto('', 'category', 'class="pictofixedwidth"').$form->multiselectarray('categories', $TCats, $TSelectedCats, 0, 0, 'quatrevingtpercent widthcentpercentminusx'); +$sortfield = GETPOST("sortfield",'alpha'); +$sortorder = GETPOST("sortorder",'alpha'); +if (! $sortorder) $sortorder="ASC"; +if (! $sortfield) +{ + if ($client) + { + $sortfield="f.datef"; + $sortorder="DESC"; + } + else + { + $sortfield="s.nom"; + $sortorder="ASC"; + } +} + +// Start date +print ''.$langs->trans('StartDate').' ('.$langs->trans("DateValidation").')'; +$form->select_date($startdate,'startdate','','',1,"sel",1,1); @@ -168,10 +127,3 @@ -print '
'.$langs->trans('DateStart').' ('.$langs->trans("DateValidation").')'; -print $form->selectDate($startdate, 'startdate', '', '', 1, "sel", 1, 1); -print ''.$langs->trans('DateEnd').' ('.$langs->trans("DateValidation").')'; -print $form->selectDate($enddate, 'enddate', '', '', 1, "sel", 1, 1); +print ''.$langs->trans('EndDate').' ('.$langs->trans("DateValidation").')'; +$form->select_date($enddate,'enddate','','',1,"sel",1,1); @@ -180 +132 @@ -print ''; +print ''; @@ -187 +139 @@ -print ''; +print '
'; @@ -190,2 +142,2 @@ -print '"; + print "\n"; + print "\n"; + print "\n"; + if (! empty($conf->global->DISPLAY_MARGIN_RATES)) + print "\n"; + if (! empty($conf->global->DISPLAY_MARK_RATES)) + print "\n"; + print "\n"; + + print "
'.$langs->trans("TotalMargin").''; -print ' '.$langs->getCurrencySymbol($conf->currency).''; // set by jquery (see below) +print '
'.$langs->trans("TotalMargin").''; +print ''; // set by jquery (see below) @@ -195,2 +147,2 @@ -if (getDolGlobalString('DISPLAY_MARGIN_RATES')) { - print '
'.$langs->trans("MarginRate").''; +if (! empty($conf->global->DISPLAY_MARGIN_RATES)) { + print '
'.$langs->trans("MarginRate").''; @@ -202,2 +154,2 @@ -if (getDolGlobalString('DISPLAY_MARK_RATES')) { - print '
'.$langs->trans("MarkRate").''; +if (! empty($conf->global->DISPLAY_MARK_RATES)) { + print '
'.$langs->trans("MarkRate").''; @@ -209,3 +160,0 @@ - -print dol_get_fiche_end(); - @@ -214,2 +162,0 @@ -$invoice_status_except_list = array(Facture::STATUS_DRAFT, Facture::STATUS_ABANDONED); - @@ -217,44 +164,19 @@ -$sql .= " s.rowid as socid, s.nom as name, s.code_client, s.client,"; -if ($client) { - $sql .= " f.rowid as facid, f.ref, f.total_ht, f.datef, f.paye, f.type, f.fk_statut as statut,"; -} -$sql .= " sum(d.total_ht) as selling_price,"; -// Note: qty and buy_price_ht is always positive (if not, your database may be corrupted, you can update this) - -$sql .= " sum(".$db->ifsql('(d.total_ht < 0 OR (d.total_ht = 0 AND f.type = 2))', '-1 * d.qty * d.buy_price_ht * (d.situation_percent / 100)', 'd.qty * d.buy_price_ht * (d.situation_percent / 100)').") as buying_price,"; -$sql .= " sum(".$db->ifsql('(d.total_ht < 0 OR (d.total_ht = 0 AND f.type = 2))', '-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty * (d.situation_percent / 100)))', 'd.total_ht - (d.buy_price_ht * d.qty * (d.situation_percent / 100))').") as marge"; - -$sql .= " FROM ".MAIN_DB_PREFIX."societe as s"; -$sql .= ", ".MAIN_DB_PREFIX."facture as f"; -$sql .= ", ".MAIN_DB_PREFIX."facturedet as d"; -if (!empty($TSelectedCats)) { - $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_product as cp ON cp.fk_product=d.fk_product'; -} - -if (!$user->hasRight('societe', 'client', 'voir') && !$socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; -} -$sql .= " WHERE f.fk_soc = s.rowid"; -if ($socid > 0) { - $sql .= ' AND s.rowid = '.((int) $socid); -} -if (!$user->hasRight('societe', 'client', 'voir') && !$socid) { - $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id); -} -$sql .= " AND f.fk_statut NOT IN (".$db->sanitize(implode(', ', $invoice_status_except_list)).")"; -$sql .= ' AND s.entity IN ('.getEntity('societe').')'; -$sql .= ' AND f.entity IN ('.getEntity('invoice').')'; -$sql .= " AND d.fk_facture = f.rowid"; -$sql .= " AND (d.product_type = 0 OR d.product_type = 1)"; -if (!empty($TSelectedProducts)) { - $sql .= ' AND d.fk_product IN ('.$db->sanitize(implode(',', $TSelectedProducts)).')'; -} -if (!empty($TSelectedCats)) { - $sql .= ' AND cp.fk_categorie IN ('.$db->sanitize(implode(',', $TSelectedCats)).')'; -} -if (!empty($startdate)) { - $sql .= " AND f.datef >= '".$db->idate($startdate)."'"; -} -if (!empty($enddate)) { - $sql .= " AND f.datef <= '".$db->idate($enddate)."'"; -} +$sql.= " s.rowid as socid, s.nom as name, s.code_client, s.client,"; +if ($client) $sql.= " f.rowid as facid, f.facnumber, f.total as total_ht, f.datef, f.paye, f.fk_statut as statut,"; +$sql.= " sum(d.total_ht) as selling_price,"; +$sql.= " sum(".$db->ifsql('d.total_ht < 0','d.qty * d.buy_price_ht * -1','d.qty * d.buy_price_ht').") as buying_price,"; +$sql.= " sum(".$db->ifsql('d.total_ht < 0','-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty))','d.total_ht - (d.buy_price_ht * d.qty)').") as marge"; +$sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; +$sql.= ", ".MAIN_DB_PREFIX."facture as f"; +$sql.= ", ".MAIN_DB_PREFIX."facturedet as d"; +$sql.= " WHERE f.fk_soc = s.rowid"; +$sql.= " AND f.fk_statut > 0"; +$sql.= " AND s.entity = ".$conf->entity; +$sql.= " AND d.fk_facture = f.rowid"; +$sql.= " AND (d.product_type = 0 OR d.product_type = 1)"; +if ($client) + $sql.= " AND f.fk_soc = ".$socid; +if (!empty($startdate)) + $sql.= " AND f.datef >= '".$db->idate($startdate)."'"; +if (!empty($enddate)) + $sql.= " AND f.datef <= '".$db->idate($enddate)."'"; @@ -262,3 +184 @@ -// We should not use this here. Option ForceBuyingPriceIfNull should have effect only when inserting data. Once data is recorded, it must be used as it is for report. -// We keep it with value ForceBuyingPriceIfNull = 2 for retroactive effect but results are unpredicable. -if (getDolGlobalInt('ForceBuyingPriceIfNull') == 2) { +if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) @@ -266,7 +186,3 @@ -} -if ($client) { - $sql .= " GROUP BY s.rowid, s.nom, s.code_client, s.client, f.rowid, f.ref, f.total_ht, f.datef, f.paye, f.type, f.fk_statut"; -} else { - $sql .= " GROUP BY s.rowid, s.nom, s.code_client, s.client"; -} -$sql .= $db->order($sortfield, $sortorder); +if ($client) $sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client, f.rowid, f.facnumber, f.total, f.datef, f.paye, f.fk_statut"; +else $sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client"; +$sql.=$db->order($sortfield,$sortorder); @@ -276,27 +191,0 @@ -$param = '&socid='.((int) $socid); -if (GETPOST('startdatemonth', 'int')) { - $param .= '&startdateyear='.GETPOST('startdateyear', 'int'); - $param .= '&startdatemonth='.GETPOST('startdatemonth', 'int'); - $param .= '&startdateday='.GETPOST('startdateday', 'int'); -} -if (GETPOST('enddatemonth', 'int')) { - $param .= '&enddateyear='.GETPOST('enddateyear', 'int'); - $param .= '&enddatemonth='.GETPOST('enddatemonth', 'int'); - $param .= '&enddateday='.GETPOST('enddateday', 'int'); -} -$listofproducts = GETPOST('products', 'array:int'); -if (is_array($listofproducts)) { - foreach ($listofproducts as $val) { - $param .= '&products[]='.$val; - } -} -$listofcateg = GETPOST('categories', 'array:int'); -if (is_array($listofcateg)) { - foreach ($listofcateg as $val) { - $param .= '&categories[]='.$val; - } -} - -$totalMargin = 0; -$marginRate = ''; -$markRate = ''; @@ -305 +194,2 @@ -if ($result) { +if ($result) +{ @@ -308,10 +198,2 @@ - print '
'; - print_barre_liste($langs->trans("MarginDetails"), $page, $_SERVER["PHP_SELF"], "", $sortfield, $sortorder, '', $num, $num, '', 0, '', '', 0, 1); - - if (getDolGlobalString('MARGIN_TYPE') == "1") { - $labelcostprice = 'BuyingPrice'; - } else { // value is 'costprice' or 'pmp' - $labelcostprice = 'CostPrice'; - } - - $moreforfilter = ''; + print '
'; + print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"",$sortfield,$sortorder,'',0,0,''); @@ -320,2 +202 @@ - print '
'; - print ''."\n"; + print "
"; @@ -324,15 +205,13 @@ - if (!empty($client)) { - print_liste_field_titre("Invoice", $_SERVER["PHP_SELF"], "f.ref", "", $param, '', $sortfield, $sortorder); - print_liste_field_titre("DateInvoice", $_SERVER["PHP_SELF"], "f.datef", "", $param, 'align="center"', $sortfield, $sortorder); - } else { - print_liste_field_titre("Customer", $_SERVER["PHP_SELF"], "s.nom", "", $param, '', $sortfield, $sortorder); - } - print_liste_field_titre("SellingPrice", $_SERVER["PHP_SELF"], "selling_price", "", $param, 'align="right"', $sortfield, $sortorder); - print_liste_field_titre($labelcostprice, $_SERVER["PHP_SELF"], "buying_price", "", $param, 'align="right"', $sortfield, $sortorder); - print_liste_field_titre("Margin", $_SERVER["PHP_SELF"], "marge", "", $param, 'align="right"', $sortfield, $sortorder); - if (getDolGlobalString('DISPLAY_MARGIN_RATES')) { - print_liste_field_titre("MarginRate", $_SERVER["PHP_SELF"], "", "", $param, 'align="right"', $sortfield, $sortorder); - } - if (getDolGlobalString('DISPLAY_MARK_RATES')) { - print_liste_field_titre("MarkRate", $_SERVER["PHP_SELF"], "", "", $param, 'align="right"', $sortfield, $sortorder); - } + if (! empty($client)) { + print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&socid=".$socid,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&socid=".$socid,'align="center"',$sortfield,$sortorder); + } + else + print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&socid=".$socid,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&socid=".$socid,'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&socid=".$socid,'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&socid=".$socid,'align="right"',$sortfield,$sortorder); + if (! empty($conf->global->DISPLAY_MARGIN_RATES)) + print_liste_field_titre($langs->trans("MarginRate"),$_SERVER["PHP_SELF"],"","","&socid=".$socid,'align="right"',$sortfield,$sortorder); + if (! empty($conf->global->DISPLAY_MARK_RATES)) + print_liste_field_titre($langs->trans("MarkRate"),$_SERVER["PHP_SELF"],"","","&socid=".$socid,'align="right"',$sortfield,$sortorder); @@ -344,2 +223,8 @@ - if ($num > 0) { - while ($i < $num /*&& $i < $conf->liste_limit*/) { + $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT); + + if ($num > 0) + { + $var=True; + + while ($i < $num /*&& $i < $conf->liste_limit*/) + { @@ -352,6 +237,4 @@ - if ($marge < 0) { - $marginRate = ($pa != 0) ? -1 * (100 * $marge / $pa) : ''; - $markRate = ($pv != 0) ? -1 * (100 * $marge / $pv) : ''; - } else { - $marginRate = ($pa != 0) ? (100 * $marge / $pa) : ''; - $markRate = ($pv != 0) ? (100 * $marge / $pv) : ''; + if ($marge < 0) + { + $marginRate = ($pa != 0)?-1*(100 * $marge / $pa):'' ; + $markRate = ($pv != 0)?-1*(100 * $marge / $pv):'' ; @@ -359,2 +242,9 @@ - - print ''; + else + { + $marginRate = ($pa != 0)?(100 * $marge / $pa):'' ; + $markRate = ($pv != 0)?(100 * $marge / $pv):'' ; + } + + $var=!$var; + + print ""; @@ -362,6 +252,3 @@ - $invoicestatic->id = $objp->facid; - $invoicestatic->ref = $objp->ref; - $invoicestatic->statut = $objp->statut; - $invoicestatic->type = $objp->type; - - print ''; - print ''; - } else { - $companystatic->id = $objp->socid; - $companystatic->name = $objp->name; - $companystatic->client = $objp->client; - - print ''; - } - - print ''; - print ''; - print ''; - if (getDolGlobalString('DISPLAY_MARGIN_RATES')) { - print ''; - } - if (getDolGlobalString('DISPLAY_MARK_RATES')) { - print ''; - } + print "\n"; + print ""; + } + else { + $companystatic->id=$objp->socid; + $companystatic->name=$objp->name; + $companystatic->client=$objp->client; + print "\n"; + } + + print "\n"; + print "\n"; + print "\n"; + if (! empty($conf->global->DISPLAY_MARGIN_RATES)) + print "\n"; + if (! empty($conf->global->DISPLAY_MARK_RATES)) + print "\n"; @@ -398 +283 @@ - + $var=!$var; @@ -400 +285 @@ - /*if ($totalMargin < 0) + if ($totalMargin < 0) @@ -406,4 +291,4 @@ - {*/ - $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) : ''; - $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) : ''; - //} + { + $marginRate = ($cumul_achat != 0)?(100 * $totalMargin / $cumul_achat):''; + $markRate = ($cumul_vente != 0)?(100 * $totalMargin / $cumul_vente):''; + } @@ -412,20 +297,18 @@ - if ($client) { - print '"; - print ''; - print ''; - print ''; - if (getDolGlobalString('DISPLAY_MARGIN_RATES')) { - print ''; - } - if (getDolGlobalString('DISPLAY_MARK_RATES')) { - print ''; - } - print ''; - - print '
'; + print ''; + $invoicestatic->id=$objp->facid; + $invoicestatic->ref=$objp->facnumber; @@ -369,20 +256,18 @@ - print ''; - print dol_print_date($db->jdate($objp->datef), 'day').''.$companystatic->getNomUrl(1, 'margin').''.price(price2num($pv, 'MT')).''.price(price2num($pa, 'MT')).''.price(price2num($marge, 'MT')).''.(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%").''.(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%").'"; + print dol_print_date($db->jdate($objp->datef),'day')."".$companystatic->getNomUrl(1,'customer')."".price($pv, null, null, null, null, $rounding)."".price($pa, null, null, null, null, $rounding)."".price($marge, null, null, null, null, $rounding)."".(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")."".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")."'; - } else { - print ''; - } - print $langs->trans('TotalMargin')."'.price(price2num($cumul_vente, 'MT')).''.price(price2num($cumul_achat, 'MT')).''.price(price2num($totalMargin, 'MT')).''.(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%").''.(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%").'
'; - print '
'; -} else { + if ($client) + print '
'; + else + print ''; + print $langs->trans('TotalMargin')."".price($cumul_vente, null, null, null, null, $rounding)."".price($cumul_achat, null, null, null, null, $rounding)."".price($totalMargin, null, null, null, null, $rounding)."".(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")."".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")."
"; +} +else +{ @@ -436 +319,7 @@ -print '