* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License, version 3. */ /** * \file htdocs/custom/mahnung/core/boxes/box_mahnung_offen.php * \ingroup mahnung * \brief Widget: Aelteste offene Kundenrechnungen mit Mahnstufe. */ require_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php'; require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php'; require_once DOL_DOCUMENT_ROOT.'/custom/mahnung/class/mahnung.class.php'; /** * Widget: Offene Kundenrechnungen mit aktueller Mahnstufe. */ class box_mahnung_offen extends ModeleBoxes { public $boxcode = "mahnungoffenerechnungen"; public $boximg = "fa-envelope-open-text"; public $boxlabel = "MahnungBoxOffeneRechnungen"; public $depends = array("facture", "mahnung"); /** * @param DoliDB $db * @param string $param */ public function __construct($db, $param) { global $user; $this->db = $db; $this->hidden = !($user->hasRight('facture', 'lire')); } /** * @param int $max */ public function loadBox($max = 5) { global $conf, $user, $langs; $this->max = $max; $langs->loadLangs(array('bills', 'mahnung@mahnung')); $facturestatic = new Facture($this->db); $societestatic = new Societe($this->db); $this->info_box_head = array( 'text' => $langs->trans("MahnungBoxOffeneRechnungen", $this->max), ); if (!$user->hasRight('facture', 'lire')) { $this->info_box_contents[0][0] = array( 'td' => 'class="nohover left"', 'text' => ''.$langs->trans("ReadPermissionNotAllowed").'' ); return; } // Offene Rechnungen mit optionalem LEFT JOIN auf letzte aktive Mahnung $sql = "SELECT s.rowid as socid, s.nom as name, s.logo, s.email, s.entity,"; $sql .= " s.code_client, s.client,"; $sql .= " f.rowid as facid, f.ref, f.type, f.datef as date,"; $sql .= " f.date_lim_reglement as datelimit,"; $sql .= " f.total_ht, f.total_tva, f.total_ttc,"; $sql .= " f.paye, f.fk_statut as status,"; $sql .= " COALESCE(SUM(pf.amount), 0) as am,"; // Letzte Mahnstufe per Subquery $sql .= " (SELECT m2.stufe FROM ".MAIN_DB_PREFIX."mahnung_mahnung as m2"; $sql .= " WHERE m2.fk_facture = f.rowid AND m2.status NOT IN (".Mahnung::STATUS_STORNIERT.")"; $sql .= " ORDER BY m2.stufe DESC LIMIT 1) as mahnstufe,"; $sql .= " (SELECT m3.date_mahnung FROM ".MAIN_DB_PREFIX."mahnung_mahnung as m3"; $sql .= " WHERE m3.fk_facture = f.rowid AND m3.status NOT IN (".Mahnung::STATUS_STORNIERT.")"; $sql .= " ORDER BY m3.stufe DESC LIMIT 1) as mahndatum"; $sql .= " FROM ".MAIN_DB_PREFIX."facture as f"; $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc"; $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture"; $sql .= " WHERE f.entity IN (".getEntity('invoice').")"; $sql .= " AND f.paye = 0"; $sql .= " AND f.fk_statut = 1"; $sql .= " AND f.date_lim_reglement IS NOT NULL"; $sql .= " AND f.date_lim_reglement < '".$this->db->idate(dol_now())."'"; if (empty($user->socid) && !$user->hasRight('societe', 'client', 'voir')) { $sql .= " AND s.rowid IN (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_user = ".((int) $user->id).")"; } if ($user->socid) { $sql .= " AND s.rowid = ".((int) $user->socid); } $sql .= " GROUP BY s.rowid, s.nom, s.logo, s.email, s.entity, s.code_client, s.client,"; $sql .= " f.rowid, f.ref, f.type, f.datef, f.date_lim_reglement,"; $sql .= " f.total_ht, f.total_tva, f.total_ttc, f.paye, f.fk_statut"; $sql .= " ORDER BY f.date_lim_reglement ASC, f.ref ASC"; $sql .= $this->db->plimit($this->max + 1, 0); $result = $this->db->query($sql); if (!$result) { $this->info_box_contents[0][0] = array( 'td' => '', 'maxlength' => 500, 'text' => $this->db->error().' sql='.$sql, ); return; } $num = $this->db->num_rows($result); $line = 0; $l_due_date = $langs->trans('Late').' ('.strtolower($langs->trans('DateDue')).': %s)'; $totalHt = 0; while ($line < min($num, $this->max)) { $objp = $this->db->fetch_object($result); $datelimit = $this->db->jdate($objp->datelimit); $tageVerzug = (int) floor((dol_now() - $datelimit) / 86400); $facturestatic->id = $objp->facid; $facturestatic->ref = $objp->ref; $facturestatic->type = $objp->type; $facturestatic->total_ht = $objp->total_ht; $facturestatic->total_tva = $objp->total_tva; $facturestatic->total_ttc = $objp->total_ttc; $facturestatic->date = $this->db->jdate($objp->date); $facturestatic->date_lim_reglement = $datelimit; $facturestatic->statut = $objp->status; $facturestatic->status = $objp->status; $facturestatic->paye = $objp->paye; $facturestatic->paid = $objp->paye; $facturestatic->alreadypaid = $objp->am; $facturestatic->totalpaid = $objp->am; $societestatic->id = $objp->socid; $societestatic->name = $objp->name; $societestatic->code_client = $objp->code_client; $societestatic->client = $objp->client; $societestatic->logo = $objp->logo; $societestatic->email = $objp->email; $societestatic->entity = $objp->entity; $late = ''; if ($facturestatic->hasDelay()) { $late = img_warning(sprintf($l_due_date, dol_print_date($datelimit, 'day', 'tzuserrel'))); } // Mahnstufe-Badge $mahnBadge = ''; if (!empty($objp->mahnstufe)) { $stufe = (int) $objp->mahnstufe; $colors = array(1 => '#4a90d9', 2 => '#e68a00', 3 => '#cc3333'); $labels = array(1 => 'Stufe 1', 2 => 'Stufe 2', 3 => 'Stufe 3'); $color = $colors[$stufe] ?? '#666'; $label = $labels[$stufe] ?? 'Stufe '.$stufe; $mahnDatum = $objp->mahndatum ? dol_print_date($this->db->jdate($objp->mahndatum), 'day') : ''; $tooltip = $label.($mahnDatum ? ' vom '.$mahnDatum : ''); $mahnBadge = ' '.$label.''; } // Spalte 1: Rechnung + Warnung $this->info_box_contents[$line][] = array( 'td' => 'class="nowraponall"', 'text' => $facturestatic->getNomUrl(1), 'text2' => $late, 'asis' => 1, ); // Spalte 2: Kunde $this->info_box_contents[$line][] = array( 'td' => 'class="tdoverflowmax150 maxwidth150onsmartphone"', 'text' => $societestatic->getNomUrl(1, '', 44), 'asis' => 1, ); // Spalte 3: Betrag $this->info_box_contents[$line][] = array( 'td' => 'class="nowraponall right amount"', 'text' => price($objp->total_ht, 0, $langs, 0, -1, -1, $conf->currency), ); // Spalte 4: Faelligkeitsdatum $this->info_box_contents[$line][] = array( 'td' => 'class="center nowraponall" title="'.dol_escape_htmltag($langs->trans("DateDue").': '.dol_print_date($datelimit, 'day', 'tzuserrel')).'"', 'text' => dol_print_date($datelimit, 'day', 'tzuserrel'), ); // Spalte 5: Tage Verzug $this->info_box_contents[$line][] = array( 'td' => 'class="center nowraponall"', 'text' => $tageVerzug.' T.', ); // Spalte 6: Mahnstufe $this->info_box_contents[$line][] = array( 'td' => 'class="center nowraponall"', 'text' => !empty($mahnBadge) ? $mahnBadge : '', 'asis' => 1, ); $totalHt += (float) $objp->total_ht; $line++; } if ($this->max < $num) { $this->info_box_contents[$line][] = array('td' => 'colspan="6"', 'text' => '...'); $line++; } if ($num > 0) { // Summenzeile $this->info_box_contents[$line][] = array( 'tr' => 'class="liste_total"', 'td' => 'class="liste_total"', 'text' => $langs->trans("Total"), ); $this->info_box_contents[$line][] = array( 'td' => 'class="liste_total"', 'text' => ' ', ); $this->info_box_contents[$line][] = array( 'td' => 'class="nowraponall right liste_total"', 'text' => price($totalHt, 0, $langs, 0, -1, -1, $conf->currency), ); $this->info_box_contents[$line][] = array( 'td' => 'class="liste_total"', 'text' => ' ', ); $this->info_box_contents[$line][] = array( 'td' => 'class="liste_total"', 'text' => ' ', ); $this->info_box_contents[$line][] = array( 'td' => 'class="liste_total"', 'text' => ' ', ); } $this->db->free($result); } /** * @param array|null $head * @param array|null $contents * @param int $nooutput * @return string */ public function showBox($head = null, $contents = null, $nooutput = 0) { return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput); } }