From a3fb8d3e14c63096a7aa907de2614737c85afc9a Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 5 Apr 2021 19:34:56 +0200 Subject: [PATCH] Factorize code --- htdocs/comm/index.php | 1 - htdocs/compta/facture/index.php | 471 +------------- htdocs/compta/index.php | 656 +------------------- htdocs/core/lib/invoice.lib.php | 1020 +++++++++++++++++++++++++++++++ htdocs/fourn/facture/index.php | 467 +------------- 5 files changed, 1069 insertions(+), 1546 deletions(-) diff --git a/htdocs/comm/index.php b/htdocs/comm/index.php index 97fa9eb0cfc..149f28f42d3 100644 --- a/htdocs/comm/index.php +++ b/htdocs/comm/index.php @@ -28,7 +28,6 @@ */ require '../main.inc.php'; - require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/agenda.lib.php'; require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php'; diff --git a/htdocs/compta/facture/index.php b/htdocs/compta/facture/index.php index ba0d8e2f2aa..0b2f4538636 100644 --- a/htdocs/compta/facture/index.php +++ b/htdocs/compta/facture/index.php @@ -25,6 +25,7 @@ require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/invoice.lib.php'; // Security check restrictedArea($user, 'facture'); @@ -39,14 +40,17 @@ if (isset($user->socid) && $user->socid > 0) { $socid = $user->socid; } +$max = $conf->global->MAIN_SIZE_SHORTLIST_LIMIT; + // Maximum elements of the tables $maxDraftCount = empty($conf->global->MAIN_MAXLIST_OVERLOAD) ? 500 : $conf->global->MAIN_MAXLIST_OVERLOAD; $maxLatestEditCount = 5; $maxOpenCount = empty($conf->global->MAIN_MAXLIST_OVERLOAD) ? 500 : $conf->global->MAIN_MAXLIST_OVERLOAD; + /* -* View -*/ + * View + */ llxHeader("", $langs->trans("CustomersInvoicesArea"), "EN:Customers_Invoices|FR:Factures_Clients|ES:Facturas_a_clientes"); @@ -56,24 +60,18 @@ print '
'; print '
'; -// This is useless due to the global search combo -if (!empty($conf->global->MAIN_SEARCH_FORM_ON_HOME_AREAS)) { - print getAreaSearchFrom(); - print '
'; -} - -print getPieChart($socid); +print getCustomerInvoicePieChart($socid); print '
'; -print getDraftTable($maxDraftCount, $socid); +print getCustomerInvoiceDraftTable($max, $socid); print '
'; print '
'; print '
'; -print getLatestEditTable($maxLatestEditCount, $socid); +print getCustomerInvoiceLatestEditTable($maxLatestEditCount, $socid); print '
'; -print getOpenTable($maxOpenCount, $socid); +print getCustomerInvoiceUnpaidOpenTable($max, $socid); print '
'; print '
'; @@ -83,452 +81,3 @@ print '
'; // End of page llxFooter(); $db->close(); - -/** - * Return a HTML string that contains a additional search form - * - * @return string A HTML string that contains a additional search form - */ -function getAreaSearchFrom() -{ - global $langs; - - $result = '
'; - $result .= '
'; - $result .= ''; - $result .= ''; - - $result .= ''; - $result .= ''; - $result .= ''; - - $result .= ''; - $result .= ''; - $result .= ''; - $result .= ''; - - $result .= "
'.$langs->trans("Search").'
'.$langs->trans("Invoice").':
"; - $result .= "
"; - $result .= "
"; - - return $result; -} - -/** - * Return a HTML table that contains a pie chart of customer invoices - * - * @param int $socid (Optional) Show only results from the customer with this id - * @return string A HTML table that contains a pie chart of customer invoices - */ -function getPieChart($socid = 0) -{ - global $conf, $db, $langs, $user; - - $sql = "SELECT count(f.rowid), f.fk_statut"; - $sql .= " FROM ".MAIN_DB_PREFIX."societe as s"; - $sql .= ", ".MAIN_DB_PREFIX."facture as f"; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE f.fk_soc = s.rowid"; - $sql .= " AND f.entity IN (".getEntity('facture').")"; - if ($user->socid) { - $sql .= ' AND f.fk_soc = '.$user->socid; - } - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; - } - $sql .= " GROUP BY f.fk_statut"; - - $resql = $db->query($sql); - if (!$resql) { - dol_print_error($db); - return ''; - } - - $num = $db->num_rows($resql); - $i = 0; - - $total = 0; - $vals = []; - - while ($i < $num) { - $row = $db->fetch_row($resql); - if ($row) { - $vals[$row[1]] = $row[0]; - $total += $row[0]; - } - - $i++; - } - - $db->free($resql); - - $result = '
'; - $result .= ''; - $result .= ''; - $result .= ''; - $result .= ''; - - $objectstatic = new Facture($db); - $array = [Facture::STATUS_DRAFT, Facture::STATUS_VALIDATED, Facture::STATUS_CLOSED, Facture::STATUS_ABANDONED]; - $dataseries = []; - - foreach ($array as $status) { - $objectstatic->statut = $status; - $objectstatic->paye = $status == Facture::STATUS_CLOSED ? -1 : 0; - - $dataseries[] = [$objectstatic->getLibStatut(1), (isset($vals[$status]) ? (int) $vals[$status] : 0)]; - if (!$conf->use_javascript_ajax) { - $result .= ''; - $result .= ''; - $result .= ''; - $result .= ''; - } - } - - if ($conf->use_javascript_ajax) { - $dolgraph = new DolGraph(); - $dolgraph->SetData($dataseries); - $dolgraph->setShowLegend(2); - $dolgraph->setShowPercent(1); - $dolgraph->SetType(['pie']); - $dolgraph->setHeight('200'); - $dolgraph->draw('idgraphthirdparties'); - - $result .= ''; - $result .= ''; - $result .= ''; - } - - $result .= ''; - $result .= ''; - $result .= ''; - $result .= ''; - - $result .= '
'.$langs->trans("Statistics").' - '.$langs->trans("CustomerInvoice").'
'.$objectstatic->getLibStatut(0).''.(isset($vals[$status]) ? $vals[$status] : 0).'
'.$dolgraph->show($total ? 0 : 1).'
'.$langs->trans("Total").''.$total.'
'; - $result .= '
'; - return $result; -} - -/** - * Return a HTML table that contains a list with customer invoice drafts - * - * @param int $maxCount (Optional) The maximum count of elements inside the table - * @param int $socid (Optional) Show only results from the customer with this id - * @return string A HTML table that contains a list with customer invoice drafts - */ -function getDraftTable($maxCount = 500, $socid = 0) -{ - global $db, $langs, $user; - - $sql = "SELECT f.rowid, f.ref, s.nom as socname, s.rowid as socid, s.canvas, s.client, f.total as total_ttc"; - $sql .= " FROM ".MAIN_DB_PREFIX."facture as f"; - $sql .= ", ".MAIN_DB_PREFIX."societe as s"; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE f.fk_soc = s.rowid"; - $sql .= " AND f.entity IN (".getEntity('facture').")"; - $sql .= " AND f.fk_statut = ".Facture::STATUS_DRAFT; - if ($socid) { - $sql .= " AND f.fk_soc = ".((int) $socid); - } - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; - } - $sql .= $db->plimit($maxCount, 0); - - $resql = $db->query($sql); - if (!$resql) { - dol_print_error($db); - return ''; - } - - $num = $db->num_rows($resql); - - $result = '
'; - $result .= ''; - - $result .= ''; - $result .= ''; - $result .= ''; - - if ($num < 1) { - $result .= '
'; - $result .= $langs->trans("CustomersDraftInvoices"); - $result .= ' '; - $result .= ''.$num.''; - $result .= ''; - $result .= '
'; - $result .= '
'; - return $result; - } - - $objectstatic = new Facture($db); - $companystatic = new Societe($db); - $nbofloop = min($num, $maxCount); - $total = 0; - $i = 0; - - while ($i < $nbofloop) { - $obj = $db->fetch_object($resql); - - $objectstatic->id = $obj->rowid; - $objectstatic->ref = $obj->ref; - - $companystatic->id = $obj->socid; - $companystatic->name = $obj->socname; - $companystatic->client = $obj->client; - $companystatic->canvas = $obj->canvas; - - $result .= ''; - $result .= ''.$objectstatic->getNomUrl(1).''; - $result .= ''.$companystatic->getNomUrl(1, 'customer', 24).''; - $result .= ''.price($obj->total_ttc).''; - $result .= ''; - - $i++; - $total += $obj->total_ttc; - } - - if ($num > $nbofloop) { - $result .= ''; - $result .= ''.$langs->trans("XMoreLines", ($num - $nbofloop)).''; - $result .= ''; - } elseif ($total > 0) { - $result .= ''; - $result .= ''.$langs->trans("Total").''; - $result .= ''.price($total).''; - $result .= ''; - } - - $result .= ''; - $result .= ''; - return $result; -} - -/** - * Return a HTML table that contains a list with latest edited customer invoices - * - * @param int $maxCount (Optional) The maximum count of elements inside the table - * @param int $socid (Optional) Show only results from the customer with this id - * @return string A HTML table that contains a list with latest edited customer invoices - */ -function getLatestEditTable($maxCount = 5, $socid = 0) -{ - global $conf, $db, $langs, $user; - - $sql = "SELECT f.rowid, f.entity, f.ref, f.fk_statut as status, f.paye, s.nom as socname, s.rowid as socid, s.canvas, s.client,"; - $sql .= " f.datec"; - $sql .= " FROM ".MAIN_DB_PREFIX."facture as f"; - $sql .= ", ".MAIN_DB_PREFIX."societe as s"; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE f.fk_soc = s.rowid"; - $sql .= " AND f.entity IN (".getEntity('facture').")"; - if ($socid) { - $sql .= " AND f.fk_soc = ".((int) $socid); - } - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; - } - $sql .= " ORDER BY f.tms DESC"; - $sql .= $db->plimit($maxCount, 0); - - $resql = $db->query($sql); - if (!$resql) { - dol_print_error($db); - } - - $num = $db->num_rows($resql); - - $result = '
'; - $result .= ''; - - $result .= ''; - $result .= ''; - $result .= ''; - - if ($num < 1) { - $result .= '
'.$langs->trans("LastCustomersBills", $maxCount).'
'; - $result .= '
'; - return $result; - } - - $formfile = new FormFile($db); - $objectstatic = new Facture($db); - $companystatic = new Societe($db); - $i = 0; - - while ($i < $num) { - $obj = $db->fetch_object($resql); - - $objectstatic->id = $obj->rowid; - $objectstatic->ref = $obj->ref; - $objectstatic->paye = $obj->paye; - $objectstatic->statut = $obj->status; - - $companystatic->id = $obj->socid; - $companystatic->name = $obj->socname; - $companystatic->client = $obj->client; - $companystatic->canvas = $obj->canvas; - - $filename = dol_sanitizeFileName($obj->ref); - $filedir = $conf->propal->multidir_output[$obj->entity].'/'.$filename; - - $result .= ''; - - $result .= ''; - $result .= ''; - $result .= ''; - - $result .= ''; - $result .= ''; - $result .= ''; - - $result .= ''; - $result .= '
'.$objectstatic->getNomUrl(1).' '.$formfile->getDocumentsLink($objectstatic->element, $filename, $filedir).'
'; - $result .= ''; - - $result .= ''.$companystatic->getNomUrl(1, 'customer').''; - $result .= ''.dol_print_date($db->jdate($obj->datec), 'day').''; - $result .= ''.$objectstatic->getLibStatut(5).''; - - $result .= ''; - - $i++; - } - - $result .= ''; - $result .= ''; - return $result; -} - -/** - * Return a HTML table that contains a list with open (unpaid) customer invoices - * - * @param int $maxCount (Optional) The maximum count of elements inside the table - * @param int $socid (Optional) Show only results from the customer with this id - * @return string A HTML table that conatins a list with open (unpaid) customer invoices - */ -function getOpenTable($maxCount = 500, $socid = 0) -{ - global $conf, $db, $langs, $user; - - $sql = "SELECT s.nom as socname, s.rowid as socid, s.canvas, s.client"; - $sql .= ", f.rowid as id, f.entity, f.total as total_ttc, f.total as total_ht, f.ref, f.fk_statut"; - $sql .= ", f.datef as df, f.date_lim_reglement as datelimite"; - $sql .= " FROM ".MAIN_DB_PREFIX."societe as s"; - $sql .= ", ".MAIN_DB_PREFIX."facture as f"; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE f.fk_soc = s.rowid"; - $sql .= " AND f.entity IN (".getEntity('facture').")"; - $sql .= " AND f.fk_statut = ".Facture::STATUS_VALIDATED; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; - } - if ($socid) { - $sql .= " AND s.rowid = ".((int) $socid); - } - $sql .= " ORDER BY f.rowid DESC"; - $sql .= $db->plimit($maxCount, 0); - - $resql = $db->query($sql); - if (!$resql) { - dol_print_error($db); - } - - $num = $db->num_rows($resql); - - $result = '
'; - $result .= ''; - - $result .= ''; - $result .= ''; - $result .= ''; - - if ($num < 1) { - $result .= '
'; - $result .= $langs->trans("BillsCustomersUnpaid"); - $result .= ' '; - $result .= ''.$num.''; - $result .= ''; - $result .= '
'; - $result .= '
'; - return $result; - } - - $objectstatic = new Facture($db); - $companystatic = new Societe($db); - $formfile = new FormFile($db); - $nbofloop = min($num, $maxCount); - $now = dol_now(); - $total = 0; - $i = 0; - - while ($i < $nbofloop) { - $obj = $db->fetch_object($resql); - - $objectstatic->id = $obj->id; - $objectstatic->ref = $obj->ref; - - $companystatic->id = $obj->socid; - $companystatic->name = $obj->socname; - $companystatic->client = $obj->client; - $companystatic->canvas = $obj->canvas; - - $filename = dol_sanitizeFileName($obj->ref); - $filedir = $conf->propal->multidir_output[$obj->entity].'/'.dol_sanitizeFileName($obj->ref); - - $result .= ''; - - $result .= ''; - $result .= ''; - $result .= ''; - - $result .= ''; - - $result .= ''; - - $result .= ''; - - $result .= ''; - $result .= '
'.$objectstatic->getNomUrl(1).''; - - if ($db->jdate($obj->dfv) < ($now - $conf->propal->cloture->warning_delay)) { - $result .= img_warning($langs->trans("Late")); - } - - $result .= ''.$formfile->getDocumentsLink($objectstatic->element, $filename, $filedir).'
'; - $result .= ''; - - $result .= ''.$companystatic->getNomUrl(1, 'customer', 44).''; - $result .= ''.dol_print_date($db->jdate($obj->df), 'day').''; - $result .= ''.price($obj->total_ttc).''; - - $result .= ''; - - $i++; - $total += $obj->total_ttc; - } - - if ($num > $nbofloop) { - $result .= ''; - $result .= ''.$langs->trans("XMoreLines", ($num - $nbofloop)).''; - $result .= ''; - } elseif ($total > 0) { - $result .= ''; - $result .= ''.$langs->trans("Total").''; - $result .= ''.price($total).''; - $result .= ' '; - $result .= ''; - } - - $result .= ''; - $result .= ''; - return $result; -} diff --git a/htdocs/compta/index.php b/htdocs/compta/index.php index a24bce0e0e2..8823e399d77 100644 --- a/htdocs/compta/index.php +++ b/htdocs/compta/index.php @@ -38,6 +38,8 @@ require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php'; require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php'; require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php'; require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/invoice.lib.php'; // L'espace compta/treso doit toujours etre actif car c'est un espace partage // par de nombreux modules (banque, facture, commande a facturer, etc...) independamment @@ -64,13 +66,21 @@ if ($user->socid > 0) { $max = $conf->global->MAIN_SIZE_SHORTLIST_LIMIT; +// Maximum elements of the tables +$maxDraftCount = empty($conf->global->MAIN_MAXLIST_OVERLOAD) ? 500 : $conf->global->MAIN_MAXLIST_OVERLOAD; +$maxLatestEditCount = 5; +$maxOpenCount = empty($conf->global->MAIN_MAXLIST_OVERLOAD) ? 500 : $conf->global->MAIN_MAXLIST_OVERLOAD; + // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array $hookmanager->initHooks(array('invoiceindex')); + /* * Actions */ +// None + /* * View @@ -89,301 +99,19 @@ print load_fiche_titre($langs->trans("AccountancyTreasuryArea"), '', 'bill'); print '
'; - -if (!empty($conf->global->MAIN_SEARCH_FORM_ON_HOME_AREAS)) { // This is useless due to the global search combo - // Search customer invoices - if (!empty($conf->facture->enabled) && $user->rights->facture->lire) { - $listofsearchfields['search_invoice'] = array('text'=>'CustomerInvoice'); - } - // Search supplier invoices - if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) || !empty($conf->supplier_order->enabled) || !empty($conf->supplier_invoice->enabled)) && $user->rights->fournisseur->lire) { - $listofsearchfields['search_supplier_invoice'] = array('text'=>'SupplierInvoice'); - } - if (!empty($conf->don->enabled) && $user->rights->don->lire) { - $langs->load("donations"); - $listofsearchfields['search_donation'] = array('text'=>'Donation'); - } - - if (count($listofsearchfields)) { - print '
'; - print ''; - print '
'; - print ''; - $i = 0; - foreach ($listofsearchfields as $key => $value) { - if ($i == 0) { - print ''; - } - print ''; - print ''; - if ($i == 0) { - print ''; - } - print ''; - $i++; - } - print '
'.$langs->trans("Search").'
'; - print '
'; - print '
'; - print '
'; - } -} - - -/** - * Draft customers invoices - */ -if (!empty($conf->facture->enabled) && $user->rights->facture->lire) { - $tmpinvoice = new Facture($db); - - $sql = "SELECT f.rowid, f.ref, f.datef as date, f.total as total_ht, f.tva as total_tva, f.total_ttc, f.ref_client"; - $sql .= ", f.type, f.fk_statut as status, f.paye"; - $sql .= ", s.nom as name"; - $sql .= ", s.rowid as socid, s.email"; - $sql .= ", s.code_client, s.code_compta, s.code_fournisseur, s.code_compta_fournisseur"; - $sql .= ", cc.rowid as country_id, cc.code as country_code"; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= ", sc.fk_soc, sc.fk_user "; - } - $sql .= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."societe as s LEFT JOIN ".MAIN_DB_PREFIX."c_country as cc ON cc.rowid = s.fk_pays"; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE s.rowid = f.fk_soc AND f.fk_statut = ".Facture::STATUS_DRAFT; - $sql .= " AND f.entity IN (".getEntity('invoice').")"; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; - } - - if ($socid) { - $sql .= " AND f.fk_soc = $socid"; - } - // Add where from hooks - $parameters = array(); - $reshook = $hookmanager->executeHooks('printFieldListWhereCustomerDraft', $parameters); - $sql .= $hookmanager->resPrint; - - $sql .= " GROUP BY f.rowid, f.ref, f.datef, f.total, f.tva, f.total_ttc, f.ref_client, f.type, f.fk_statut, f.paye,"; - $sql .= " s.nom, s.rowid, s.email, s.code_client, s.code_compta, s.code_fournisseur, s.code_compta_fournisseur,"; - $sql .= " cc.rowid, cc.code"; - if (!$user->rights->societe->client->voir && !$socid) { - $sql.= ", sc.fk_soc, sc.fk_user"; - } - - // Add Group from hooks - $parameters = array(); - $reshook = $hookmanager->executeHooks('printFieldListGroupByCustomerDraft', $parameters); - $sql .= $hookmanager->resPrint; - - $resql = $db->query($sql); - - if ($resql) { - $num = $db->num_rows($resql); - - print '
'; - print ''; - - print ''; - print ''; - print ''; - - if ($num) { - $companystatic = new Societe($db); - - $i = 0; - $othernb = 0; - $tot_ttc = 0; - while ($i < $num && $i < $conf->liste_limit) { - $obj = $db->fetch_object($resql); - - if ($i >= $max) { - $othernb += 1; - $i++; - $tot_ttc += $obj->total_ttc; - continue; - } - - $tmpinvoice->id = $obj->rowid; - $tmpinvoice->ref = $obj->ref; - $tmpinvoice->date = $db->jdate($obj->date); - $tmpinvoice->type = $obj->type; - $tmpinvoice->total_ht = $obj->total_ht; - $tmpinvoice->total_tva = $obj->total_tva; - $tmpinvoice->total_ttc = $obj->total_ttc; - $tmpinvoice->ref_client = $obj->ref_client; - $tmpinvoice->statut = $obj->status; - $tmpinvoice->paye = $obj->paye; - - $companystatic->id = $obj->socid; - $companystatic->name = $obj->name; - $companystatic->email = $obj->email; - $companystatic->country_id = $obj->country_id; - $companystatic->country_code = $obj->country_code; - $companystatic->client = 1; - $companystatic->code_client = $obj->code_client; - $companystatic->code_fournisseur = $obj->code_fournisseur; - $companystatic->code_compta = $obj->code_compta; - $companystatic->code_compta_fournisseur = $obj->code_compta_fournisseur; - - print ''; - print ''; - print ''; - print ''; - $tot_ttc += $obj->total_ttc; - $i++; - } - - if ($othernb) { - print ''; - print ''; - print "\n"; - } - - print ''; - print ''; - print ''; - } else { - print ''; - } - print "
'; - print $langs->trans("CustomersDraftInvoices").' '; - print ''; - print ''.$num.''; - print ''; - print '
'; - print $tmpinvoice->getNomUrl(1, ''); - print ''; - print $companystatic->getNomUrl(1, 'customer'); - print ''.price($obj->total_ttc).'
'; - print ''.$langs->trans("More").'... ('.$othernb.')'; - print '
'.$langs->trans("Total").''.price($tot_ttc).'
'.$langs->trans("NoInvoice").'

"; - $db->free($resql); - } else { - dol_print_error($db); - } -} - -/** - * Draft suppliers invoices - */ -if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) || !empty($conf->supplier_invoice->enabled)) && $user->rights->fournisseur->facture->lire) { - $facturesupplierstatic = new FactureFournisseur($db); - - $sql = "SELECT f.ref, f.rowid, f.total_ht, f.total_tva, f.total_ttc, f.type, f.ref_supplier, f.fk_statut as status, f.paye"; - $sql .= ", s.nom as name"; - $sql .= ", s.rowid as socid, s.email"; - $sql .= ", s.code_client, s.code_compta"; - $sql .= ", s.code_fournisseur, s.code_compta_fournisseur"; - $sql .= ", cc.rowid as country_id, cc.code as country_code"; - $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f, ".MAIN_DB_PREFIX."societe as s LEFT JOIN ".MAIN_DB_PREFIX."c_country as cc ON cc.rowid = s.fk_pays"; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE s.rowid = f.fk_soc AND f.fk_statut = ".FactureFournisseur::STATUS_DRAFT; - $sql .= " AND f.entity IN (".getEntity('invoice').')'; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; - } - if ($socid) { - $sql .= " AND f.fk_soc = ".$socid; - } - // Add where from hooks - $parameters = array(); - $reshook = $hookmanager->executeHooks('printFieldListWhereSupplierDraft', $parameters); - $sql .= $hookmanager->resPrint; - $resql = $db->query($sql); - - if ($resql) { - $num = $db->num_rows($resql); - - print '
'; - print ''; - - print ''; - print ''; - print ''; - - if ($num) { - $companystatic = new Societe($db); - - $i = 0; - $othernb = 0; - $tot_ttc = 0; - while ($i < $num && $i < $conf->liste_limit) { - $obj = $db->fetch_object($resql); - - if ($i >= $max) { - $othernb += 1; - $i++; - $tot_ttc += $obj->total_ttc; - continue; - } - - $facturesupplierstatic->ref = $obj->ref; - $facturesupplierstatic->id = $obj->rowid; - $facturesupplierstatic->total_ht = $obj->total_ht; - $facturesupplierstatic->total_tva = $obj->total_tva; - $facturesupplierstatic->total_ttc = $obj->total_ttc; - $facturesupplierstatic->ref_supplier = $obj->ref_supplier; - $facturesupplierstatic->type = $obj->type; - $facturesupplierstatic->statut = $obj->status; - $facturesupplierstatic->paye = $obj->paye; - - $companystatic->id = $obj->socid; - $companystatic->name = $obj->name; - $companystatic->email = $obj->email; - $companystatic->country_id = $obj->country_id; - $companystatic->country_code = $obj->country_code; - $companystatic->fournisseur = 1; - $companystatic->code_client = $obj->code_client; - $companystatic->code_fournisseur = $obj->code_fournisseur; - $companystatic->code_compta = $obj->code_compta; - $companystatic->code_compta_fournisseur = $obj->code_compta_fournisseur; - - print ''; - print ''; - print ''; - print ''; - $tot_ttc += $obj->total_ttc; - $i++; - } - - if ($othernb) { - print ''; - print ''; - print "\n"; - } - - print ''; - print ''; - print ''; - } else { - print ''; - } - print "
'; - print $langs->trans("SuppliersDraftInvoices").' '; - print ''; - print ''.$num.''; - print ''; - print '
'; - print $facturesupplierstatic->getNomUrl(1, ''); - print ''; - print $companystatic->getNomUrl(1, 'supplier'); - print ''.price($obj->total_ttc).'
'; - print ''.$langs->trans("More").'... ('.$othernb.')'; - print '
'.$langs->trans("Total").''.price($tot_ttc).'
'.$langs->trans("NoInvoice").'

"; - $db->free($resql); - } else { - dol_print_error($db); - } -} - +print getCustomerInvoicePieChart($socid); +print '
'; +print getPurchaseInvoicePieChart($socid); +print '
'; +print getCustomerInvoiceDraftTable($max, $socid); +print '
'; +print getDraftSupplierTable($max, $socid); print '
'; // Latest modified customer invoices -if (!empty($conf->facture->enabled) && $user->rights->facture->lire) { +if (!empty($conf->facture->enabled) && !empty($user->rights->facture->lire)) { $langs->load("boxes"); $tmpinvoice = new Facture($db); @@ -495,7 +223,7 @@ if (!empty($conf->facture->enabled) && $user->rights->facture->lire) { if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { print ''.price($obj->total_ht).''; } - print ''.price($obj->total_ttc).''; + print ''.price($obj->total_ttc).''; print ''.dol_print_date($db->jdate($obj->tms), 'day').''; print ''.$tmpinvoice->getLibStatut(3, $obj->am).''; print ''; @@ -529,9 +257,8 @@ if (!empty($conf->facture->enabled) && $user->rights->facture->lire) { } - // Last modified supplier invoices -if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) || !empty($conf->supplier_invoice->enabled)) && $user->rights->fournisseur->facture->lire) { +if (((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || !empty($conf->supplier_invoice->enabled)) && $user->rights->fournisseur->facture->lire) { $langs->load("boxes"); $facstatic = new FactureFournisseur($db); @@ -622,7 +349,7 @@ if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SU if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { print ''.price($obj->total_ht).''; } - print ''.price($obj->total_ttc).''; + print ''.price($obj->total_ttc).''; print ''.dol_print_date($db->jdate($obj->tms), 'day').''; print ''.$facstatic->getLibStatut(3).''; print ''; @@ -655,7 +382,7 @@ if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SU // Latest donations -if (!empty($conf->don->enabled) && $user->rights->don->lire) { +if (!empty($conf->don->enabled) && !empty($user->rights->don->lire)) { include_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php'; $langs->load("boxes"); @@ -718,7 +445,7 @@ if (!empty($conf->don->enabled) && $user->rights->don->lire) { print ''; print ''.$donationstatic->getNomUrl(1).''; print ''.$label.''; - print ''.price($objp->amount).''; + print ''.price($objp->amount).''; print ''.dol_print_date($db->jdate($objp->dm), 'day').''; print ''.$donationstatic->getLibStatut(3).''; print ''; @@ -745,7 +472,7 @@ if (!empty($conf->don->enabled) && $user->rights->don->lire) { /** * Social contributions to pay */ -if (!empty($conf->tax->enabled) && $user->rights->tax->charges->lire) { +if (!empty($conf->tax->enabled) && !empty($user->rights->tax->charges->lire)) { if (!$socid) { $chargestatic = new ChargeSociales($db); @@ -802,8 +529,8 @@ if (!empty($conf->tax->enabled) && $user->rights->tax->charges->lire) { print ''; print ''.$chargestatic->getNomUrl(1).''; print ''.dol_print_date($db->jdate($obj->date_ech), 'day').''; - print ''.price($obj->amount).''; - print ''.price($obj->sumpaid).''; + print ''.price($obj->amount).''; + print ''.price($obj->sumpaid).''; print ''.$chargestatic->getLibStatut(3).''; print ''; @@ -953,8 +680,8 @@ if (!empty($conf->facture->enabled) && !empty($conf->commande->enabled) && $user if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { print ''.price($obj->total_ht).''; } - print ''.price($obj->total_ttc).''; - print ''.price($obj->total_ttc - $obj->tot_fttc).''; + print ''.price($obj->total_ttc).''; + print ''.price($obj->total_ttc - $obj->tot_fttc).''; print ''.$commandestatic->getLibStatut(3).''; print ''; $tot_ht += $obj->total_ht; @@ -974,10 +701,10 @@ if (!empty($conf->facture->enabled) && !empty($conf->commande->enabled) && $user print ''.$langs->trans("Total").'   ('.$langs->trans("RemainderToBill").': '.price($tot_tobill).') '; if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { - print ''.price($tot_ht).''; + print ''.price($tot_ht).''; } - print ''.price($tot_ttc).''; - print ''.price($tot_tobill).''; + print ''.price($tot_ttc).''; + print ''.price($tot_tobill).''; print ' '; print ''; print '

'; @@ -988,328 +715,9 @@ if (!empty($conf->facture->enabled) && !empty($conf->commande->enabled) && $user } } -/* - * Unpaid customers invoices - */ -if (!empty($conf->facture->enabled) && $user->rights->facture->lire) { - $tmpinvoice = new Facture($db); - - $sql = "SELECT f.rowid, f.ref, f.fk_statut as status, f.datef, f.type, f.total as total_ht, f.tva as total_tva, f.total_ttc, f.paye, f.tms"; - $sql .= ", f.date_lim_reglement as datelimite"; - $sql .= ", s.nom as name"; - $sql .= ", s.rowid as socid, s.email"; - $sql .= ", s.code_client, s.code_compta"; - $sql .= ", s.code_fournisseur, s.code_compta_fournisseur"; - $sql .= ", cc.rowid as country_id, cc.code as country_code"; - $sql .= ", sum(pf.amount) as am"; - $sql .= " FROM ".MAIN_DB_PREFIX."societe as s LEFT JOIN ".MAIN_DB_PREFIX."c_country as cc ON cc.rowid = s.fk_pays,".MAIN_DB_PREFIX."facture as f"; - $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf on f.rowid=pf.fk_facture"; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE s.rowid = f.fk_soc AND f.paye = 0 AND f.fk_statut = ".Facture::STATUS_VALIDATED; - $sql .= " AND f.entity IN (".getEntity('invoice').')'; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; - } - if ($socid) { - $sql .= " AND f.fk_soc = ".$socid; - } - // Add where from hooks - $parameters = array(); - $reshook = $hookmanager->executeHooks('printFieldListWhereCustomerUnpaid', $parameters); - $sql .= $hookmanager->resPrint; - - $sql .= " GROUP BY f.rowid, f.ref, f.fk_statut, f.datef, f.type, f.total, f.tva, f.total_ttc, f.paye, f.tms, f.date_lim_reglement,"; - $sql .= " s.nom, s.rowid, s.email, s.code_client, s.code_compta, cc.rowid, cc.code"; - $sql .= ", s.code_fournisseur, s.code_compta_fournisseur"; - $sql .= " ORDER BY f.datef ASC, f.ref ASC"; - - $resql = $db->query($sql); - if ($resql) { - $num = $db->num_rows($resql); - $i = 0; - $othernb = 0; - - print '
'; - print ''; - - print ''; - print ''; - - print ''; - if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { - print ''; - } - print ''; - print ''; - print ''; - print ''; - if ($num) { - $societestatic = new Societe($db); - $total_ttc = $totalam = $total = 0; - while ($i < $num && $i < $conf->liste_limit) { - $obj = $db->fetch_object($resql); - - if ($i >= $max) { - $othernb += 1; - $i++; - $total += $obj->total_ht; - $total_ttc += $obj->total_ttc; - continue; - } - - $tmpinvoice->ref = $obj->ref; - $tmpinvoice->id = $obj->rowid; - $tmpinvoice->total_ht = $obj->total_ht; - $tmpinvoice->total_tva = $obj->total_tva; - $tmpinvoice->total_ttc = $obj->total_ttc; - $tmpinvoice->type = $obj->type; - $tmpinvoice->statut = $obj->status; - $tmpinvoice->paye = $obj->paye; - $tmpinvoice->date_lim_reglement = $db->jdate($obj->datelimite); - - $societestatic->id = $obj->socid; - $societestatic->name = $obj->name; - $societestatic->email = $obj->email; - $societestatic->country_id = $obj->country_id; - $societestatic->country_code = $obj->country_code; - $societestatic->client = 1; - $societestatic->code_client = $obj->code_client; - $societestatic->code_fournisseur = $obj->code_fournisseur; - $societestatic->code_compta = $obj->code_compta; - $societestatic->code_compta_fournisseur = $obj->code_compta_fournisseur; - - print ''; - print ''; - print ''; - print ''; - if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { - print ''; - } - print ''; - print ''; - print ''; - print ''; - - $total_ttc += $obj->total_ttc; - $total += $obj->total_ht; - $totalam += $obj->am; - - $i++; - } - - if ($othernb) { - $colspan = 6; - if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { - $colspan++; - } - print ''; - print ''; - print "\n"; - } - - print ''; - print ''; - if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { - print ''; - } - print ''; - print ''; - print ''; - print ''; - } else { - $colspan = 6; - if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { - $colspan++; - } - print ''; - } - print '
'; - print $langs->trans("BillsCustomersUnpaid", $num).' '; - print ''; - print ''.$num.''; - print ''; - print ''.$langs->trans("DateDue").''.$langs->trans("AmountHT").''.$langs->trans("AmountTTC").''.$langs->trans("Received").' 
'; - - print ''; - print ''; - print ''; - print '
'; - print $tmpinvoice->getNomUrl(1, ''); - print ''; - if ($tmpinvoice->hasDelay()) { - print img_warning($langs->trans("Late")); - } - print ''; - $filename = dol_sanitizeFileName($obj->ref); - $filedir = $conf->facture->dir_output.'/'.dol_sanitizeFileName($obj->ref); - $urlsource = $_SERVER['PHP_SELF'].'?facid='.$obj->rowid; - print $formfile->getDocumentsLink($tmpinvoice->element, $filename, $filedir); - print '
'; - - print '
'; - print $societestatic->getNomUrl(1, 'customer'); - print ''.dol_print_date($db->jdate($obj->datelimite), 'day').''.price($obj->total_ht).''.price($obj->total_ttc).''.price($obj->am).''.$tmpinvoice->getLibStatut(3, $obj->am).'
'; - print ''.$langs->trans("More").'... ('.$othernb.')'; - print '
'.$langs->trans("Total").'   ('.$langs->trans("RemainderToTake").': '.price($total_ttc - $totalam).')  '.price($total).''.price($total_ttc).''.price($totalam).' 
'.$langs->trans("NoInvoice").'

'; - $db->free($resql); - } else { - dol_print_error($db); - } -} - -/* - * Unpaid supplier invoices - */ -if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) || !empty($conf->supplier_invoice->enabled)) && $user->rights->fournisseur->facture->lire) { - $facstatic = new FactureFournisseur($db); - - $sql = "SELECT ff.rowid, ff.ref, ff.fk_statut as status, ff.type, ff.libelle as label, ff.total_ht, ff.total_tva, ff.total_ttc, ff.paye"; - $sql .= ", ff.date_lim_reglement"; - $sql .= ", s.nom as name"; - $sql .= ", s.rowid as socid, s.email"; - $sql .= ", s.code_client, s.code_compta"; - $sql .= ", s.code_fournisseur, s.code_compta_fournisseur"; - $sql .= ", sum(pf.amount) as am"; - $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."facture_fourn as ff"; - $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf on ff.rowid=pf.fk_facturefourn"; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE s.rowid = ff.fk_soc"; - $sql .= " AND ff.entity = ".$conf->entity; - $sql .= " AND ff.paye = 0"; - $sql .= " AND ff.fk_statut = ".FactureFournisseur::STATUS_VALIDATED; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; - } - if ($socid) { - $sql .= " AND ff.fk_soc = ".$socid; - } - // Add where from hooks - $parameters = array(); - $reshook = $hookmanager->executeHooks('printFieldListWhereSupplierUnpaid', $parameters); - $sql .= $hookmanager->resPrint; - - $sql .= " GROUP BY ff.rowid, ff.ref, ff.fk_statut, ff.type, ff.libelle, ff.total_ht, ff.tva, ff.total_tva, ff.total_ttc, ff.paye, ff.date_lim_reglement,"; - $sql .= " s.nom, s.rowid, s.email, s.code_client, s.code_fournisseur, s.code_compta, s.code_compta_fournisseur"; - $sql .= " ORDER BY ff.date_lim_reglement ASC"; - - $resql = $db->query($sql); - if ($resql) { - $num = $db->num_rows($resql); - $othernb = 0; - - print '
'; - print ''; - - print ''; - print ''; - - print ''; - if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { - print ''; - } - print ''; - print ''; - print ''; - print "\n"; - $societestatic = new Societe($db); - if ($num) { - $i = 0; - $total = $total_ttc = $totalam = 0; - while ($i < $num) { - $obj = $db->fetch_object($resql); - - if ($i >= $max) { - $othernb += 1; - $i++; - $total += $obj->total_ht; - $total_ttc += $obj->total_ttc; - continue; - } - - $facstatic->ref = $obj->ref; - $facstatic->id = $obj->rowid; - $facstatic->type = $obj->type; - $facstatic->total_ht = $obj->total_ht; - $facstatic->total_tva = $obj->total_tva; - $facstatic->total_ttc = $obj->total_ttc; - $facstatic->statut = $obj->status; - $facstatic->paye = $obj->paye; - - $societestatic->id = $obj->socid; - $societestatic->name = $obj->name; - $societestatic->email = $obj->email; - $societestatic->client = 0; - $societestatic->fournisseur = 1; - $societestatic->code_client = $obj->code_client; - $societestatic->code_fournisseur = $obj->code_fournisseur; - $societestatic->code_compta = $obj->code_compta; - $societestatic->code_compta_fournisseur = $obj->code_compta_fournisseur; - - print ''; - print ''; - print ''; - if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { - print ''; - } - print ''; - print ''; - print ''; - print ''; - $total += $obj->total_ht; - $total_ttc += $obj->total_ttc; - $totalam += $obj->am; - $i++; - } - - if ($othernb) { - $colspan = 6; - if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { - $colspan++; - } - print ''; - print ''; - print "\n"; - } - - print ''; - print ''; - if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { - print ''; - } - print ''; - print ''; - print ''; - print ''; - } else { - $colspan = 6; - if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { - $colspan++; - } - print ''; - } - print '
'; - print $langs->trans("BillsSuppliersUnpaid", $num).' '; - print ''; - print ''.$num.''; - print ''; - print ''.$langs->trans("DateDue").''.$langs->trans("AmountHT").''.$langs->trans("AmountTTC").''.$langs->trans("Paid").' 
'; - print $facstatic->getNomUrl(1, ''); - print ''.$societestatic->getNomUrl(1, 'supplier').''.dol_print_date($db->jdate($obj->date_lim_reglement), 'day').''.price($obj->total_ht).''.price($obj->total_ttc).''.price($obj->am).''.$facstatic->getLibStatut(3, $obj->am).'
'; - print ''.$langs->trans("More").'... ('.$othernb.')'; - print '
'.$langs->trans("Total").'   ('.$langs->trans("RemainderToPay").': '.price($total_ttc - $totalam).')  '.price($total).''.price($total_ttc).''.price($totalam).' 
'.$langs->trans("NoInvoice").'

'; - } else { - dol_print_error($db); - } -} - - // TODO Mettre ici recup des actions en rapport avec la compta -$resql = 0; +$resql = ''; if ($resql) { print '
'; print ''; diff --git a/htdocs/core/lib/invoice.lib.php b/htdocs/core/lib/invoice.lib.php index 89e7f8c8e55..acf714be32b 100644 --- a/htdocs/core/lib/invoice.lib.php +++ b/htdocs/core/lib/invoice.lib.php @@ -215,3 +215,1023 @@ function invoice_rec_prepare_head($object) return $head; } + +/** + * Return a HTML table that contains a pie chart of customer invoices + * + * @param int $socid (Optional) Show only results from the customer with this id + * @return string A HTML table that contains a pie chart of customer invoices + */ +function getCustomerInvoicePieChart($socid = 0) +{ + global $conf, $db, $langs, $user; + + if (empty($conf->facture->enabled) || empty($user->rights->facture->lire)) { + return ''; + } + + $sql = "SELECT count(f.rowid), f.fk_statut"; + $sql .= " FROM ".MAIN_DB_PREFIX."societe as s"; + $sql .= ", ".MAIN_DB_PREFIX."facture as f"; + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + } + $sql .= " WHERE f.fk_soc = s.rowid"; + $sql .= " AND f.entity IN (".getEntity('facture').")"; + if ($user->socid) { + $sql .= ' AND f.fk_soc = '.$user->socid; + } + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; + } + $sql .= " GROUP BY f.fk_statut"; + + $resql = $db->query($sql); + if (!$resql) { + dol_print_error($db); + return ''; + } + + $num = $db->num_rows($resql); + $i = 0; + + $total = 0; + $vals = []; + + while ($i < $num) { + $row = $db->fetch_row($resql); + if ($row) { + $vals[$row[1]] = $row[0]; + $total += $row[0]; + } + + $i++; + } + + $db->free($resql); + + $result = '
'; + $result .= '
'; + $result .= ''; + $result .= ''; + $result .= ''; + + $objectstatic = new Facture($db); + $array = [Facture::STATUS_DRAFT, Facture::STATUS_VALIDATED, Facture::STATUS_CLOSED, Facture::STATUS_ABANDONED]; + $dataseries = []; + + foreach ($array as $status) { + $objectstatic->statut = $status; + $objectstatic->paye = $status == Facture::STATUS_CLOSED ? -1 : 0; + + $dataseries[] = [$objectstatic->getLibStatut(1), (isset($vals[$status]) ? (int) $vals[$status] : 0)]; + if (!$conf->use_javascript_ajax) { + $result .= ''; + $result .= ''; + $result .= ''; + $result .= ''; + } + } + + if ($conf->use_javascript_ajax) { + $dolgraph = new DolGraph(); + $dolgraph->SetData($dataseries); + $dolgraph->setShowLegend(2); + $dolgraph->setShowPercent(1); + $dolgraph->SetType(['pie']); + $dolgraph->setHeight('200'); + $dolgraph->draw('idgraphcustomerinvoices'); + + $result .= ''; + $result .= ''; + $result .= ''; + } + + $result .= ''; + $result .= ''; + $result .= ''; + $result .= ''; + + $result .= '
'.$langs->trans("Statistics").' - '.$langs->trans("CustomerInvoice").'
'.$objectstatic->getLibStatut(0).''.(isset($vals[$status]) ? $vals[$status] : 0).'
'.$dolgraph->show($total ? 0 : 1).'
'.$langs->trans("Total").''.$total.'
'; + $result .= '
'; + + return $result; +} + + +/** + * Return a HTML table that contains a pie chart of supplier invoices + * + * @param int $socid (Optional) Show only results from the supplier with this id + * @return string A HTML table that contains a pie chart of supplier invoices + */ +function getPurchaseInvoicePieChart($socid = 0) +{ + global $conf, $db, $langs, $user; + + if (!(!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) || !empty($conf->supplier_invoice->enabled)) && $user->rights->fournisseur->facture->lire) { + return ''; + } + + $sql = "SELECT count(f.rowid), f.fk_statut"; + $sql .= " FROM ".MAIN_DB_PREFIX."societe as s"; + $sql .= ", ".MAIN_DB_PREFIX."facture_fourn as f"; + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + } + $sql .= " WHERE f.fk_soc = s.rowid"; + $sql .= " AND f.entity IN (".getEntity('facture_fourn').")"; + if ($user->socid) { + $sql .= ' AND f.fk_soc = '.$user->socid; + } + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; + } + $sql .= " GROUP BY f.fk_statut"; + + $resql = $db->query($sql); + if (!$resql) { + dol_print_error($db); + return ''; + } + + $num = $db->num_rows($resql); + $i = 0; + + $total = 0; + $vals = []; + + while ($i < $num) { + $row = $db->fetch_row($resql); + if ($row) { + $vals[$row[1]] = $row[0]; + $total += $row[0]; + } + + $i++; + } + + $db->free($resql); + + $result = '
'; + $result .= ''; + + $result .= ''; + $result .= ''; + $result .= ''; + + $objectstatic = new FactureFournisseur($db); + $array = [FactureFournisseur::STATUS_DRAFT, FactureFournisseur::STATUS_VALIDATED, FactureFournisseur::STATUS_CLOSED, FactureFournisseur::STATUS_ABANDONED]; + $dataseries = []; + + foreach ($array as $status) { + $objectstatic->statut = $status; + $objectstatic->paye = $status == FactureFournisseur::STATUS_CLOSED ? -1 : 0; + + $dataseries[] = [$objectstatic->getLibStatut(1), (isset($vals[$status]) ? (int) $vals[$status] : 0)]; + if (!$conf->use_javascript_ajax) { + $result .= ''; + $result .= ''; + $result .= ''; + $result .= ''; + } + } + + if ($conf->use_javascript_ajax) { + $dolgraph = new DolGraph(); + $dolgraph->SetData($dataseries); + $dolgraph->setShowLegend(2); + $dolgraph->setShowPercent(1); + $dolgraph->SetType(['pie']); + $dolgraph->setHeight('200'); + $dolgraph->draw('idgraphpurchaseinvoices'); + + $result .= ''; + $result .= ''; + $result .= ''; + } + + $result .= ''; + $result .= ''; + $result .= ''; + $result .= ''; + + $result .= '
'.$langs->trans("Statistics").' - '.$langs->trans("SupplierInvoice").'
'.$objectstatic->getLibStatut(0).''.(isset($vals[$status]) ? $vals[$status] : 0).'
'.$dolgraph->show($total ? 0 : 1).'
'.$langs->trans("Total").''.$total.'
'; + $result .= '
'; + + return $result; +} + +/** + * Return a HTML table that contains a list with customer invoice drafts + * + * @param int $maxCount (Optional) The maximum count of elements inside the table + * @param int $socid (Optional) Show only results from the customer with this id + * @return string A HTML table that contains a list with customer invoice drafts + */ +function getCustomerInvoiceDraftTable($maxCount = 500, $socid = 0) +{ + global $conf, $db, $langs, $user, $hookmanager; + + $result = ''; + $tmpinvoice = new Facture($db); + + $sql = "SELECT f.rowid, f.ref, f.datef as date, f.total as total_ht, f.tva as total_tva, f.total_ttc, f.ref_client"; + $sql .= ", f.type, f.fk_statut as status, f.paye"; + $sql .= ", s.nom as name"; + $sql .= ", s.rowid as socid, s.email"; + $sql .= ", s.code_client, s.code_compta, s.code_fournisseur, s.code_compta_fournisseur"; + $sql .= ", cc.rowid as country_id, cc.code as country_code"; + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= ", sc.fk_soc, sc.fk_user "; + } + $sql .= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."societe as s LEFT JOIN ".MAIN_DB_PREFIX."c_country as cc ON cc.rowid = s.fk_pays"; + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + } + $sql .= " WHERE s.rowid = f.fk_soc AND f.fk_statut = ".Facture::STATUS_DRAFT; + $sql .= " AND f.entity IN (".getEntity('invoice').")"; + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; + } + + if ($socid) { + $sql .= " AND f.fk_soc = $socid"; + } + // Add where from hooks + $parameters = array(); + $reshook = $hookmanager->executeHooks('printFieldListWhereCustomerDraft', $parameters); + $sql .= $hookmanager->resPrint; + + $sql .= " GROUP BY f.rowid, f.ref, f.datef, f.total, f.tva, f.total_ttc, f.ref_client, f.type, f.fk_statut, f.paye,"; + $sql .= " s.nom, s.rowid, s.email, s.code_client, s.code_compta, s.code_fournisseur, s.code_compta_fournisseur,"; + $sql .= " cc.rowid, cc.code"; + if (!$user->rights->societe->client->voir && !$socid) { + $sql.= ", sc.fk_soc, sc.fk_user"; + } + + // Add Group from hooks + $parameters = array(); + $reshook = $hookmanager->executeHooks('printFieldListGroupByCustomerDraft', $parameters); + $sql .= $hookmanager->resPrint; + + $resql = $db->query($sql); + + if ($resql) { + $num = $db->num_rows($resql); + + $result .= '
'; + $result .= ''; + + $result .= ''; + $result .= ''; + $result .= ''; + + if ($num) { + $companystatic = new Societe($db); + + $i = 0; + $othernb = 0; + $tot_ttc = 0; + while ($i < $num) { + $obj = $db->fetch_object($resql); + + if ($i >= $maxCount) { + $othernb += 1; + $i++; + $tot_ttc += $obj->total_ttc; + continue; + } + + $tmpinvoice->id = $obj->rowid; + $tmpinvoice->ref = $obj->ref; + $tmpinvoice->date = $db->jdate($obj->date); + $tmpinvoice->type = $obj->type; + $tmpinvoice->total_ht = $obj->total_ht; + $tmpinvoice->total_tva = $obj->total_tva; + $tmpinvoice->total_ttc = $obj->total_ttc; + $tmpinvoice->ref_client = $obj->ref_client; + $tmpinvoice->statut = $obj->status; + $tmpinvoice->paye = $obj->paye; + + $companystatic->id = $obj->socid; + $companystatic->name = $obj->name; + $companystatic->email = $obj->email; + $companystatic->country_id = $obj->country_id; + $companystatic->country_code = $obj->country_code; + $companystatic->client = 1; + $companystatic->code_client = $obj->code_client; + $companystatic->code_fournisseur = $obj->code_fournisseur; + $companystatic->code_compta = $obj->code_compta; + $companystatic->code_compta_fournisseur = $obj->code_compta_fournisseur; + + $result .= ''; + $result .= ''; + $result .= ''; + $result .= ''; + $tot_ttc += $obj->total_ttc; + $i++; + } + + if ($othernb) { + $result .= ''; + $result .= ''; + $result .= "\n"; + } + + $result .= ''; + $result .= ''; + $result .= ''; + } else { + $result .= ''; + } + $result .= "
'; + $result .= $langs->trans("CustomersDraftInvoices").' '; + $result .= ''; + $result .= ''.$num.''; + $result .= ''; + $result .= '
'; + $result .= $tmpinvoice->getNomUrl(1, ''); + $result .= ''; + $result .= $companystatic->getNomUrl(1, 'customer'); + $result .= ''.price($obj->total_ttc).'
'; + $result .= ''.$langs->trans("More").'... ('.$othernb.')'; + $result .= '
'.$langs->trans("Total").''.price($tot_ttc).'
'.$langs->trans("NoInvoice").'
"; + $db->free($resql); + } else { + dol_print_error($db); + } + + return $result; +} + +/** + * Return a HTML table that contains a list with customer invoice drafts + * + * @param int $maxCount (Optional) The maximum count of elements inside the table + * @param int $socid (Optional) Show only results from the customer with this id + * @return string A HTML table that contains a list with customer invoice drafts + */ +function getDraftSupplierTable($maxCount = 500, $socid = 0) +{ + global $conf, $db, $langs, $user, $hookmanager; + + $result = ''; + $facturesupplierstatic = new FactureFournisseur($db); + + $sql = "SELECT f.ref, f.rowid, f.total_ht, f.total_tva, f.total_ttc, f.type, f.ref_supplier, f.fk_statut as status, f.paye"; + $sql .= ", s.nom as name"; + $sql .= ", s.rowid as socid, s.email"; + $sql .= ", s.code_client, s.code_compta"; + $sql .= ", s.code_fournisseur, s.code_compta_fournisseur"; + $sql .= ", cc.rowid as country_id, cc.code as country_code"; + $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f, ".MAIN_DB_PREFIX."societe as s LEFT JOIN ".MAIN_DB_PREFIX."c_country as cc ON cc.rowid = s.fk_pays"; + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + } + $sql .= " WHERE s.rowid = f.fk_soc AND f.fk_statut = ".FactureFournisseur::STATUS_DRAFT; + $sql .= " AND f.entity IN (".getEntity('invoice').')'; + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; + } + if ($socid) { + $sql .= " AND f.fk_soc = ".$socid; + } + // Add where from hooks + $parameters = array(); + $reshook = $hookmanager->executeHooks('printFieldListWhereSupplierDraft', $parameters); + $sql .= $hookmanager->resPrint; + $resql = $db->query($sql); + + if ($resql) { + $num = $db->num_rows($resql); + + $result .= '
'; + $result .= ''; + + $result .= ''; + $result .= ''; + $result .= ''; + + if ($num) { + $companystatic = new Societe($db); + + $i = 0; + $othernb = 0; + $tot_ttc = 0; + while ($i < $num) { + $obj = $db->fetch_object($resql); + + if ($i >= $maxCount) { + $othernb += 1; + $i++; + $tot_ttc += $obj->total_ttc; + continue; + } + + $facturesupplierstatic->ref = $obj->ref; + $facturesupplierstatic->id = $obj->rowid; + $facturesupplierstatic->total_ht = $obj->total_ht; + $facturesupplierstatic->total_tva = $obj->total_tva; + $facturesupplierstatic->total_ttc = $obj->total_ttc; + $facturesupplierstatic->ref_supplier = $obj->ref_supplier; + $facturesupplierstatic->type = $obj->type; + $facturesupplierstatic->statut = $obj->status; + $facturesupplierstatic->paye = $obj->paye; + + $companystatic->id = $obj->socid; + $companystatic->name = $obj->name; + $companystatic->email = $obj->email; + $companystatic->country_id = $obj->country_id; + $companystatic->country_code = $obj->country_code; + $companystatic->fournisseur = 1; + $companystatic->code_client = $obj->code_client; + $companystatic->code_fournisseur = $obj->code_fournisseur; + $companystatic->code_compta = $obj->code_compta; + $companystatic->code_compta_fournisseur = $obj->code_compta_fournisseur; + + $result .= ''; + $result .= ''; + $result .= ''; + $result .= ''; + $tot_ttc += $obj->total_ttc; + $i++; + } + + if ($othernb) { + $result .= ''; + $result .= ''; + $result .= "\n"; + } + + $result .= ''; + $result .= ''; + $result .= ''; + } else { + $result .= ''; + } + $result .= "
'; + $result .= $langs->trans("SuppliersDraftInvoices").' '; + $result .= ''; + $result .= ''.$num.''; + $result .= ''; + $result .= '
'; + $result .= $facturesupplierstatic->getNomUrl(1, ''); + $result .= ''; + $result .= $companystatic->getNomUrl(1, 'supplier'); + $result .= ''.price($obj->total_ttc).'
'; + $result .= ''.$langs->trans("More").'... ('.$othernb.')'; + $result .= '
'.$langs->trans("Total").''.price($tot_ttc).'
'.$langs->trans("NoInvoice").'
"; + $db->free($resql); + } else { + dol_print_error($db); + } + + return $result; +} + + +/** + * Return a HTML table that contains a list with latest edited customer invoices + * + * @param int $maxCount (Optional) The maximum count of elements inside the table + * @param int $socid (Optional) Show only results from the customer with this id + * @return string A HTML table that contains a list with latest edited customer invoices + */ +function getCustomerInvoiceLatestEditTable($maxCount = 5, $socid = 0) +{ + global $conf, $db, $langs, $user; + + $sql = "SELECT f.rowid, f.entity, f.ref, f.fk_statut as status, f.paye, s.nom as socname, s.rowid as socid, s.canvas, s.client,"; + $sql .= " f.datec"; + $sql .= " FROM ".MAIN_DB_PREFIX."facture as f"; + $sql .= ", ".MAIN_DB_PREFIX."societe as s"; + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + } + $sql .= " WHERE f.fk_soc = s.rowid"; + $sql .= " AND f.entity IN (".getEntity('facture').")"; + if ($socid) { + $sql .= " AND f.fk_soc = ".((int) $socid); + } + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; + } + $sql .= " ORDER BY f.tms DESC"; + $sql .= $db->plimit($maxCount, 0); + + $resql = $db->query($sql); + if (!$resql) { + dol_print_error($db); + } + + $num = $db->num_rows($resql); + + $result = '
'; + $result .= ''; + + $result .= ''; + $result .= ''; + $result .= ''; + + if ($num < 1) { + $result .= '
'.$langs->trans("LastCustomersBills", $maxCount).'
'; + $result .= '
'; + return $result; + } + + $formfile = new FormFile($db); + $objectstatic = new Facture($db); + $companystatic = new Societe($db); + $i = 0; + + while ($i < $num) { + $obj = $db->fetch_object($resql); + + $objectstatic->id = $obj->rowid; + $objectstatic->ref = $obj->ref; + $objectstatic->paye = $obj->paye; + $objectstatic->statut = $obj->status; + + $companystatic->id = $obj->socid; + $companystatic->name = $obj->socname; + $companystatic->client = $obj->client; + $companystatic->canvas = $obj->canvas; + + $filename = dol_sanitizeFileName($obj->ref); + $filedir = $conf->propal->multidir_output[$obj->entity].'/'.$filename; + + $result .= ''; + + $result .= ''; + $result .= ''; + $result .= ''; + + $result .= ''; + $result .= ''; + $result .= ''; + + $result .= ''; + $result .= '
'.$objectstatic->getNomUrl(1).' '.$formfile->getDocumentsLink($objectstatic->element, $filename, $filedir).'
'; + $result .= ''; + + $result .= ''.$companystatic->getNomUrl(1, 'customer').''; + $result .= ''.dol_print_date($db->jdate($obj->datec), 'day').''; + $result .= ''.$objectstatic->getLibStatut(5).''; + + $result .= ''; + + $i++; + } + + $result .= ''; + $result .= '
'; + return $result; +} + +/** + * Return a HTML table that contains a list with latest edited supplier invoices + * + * @param int $maxCount (Optional) The maximum count of elements inside the table + * @param int $socid (Optional) Show only results from the supplier with this id + * @return string A HTML table that contains a list with latest edited supplier invoices + */ +function getPurchaseInvoiceLatestEditTable($maxCount = 5, $socid = 0) +{ + global $conf, $db, $langs, $user; + + $sql = "SELECT f.rowid, f.entity, f.ref, f.fk_statut as status, f.paye, s.nom as socname, s.rowid as socid, s.canvas, s.client,"; + $sql .= " f.datec"; + $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f"; + $sql .= ", ".MAIN_DB_PREFIX."societe as s"; + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + } + $sql .= " WHERE f.fk_soc = s.rowid"; + $sql .= " AND f.entity IN (".getEntity('facture_fourn').")"; + if ($socid) { + $sql .= " AND f.fk_soc = ".$socid; + } + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; + } + $sql .= " ORDER BY f.tms DESC"; + $sql .= $db->plimit($maxCount, 0); + + $resql = $db->query($sql); + if (!$resql) { + dol_print_error($db); + return ''; + } + + $num = $db->num_rows($resql); + + $result = '
'; + $result .= ''; + $result .= ''; + $result .= ''; + $result .= ''; + + if ($num < 1) { + $result .= '
'.$langs->trans("BoxTitleLastSupplierBills", $maxCount).'
'; + $result .= '
'; + return $result; + } + + $objectstatic = new FactureFournisseur($db); + $companystatic = new Societe($db); + $formfile = new FormFile($db); + $i = 0; + + while ($i < $num) { + $obj = $db->fetch_object($resql); + + $objectstatic->id = $obj->rowid; + $objectstatic->ref = $obj->ref; + $objectstatic->paye = $obj->paye; + $objectstatic->statut = $obj->status; + + $companystatic->id = $obj->socid; + $companystatic->name = $obj->socname; + $companystatic->client = $obj->client; + $companystatic->canvas = $obj->canvas; + + $filename = dol_sanitizeFileName($obj->ref); + $filedir = $conf->propal->multidir_output[$obj->entity].'/'.$filename; + + $result .= ''; + + $result .= ''; + $result .= ''; + $result .= ''; + + $result .= ''; + $result .= ''; + $result .= ''; + + $result .= ''; + $result .= '
'.$objectstatic->getNomUrl(1).' '.$formfile->getDocumentsLink($objectstatic->element, $filename, $filedir).'
'; + $result .= ''; + + $result .= ''.$companystatic->getNomUrl(1, 'supplier').''; + $result .= ''.dol_print_date($db->jdate($obj->datec), 'day').''; + $result .= ''.$objectstatic->getLibStatut(5).''; + + $result .= ''; + + $i++; + } + + $result .= ''; + $result .= '
'; + return $result; +} + +/** + * Return a HTML table that contains of unpaid customers invoices + * + * @param int $maxCount (Optional) The maximum count of elements inside the table + * @param int $socid (Optional) Show only results from the supplier with this id + * @return string A HTML table that conatins a list with open (unpaid) supplier invoices + */ +function getCustomerInvoiceUnpaidOpenTable($maxCount = 500, $socid = 0) +{ + global $conf, $db, $langs, $user, $hookmanager; + + $result = ''; + + if (!empty($conf->facture->enabled) && !empty($user->rights->facture->lire)) { + $tmpinvoice = new Facture($db); + + $sql = "SELECT f.rowid, f.ref, f.fk_statut as status, f.datef, f.type, f.total as total_ht, f.tva as total_tva, f.total_ttc, f.paye, f.tms"; + $sql .= ", f.date_lim_reglement as datelimite"; + $sql .= ", s.nom as name"; + $sql .= ", s.rowid as socid, s.email"; + $sql .= ", s.code_client, s.code_compta"; + $sql .= ", s.code_fournisseur, s.code_compta_fournisseur"; + $sql .= ", cc.rowid as country_id, cc.code as country_code"; + $sql .= ", sum(pf.amount) as am"; + $sql .= " FROM ".MAIN_DB_PREFIX."societe as s LEFT JOIN ".MAIN_DB_PREFIX."c_country as cc ON cc.rowid = s.fk_pays,".MAIN_DB_PREFIX."facture as f"; + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf on f.rowid=pf.fk_facture"; + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + } + $sql .= " WHERE s.rowid = f.fk_soc AND f.paye = 0 AND f.fk_statut = ".Facture::STATUS_VALIDATED; + $sql .= " AND f.entity IN (".getEntity('invoice').')'; + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; + } + if ($socid) { + $sql .= " AND f.fk_soc = ".$socid; + } + // Add where from hooks + $parameters = array(); + $reshook = $hookmanager->executeHooks('printFieldListWhereCustomerUnpaid', $parameters); + $sql .= $hookmanager->resPrint; + + $sql .= " GROUP BY f.rowid, f.ref, f.fk_statut, f.datef, f.type, f.total, f.tva, f.total_ttc, f.paye, f.tms, f.date_lim_reglement,"; + $sql .= " s.nom, s.rowid, s.email, s.code_client, s.code_compta, cc.rowid, cc.code"; + $sql .= ", s.code_fournisseur, s.code_compta_fournisseur"; + $sql .= " ORDER BY f.datef ASC, f.ref ASC"; + + $resql = $db->query($sql); + if ($resql) { + $num = $db->num_rows($resql); + $i = 0; + $othernb = 0; + + $formfile = new FormFile($db); + + print '
'; + print ''; + + print ''; + print ''; + + print ''; + if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { + print ''; + } + print ''; + print ''; + print ''; + print ''; + if ($num) { + $societestatic = new Societe($db); + $total_ttc = $totalam = $total = 0; + while ($i < $num) { + $obj = $db->fetch_object($resql); + + if ($i >= $maxCount) { + $othernb += 1; + $i++; + $total += $obj->total_ht; + $total_ttc += $obj->total_ttc; + continue; + } + + $tmpinvoice->ref = $obj->ref; + $tmpinvoice->id = $obj->rowid; + $tmpinvoice->total_ht = $obj->total_ht; + $tmpinvoice->total_tva = $obj->total_tva; + $tmpinvoice->total_ttc = $obj->total_ttc; + $tmpinvoice->type = $obj->type; + $tmpinvoice->statut = $obj->status; + $tmpinvoice->paye = $obj->paye; + $tmpinvoice->date_lim_reglement = $db->jdate($obj->datelimite); + + $societestatic->id = $obj->socid; + $societestatic->name = $obj->name; + $societestatic->email = $obj->email; + $societestatic->country_id = $obj->country_id; + $societestatic->country_code = $obj->country_code; + $societestatic->client = 1; + $societestatic->code_client = $obj->code_client; + $societestatic->code_fournisseur = $obj->code_fournisseur; + $societestatic->code_compta = $obj->code_compta; + $societestatic->code_compta_fournisseur = $obj->code_compta_fournisseur; + + print ''; + print ''; + print ''; + print ''; + if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { + print ''; + } + print ''; + print ''; + print ''; + print ''; + + $total_ttc += $obj->total_ttc; + $total += $obj->total_ht; + $totalam += $obj->am; + + $i++; + } + + if ($othernb) { + $colspan = 6; + if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { + $colspan++; + } + print ''; + print ''; + print "\n"; + } + + print ''; + print ''; + if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { + print ''; + } + print ''; + print ''; + print ''; + print ''; + } else { + $colspan = 6; + if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { + $colspan++; + } + print ''; + } + print '
'; + print $langs->trans("BillsCustomersUnpaid", $num).' '; + print ''; + print ''.$num.''; + print ''; + print ''.$langs->trans("DateDue").''.$langs->trans("AmountHT").''.$langs->trans("AmountTTC").''.$langs->trans("Received").' 
'; + + print ''; + print ''; + print ''; + print '
'; + print $tmpinvoice->getNomUrl(1, ''); + print ''; + if ($tmpinvoice->hasDelay()) { + print img_warning($langs->trans("Late")); + } + print ''; + $filename = dol_sanitizeFileName($obj->ref); + $filedir = $conf->facture->dir_output.'/'.dol_sanitizeFileName($obj->ref); + $urlsource = $_SERVER['PHP_SELF'].'?facid='.$obj->rowid; + print $formfile->getDocumentsLink($tmpinvoice->element, $filename, $filedir); + print '
'; + + print '
'; + print $societestatic->getNomUrl(1, 'customer'); + print ''.dol_print_date($db->jdate($obj->datelimite), 'day').''.price($obj->total_ht).''.price($obj->total_ttc).''.price($obj->am).''.$tmpinvoice->getLibStatut(3, $obj->am).'
'; + print ''.$langs->trans("More").'... ('.$othernb.')'; + print '
'.$langs->trans("Total").'   ('.$langs->trans("RemainderToTake").': '.price($total_ttc - $totalam).')  '.price($total).''.price($total_ttc).''.price($totalam).' 
'.$langs->trans("NoInvoice").'

'; + $db->free($resql); + } else { + dol_print_error($db); + } + } + + return $result; +} + + +/** + * Return a HTML table that contains of unpaid purchase invoices + * + * @param int $maxCount (Optional) The maximum count of elements inside the table + * @param int $socid (Optional) Show only results from the supplier with this id + * @return string A HTML table that conatins a list with open (unpaid) supplier invoices + */ +function getPurchaseInvoiceUnpaidOpenTable($maxCount = 500, $socid = 0) +{ + global $conf, $db, $langs, $user, $hookmanager; + + $result = ''; + + if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) || !empty($conf->supplier_invoice->enabled)) && $user->rights->fournisseur->facture->lire) { + $facstatic = new FactureFournisseur($db); + + $sql = "SELECT ff.rowid, ff.ref, ff.fk_statut as status, ff.type, ff.libelle as label, ff.total_ht, ff.total_tva, ff.total_ttc, ff.paye"; + $sql .= ", ff.date_lim_reglement"; + $sql .= ", s.nom as name"; + $sql .= ", s.rowid as socid, s.email"; + $sql .= ", s.code_client, s.code_compta"; + $sql .= ", s.code_fournisseur, s.code_compta_fournisseur"; + $sql .= ", sum(pf.amount) as am"; + $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."facture_fourn as ff"; + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf on ff.rowid=pf.fk_facturefourn"; + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + } + $sql .= " WHERE s.rowid = ff.fk_soc"; + $sql .= " AND ff.entity = ".$conf->entity; + $sql .= " AND ff.paye = 0"; + $sql .= " AND ff.fk_statut = ".FactureFournisseur::STATUS_VALIDATED; + if (!$user->rights->societe->client->voir && !$socid) { + $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; + } + if ($socid) { + $sql .= " AND ff.fk_soc = ".$socid; + } + // Add where from hooks + $parameters = array(); + $reshook = $hookmanager->executeHooks('printFieldListWhereSupplierUnpaid', $parameters); + $sql .= $hookmanager->resPrint; + + $sql .= " GROUP BY ff.rowid, ff.ref, ff.fk_statut, ff.type, ff.libelle, ff.total_ht, ff.tva, ff.total_tva, ff.total_ttc, ff.paye, ff.date_lim_reglement,"; + $sql .= " s.nom, s.rowid, s.email, s.code_client, s.code_fournisseur, s.code_compta, s.code_compta_fournisseur"; + $sql .= " ORDER BY ff.date_lim_reglement ASC"; + + $resql = $db->query($sql); + if ($resql) { + $num = $db->num_rows($resql); + $othernb = 0; + + $formfile = new FormFile($db); + + print '
'; + print ''; + + print ''; + print ''; + + print ''; + if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { + print ''; + } + print ''; + print ''; + print ''; + print "\n"; + $societestatic = new Societe($db); + if ($num) { + $i = 0; + $total = $total_ttc = $totalam = 0; + while ($i < $num) { + $obj = $db->fetch_object($resql); + + if ($i >= $maxCount) { + $othernb += 1; + $i++; + $total += $obj->total_ht; + $total_ttc += $obj->total_ttc; + continue; + } + + $facstatic->ref = $obj->ref; + $facstatic->id = $obj->rowid; + $facstatic->type = $obj->type; + $facstatic->total_ht = $obj->total_ht; + $facstatic->total_tva = $obj->total_tva; + $facstatic->total_ttc = $obj->total_ttc; + $facstatic->statut = $obj->status; + $facstatic->paye = $obj->paye; + + $societestatic->id = $obj->socid; + $societestatic->name = $obj->name; + $societestatic->email = $obj->email; + $societestatic->client = 0; + $societestatic->fournisseur = 1; + $societestatic->code_client = $obj->code_client; + $societestatic->code_fournisseur = $obj->code_fournisseur; + $societestatic->code_compta = $obj->code_compta; + $societestatic->code_compta_fournisseur = $obj->code_compta_fournisseur; + + print ''; + print ''; + print ''; + if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { + print ''; + } + print ''; + print ''; + print ''; + print ''; + $total += $obj->total_ht; + $total_ttc += $obj->total_ttc; + $totalam += $obj->am; + $i++; + } + + if ($othernb) { + $colspan = 6; + if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { + $colspan++; + } + print ''; + print ''; + print "\n"; + } + + print ''; + print ''; + if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { + print ''; + } + print ''; + print ''; + print ''; + print ''; + } else { + $colspan = 6; + if (!empty($conf->global->MAIN_SHOW_HT_ON_SUMMARY)) { + $colspan++; + } + print ''; + } + print '
'; + print $langs->trans("BillsSuppliersUnpaid", $num).' '; + print ''; + print ''.$num.''; + print ''; + print ''.$langs->trans("DateDue").''.$langs->trans("AmountHT").''.$langs->trans("AmountTTC").''.$langs->trans("Paid").' 
'; + print $facstatic->getNomUrl(1, ''); + print ''.$societestatic->getNomUrl(1, 'supplier').''.dol_print_date($db->jdate($obj->date_lim_reglement), 'day').''.price($obj->total_ht).''.price($obj->total_ttc).''.price($obj->am).''.$facstatic->getLibStatut(3, $obj->am).'
'; + print ''.$langs->trans("More").'... ('.$othernb.')'; + print '
'.$langs->trans("Total").'   ('.$langs->trans("RemainderToPay").': '.price($total_ttc - $totalam).')  '.price($total).''.price($total_ttc).''.price($totalam).' 
'.$langs->trans("NoInvoice").'

'; + } else { + dol_print_error($db); + } + } + + return $result; +} diff --git a/htdocs/fourn/facture/index.php b/htdocs/fourn/facture/index.php index 1e3c4f5e686..e76426f54c2 100644 --- a/htdocs/fourn/facture/index.php +++ b/htdocs/fourn/facture/index.php @@ -25,6 +25,7 @@ require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/invoice.lib.php'; // Security check restrictedArea($user, 'fournisseur', 0, '', 'facture'); @@ -39,6 +40,8 @@ if (isset($user->socid) && $user->socid > 0) { $socid = $user->socid; } +$max = $conf->global->MAIN_SIZE_SHORTLIST_LIMIT; + // Maximum elements of the tables $maxDraftCount = empty($conf->global->MAIN_MAXLIST_OVERLOAD) ? 500 : $conf->global->MAIN_MAXLIST_OVERLOAD; $maxLatestEditCount = 5; @@ -56,24 +59,18 @@ print '
'; print '
'; -// This is useless due to the global search combo -if (!empty($conf->global->MAIN_SEARCH_FORM_ON_HOME_AREAS)) { - print getAreaSearchFrom(); - print '
'; -} - -print getPieChart($socid); +print getPurchaseInvoicePieChart($socid); print '
'; -print getDraftTable($maxDraftCount, $socid); +print getDraftSupplierTable($maxDraftCount, $socid); print '
'; print '
'; print '
'; -print getLatestEditTable($maxLatestEditCount, $socid); +print getPurchaseInvoiceLatestEditTable($maxLatestEditCount, $socid); print '
'; -print getOpenTable($maxOpenCount, $socid); +print getPurchaseInvoiceUnpaidOpenTable($max, $socid); print '
'; print '
'; @@ -83,453 +80,3 @@ print '
'; // End of page llxFooter(); $db->close(); - -/** - * Return a HTML string that contains a additional search form - * - * @return string A HTML string that contains a additional search form - */ -function getAreaSearchFrom() -{ - global $langs; - - $result = '
'; - $result .= '
'; - $result .= ''; - $result .= ''; - - $result .= ''; - $result .= ''; - $result .= ''; - - $result .= ''; - $result .= ''; - $result .= ''; - $result .= ''; - - $result .= "
'.$langs->trans("Search").'
'.$langs->trans("Invoice").':
"; - $result .= "
"; - $result .= "
"; - - return $result; -} - -/** - * Return a HTML table that contains a pie chart of supplier invoices - * - * @param int $socid (Optional) Show only results from the supplier with this id - * @return string A HTML table that contains a pie chart of supplier invoices - */ -function getPieChart($socid = 0) -{ - global $conf, $db, $langs, $user; - - $sql = "SELECT count(f.rowid), f.fk_statut"; - $sql .= " FROM ".MAIN_DB_PREFIX."societe as s"; - $sql .= ", ".MAIN_DB_PREFIX."facture_fourn as f"; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE f.fk_soc = s.rowid"; - $sql .= " AND f.entity IN (".getEntity('facture_fourn').")"; - if ($user->socid) { - $sql .= ' AND f.fk_soc = '.$user->socid; - } - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; - } - $sql .= " GROUP BY f.fk_statut"; - - $resql = $db->query($sql); - if (!$resql) { - dol_print_error($db); - return ''; - } - - $num = $db->num_rows($resql); - $i = 0; - - $total = 0; - $vals = []; - - while ($i < $num) { - $row = $db->fetch_row($resql); - if ($row) { - $vals[$row[1]] = $row[0]; - $total += $row[0]; - } - - $i++; - } - - $db->free($resql); - - $result = '
'; - $result .= ''; - - $result .= ''; - $result .= ''; - $result .= ''; - - $objectstatic = new FactureFournisseur($db); - $array = [FactureFournisseur::STATUS_DRAFT, FactureFournisseur::STATUS_VALIDATED, FactureFournisseur::STATUS_CLOSED, FactureFournisseur::STATUS_ABANDONED]; - $dataseries = []; - - foreach ($array as $status) { - $objectstatic->statut = $status; - $objectstatic->paye = $status == FactureFournisseur::STATUS_CLOSED ? -1 : 0; - - $dataseries[] = [$objectstatic->getLibStatut(1), (isset($vals[$status]) ? (int) $vals[$status] : 0)]; - if (!$conf->use_javascript_ajax) { - $result .= ''; - $result .= ''; - $result .= ''; - $result .= ''; - } - } - - if ($conf->use_javascript_ajax) { - $dolgraph = new DolGraph(); - $dolgraph->SetData($dataseries); - $dolgraph->setShowLegend(2); - $dolgraph->setShowPercent(1); - $dolgraph->SetType(['pie']); - $dolgraph->setHeight('200'); - $dolgraph->draw('idgraphthirdparties'); - - $result .= ''; - $result .= ''; - $result .= ''; - } - - $result .= ''; - $result .= ''; - $result .= ''; - $result .= ''; - - $result .= '
'.$langs->trans("Statistics").' - '.$langs->trans("SupplierInvoice").'
'.$objectstatic->getLibStatut(0).''.(isset($vals[$status]) ? $vals[$status] : 0).'
'.$dolgraph->show($total ? 0 : 1).'
'.$langs->trans("Total").''.$total.'
'; - $result .= '
'; - - return $result; -} - -/** - * Return a HTML table that contains a list with supplier invoice drafts - * - * @param int $maxCount (Optional) The maximum count of elements inside the table - * @param int $socid (Optional) Show only results from the supplier with this id - * @return string A HTML table that contains a list with supplier invoice drafts - */ -function getDraftTable($maxCount = 500, $socid = 0) -{ - global $db, $langs, $user; - - $sql = "SELECT f.rowid, f.ref, s.nom as socname, s.rowid as socid, s.canvas, s.client, f.total_ttc"; - $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f"; - $sql .= ", ".MAIN_DB_PREFIX."societe as s"; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE f.fk_soc = s.rowid"; - $sql .= " AND f.entity IN (".getEntity('facture_fourn').")"; - $sql .= " AND f.fk_statut = ".FactureFournisseur::STATUS_DRAFT; - if ($socid) { - $sql .= " AND f.fk_soc = ".$socid; - } - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; - } - $sql .= $db->plimit($maxCount, 0); - - $resql = $db->query($sql); - if (!$resql) { - dol_print_error($db); - return ''; - } - - $num = $db->num_rows($resql); - - $result = '
'; - $result .= ''; - - $result .= ''; - $result .= ''; - $result .= ''; - - if ($num < 1) { - $result .= '
'; - $result .= $langs->trans("SuppliersDraftInvoices"); - $result .= ' '; - $result .= ''.$num.''; - $result .= ''; - $result .= '
'; - $result .= '
'; - return $result; - } - - $objectstatic = new FactureFournisseur($db); - $companystatic = new Societe($db); - $nbofloop = min($num, $maxCount); - $total = 0; - $i = 0; - - while ($i < $nbofloop) { - $obj = $db->fetch_object($resql); - - $objectstatic->id = $obj->rowid; - $objectstatic->ref = $obj->ref; - - $companystatic->id = $obj->socid; - $companystatic->name = $obj->socname; - $companystatic->client = $obj->client; - $companystatic->canvas = $obj->canvas; - - $result .= ''; - $result .= ''.$objectstatic->getNomUrl(1).''; - $result .= ''.$companystatic->getNomUrl(1, 'supplier', 24).''; - $result .= ''.price($obj->total_ttc).''; - $result .= ''; - - $i++; - $total += $obj->total_ttc; - } - - if ($num > $nbofloop) { - $result .= ''; - $result .= ''.$langs->trans("XMoreLines", ($num - $nbofloop)).''; - $result .= ''; - } elseif ($total > 0) { - $result .= ''; - $result .= ''.$langs->trans("Total").''; - $result .= ''.price($total).''; - $result .= ''; - } - - $result .= ''; - $result .= ''; - return $result; -} - -/** - * Return a HTML table that contains a list with latest edited supplier invoices - * - * @param int $maxCount (Optional) The maximum count of elements inside the table - * @param int $socid (Optional) Show only results from the supplier with this id - * @return string A HTML table that contains a list with latest edited supplier invoices - */ -function getLatestEditTable($maxCount = 5, $socid = 0) -{ - global $conf, $db, $langs, $user; - - $sql = "SELECT f.rowid, f.entity, f.ref, f.fk_statut as status, f.paye, s.nom as socname, s.rowid as socid, s.canvas, s.client,"; - $sql .= " f.datec"; - $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f"; - $sql .= ", ".MAIN_DB_PREFIX."societe as s"; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE f.fk_soc = s.rowid"; - $sql .= " AND f.entity IN (".getEntity('facture_fourn').")"; - if ($socid) { - $sql .= " AND f.fk_soc = ".$socid; - } - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; - } - $sql .= " ORDER BY f.tms DESC"; - $sql .= $db->plimit($maxCount, 0); - - $resql = $db->query($sql); - if (!$resql) { - dol_print_error($db); - return ''; - } - - $num = $db->num_rows($resql); - - $result = '
'; - $result .= ''; - $result .= ''; - $result .= ''; - $result .= ''; - - if ($num < 1) { - $result .= '
'.$langs->trans("BoxTitleLastSupplierBills", $maxCount).'
'; - $result .= '
'; - return $result; - } - - $objectstatic = new FactureFournisseur($db); - $companystatic = new Societe($db); - $formfile = new FormFile($db); - $i = 0; - - while ($i < $num) { - $obj = $db->fetch_object($resql); - - $objectstatic->id = $obj->rowid; - $objectstatic->ref = $obj->ref; - $objectstatic->paye = $obj->paye; - $objectstatic->statut = $obj->status; - - $companystatic->id = $obj->socid; - $companystatic->name = $obj->socname; - $companystatic->client = $obj->client; - $companystatic->canvas = $obj->canvas; - - $filename = dol_sanitizeFileName($obj->ref); - $filedir = $conf->propal->multidir_output[$obj->entity].'/'.$filename; - - $result .= ''; - - $result .= ''; - $result .= ''; - $result .= ''; - - $result .= ''; - $result .= ''; - $result .= ''; - - $result .= ''; - $result .= '
'.$objectstatic->getNomUrl(1).' '.$formfile->getDocumentsLink($objectstatic->element, $filename, $filedir).'
'; - $result .= ''; - - $result .= ''.$companystatic->getNomUrl(1, 'supplier').''; - $result .= ''.dol_print_date($db->jdate($obj->datec), 'day').''; - $result .= ''.$objectstatic->getLibStatut(5).''; - - $result .= ''; - - $i++; - } - - $result .= ''; - $result .= ''; - return $result; -} - -/** - * Return a HTML table that contains a list with open (unpaid) supplier invoices - * - * @param int $maxCount (Optional) The maximum count of elements inside the table - * @param int $socid (Optional) Show only results from the supplier with this id - * @return string A HTML table that conatins a list with open (unpaid) supplier invoices - */ -function getOpenTable($maxCount = 500, $socid = 0) -{ - global $conf, $db, $langs, $user; - - $sql = "SELECT s.nom as socname, s.rowid as socid, s.canvas, s.client"; - $sql .= ", f.rowid as id, f.entity, f.total_ttc, f.total_ht, f.ref, f.fk_statut"; - $sql .= ", f.datef as df, f.date_lim_reglement as datelimite"; - $sql .= " FROM ".MAIN_DB_PREFIX."societe as s"; - $sql .= ", ".MAIN_DB_PREFIX."facture_fourn as f"; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } - $sql .= " WHERE f.fk_soc = s.rowid"; - $sql .= " AND f.entity IN (".getEntity('facture_fourn').")"; - $sql .= " AND f.fk_statut = ".FactureFournisseur::STATUS_VALIDATED; - if (!$user->rights->societe->client->voir && !$socid) { - $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; - } - if ($socid) { - $sql .= " AND s.rowid = ".$socid; - } - $sql .= " ORDER BY f.rowid DESC"; - $sql .= $db->plimit($maxCount, 0); - - $resql = $db->query($sql); - if (!$resql) { - dol_print_error($db); - return ''; - } - - $num = $db->num_rows($resql); - - $result = '
'; - $result .= ''; - $result .= ''; - $result .= ''; - $result .= ''; - - if ($num < 1) { - $result .= '
'; - $result .= $langs->trans("BillsCustomersUnpaid"); - $result .= ' '; - $result .= ''.$num.''; - $result .= ''; - $result .= '
'; - $result .= '
'; - return $result; - } - - $objectstatic = new FactureFournisseur($db); - $companystatic = new Societe($db); - $formfile = new FormFile($db); - $nbofloop = min($num, $maxCount); - $now = dol_now(); - $total = 0; - $i = 0; - - while ($i < $nbofloop) { - $obj = $db->fetch_object($resql); - - $objectstatic->id = $obj->id; - $objectstatic->ref = $obj->ref; - - $companystatic->id = $obj->socid; - $companystatic->name = $obj->socname; - $companystatic->client = $obj->client; - $companystatic->canvas = $obj->canvas; - - $filename = dol_sanitizeFileName($obj->ref); - $filedir = $conf->propal->multidir_output[$obj->entity].'/'.$filename; - - $result .= ''; - - $result .= ''; - $result .= ''; - $result .= ''; - - $result .= ''; - $result .= ''; - - $result .= ''; - - $result .= ''; - $result .= '
'.$objectstatic->getNomUrl(1).''; - - if ($db->jdate($obj->dfv) < ($now - $conf->propal->cloture->warning_delay)) { - $result .= img_warning($langs->trans("Late")); - } - - $result .= ''.$formfile->getDocumentsLink($objectstatic->element, $filename, $filedir).'
'; - $result .= ''; - - $result .= ''.$companystatic->getNomUrl(1, 'customer', 44).''; - $result .= ''.dol_print_date($db->jdate($obj->df), 'day').''; - $result .= ''.price($obj->total_ttc).''; - - $result .= ''; - - $i++; - $total += $obj->total_ttc; - } - - if ($num > $nbofloop) { - $result .= ''; - $result .= ''.$langs->trans("XMoreLines", ($num - $nbofloop)).''; - $result .= ''; - } elseif ($total > 0) { - $result .= ''; - $result .= ''.$langs->trans("Total").''; - $result .= ''.price($total).''; - $result .= ' '; - $result .= ''; - } - - $result .= ''; - $result .= ''; - return $result; -}