PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/2.071.0/_document_echeancier.class.php

https://github.com/crepeausucre/soothERP
PHP | 1032 lines | 708 code | 209 blank | 115 comment | 185 complexity | 5ecfc72e6912f5a03524fa04a540cac0 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0
  1. <?php
  2. class document_echeance{
  3. public static $nom_table = "doc_echeanciers"; // Le nom de la table dans la base de données
  4. public static $types_reglements = array (1 => 'Acompte', 2 => 'Arrhes', 3 => 'Echeance', 4 => 'Solde');
  5. private $id_doc_echeance;
  6. private $ref_doc;
  7. private $montant;
  8. private $pourcentage;
  9. private $id_mode_reglement;
  10. private $date;
  11. private $jour;
  12. private $type_reglement;
  13. //******************************************
  14. public function __construct($id_echeance){
  15. global $bdd;
  16. if(is_int(intval($id_echeance))){
  17. $query = "SELECT *
  18. FROM ".self::$nom_table."
  19. WHERE id_doc_echeance = ".$id_echeance." ORDER BY id_doc_echeance;
  20. ";
  21. if($resultat = $bdd->query($query)){
  22. if($echeance = $resultat->fetchObject()){
  23. $this->id_doc_echeance = $echeance->id_doc_echeance;
  24. $this->ref_doc = $echeance->ref_doc;
  25. $this->montant = $echeance->montant;
  26. $this->pourcentage = $echeance->pourcentage;
  27. $this->id_mode_reglement = $echeance->id_mode_reglement;
  28. $this->date = $echeance->date;
  29. $this->jour = $echeance->jour;
  30. $this->type_reglement = array_search($echeance->type_reglement,self::$types_reglements);
  31. }
  32. }
  33. }
  34. }
  35. //******************************************
  36. public static function _create($ref_doc, $id_mode_reglement = "NULL", $type_reglement = 1){
  37. global $bdd;
  38. //Verifs des parametres
  39. if( !isset($ref_doc) ) { return false; }
  40. if(!is_string($ref_doc) || $ref_doc=="" || !(is_int(intval($id_mode_reglement)) || is_null($id_mode_reglement)) || !is_int(intval($type_reglement)) || !array_key_exists($type_reglement, self::$types_reglements)) { return false; }
  41. $query = "INSERT INTO ".self::$nom_table."
  42. (ref_doc, id_mode_reglement, type_reglement)
  43. VALUES ('".$ref_doc."', ".num_or_null($id_mode_reglement).", '".self::$types_reglements[$type_reglement]."');";
  44. if(!$bdd->query($query)){
  45. return false;
  46. }
  47. $id = $bdd->lastInsertId();
  48. return new document_echeance($id);
  49. }
  50. //******************************************
  51. public function save(){
  52. global $bdd;
  53. $query = "UPDATE ".self::$nom_table."
  54. SET ref_doc = '".$this->ref_doc."',
  55. montant = ".num_or_null($this->montant).",
  56. pourcentage = ".num_or_null($this->pourcentage).",
  57. id_mode_reglement = ".num_or_null($this->id_mode_reglement).",
  58. date = ".text_or_null($this->date).",
  59. jour = ".text_or_null(strval($this->jour)).",
  60. type_reglement = '".self::$types_reglements[$this->type_reglement]."'
  61. WHERE id_doc_echeance = ".$this->id_doc_echeance.";";
  62. if($bdd->query($query)){
  63. return true;
  64. }else{
  65. return false;
  66. }
  67. }
  68. //******************************************
  69. public function is_fdm(){
  70. if(strpos($this->jour,"FDM") === false){
  71. return false;
  72. }else{
  73. return true;
  74. }
  75. }
  76. //******************************************
  77. public function is_acompte(){
  78. if($this->type_reglement == 1 OR $this->type_reglement == 2){
  79. return true;
  80. }else{
  81. return false;
  82. }
  83. }
  84. //******************************************
  85. public function is_solde(){
  86. if( $this->type_reglement == 4 ){
  87. return true;
  88. }else{
  89. return false;
  90. }
  91. }
  92. //******************************************
  93. public function is_echue(){
  94. if( $this->date != "" && $this->date <= strftime("%Y-%m-%d",time()) ){
  95. return true;
  96. }else{
  97. return false;
  98. }
  99. }
  100. //******************************************
  101. public function is_echeance(){
  102. if( array_search($this->type_reglement,self::$types_reglements) == 3 ){
  103. return true;
  104. }else{
  105. return false;
  106. }
  107. }
  108. //*************************************
  109. // GETTERS
  110. //*************************************
  111. //*****************************************************
  112. // getter
  113. // result : int
  114. function get_Id_doc_echeance(){
  115. return $this->id_doc_echeance;
  116. }
  117. //*****************************************************
  118. // getter
  119. // result : varchar
  120. function get_Ref_doc(){
  121. return $this->ref_doc;
  122. }
  123. //*****************************************************
  124. // getter
  125. // result : float
  126. function get_Montant(){
  127. return $this->montant;
  128. }
  129. //*****************************************************
  130. // getter
  131. // result : float
  132. function get_Pourcentage(){
  133. return $this->pourcentage;
  134. }
  135. //*****************************************************
  136. // getter
  137. // result : int
  138. function get_Mode_reglement(){
  139. return $this->id_mode_reglement;
  140. }
  141. //*****************************************************
  142. // getter
  143. // result : date
  144. function get_Date(){
  145. return $this->date;
  146. }
  147. //*****************************************************
  148. // getter
  149. // result : varchar
  150. function get_Jour(){
  151. return $this->jour;
  152. }
  153. //*****************************************************
  154. // getter
  155. // result : int
  156. function get_Nbjours(){
  157. if (is_null($this->jour)) { return null; }
  158. if (strpos($this->jour,"FDM") === false){
  159. return intval($this->jour);
  160. }else{
  161. return intval( substr($this->jour, 0, strlen($this->jour)-3) );
  162. }
  163. }
  164. //*****************************************************
  165. // getter
  166. // result : string
  167. function get_Type_reglement(){
  168. return self::$types_reglements[$this->type_reglement];
  169. }
  170. //*****************************************************
  171. // getter
  172. // result : int
  173. function get_Type_reglement_int(){
  174. return $this->type_reglement;
  175. }
  176. //*****************************************************
  177. // static
  178. // param : int
  179. // result : str
  180. public static function _get_Type_reglement_lib($type_reglement){
  181. if ( array_key_exists($type_reglement, self::$types_reglements)){
  182. return self::$types_reglements[$type_reglement];
  183. }
  184. return false;
  185. }
  186. //*************************************
  187. // SETTERS
  188. //*************************************
  189. //*****************************************************
  190. // setter
  191. // @param : int
  192. function set_Id_doc_echeance($id_echeance){
  193. if(is_int($id_echeance)){
  194. $this->id_doc_echeance = $id_echeance;
  195. return true;
  196. }else{
  197. return false;
  198. }
  199. }
  200. //*****************************************************
  201. // setter
  202. // @param : varchar
  203. function set_Ref_doc($ref_doc){
  204. if(is_string($ref_doc)){
  205. $this->ref_doc = $ref_doc;
  206. return true;
  207. }else{
  208. return false;
  209. }
  210. }
  211. //*****************************************************
  212. // setter
  213. // @param : float
  214. function set_Montant($montant){
  215. if(is_numeric($montant)){
  216. $this->montant = $montant;
  217. return true;
  218. }else{
  219. return false;
  220. }
  221. }
  222. //*****************************************************
  223. // setter
  224. // @param : float
  225. function set_Pourcentage($pourcentage){
  226. if(is_numeric($pourcentage)){
  227. $this->pourcentage = $pourcentage;
  228. return true;
  229. }else{
  230. return false;
  231. }
  232. }
  233. //*****************************************************
  234. // setter
  235. // @param : int
  236. function set_Mode_reglement($id_mode_reglement){
  237. if(is_int(intval($id_mode_reglement))){
  238. $this->id_mode_reglement = $id_mode_reglement;
  239. return true;
  240. }else{
  241. return false;
  242. }
  243. }
  244. //*****************************************************
  245. // setter
  246. // @param : date
  247. function set_Date($date){
  248. if(is_string($date)){
  249. $this->date = $date;
  250. return true;
  251. }else{
  252. return false;
  253. }
  254. }
  255. //*****************************************************
  256. // setter
  257. // @param : jour
  258. // @param : boolean (Fin de mois)
  259. function set_Jour($jour, $fdm = false){
  260. if(is_int($jour)){
  261. $this->jour = strval($jour);
  262. if($fdm){
  263. $this->jour .= "FDM";
  264. return true;
  265. }
  266. }else{
  267. return false;
  268. }
  269. }
  270. //*****************************************************
  271. // setter
  272. // @param : int / string
  273. function set_Type_reglement($type_reglement){
  274. if(is_int($type_reglement)){
  275. $this->type_reglement = $type_reglement;
  276. return true;
  277. }elseif(is_string($type_reglement)){
  278. $type_reglement_id = array_search($type_reglement, self::$types_reglements);
  279. if($type_reglement_id === false){
  280. return false;
  281. }else{
  282. $this->type_reglement = $type_reglement_id;
  283. return true;
  284. }
  285. }else{
  286. return false;
  287. }
  288. }
  289. }
  290. //************************************************************************************************************************************
  291. class document_echeancier{
  292. private $ref_doc;
  293. private $echeances = array();
  294. private $echeances_loaded = false;
  295. private $papa;
  296. private $solde_exist = false;
  297. private $acompte_exist = false;
  298. public function __construct($ref_doc, &$papa = null){
  299. if (is_string($ref_doc)){
  300. $this->ref_doc = $ref_doc;
  301. $this->load_echeances();
  302. if(!is_null($papa) && is_subclass_of($papa,"document")){
  303. $this->papa = $papa;
  304. }
  305. }
  306. }
  307. public function create_from_ref_contact(){
  308. global $CLIENT_ID_PROFIL;
  309. if(!$this->papa_est_la()) { return false; }
  310. $contact = $this->papa->getContact();
  311. if ($contact != ""){
  312. $profils = $contact->getProfils();
  313. if(isset($profils[$CLIENT_ID_PROFIL]) ) {
  314. $client_profil = $profils[$CLIENT_ID_PROFIL];
  315. if(!is_null($client_profil->getPrepaiement_ratio()) && !is_null($client_profil->getPrepaiement_type()) && $client_profil->getPrepaiement_ratio()>0 ){
  316. $this->maj_acompte($client_profil->getPrepaiement_ratio(), $client_profil->getPrepaiement_type());
  317. }
  318. if(!is_null($client_profil->getDelai_reglement())){
  319. $this->maj_solde_delai_reglement($client_profil->getDelai_reglement());
  320. }
  321. $this->set_Mode_Reglement($client_profil->getId_reglement_mode_favori());
  322. }
  323. }
  324. }
  325. //******************************************
  326. function set_Mode_Reglement($id_reglement_mode)
  327. {
  328. foreach ($this->echeances as $echeance)
  329. {
  330. $echeance->set_Mode_Reglement($id_reglement_mode);
  331. $echeance->save();
  332. }
  333. }
  334. function maj_acompte($prepaiement_ratio, $prepaiement_type){
  335. if (!is_null($prepaiement_ratio) && !is_null($prepaiement_type) && $prepaiement_ratio>0 ){
  336. if (!$this->echeances_loaded) { $this->load_echeances(); }
  337. if ($this->acompte_exist){
  338. $this->echeances[$this->acompte_exist]->set_Pourcentage($prepaiement_ratio);
  339. $this->echeances[$this->acompte_exist]->set_Type_reglement($prepaiement_type);
  340. $this->echeances[$this->acompte_exist]->save();
  341. }else{
  342. $acompte = document_echeance::_create($this->ref_doc, null, array_search($prepaiement_type, document_echeance::$types_reglements ));
  343. $acompte->set_Pourcentage($prepaiement_ratio);
  344. $acompte->set_Jour(0,false);
  345. $acompte->save();
  346. $this->echeances[$acompte->get_Id_doc_echeance()] = $acompte;
  347. $this->acompte_exist = $acompte->get_Id_doc_echeance();
  348. }
  349. $this->sort_echeances();
  350. }
  351. }
  352. //******************************************
  353. function maj_solde_delai_reglement($delai_reglement){
  354. if (!is_null($delai_reglement)){
  355. if (!$this->echeances_loaded) { $this->load_echeances(); }
  356. if (strpos($delai_reglement,"FDM") === false){
  357. $nbjours = intval( $delai_reglement );
  358. $fdm = false;
  359. }else{
  360. $nbjours = intval( substr($delai_reglement, 0, strlen($delai_reglement)-3) );
  361. $fdm = true;
  362. }
  363. if ($this->solde_exist){
  364. $this->echeances[$this->solde_exist]->set_Jour($nbjours,$fdm);
  365. $this->echeances[$this->solde_exist]->save();
  366. }else{
  367. $solde = document_echeance::_create($this->ref_doc, null, 4);
  368. $solde->set_Jour($nbjours,$fdm);
  369. $solde->save();
  370. $this->echeances[$solde->get_Id_doc_echeance()] = $solde;
  371. $this->solde_exist = $solde->get_Id_doc_echeance();
  372. }
  373. }
  374. }
  375. //******************************************
  376. private function papa_est_la(){
  377. if(is_object($this->papa)){
  378. if(is_subclass_of($this->papa,"document")){
  379. return true;
  380. }
  381. }
  382. return false;
  383. }
  384. //******************************************
  385. public function exist(){
  386. if (!$this->echeances_loaded) { $this->load_echeances(); }
  387. if ($this->echeances_loaded){
  388. if(count($this->echeances) > 0){
  389. return true;
  390. }
  391. }
  392. return false;
  393. }
  394. //******************************************
  395. function get_Nb_echeances_restantes(){
  396. if(!$this->papa_est_la()){ return false; }
  397. $echeances = $this->get_echeances_etat();
  398. //$date_du_jour = strftime("%Y-%m-%d",time());
  399. $nb_echeances = 0;
  400. foreach ($echeances as $echeance){
  401. if( $echeance->etat == 0 || $echeance->etat == 3 ){
  402. $nb_echeances++;
  403. }
  404. }
  405. return $nb_echeances;
  406. }
  407. //******************************************
  408. private static function _cmpAscDate($m, $n) {
  409. if( $n->get_Type_reglement_int() == 1 || $n->get_Type_reglement_int() == 2){
  410. if( $m->get_Type_reglement_int() == $n->get_Type_reglement_int() ){
  411. return 0;
  412. }else{
  413. return 1;
  414. }
  415. }
  416. if( $n->get_Type_reglement_int() == 4 ){
  417. if( $m->get_Type_reglement_int() == $n->get_Type_reglement_int() ){
  418. return 0;
  419. }else{
  420. return -1;
  421. }
  422. }
  423. /*if( $n->get_Type_reglement_int() == 3 ){
  424. if( $m->get_Type_reglement_int() == $n->get_Type_reglement_int() ){
  425. if ($m->get_Date() == $n->get_Date()) {
  426. return 0;
  427. }
  428. return ($m->get_Date() < $n->get_Date()) ? -1 : 1;
  429. }else{
  430. return ($m->get_Type_reglement_int() < $n->get_Type_reglement_int()) ? -1 : 1;
  431. }
  432. }*/
  433. if( $n->get_Type_reglement_int() == 3 ){
  434. if( $m->get_Type_reglement_int() == $n->get_Type_reglement_int() ){
  435. if ($m->get_Jour() == $n->get_Jour()) {
  436. return 0;
  437. }
  438. return ($m->get_Jour() < $n->get_Jour()) ? -1 : 1;
  439. }else{
  440. return ($m->get_Type_reglement_int() < $n->get_Type_reglement_int()) ? -1 : 1;
  441. }
  442. }
  443. if ($m->get_Date() == $n->get_Date()) {
  444. return 0;
  445. }
  446. return ($m->get_Date() < $n->get_Date()) ? -1 : 1;
  447. }
  448. //******************************************
  449. public function add_pourcent_jours($id_mode_reglement, $type_reglement, $pourcentage, $nb_jours, $findemois = false){
  450. if(!isset($pourcentage) || !isset($nb_jours) || !isset($id_mode_reglement) || !isset($type_reglement)){
  451. return false;
  452. }
  453. if ($echeance = document_echeance::_create($this->ref_doc, $id_mode_reglement, $type_reglement)){
  454. $echeance->set_Pourcentage($pourcentage);
  455. $echeance->set_Jour($nb_jours, $findemois);
  456. if ($echeance->save()){
  457. $this->echeances[$echeance->get_Id_doc_echeance()] = $echeance;
  458. $this->sort_echeances();
  459. }
  460. }
  461. }
  462. public function add_montant_jours($id_mode_reglement, $type_reglement, $montant, $nb_jours, $findemois = false){
  463. if (!isset($id_mode_reglement) || !isset($type_reglement) || !isset($montant) || !isset($nb_jours)){
  464. return false;
  465. }
  466. if ($echeance = document_echeance::_create($this->ref_doc, $id_mode_reglement, $type_reglement)){
  467. $echeance->set_Montant($montant);
  468. $echeance->set_Jour($nb_jours, $findemois);
  469. if ($echeance->save()){
  470. $this->echeances[$echeance->get_Id_doc_echeance()] = $echeance;
  471. $this->sort_echeances();
  472. }
  473. }
  474. }
  475. //******************************************
  476. public function maj_pourcent_jours($id_mode_reglement, $type_reglement, $pourcentage, $nb_jours, $findemois = false){
  477. if(!isset($pourcentage) || !isset($nb_jours) || !isset($id_mode_reglement) || !isset($type_reglement)){
  478. return false;
  479. }
  480. global $bdd;
  481. $query = "INSERT INTO ".document_echeance::$nom_table."
  482. (ref_doc, id_mode_reglement, type_reglement)
  483. VALUES ('".$this->ref_doc."', ".num_or_null($id_mode_reglement).", '".$type_reglement."');";
  484. if(!$bdd->query($query)){
  485. return false;
  486. }
  487. $id = $bdd->lastInsertId();
  488. $echeance = new document_echeance($id);
  489. $echeance->set_Pourcentage($pourcentage);
  490. $echeance->set_Jour($nb_jours, $findemois);
  491. if ($echeance->save()){
  492. $this->echeances[$echeance->get_Id_doc_echeance()] = $echeance;
  493. $this->sort_echeances();
  494. }
  495. }
  496. public function maj_montant_jours($id_mode_reglement, $type_reglement, $montant, $nb_jours, $findemois = false){
  497. if(!isset($montant) || !isset($nb_jours) || !isset($id_mode_reglement) || !isset($type_reglement)){
  498. return false;
  499. }
  500. global $bdd;
  501. $query = "INSERT INTO ".document_echeance::$nom_table."
  502. (ref_doc, id_mode_reglement, type_reglement)
  503. VALUES ('".$this->ref_doc."', ".num_or_null($id_mode_reglement).", '".$type_reglement."');";
  504. if(!$bdd->query($query)){
  505. return false;
  506. }
  507. $id = $bdd->lastInsertId();
  508. $echeance = new document_echeance($id);
  509. $echeance->set_Montant($montant);
  510. $echeance->set_Jour($nb_jours, $findemois);
  511. if ($echeance->save()){
  512. $this->echeances[$echeance->get_Id_doc_echeance()] = $echeance;
  513. $this->sort_echeances();
  514. }
  515. }
  516. //******************************************
  517. public function suppr_echeancier(){
  518. global $bdd;
  519. if (isset($this->ref_doc)){
  520. $query = "DELETE FROM ".document_echeance::$nom_table."
  521. WHERE ref_doc = '".$this->ref_doc."';";
  522. $bdd->query($query);
  523. }
  524. $this->echeances = null;
  525. }
  526. //******************************************
  527. private function load_echeances(){
  528. global $bdd;
  529. if ($this->echeances_loaded) { $this->echeances = array(); $this->echeances_loaded= false; }
  530. $query = "SELECT id_doc_echeance FROM ".document_echeance::$nom_table."
  531. WHERE ref_doc = '".$this->ref_doc."' ORDER BY id_doc_echeance;";
  532. if($resultat = $bdd->query($query)){
  533. while($echeance = $resultat->fetchObject()){
  534. $this->echeances[$echeance->id_doc_echeance] = new document_echeance($echeance->id_doc_echeance);
  535. if( $this->echeances[$echeance->id_doc_echeance]->is_acompte() ){ $this->acompte_exist = $echeance->id_doc_echeance; }
  536. if( $this->echeances[$echeance->id_doc_echeance]->is_solde() ){ $this->solde_exist = $echeance->id_doc_echeance; }
  537. }
  538. $this->echeances_loaded = true;
  539. }
  540. }
  541. //******************************************
  542. public function sort_echeances(){
  543. if (!$this->echeances_loaded) { $this->load_echeances(); }
  544. if($this->echeances_loaded){
  545. $this->update_dates();
  546. usort($this->echeances, array('document_echeancier', '_cmpascDate'));
  547. $this->update_montants();
  548. }
  549. }
  550. //******************************************
  551. private function fin_du_mois($date_stamp){
  552. return strtotime('-1 second',strtotime('+1 month',strtotime(date('m',$date_stamp).'/01/'.date('Y',$date_stamp).' 00:00:00')));
  553. }
  554. //******************************************
  555. private function update_dates(){
  556. if(!$this->papa_est_la()){ return false; }
  557. if (!$this->echeances_loaded) { $this->load_echeances(); }
  558. if($this->echeances_loaded){
  559. foreach( $this->echeances as $echeance_id => $echeance ){
  560. $this->update_date_echeance($echeance_id);
  561. }
  562. }
  563. }
  564. //******************************************
  565. private function update_montants(){
  566. if (!$this->echeances_loaded) { $this->load_echeances(); }
  567. if($this->echeances_loaded){
  568. foreach( $this->echeances as $echeance_id => $echeance ){
  569. $this->update_montant_echeance($echeance_id);
  570. }
  571. }
  572. }
  573. //******************************************
  574. private function update_date_echeance($id_echeance){
  575. global $COMMANDE_CLIENT_ID_TYPE_DOC;
  576. global $LIVRAISON_CLIENT_ID_TYPE_DOC;
  577. if(!$this->papa_est_la()){ return false; }
  578. if (!$this->echeances_loaded) { $this->load_echeances(); }
  579. if (!isset($this->echeances[$id_echeance])){ return false; }
  580. $echeance = &$this->echeances[$id_echeance];
  581. //Conditions de recalcul des dates ( si acompte ou arrhes que pour les CDC / si solde que pour les FAC)
  582. $type_reglement = $echeance->get_Type_reglement_int();
  583. switch ( $type_reglement ){
  584. case ($type_reglement == 1 || $type_reglement == 2) :
  585. $nb_jours = $echeance->get_Nbjours();
  586. $new_date_stamp = 0;
  587. $date_commande = $this->get_Date_commande();
  588. if (!is_null($nb_jours) && ($date_commande) ){
  589. $new_date_stamp = strtotime("+ ".$nb_jours."days",strtotime($date_commande));
  590. if($echeance->is_fdm()){
  591. $new_date_stamp = $this->fin_du_mois($new_date_stamp);
  592. }
  593. }
  594. if($new_date_stamp != 0){
  595. $echeance->set_Date(strftime("%Y-%m-%d",$new_date_stamp));
  596. $echeance->save();
  597. }
  598. break;
  599. case ($type_reglement == 3 || $type_reglement == 4) :
  600. $nb_jours = $echeance->get_Nbjours();
  601. $new_date_stamp = 0;
  602. $date_facture = $this->get_Date_facture();
  603. if (!is_null($nb_jours) && ($date_facture) ){
  604. $new_date_stamp = strtotime("+ ".$nb_jours."days",strtotime($date_facture));
  605. if($echeance->is_fdm()){
  606. $new_date_stamp = $this->fin_du_mois($new_date_stamp);
  607. }
  608. }
  609. if($new_date_stamp != 0){
  610. $echeance->set_Date(strftime("%Y-%m-%d",$new_date_stamp));
  611. $echeance->save();
  612. }
  613. if($date_facture === false){
  614. $echeance->set_Date("");
  615. $echeance->save();
  616. }
  617. break;
  618. }
  619. }
  620. //******************************************
  621. private function update_montant_echeance($id_echeance){
  622. global $CALCUL_TARIFS_NB_DECIMALS;
  623. if (!$this->echeances_loaded) { $this->load_echeances(); }
  624. if (!isset($this->echeances[$id_echeance])){ return false; }
  625. $echeance = &$this->echeances[$id_echeance];
  626. $pourcentage = $echeance->get_Pourcentage();
  627. $new_montant = null;
  628. if ($echeance->get_Type_reglement_int() == 4){
  629. $new_montant = $this->get_Montant_ref() - $this->get_Montant_jusqua_moi($id_echeance);
  630. }
  631. if (!is_null($pourcentage)){
  632. $new_montant = $this->get_Montant_ref() * $pourcentage / 100;
  633. }
  634. if(!is_null($new_montant)){
  635. $echeance->set_Montant(round($new_montant,$CALCUL_TARIFS_NB_DECIMALS));
  636. $echeance->save();
  637. }
  638. }
  639. //******************************************
  640. public function get_echeances(){
  641. if (!$this->echeances_loaded) { $this->load_echeances(); }
  642. $this->sort_echeances();
  643. return $this->echeances;
  644. }
  645. //******************************************
  646. protected function get_Reglements_montant_todate($date){
  647. if ( !$this->papa_est_la() ){ return false; }
  648. $solde = 0;
  649. $reglements = $this->papa->getReglements();
  650. foreach($reglements as $reglement){
  651. if ($reglement->valide && substr($reglement->date_reglement,0,10) <= $date) {
  652. $solde += round($reglement->montant_on_doc,2);
  653. }
  654. }
  655. return $solde;
  656. }
  657. //******************************************
  658. public function get_echeances_etat(){
  659. if ( !$this->papa_est_la() ){ return false; }
  660. $base_echeances = $this->get_echeances();
  661. $reglements = $this->papa->getReglements();
  662. $solde = 0;
  663. $montant_echeances = 0;
  664. $result = array();
  665. foreach($reglements as $reglement){
  666. if ($reglement->valide) {
  667. $solde += $reglement->montant_on_doc;
  668. }
  669. }
  670. foreach ($base_echeances as $echeance){
  671. $res = new stdclass;
  672. $res->id_echeance = $echeance->get_Id_doc_echeance();
  673. $res->date = $echeance->get_Date();
  674. $res->jour = $echeance->get_Jour();
  675. $res->type_reglement = $echeance->get_Type_reglement();
  676. $res->mode_reglement = $echeance->get_Mode_reglement();
  677. $res->pourcentage = $echeance->get_Pourcentage();
  678. $res->montant = round($echeance->get_Montant(),2);
  679. if ($echeance->is_echue()){
  680. $montant_echeances += $res->montant;
  681. $solde -= $res->montant;
  682. if ( $solde<0 ){
  683. $res->etat = 3;
  684. }else{
  685. if ( $this->get_Reglements_montant_todate( $echeance->get_Date() ) >= $montant_echeances ){
  686. $res->etat = 1;
  687. }else{
  688. $res->etat = 2;
  689. }
  690. }
  691. }else{
  692. $res->etat = 0;
  693. }
  694. $result[] = $res;
  695. }
  696. return $result;
  697. }
  698. //******************************************
  699. public function get_Date_ref(){
  700. if ( !$this->papa_est_la() ){ return false; }
  701. return substr( $this->papa->getDate_creation(), 0, 10 );
  702. }
  703. //******************************************
  704. public function get_Date_livraison(){
  705. global $LIVRAISON_CLIENT_ID_TYPE_DOC;
  706. if ( !$this->papa_est_la() ){ return false; }
  707. $liaisons = $this->papa->getLiaisons();
  708. $ref_doc = self::get_ref_doc_from_type ($LIVRAISON_CLIENT_ID_TYPE_DOC, $this->papa->getID_TYPE_DOC(), $this->papa->getRef_doc());
  709. if ($ref_doc){
  710. $doc = open_doc ($ref_doc);
  711. return substr( $doc->getDate_creation(), 0, 10 );
  712. }
  713. return false;
  714. }
  715. //******************************************
  716. public function get_Date_facture(){
  717. global $FACTURE_CLIENT_ID_TYPE_DOC;
  718. if ( !$this->papa_est_la() ){ return false; }
  719. $liaisons = $this->papa->getLiaisons();
  720. $ref_doc = self::get_ref_doc_from_type ($FACTURE_CLIENT_ID_TYPE_DOC, $this->papa->getID_TYPE_DOC(), $this->papa->getRef_doc());
  721. if ($ref_doc){
  722. $doc = open_doc ($ref_doc);
  723. return substr( $doc->getDate_creation(), 0, 10 );
  724. }
  725. return false;
  726. }
  727. //******************************************
  728. public function get_Date_commande(){
  729. global $COMMANDE_CLIENT_ID_TYPE_DOC;
  730. if ( !$this->papa_est_la() ){ return false; }
  731. $liaisons = $this->papa->getLiaisons();
  732. $ref_doc = self::get_ref_doc_from_type ($COMMANDE_CLIENT_ID_TYPE_DOC, $this->papa->getID_TYPE_DOC(), $this->papa->getRef_doc());
  733. if ($ref_doc){
  734. $doc = open_doc ($ref_doc);
  735. return substr( $doc->getDate_creation(), 0, 10 );
  736. }
  737. return false;
  738. }
  739. //******************************************
  740. public static function get_ref_doc_from_type ($id_type_doc_search, $mon_id_type_doc, $ref_doc_depart, $ref_doc_init = ""){
  741. if ($ref_doc_init == $ref_doc_depart){
  742. return (false);
  743. }
  744. if ($ref_doc_init == ""){
  745. $ref_doc_init = $ref_doc_depart;
  746. }
  747. global $DEVIS_CLIENT_ID_TYPE_DOC, $LIVRAISON_CLIENT_ID_TYPE_DOC, $COMMANDE_CLIENT_ID_TYPE_DOC, $FACTURE_CLIENT_ID_TYPE_DOC;
  748. $id_type_doc_allowed = array ($DEVIS_CLIENT_ID_TYPE_DOC, $LIVRAISON_CLIENT_ID_TYPE_DOC, $COMMANDE_CLIENT_ID_TYPE_DOC, $FACTURE_CLIENT_ID_TYPE_DOC);
  749. if ($id_type_doc_search == $mon_id_type_doc) { return $ref_doc_depart; }
  750. $sens = ($mon_id_type_doc > $id_type_doc_search);
  751. $liaisons = document::charger_liaisons_doc($ref_doc_depart);
  752. foreach ($liaisons[$sens ? 'source' : 'dest'] as $doc){
  753. if (in_array($doc->id_type_doc, $id_type_doc_allowed)){
  754. $doc = get_object_vars($doc);
  755. if ($doc['id_type_doc'] == $id_type_doc_search ){
  756. return $doc[$sens ? 'ref_doc_source' : 'ref_doc_destination'] ;
  757. }else{
  758. if ($result = self::get_ref_doc_from_type ($id_type_doc_search, $doc['id_type_doc'], $doc[$sens ? 'ref_doc_source' : 'ref_doc_destination'], $ref_doc_init)){
  759. return $result;
  760. }else{
  761. return $ref_doc_depart;
  762. }
  763. }
  764. }
  765. }
  766. if ($sens) {
  767. return $ref_doc_depart;
  768. }
  769. return false;
  770. }
  771. //******************************************
  772. public function get_Montant_ref(){
  773. if ( !$this->papa_est_la() ){ return false; }
  774. return $this->papa->getMontant_ttc();
  775. }
  776. //******************************************
  777. public function get_Montant_jusqua_moi($id){
  778. if($this->echeances_loaded){
  779. $montant = 0;
  780. foreach($this->echeances as $echeance_id => $echeance){
  781. if( $echeance_id == $id ) { break; }
  782. $montant += $echeance->get_Montant();
  783. }
  784. return $montant;
  785. }else{
  786. return false;
  787. }
  788. }
  789. //******************************************
  790. public function get_Last_echeance($ref_doc = "")
  791. {
  792. global $bdd;
  793. if ($ref_doc == "")
  794. return false;
  795. $query = "SELECT date FROM `doc_echeanciers`
  796. WHERE type_reglement LIKE 'Solde'
  797. AND ref_doc = '".$ref_doc."';";
  798. $resultat = $bdd->query($query);
  799. if (($result = $resultat->fetchObject()) != false)
  800. return $result->date;
  801. else
  802. return false;
  803. }
  804. //******************************************
  805. public function copie_to_doc($ref_doc){
  806. global $bdd;
  807. if (isset($this->ref_doc) && $ref_doc!=""){
  808. $query = "DELETE FROM ".document_echeance::$nom_table."
  809. WHERE ref_doc = '".$ref_doc."';";
  810. $bdd->query($query);
  811. $query = "INSERT INTO ".document_echeance::$nom_table."
  812. (ref_doc,montant,pourcentage,id_mode_reglement,date,jour,type_reglement)
  813. SELECT '".$ref_doc."',montant,pourcentage,id_mode_reglement,date,jour,type_reglement
  814. FROM ".document_echeance::$nom_table."
  815. WHERE ref_doc = '".$this->ref_doc."' ORDER BY id_doc_echeance;";
  816. if($bdd->query($query)){
  817. return true;
  818. }
  819. }
  820. return false;
  821. }
  822. }
  823. ?>