75 global $conf, $user, $langs;
80 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
81 include_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
83 $facturestatic =
new Facture($this->db);
84 $societestatic =
new Societe($this->db);
86 $langs->load(
"bills");
88 $textHead = $langs->trans(
"BoxTitleOldestUnpaidCustomerBills");
89 $this->info_box_head = array(
'text' => $langs->trans(
"BoxTitleOldestUnpaidCustomerBills", $this->max),
'limit'=>
dol_strlen($textHead));
91 if ($user->hasRight(
'facture',
'lire')) {
92 $sql1 =
"SELECT s.rowid as socid, s.nom as name, s.name_alias, s.code_client, s.client";
93 if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
94 $sql1 .=
", spe.accountancy_code_customer as code_compta";
96 $sql1 .=
", s.code_compta";
98 $sql1 .=
", s.logo, s.email, s.entity";
99 $sql1 .=
", s.tva_intra, s.siren as idprof1, s.siret as idprof2, s.ape as idprof3, s.idprof4, s.idprof5, s.idprof6";
100 $sql1 .=
", f.ref, f.date_lim_reglement as datelimite";
102 $sql1 .=
", f.datef as date";
103 $sql1 .=
", f.total_ht";
104 $sql1 .=
", f.total_tva";
105 $sql1 .=
", f.total_ttc";
106 $sql1 .=
", f.paye, f.fk_statut as status, f.rowid as facid";
107 $sql1 .=
", SUM(pf.amount) as am";
108 $sql2 =
" FROM ".MAIN_DB_PREFIX.
"societe as s";
109 if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
110 $sql2 .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity);
112 if (empty($user->rights->societe->client->voir) && !$user->socid) {
113 $sql2 .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
115 $sql2 .=
", ".MAIN_DB_PREFIX.
"facture as f";
116 $sql2 .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiement_facture as pf ON f.rowid = pf.fk_facture";
117 $sql2 .=
" WHERE f.fk_soc = s.rowid";
118 $sql2 .=
" AND f.entity IN (".getEntity(
'invoice').
")";
119 $sql2 .=
" AND f.paye = 0";
120 $sql2 .=
" AND fk_statut = 1";
121 if (empty($user->rights->societe->client->voir) && !$user->socid) {
122 $sql2 .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
125 $sql2 .=
" AND s.rowid = ".((int) $user->socid);
127 $sql3 =
" GROUP BY s.rowid, s.nom, s.name_alias, s.code_client, s.client, s.logo, s.email, s.entity, s.tva_intra, s.siren, s.siret, s.ape, s.idprof4, s.idprof5, s.idprof6,";
128 if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
129 $sql3 .=
" spe.accountancy_code_customer as code_compta,";
131 $sql3 .=
" s.code_compta,";
133 $sql3 .=
" f.rowid, f.ref, f.date_lim_reglement,";
134 $sql3 .=
" f.type, f.datef, f.total_ht, f.total_tva, f.total_ttc, f.paye, f.fk_statut";
135 $sql3 .=
" ORDER BY datelimite ASC, f.ref ASC ";
136 $sql3 .= $this->db->plimit($this->max + 1, 0);
138 $sql = $sql1.$sql2.$sql3;
140 $result = $this->db->query($sql);
142 $num = $this->db->num_rows($result);
145 $l_due_date = $langs->trans(
'Late').
' ('.strtolower($langs->trans(
'DateDue')).
': %s)';
147 while ($line < min($num, $this->max)) {
148 $objp = $this->db->fetch_object($result);
150 $datelimite = $this->db->jdate($objp->datelimite);
152 $facturestatic->id = $objp->facid;
153 $facturestatic->ref = $objp->ref;
154 $facturestatic->type = $objp->type;
155 $facturestatic->total_ht = $objp->total_ht;
156 $facturestatic->total_tva = $objp->total_tva;
157 $facturestatic->total_ttc = $objp->total_ttc;
158 $facturestatic->statut = $objp->status;
159 $facturestatic->status = $objp->status;
160 $facturestatic->date = $this->db->jdate($objp->date);
161 $facturestatic->date_lim_reglement = $this->db->jdate($objp->datelimite);
163 $facturestatic->paye = $objp->paye;
164 $facturestatic->alreadypaid = $objp->am;
166 $societestatic->id = $objp->socid;
167 $societestatic->name = $objp->name;
169 $societestatic->code_client = $objp->code_client;
170 $societestatic->code_compta = $objp->code_compta;
171 $societestatic->client = $objp->client;
172 $societestatic->logo = $objp->logo;
173 $societestatic->email = $objp->email;
174 $societestatic->entity = $objp->entity;
175 $societestatic->tva_intra = $objp->tva_intra;
176 $societestatic->idprof1 = $objp->idprof1;
177 $societestatic->idprof2 = $objp->idprof2;
178 $societestatic->idprof3 = $objp->idprof3;
179 $societestatic->idprof4 = $objp->idprof4;
180 $societestatic->idprof5 = $objp->idprof5;
181 $societestatic->idprof6 = $objp->idprof6;
184 if ($facturestatic->hasDelay()) {
188 $this->info_box_contents[$line][] = array(
189 'td' =>
'class="nowraponall"',
190 'text' => $facturestatic->getNomUrl(1),
195 $this->info_box_contents[$line][] = array(
196 'td' =>
'class="tdoverflowmax150 maxwidth150onsmartphone"',
197 'text' => $societestatic->getNomUrl(1,
'', 44),
201 $this->info_box_contents[$line][] = array(
202 'td' =>
'class="nowraponall right amount"',
203 'text' =>
price($objp->total_ht, 0, $langs, 0, -1, -1, $conf->currency),
206 $this->info_box_contents[$line][] = array(
211 $this->info_box_contents[$line][] = array(
212 'td' =>
'class="right" width="18"',
213 'text' => $facturestatic->LibStatut($objp->paye, $objp->status, 3, $objp->am),
218 if ($this->max < $num) {
219 $this->info_box_contents[$line][] = array(
'td' =>
'colspan="6"',
'text' =>
'...');
224 $this->info_box_contents[$line][0] = array(
225 'td' =>
'class="center opacitymedium"',
226 'text'=>$langs->trans(
"NoUnpaidCustomerBills")
230 $sql =
"SELECT SUM(f.total_ht) as total_ht ".$sql2;
232 $result = $this->db->query($sql);
233 $objp = $this->db->fetch_object($result);
234 $totalamount = $objp->total_ht;
237 $this->info_box_contents[$line][] = array(
238 'tr' =>
'class="liste_total_wrap"',
239 'td' =>
'class="liste_total"',
240 'text' => $langs->trans(
"Total"),
242 $this->info_box_contents[$line][] = array(
243 'td' =>
'class="liste_total"',
246 $this->info_box_contents[$line][] = array(
247 'td' =>
'class="right liste_total" ',
248 'text' =>
price($totalamount, 0, $langs, 0, -1, -1, $conf->currency),
250 $this->info_box_contents[$line][] = array(
251 'td' =>
'class="liste_total"',
254 $this->info_box_contents[$line][] = array(
255 'td' =>
'class="liste_total"',
259 $this->db->free($result);
261 $this->info_box_contents[0][0] = array(
264 'text' => ($this->db->error().
' sql='.$sql),
268 $this->info_box_contents[0][0] = array(
269 'td' =>
'class="nohover opacitymedium left"',
270 'text' => $langs->trans(
"ReadPermissionNotAllowed")