/** * Load this->bank_account attribut */ function load_ban() { require_once DOL_DOCUMENT_ROOT . "/societe/class/companybankaccount.class.php"; $bac = new CompanyBankAccount($this->db); $bac->fetch(0,$this->id); $this->bank_account = $bac; return 1; }
} else { setEventMessage($account->error, 'errors'); } } else { setEventMessage($account->error, 'errors'); } } /* * View */ $form = new Form($db); $prelevement = new BonPrelevement($db); llxHeader(); $head = societe_prepare_head2($soc); dol_fiche_head($head, 'rib', $langs->trans("ThirdParty"), 0, 'company'); $account = new CompanyBankAccount($db); if (!$id) { $account->fetch(0, $soc->id); } else { $account->fetch($id); } if (empty($account->socid)) { $account->socid = $soc->id; } /* ************************************************************************** */ /* */ /* Visu et edition */ /* */ /* ************************************************************************** */ if ($socid && $action != 'edit' && $action != "create") { // Confirm delete third party
/** * Create a withdrawal request for a standing order * * @param User $fuser User asking standing order * @param float $amount Amount we request withdraw for * @return int <0 if KO, >0 if OK */ function demande_prelevement($fuser, $amount = 0) { global $langs; $error = 0; dol_syslog(get_class($this) . "::demande_prelevement", LOG_DEBUG); if ($this->statut > self::STATUS_DRAFT && $this->paye == 0) { require_once DOL_DOCUMENT_ROOT . '/societe/class/companybankaccount.class.php'; $bac = new CompanyBankAccount($this->db); $bac->fetch(0, $this->socid); $sql = 'SELECT count(*)'; $sql .= ' FROM ' . MAIN_DB_PREFIX . 'prelevement_facture_demande'; $sql .= ' WHERE fk_facture = ' . $this->id; $sql .= ' AND traite = 0'; dol_syslog(get_class($this) . "::demande_prelevement", LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { $row = $this->db->fetch_row($resql); if ($row[0] == 0) { $now = dol_now(); $totalpaye = $this->getSommePaiement(); $totalcreditnotes = $this->getSumCreditNotesUsed(); $totaldeposits = $this->getSumDepositsUsed(); //print "totalpaye=".$totalpaye." totalcreditnotes=".$totalcreditnotes." totaldeposts=".$totaldeposits; // We can also use bcadd to avoid pb with floating points // For example print 239.2 - 229.3 - 9.9; does not return 0. //$resteapayer=bcadd($this->total_ttc,$totalpaye,$conf->global->MAIN_MAX_DECIMALS_TOT); //$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT); if (empty($amount)) { $amount = price2num($this->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits, 'MT'); } if (is_numeric($amount) && $amount != 0) { $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'prelevement_facture_demande'; $sql .= ' (fk_facture, amount, date_demande, fk_user_demande, code_banque, code_guichet, number, cle_rib)'; $sql .= ' VALUES (' . $this->id; $sql .= ",'" . price2num($amount) . "'"; $sql .= ",'" . $this->db->idate($now) . "'"; $sql .= "," . $fuser->id; $sql .= ",'" . $bac->code_banque . "'"; $sql .= ",'" . $bac->code_guichet . "'"; $sql .= ",'" . $bac->number . "'"; $sql .= ",'" . $bac->cle_rib . "')"; dol_syslog(get_class($this) . "::demande_prelevement", LOG_DEBUG); $resql = $this->db->query($sql); if (!$resql) { $this->error = $this->db->lasterror(); dol_syslog(get_class($this) . '::demandeprelevement Erreur'); $error++; } } else { $this->error = 'WithdrawRequestErrorNilAmount'; dol_syslog(get_class($this) . '::demandeprelevement WithdrawRequestErrorNilAmount'); $error++; } if (!$error) { // Force payment mode of invoice to withdraw $payment_mode_id = dol_getIdFromCode($this->db, 'PRE', 'c_paiement'); if ($payment_mode_id > 0) { $result = $this->setPaymentMethods($payment_mode_id); } } if ($error) { return -1; } return 1; } else { $this->error = "A request already exists"; dol_syslog(get_class($this) . '::demandeprelevement Impossible de creer une demande, demande deja en cours'); return 0; } } else { $this->error = $this->db->error(); dol_syslog(get_class($this) . '::demandeprelevement Erreur -2'); return -2; } } else { $this->error = "Status of invoice does not allow this"; dol_syslog(get_class($this) . "::demandeprelevement " . $this->error . " {$this->statut}, {$this->paye}, {$this->mode_reglement_id}"); return -3; } }
/** * 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(); 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"; $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 sql=" . $sql, 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->nom, LOG_ERR); $facture_errors[$fac[0]] = "Error on default bank number RIB/IBAN for thirdparty reported by function verif() " . $fact->socid . " " . $soc->nom; } } 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 = "T" . substr($year, -2) . $month; $sql = "SELECT count(*)"; $sql .= " FROM " . MAIN_DB_PREFIX . "prelevement_bons"; $sql .= " WHERE ref LIKE '" . $ref . "%'"; $sql .= " AND entity = " . $conf->entity; 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 = $ref . substr("00" . ($row[0] + 1), -2); $filebonprev = $ref; // 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 sql=" . $sql, LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { $prev_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); } $bonprev = new BonPrelevement($this->db, $dir . "/" . $file); $bonprev->id = $prev_id; } 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 = $bonprev->AddFacture($fac[0], $fac[2], $fac[8], $fac[7], $fac[3], $fac[4], $fac[5], $fac[6]); if ($ri != 0) { $error++; } /* * Update orders * */ $sql = "UPDATE " . MAIN_DB_PREFIX . "prelevement_facture_demande"; $sql .= " SET traite = 1"; $sql .= ", date_traite = '" . $this->db->idate($now) . "'"; $sql .= ", fk_prelevement_bons = " . $prev_id; $sql .= " WHERE rowid = " . $fac[1]; dol_syslog(get_class($this) . "::Create sql=" . $sql, 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) { $bonprev->date_echeance = $datetimeprev; $bonprev->reference_remise = $ref; $bonprev->numero_national_emetteur = $conf->global->PRELEVEMENT_NUMERO_NATIONAL_EMETTEUR; $bonprev->raison_sociale = $conf->global->PRELEVEMENT_RAISON_SOCIALE; $bonprev->emetteur_code_banque = $conf->global->PRELEVEMENT_CODE_BANQUE; $bonprev->emetteur_code_guichet = $conf->global->PRELEVEMENT_CODE_GUICHET; $bonprev->emetteur_numero_compte = $conf->global->PRELEVEMENT_NUMERO_COMPTE; $bonprev->emetteur_number_key = $conf->global->PRELEVEMENT_NUMBER_KEY; $bonprev->emetteur_iban = $conf->global->PRELEVEMENT_IBAN; $bonprev->emetteur_bic = $conf->global->PRELEVEMENT_BIC; $bonprev->emetteur_ics = $conf->global->PRELEVEMENT_ICS; // TODO Add this into setup of admin/prelevement.php. Ex: PRELEVEMENT_ICS = "FR78ZZZ123456"; $bonprev->factures = $factures_prev_id; //Build file $bonprev->generate(); } dol_syslog($filebonprev); dol_syslog("Fin prelevement"); } /* * Update total */ $sql = "UPDATE " . MAIN_DB_PREFIX . "prelevement_bons"; $sql .= " SET amount = " . price2num($bonprev->total); $sql .= " WHERE rowid = " . $prev_id; $sql .= " AND entity = " . $conf->entity; dol_syslog(get_class($this) . "::Create sql=" . $sql, LOG_DEBUG); $resql = $this->db->query($sql); if (!$resql) { $error++; dol_syslog("Erreur mise a jour du total - {$sql}"); } /* * Rollback or Commit */ if (!$error) { $this->db->commit(); } else { $this->db->rollback(); dol_syslog("Error", LOG_ERR); } return count($factures_prev); } else { return 0; } }
/** * Return Array of RIB * * @return array|int 0 if KO, Array of CompanyBanckAccount if OK */ function get_all_rib() { require_once DOL_DOCUMENT_ROOT . '/societe/class/companybankaccount.class.php'; $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_rib WHERE fk_soc = ".$this->id; $result = $this->db->query($sql); if (!$result) { $this->error++; $this->errors[] = $this->db->lasterror; return 0; } else { $num_rows = $this->db->num_rows($result); $rib_array = array(); if ($num_rows) { while ($obj = $this->db->fetch_object($result)) { $rib = new CompanyBankAccount($this->db); $rib->fetch($obj->rowid); $rib_array[] = $rib; } } return $rib_array; } }
/** * testCompanyBankAccountFetch * * @param int $id Id of bank account * @return void * * @depends testCompanyBankAccountCreate * The depends says test is run only if previous is ok */ public function testCompanyBankAccountFetch($id) { global $conf,$user,$langs,$db; $conf=$this->savconf; $user=$this->savuser; $langs=$this->savlangs; $db=$this->savdb; $localobject=new CompanyBankAccount($this->savdb); $result=$localobject->fetch($id); $this->assertLessThan($result, 0); print __METHOD__." id=".$id." result=".$result."\n"; return $localobject; }
} } /* * View */ llxHeader(); $head=societe_prepare_head2($soc); dol_fiche_head($head, 'rib', $langs->trans("ThirdParty"),0,'company'); $account = new CompanyBankAccount($db); $account->fetch(0,$soc->id); if (empty($account->socid)) $account->socid=$soc->id; /* ************************************************************************** */ /* */ /* Visu et edition */ /* */ /* ************************************************************************** */ if ($_GET["socid"] && $_GET["action"] != 'edit') { // Check BBAN if (! checkBanForAccount($account))
} } /* * View */ llxHeader(); $head=societe_prepare_head2($soc); dol_fiche_head($head, 'rib', $langs->trans("ThirdParty"),0,'company'); $account = new CompanyBankAccount($db); $account->fetch(0,$soc->id); if (empty($account->socid)) $account->socid=$soc->id; /* ************************************************************************** */ /* */ /* Visu et edition */ /* */ /* ************************************************************************** */ if ($_GET["socid"] && $_GET["action"] != 'edit') { print '<table class="border" width="100%">';