/** * Truncate a string to a particular length adding '...' if string larger than length. * If length = max length+1, we do no truncate to avoid having just 1 char replaced with '...'. * MAIN_DISABLE_TRUNC=1 can disable all truncings * @param string String to truncate * @param size Max string size. 0 for no limit. * @param trunc Where to trunc: right, left, middle, wrap * @param stringencoding Tell what is source string encoding * @return string Truncated string */ function dol_trunc($string, $size = 40, $trunc = 'right', $stringencoding = 'UTF-8') { global $conf; if ($size == 0) { return $string; } if (empty($conf->global->MAIN_DISABLE_TRUNC)) { // We go always here if ($trunc == 'right') { $newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string; if (dol_strlen($newstring, $stringencoding) > $size + 1) { return dol_substr($newstring, 0, $size, $stringencoding) . '...'; } else { return $string; } } if ($trunc == 'middle') { $newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string; if (dol_strlen($newstring, $stringencoding) > 2 && dol_strlen($newstring, $stringencoding) > $size + 1) { $size1 = round($size / 2); $size2 = round($size / 2); return dol_substr($newstring, 0, $size1, $stringencoding) . '...' . dol_substr($newstring, dol_strlen($newstring, $stringencoding) - $size2, $size2, $stringencoding); } else { return $string; } } if ($trunc == 'left') { $newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string; if (dol_strlen($newstring, $stringencoding) > $size + 1) { return '...' . dol_substr($newstring, dol_strlen($newstring, $stringencoding) - $size, $size, $stringencoding); } else { return $string; } } if ($trunc == 'wrap') { $newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string; if (dol_strlen($newstring, $stringencoding) > $size + 1) { return dol_substr($newstring, 0, $size, $stringencoding) . "\n" . dol_trunc(dol_substr($newstring, $size, dol_strlen($newstring, $stringencoding) - $size, $stringencoding), $size, $trunc); } else { return $string; } } } else { return $string; } }
$px2->SetMaxValue($px2->GetCeilMaxValue()); $px2->SetMinValue(min(0, $px2->GetFloorMinValue())); $px2->SetWidth($WIDTH); $px2->SetHeight($HEIGHT); $px2->SetYLabel($langs->trans("Amount")); $px2->SetShading(3); $px2->SetHorizTickIncrement(1); $px2->SetPrecisionY(0); $px2->mode = 'depth'; $px2->SetTitle($langs->trans("AmountTotal")); $px2->draw($filenameamount, $fileurlamount); } $res = $stats->getAverageByMonth($year); $data = array(); for ($i = 1; $i < 13; $i++) { $data[$i - 1] = array(ucfirst(dol_substr(dol_print_date(dol_mktime(12, 0, 0, $i, 1, $year), "%b"), 0, 3)), $res[$i]); } if (!$user->rights->societe->client->voir || $user->societe_id) { $filename_avg = $dir . '/ordersaverage-' . $user->id . '-' . $year . '.png'; if ($mode == 'customer') { $fileurl_avg = DOL_URL_ROOT . '/viewimage.php?modulepart=orderstats&file=ordersaverage-' . $user->id . '-' . $year . '.png'; } if ($mode == 'supplier') { $fileurl_avg = DOL_URL_ROOT . '/viewimage.php?modulepart=orderstatssupplier&file=ordersaverage-' . $user->id . '-' . $year . '.png'; } } else { $filename_avg = $dir . '/ordersaverage-' . $year . '.png'; if ($mode == 'customer') { $fileurl_avg = DOL_URL_ROOT . '/viewimage.php?modulepart=orderstats&file=ordersaverage-' . $year . '.png'; } if ($mode == 'supplier') {
/** * Renvoie le nombre d'element par mois pour une annee donnee * * @param int $year Year * @param string $sql SQL * @return array */ function _getAmountByMonth($year, $sql) { $result = array(); $res = array(); dol_syslog(get_class($this) . "::_getAmountByMonth sql=" . $sql); $resql = $this->db->query($sql); if ($resql) { $num = $this->db->num_rows($resql); $i = 0; while ($i < $num) { $row = $this->db->fetch_row($resql); $j = $row[0] * 1; $result[$j] = $row[1]; $i++; } $this->db->free($resql); } else { dol_print_error($this->db); } for ($i = 1; $i < 13; $i++) { $res[$i] = (int) round(isset($result[$i]) ? $result[$i] : 0); } $data = array(); for ($i = 1; $i < 13; $i++) { $month = dol_print_date(dol_mktime(12, 0, 0, $i, 1, $year), "%b"); $month = dol_substr($month, 0, 3); $data[$i - 1] = array(ucfirst($month), $res[$i]); } return $data; }
/** * Create a withdraw * TODO delete params banque and agence when not necesary * * @param int $banque dolibarr mysoc bank * @param int $agence dolibarr mysoc bank office (guichet) * @param string $mode real=do action, simu=test only * @return int <0 if KO, nbre of invoice withdrawed if OK */ function Create($banque = 0, $agence = 0, $mode = 'real') { global $conf, $langs; dol_syslog(get_class($this) . "::Create banque={$banque} agence={$agence}"); require_once DOL_DOCUMENT_ROOT . "/compta/facture/class/facture.class.php"; require_once DOL_DOCUMENT_ROOT . "/societe/class/societe.class.php"; $error = 0; $datetimeprev = time(); $month = strftime("%m", $datetimeprev); $year = strftime("%Y", $datetimeprev); $puser = new User($this->db, $conf->global->PRELEVEMENT_USER); /* * Read invoices */ $factures = array(); $factures_prev = array(); $factures_result = array(); $factures_prev_id = array(); $factures_errors = array(); if (!$error) { $sql = "SELECT f.rowid, pfd.rowid as pfdrowid, f.fk_soc"; $sql .= ", pfd.code_banque, pfd.code_guichet, pfd.number, pfd.cle_rib"; $sql .= ", pfd.amount"; $sql .= ", s.nom as name"; $sql .= " FROM " . MAIN_DB_PREFIX . "facture as f"; $sql .= ", " . MAIN_DB_PREFIX . "societe as s"; $sql .= ", " . MAIN_DB_PREFIX . "prelevement_facture_demande as pfd"; //if ($banque || $agence) $sql.= ", ".MAIN_DB_PREFIX."societe_rib as sr"; $sql .= " WHERE f.rowid = pfd.fk_facture"; $sql .= " AND f.entity = " . $conf->entity; $sql .= " AND s.rowid = f.fk_soc"; //if ($banque || $agence) $sql.= " AND s.rowid = sr.fk_soc"; $sql .= " AND f.fk_statut = 1"; $sql .= " AND f.paye = 0"; $sql .= " AND pfd.traite = 0"; $sql .= " AND f.total_ttc > 0"; //if ($banque) $sql.= " AND sr.code_banque = '".$conf->global->PRELEVEMENT_CODE_BANQUE."'"; //if ($agence) $sql.= " AND sr.code_guichet = '".$conf->global->PRELEVEMENT_CODE_GUICHET."'"; dol_syslog(get_class($this) . "::Create", LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { $num = $this->db->num_rows($resql); $i = 0; while ($i < $num) { $row = $this->db->fetch_row($resql); $factures[$i] = $row; // All fields $i++; } $this->db->free($resql); dol_syslog($i . " invoices to withdraw"); } else { $error = 1; dol_syslog("Erreur -1"); dol_syslog($this->db->error()); } } if (!$error) { require_once DOL_DOCUMENT_ROOT . '/societe/class/companybankaccount.class.php'; $soc = new Societe($this->db); // Check RIB $i = 0; dol_syslog("Start RIB check"); if (count($factures) > 0) { foreach ($factures as $key => $fac) { $fact = new Facture($this->db); if ($fact->fetch($fac[0]) >= 0) { if ($soc->fetch($fact->socid) >= 0) { $bac = new CompanyBankAccount($this->db); $bac->fetch(0, $soc->id); if ($bac->verif() >= 1) { $factures_prev[$i] = $fac; /* second tableau necessaire pour BonPrelevement */ $factures_prev_id[$i] = $fac[0]; $i++; } else { dol_syslog("Error on default bank number RIB/IBAN for thirdparty reported by verif() " . $fact->socid . " " . $soc->name, LOG_ERR); $this->invoice_in_error[$fac[0]] = "Error on default bank number RIB/IBAN for invoice " . $fact->getNomUrl(0) . " for thirdparty (reported by function verif) " . $soc->name; } } else { dol_syslog("Failed to read company", LOG_ERR); } } else { dol_syslog("Failed to read invoice", LOG_ERR); } } } else { dol_syslog("No invoice to process"); } } $ok = 0; // Withdraw invoices in factures_prev array $out = count($factures_prev) . " invoices will be withdrawn."; //print $out."\n"; dol_syslog($out); if (count($factures_prev) > 0) { if ($mode == 'real') { $ok = 1; } else { print $langs->trans("ModeWarning"); //"Option for real mode was not set, we stop after this simulation\n"; } } if ($ok) { /* * We are in real mode. * We create withdraw receipt and build withdraw into disk */ $this->db->begin(); $now = dol_now(); /* * Traitements */ if (!$error) { $ref = substr($year, -2) . $month; $sql = "SELECT substring(ref from char_length(ref) - 1)"; $sql .= " FROM " . MAIN_DB_PREFIX . "prelevement_bons"; $sql .= " WHERE ref LIKE '%" . $ref . "%'"; $sql .= " AND entity = " . $conf->entity; $sql .= " ORDER BY ref DESC LIMIT 1"; dol_syslog(get_class($this) . "::Create sql=" . $sql, LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { $row = $this->db->fetch_row($resql); } else { $error++; dol_syslog("Erreur recherche reference"); } $ref = "T" . $ref . str_pad(dol_substr("00" . intval($row[0]) + 1, 0, 2), 2, "0", STR_PAD_LEFT); $this->filename = $conf->prelevement->dir_output . '/receipts/' . $ref . '.xml'; // Create withdraw receipt in database $sql = "INSERT INTO " . MAIN_DB_PREFIX . "prelevement_bons ("; $sql .= " ref, entity, datec"; $sql .= ") VALUES ("; $sql .= "'" . $this->db->escape($ref) . "'"; $sql .= ", " . $conf->entity; $sql .= ", '" . $this->db->idate($now) . "'"; $sql .= ")"; dol_syslog(get_class($this) . "::Create", LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "prelevement_bons"); $dir = $conf->prelevement->dir_output . '/receipts'; $file = $filebonprev; if (!is_dir($dir)) { dol_mkdir($dir); } } else { $error++; dol_syslog("Erreur creation du bon de prelevement"); } } /* * Create withdrawal receipt */ if (!$error) { if (count($factures_prev) > 0) { foreach ($factures_prev as $fac) { // Fetch invoice $fact = new Facture($this->db); $fact->fetch($fac[0]); /* * Add standing order * * * $fac[3] : banque * $fac[4] : guichet * $fac[5] : number * $fac[6] : cle rib * $fac[7] : amount * $fac[8] : client nom * $fac[2] : client id */ $ri = $this->AddFacture($fac[0], $fac[2], $fac[8], $fac[7], $fac[3], $fac[4], $fac[5], $fac[6]); if ($ri != 0) { $error++; } // Update invoice requests as done $sql = "UPDATE " . MAIN_DB_PREFIX . "prelevement_facture_demande"; $sql .= " SET traite = 1"; $sql .= ", date_traite = '" . $this->db->idate($now) . "'"; $sql .= ", fk_prelevement_bons = " . $this->id; $sql .= " WHERE rowid = " . $fac[1]; dol_syslog(get_class($this) . "::Create", LOG_DEBUG); $resql = $this->db->query($sql); if (!$resql) { $error++; dol_syslog("Erreur mise a jour des demandes"); dol_syslog($this->db->error()); } } } } if (!$error) { /* * Withdraw receipt */ dol_syslog("Debut prelevement - Nombre de factures " . count($factures_prev)); if (count($factures_prev) > 0) { $this->date_echeance = $datetimeprev; $this->reference_remise = $ref; $this->numero_national_emetteur = $conf->global->PRELEVEMENT_NUMERO_NATIONAL_EMETTEUR; $this->raison_sociale = $conf->global->PRELEVEMENT_RAISON_SOCIALE; $this->emetteur_code_banque = $conf->global->PRELEVEMENT_CODE_BANQUE; $this->emetteur_code_guichet = $conf->global->PRELEVEMENT_CODE_GUICHET; $this->emetteur_numero_compte = $conf->global->PRELEVEMENT_NUMERO_COMPTE; $this->emetteur_number_key = $conf->global->PRELEVEMENT_NUMBER_KEY; $this->emetteur_iban = $conf->global->PRELEVEMENT_IBAN; $this->emetteur_bic = $conf->global->PRELEVEMENT_BIC; $this->emetteur_ics = $conf->global->PRELEVEMENT_ICS; // TODO Add this into setup of admin/prelevement.php. Ex: PRELEVEMENT_ICS = "FR78ZZZ123456"; $this->factures = $factures_prev_id; // Generation of SEPA file $this->generate(); } dol_syslog($filebonprev); dol_syslog("Fin prelevement"); } /* * Update total */ $sql = "UPDATE " . MAIN_DB_PREFIX . "prelevement_bons"; $sql .= " SET amount = " . price2num($this->total); $sql .= " WHERE rowid = " . $this->id; $sql .= " AND entity = " . $conf->entity; dol_syslog(get_class($this) . "::Create", LOG_DEBUG); $resql = $this->db->query($sql); if (!$resql) { $error++; dol_syslog("Erreur mise a jour du total - {$sql}"); } if (!$error) { $this->db->commit(); } else { $this->db->rollback(); dol_syslog("Error", LOG_ERR); } return count($factures_prev); } else { return 0; } }
/** * Renvoie le montant moyen par mois pour une annee donnee * * @param int $year Year * @param string $sql SQL * @param int $format 0=Label of absiss is a translated text, 1=Label of absiss is a number * @return array */ function _getAverageByMonth($year, $sql, $format = 0) { $result = array(); $res = array(); dol_syslog(get_class($this) . '::' . __FUNCTION__ . "", LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { $num = $this->db->num_rows($resql); $i = 0; $j = 0; while ($i < $num) { $row = $this->db->fetch_row($resql); $j = $row[0] * 1; $result[$j] = $row[1]; $i++; } $this->db->free($resql); } else { dol_print_error($this->db); } for ($i = 1; $i < 13; $i++) { $res[$i] = isset($result[$i]) ? $result[$i] : 0; } $data = array(); for ($i = 1; $i < 13; $i++) { $month = dol_print_date(dol_mktime(12, 0, 0, $i, 1, $year), $format ? "%m" : "%b"); $month = dol_substr($month, 0, 3); $data[$i - 1] = array($month, $res[$i]); } return $data; }
/** * Truncate a string to a particular length adding '...' if string larger than length. * If length = max length+1, we do no truncate to avoid having just 1 char replaced with '...'. * MAIN_DISABLE_TRUNC=1 can disable all truncings * * @param string $string String to truncate * @param int $size Max string size visible. 0 for no limit. Final string size can be 1 more (if size was max+1) or 3 more (if we added ...) * @param string $trunc Where to trunc: right, left, middle (size must be a 2 power), wrap * @param string $stringencoding Tell what is source string encoding * @param int $nodot Truncation do not add ... after truncation. So it's an exact truncation. * @param int $display Trunc is use to display and can be changed for small screen * @return string Truncated string */ function dol_trunc($string, $size = 40, $trunc = 'right', $stringencoding = 'UTF-8', $nodot = 0, $display = 0) { global $conf; if (empty($stringencoding)) { $stringencoding = 'UTF-8'; } if ($size == 0 || !empty($conf->global->MAIN_DISABLE_TRUNC)) { return $string; } // reduce for small screen if ($conf->dol_optimize_smallscreen == 1 && $display == 1) { $size = round($size / 3); } // We go always here if ($trunc == 'right') { $newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string; if (dol_strlen($newstring, $stringencoding) > $size + ($nodot ? 0 : 1)) { return dol_substr($newstring, 0, $size, $stringencoding) . ($nodot ? '' : '...'); } else { return $string; } } elseif ($trunc == 'middle') { $newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string; if (dol_strlen($newstring, $stringencoding) > 2 && dol_strlen($newstring, $stringencoding) > $size + 1) { $size1 = round($size / 2); $size2 = round($size / 2); return dol_substr($newstring, 0, $size1, $stringencoding) . '...' . dol_substr($newstring, dol_strlen($newstring, $stringencoding) - $size2, $size2, $stringencoding); } else { return $string; } } elseif ($trunc == 'left') { $newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string; if (dol_strlen($newstring, $stringencoding) > $size + 1) { return '...' . dol_substr($newstring, dol_strlen($newstring, $stringencoding) - $size, $size, $stringencoding); } else { return $string; } } elseif ($trunc == 'wrap') { $newstring = dol_textishtml($string) ? dol_string_nohtmltag($string, 1) : $string; if (dol_strlen($newstring, $stringencoding) > $size + 1) { return dol_substr($newstring, 0, $size, $stringencoding) . "\n" . dol_trunc(dol_substr($newstring, $size, dol_strlen($newstring, $stringencoding) - $size, $stringencoding), $size, $trunc); } else { return $string; } } else { return 'BadParam3CallingDolTrunc'; } }