Clean code

This commit is contained in:
Laurent Destailleur 2023-06-04 01:42:28 +02:00
parent e4f4bbd2de
commit 20a004f3c7
6 changed files with 61 additions and 98 deletions

View File

@ -79,8 +79,8 @@ if (empty($action) && empty($id) && empty($ref)) {
}
// Load object
if ($id > 0) {
$object->fetch($id);
if ($id > 0 || $ref) {
$object->fetch($id, $ref);
}
$permissiontoread = $user->rights->tax->charges->lire;
@ -111,13 +111,11 @@ if ($reshook < 0) {
if (empty($reshook)) {
// Classify paid
if ($action == 'confirm_paid' && $user->rights->tax->charges->creer && $confirm == 'yes') {
$object->fetch($id);
if ($action == 'confirm_paid' && $permissiontoadd && $confirm == 'yes') {
$result = $object->setPaid($user);
}
if ($action == 'reopen' && $user->rights->tax->charges->creer) {
$result = $object->fetch($id);
if ($object->paye) {
$result = $object->setUnpaid($user);
if ($result > 0) {
@ -130,19 +128,16 @@ if (empty($reshook)) {
}
// Link to a project
if ($action == 'classin' && $user->rights->tax->charges->creer) {
$object->fetch($id);
if ($action == 'classin' && $permissiontoadd) {
$object->setProject(GETPOST('fk_project'));
}
if ($action == 'setfk_user' && $user->rights->tax->charges->creer) {
$object->fetch($id);
if ($action == 'setfk_user' && $permissiontoadd) {
$object->fk_user = $fk_user;
$object->update($user);
}
if ($action == 'setlib' && $user->rights->tax->charges->creer) {
$object->fetch($id);
if ($action == 'setlib' && $permissiontoadd) {
$result = $object->setValueFrom('libelle', GETPOST('lib'), '', '', 'text', '', $user, 'TAX_MODIFY');
if ($result < 0) {
setEventMessages($object->error, $object->errors, 'errors');
@ -150,8 +145,7 @@ if (empty($reshook)) {
}
// payment mode
if ($action == 'setmode' && $user->rights->tax->charges->creer) {
$object->fetch($id);
if ($action == 'setmode' && $permissiontoadd) {
$result = $object->setPaymentMethods(GETPOST('mode_reglement_id', 'int'));
if ($result < 0) {
setEventMessages($object->error, $object->errors, 'errors');
@ -159,8 +153,7 @@ if (empty($reshook)) {
}
// Bank account
if ($action == 'setbankaccount' && $user->rights->tax->charges->creer) {
$object->fetch($id);
if ($action == 'setbankaccount' && $permissiontoadd) {
$result = $object->setBankAccount(GETPOST('fk_account', 'int'));
if ($result < 0) {
setEventMessages($object->error, $object->errors, 'errors');
@ -168,8 +161,7 @@ if (empty($reshook)) {
}
// Delete social contribution
if ($action == 'confirm_delete' && $confirm == 'yes') {
$object->fetch($id);
if ($action == 'confirm_delete' && $permissiontodelete && $confirm == 'yes') {
$totalpaid = $object->getSommePaiement();
if (empty($totalpaid)) {
$result = $object->delete($user);
@ -186,7 +178,7 @@ if (empty($reshook)) {
// Add social contribution
if ($action == 'add' && $user->rights->tax->charges->creer) {
if ($action == 'add' && $permissiontoadd) {
$amount = price2num(GETPOST('amount', 'alpha'), 'MT');
if (!$dateech) {
@ -224,7 +216,7 @@ if (empty($reshook)) {
}
if ($action == 'update' && !GETPOST("cancel") && $user->rights->tax->charges->creer) {
if ($action == 'update' && !$cancel && $permissiontoadd) {
$amount = price2num(GETPOST('amount', 'alpha'), 'MT');
if (!$dateech) {
@ -245,7 +237,7 @@ if (empty($reshook)) {
$object->date_ech = $dateech;
$object->periode = $dateperiod;
$object->amount = $amount;
$object->fk_user = $fk_user;
$object->fk_user = $fk_user;
$result = $object->update($user);
if ($result <= 0) {
@ -259,12 +251,10 @@ if (empty($reshook)) {
$action = '';
}
if ($action == 'confirm_clone' && $confirm == 'yes' && ($user->rights->tax->charges->creer)) {
if ($action == 'confirm_clone' && $confirm == 'yes' && $permissiontoadd) {
$db->begin();
$originalId = $id;
$object->fetch($id);
$originalId = $object->id;
if ($object->id > 0) {
$object->id = $object->ref = null;

View File

@ -453,8 +453,12 @@ class ChargeSociales extends CommonObject
$sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales SET";
$sql .= " paye = 1";
$sql .= " WHERE rowid = ".((int) $this->id);
$return = $this->db->query($sql);
if ($return) {
$this->paye = 1;
return 1;
} else {
return -1;
@ -488,8 +492,12 @@ class ChargeSociales extends CommonObject
$sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales SET";
$sql .= " paye = 0";
$sql .= " WHERE rowid = ".((int) $this->id);
$return = $this->db->query($sql);
if ($return) {
$this->paye = 0;
return 1;
} else {
return -1;

View File

@ -478,7 +478,7 @@ class Don extends CommonObject
*/
public function update($user, $notrigger = 0)
{
global $langs, $conf;
global $langs;
$error = 0;
@ -566,7 +566,6 @@ class Don extends CommonObject
*/
public function delete($user, $notrigger = 0)
{
global $user, $conf, $langs;
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$error = 0;
@ -629,8 +628,6 @@ class Don extends CommonObject
*/
public function fetch($id, $ref = '')
{
global $conf;
$sql = "SELECT d.rowid, d.datec, d.date_valid, d.tms as datem, d.datedon,";
$sql .= " d.fk_soc as socid,d.firstname, d.lastname, d.societe, d.amount, d.fk_statut, d.address, d.zip, d.town, ";
$sql .= " d.fk_country, d.country as country_olddata, d.public, d.amount, d.fk_payment, d.paid, d.note_private, d.note_public, d.email, d.phone, ";
@ -728,7 +725,7 @@ class Don extends CommonObject
public function valid_promesse($id, $userid, $notrigger = 0)
{
// phpcs:enable
global $langs, $user;
global $user;
$error = 0;
@ -763,23 +760,6 @@ class Don extends CommonObject
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Classify the donation as paid, the donation was received
*
* @deprecated
* @see setPaid()
* @param int $id id of donation
* @param int $modepayment mode of payment
* @return int <0 if KO, >0 if OK
*/
public function set_paid($id, $modepayment = 0)
{
// phpcs:enable
dol_syslog(get_class($this)."::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
return $this->setPaid($id, $modepayment);
}
/**
* Classify the donation as paid, the donation was received
*
@ -897,8 +877,6 @@ class Don extends CommonObject
public function load_state_board()
{
// phpcs:enable
global $conf;
$this->nb = array();
$sql = "SELECT count(d.rowid) as nb";
@ -1042,7 +1020,7 @@ class Don extends CommonObject
}
}
$modelpath = "core/modules/dons/";
//$modelpath = "core/modules/dons/";
// TODO Restore use of commonGenerateDocument instead of dedicated code here
//return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);

View File

@ -386,22 +386,6 @@ class Loan extends CommonObject
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Tag loan as paid completely
*
* @deprecated
* @see setPaid()
* @param User $user Object user making change
* @return int <0 if KO, >0 if OK
*/
public function set_paid($user)
{
// phpcs:enable
dol_syslog(get_class($this)."::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
return $this->setPaid($user);
}
/**
* Tag loan as paid completely
*
@ -413,8 +397,11 @@ class Loan extends CommonObject
$sql = "UPDATE ".MAIN_DB_PREFIX."loan SET";
$sql .= " paid = ".$this::STATUS_PAID;
$sql .= " WHERE rowid = ".((int) $this->id);
$return = $this->db->query($sql);
if ($return) {
$this->paid = $this::STATUS_PAID;
return 1;
} else {
$this->error = $this->db->lasterror();
@ -449,8 +436,11 @@ class Loan extends CommonObject
$sql = "UPDATE ".MAIN_DB_PREFIX."loan SET";
$sql .= " paid = ".$this::STATUS_STARTED;
$sql .= " WHERE rowid = ".((int) $this->id);
$return = $this->db->query($sql);
if ($return) {
$this->paid = $this::STATUS_STARTED;
return 1;
} else {
$this->error = $this->db->lasterror();
@ -458,21 +448,6 @@ class Loan extends CommonObject
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Tag loan as payment as unpaid
* @deprecated
* @see setUnpaid()
* @param User $user Object user making change
* @return int <0 if KO, >0 if OK
*/
public function set_unpaid($user)
{
// phpcs:enable
dol_syslog(get_class($this)."::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
return $this->setUnpaid($user);
}
/**
* Tag loan as payment as unpaid
*
@ -484,8 +459,11 @@ class Loan extends CommonObject
$sql = "UPDATE ".MAIN_DB_PREFIX."loan SET";
$sql .= " paid = ".$this::STATUS_UNPAID;
$sql .= " WHERE rowid = ".((int) $this->id);
$return = $this->db->query($sql);
if ($return) {
$this->paid = $this::STATUS_UNPAID;
return 1;
} else {
$this->error = $this->db->lasterror();

View File

@ -124,7 +124,7 @@ class PaymentSalary extends CommonObject
*/
public function create($user, $closepaidcontrib = 0)
{
global $conf, $langs;
global $conf;
$error = 0;
@ -184,18 +184,20 @@ class PaymentSalary extends CommonObject
// If we want to closed payed invoices
if ($closepaidcontrib) {
$contrib = new Salary($this->db);
$contrib->fetch($contribid);
$paiement = $contrib->getSommePaiement();
//$creditnotes=$contrib->getSumCreditNotesUsed();
$tmpsalary = new Salary($this->db);
$tmpsalary->fetch($contribid);
$paiement = $tmpsalary->getSommePaiement();
//$creditnotes=$tmpsalary->getSumCreditNotesUsed();
$creditnotes = 0;
//$deposits=$contrib->getSumDepositsUsed();
//$deposits=$tmpsalary->getSumDepositsUsed();
$deposits = 0;
$alreadypayed = price2num($paiement + $creditnotes + $deposits, 'MT');
$remaintopay = price2num($contrib->amount - $paiement - $creditnotes - $deposits, 'MT');
$remaintopay = price2num($tmpsalary->amount - $paiement - $creditnotes - $deposits, 'MT');
if ($remaintopay == 0) {
$result = $contrib->set_paid($user);
} else dol_syslog("Remain to pay for conrib ".$contribid." not null. We do nothing.");
$result = $tmpsalary->set_paid($user);
} else {
dol_syslog("Remain to pay for conrib ".$contribid." not null. We do nothing.");
}
}
}
}

View File

@ -148,8 +148,6 @@ class Salary extends CommonObject
*/
public function update($user = null, $notrigger = 0)
{
global $conf, $langs;
$error = 0;
// Clean parameters
@ -226,10 +224,8 @@ class Salary extends CommonObject
*/
public function fetch($id, $user = null)
{
global $langs;
$sql = "SELECT";
$sql .= " s.rowid,";
$sql .= " s.tms,";
$sql .= " s.fk_user,";
$sql .= " s.datep,";
@ -246,7 +242,6 @@ class Salary extends CommonObject
$sql .= " s.fk_user_author,";
$sql .= " s.fk_user_modif,";
$sql .= " s.fk_account";
$sql .= " FROM ".MAIN_DB_PREFIX."salary as s";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON s.fk_bank = b.rowid";
$sql .= " WHERE s.rowid = ".((int) $id);
@ -649,9 +644,15 @@ class Salary extends CommonObject
$sql = "UPDATE ".MAIN_DB_PREFIX."salary SET";
$sql .= " paye = 1";
$sql .= " WHERE rowid = ".((int) $this->id);
$return = $this->db->query($sql);
if ($return) return 1;
else return -1;
if ($return) {
$this->paye = 1;
return 1;
} else {
return -1;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@ -667,9 +668,15 @@ class Salary extends CommonObject
$sql = "UPDATE ".MAIN_DB_PREFIX."salary SET";
$sql .= " paye = 0";
$sql .= " WHERE rowid = ".((int) $this->id);
$return = $this->db->query($sql);
if ($return) return 1;
else return -1;
if ($return) {
$this->paye = 0;
return 1;
} else {
return -1;
}
}