PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/src/2.071.0/_document.class.php

https://github.com/crepeausucre/soothERP
PHP | 4436 lines | 2964 code | 825 blank | 647 comment | 609 complexity | 68d1099ca24657ebef2ab4932a0c84cb MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // *************************************************************************************************************
  3. // CLASSE REGISSANT LES INFORMATIONS SUR UN DOCUMENT
  4. // *************************************************************************************************************
  5. abstract class document {
  6. protected $ref_doc;
  7. protected $lib_type_doc;
  8. protected $lib_type_printed;
  9. protected $date_creation;
  10. protected $id_etat_doc;
  11. protected $lib_etat_doc;
  12. protected $is_open;
  13. protected $code_affaire;
  14. protected $ref_contact;
  15. protected $contact;
  16. protected $nom_contact;
  17. protected $ref_adr_contact;
  18. protected $adresse_contact;
  19. protected $code_postal_contact;
  20. protected $ville_contact;
  21. protected $id_pays_contact;
  22. protected $pays_contact;
  23. protected $app_tarifs;
  24. protected $description;
  25. protected $contenu;
  26. protected $contenu_loaded;
  27. protected $contenu_materiel;
  28. protected $contenu_materiel_loaded;
  29. protected $contenu_service_abo;
  30. protected $contenu_service_abo_loaded;
  31. protected $contenu_service_conso;
  32. protected $contenu_service_conso_loaded;
  33. protected $liaisons;
  34. protected $liaisons_loaded;
  35. protected $liaisons_possibles;
  36. protected $liaisons_possibles_loaded;
  37. protected $code_file; //code md5 du nom du fichier pdf généré lors de l'envois du document
  38. protected $events;
  39. protected $events_loaded;
  40. protected $montant_ht;
  41. protected $montant_tva;
  42. protected $montant_ttc = -1; // Montant TTC du doc, = -1 quand non chargé
  43. protected $tvas;
  44. protected $ACCEPT_REGMT = 0;
  45. protected $reglements; // Règlements rapprochés à ce document
  46. protected $reglements_loaded;
  47. protected $montant_reglements; // Montant total des règlements (Inversé lorsqu'il s'agit d'emettre les règlements.)
  48. protected $montant_to_pay; // Montant restant à payer.
  49. protected $echeancier;
  50. protected $commerciaux; // commerciaux attribués à ce document
  51. protected $commerciaux_loaded;
  52. protected $code_pdf_modele; // Code du modèle utilisé pour l'impression
  53. protected $quantite_locked; // Les quantités pour ce document sont FIGEES
  54. public function __construct($ref_doc = "") {
  55. $this->ref_doc = $ref_doc;
  56. }
  57. // *************************************************************************************************************
  58. // FONCTIONS LIEES A L'OUVERTURE D'UN DOCUMENT
  59. // *************************************************************************************************************
  60. public function open_doc ($select = "", $left_join = "") {
  61. global $bdd;
  62. // Controle si la ref_document est précisée
  63. if (!$this->ref_doc) { return false; }
  64. $query = "SELECT d.ref_contact, d.nom_contact, d.ref_adr_contact, d.adresse_contact, d.code_postal_contact, d.ville_contact, d.id_pays_contact, d.app_tarifs, d.description,
  65. d.id_etat_doc, d.code_affaire, de.lib_etat_doc, de.is_open,
  66. dt.lib_type_doc, dt.lib_type_printed, pm.code_pdf_modele,
  67. d.date_creation_doc date_creation, d.code_file,
  68. p.pays pays_contact
  69. ".$select."
  70. FROM documents d
  71. LEFT JOIN documents_types dt ON d.id_type_doc = dt.id_type_doc
  72. LEFT JOIN doc_modeles_pdf dmp ON d.id_type_doc = dmp.id_type_doc && dmp.usage = 'defaut'
  73. LEFT JOIN pdf_modeles pm ON pm.id_pdf_modele = dmp.id_pdf_modele
  74. LEFT JOIN documents_etats de ON d.id_etat_doc = de.id_etat_doc
  75. LEFT JOIN pays p ON p.id_pays = d.id_pays_contact
  76. ".$left_join."
  77. WHERE d.ref_doc = '".$this->ref_doc."' ";
  78. $resultat = $bdd->query ($query);
  79. if (!$doc = $resultat->fetchObject()) { return false; }
  80. $this->ref_contact = $doc->ref_contact;
  81. $this->nom_contact = $doc->nom_contact;
  82. $this->ref_adr_contact = $doc->ref_adr_contact;
  83. $this->adresse_contact = $doc->adresse_contact;
  84. $this->code_postal_contact = $doc->code_postal_contact;
  85. $this->ville_contact = $doc->ville_contact;
  86. $this->id_pays_contact = $doc->id_pays_contact;
  87. $this->pays_contact = $doc->pays_contact;
  88. $this->app_tarifs = $doc->app_tarifs;
  89. $this->description = $doc->description;
  90. $this->id_etat_doc = $doc->id_etat_doc;
  91. $this->lib_etat_doc = $doc->lib_etat_doc;
  92. $this->is_open = $doc->is_open;
  93. $this->code_affaire = $doc->code_affaire;
  94. $this->lib_type_doc = $doc->lib_type_doc;
  95. $this->lib_type_printed = $doc->lib_type_printed;
  96. $this->code_pdf_modele = $doc->code_pdf_modele;
  97. $this->date_creation = $doc->date_creation;
  98. $this->code_file = $doc->code_file;
  99. $this->echeancier = new document_echeancier($this->getRef_doc(),$this);
  100. $this->quantite_locked = false;
  101. if(!$this->echeancier->exist()){
  102. $this->echeancier->create_from_ref_contact();
  103. }
  104. return $doc;
  105. }
  106. // *************************************************************************************************************
  107. // FONCTIONS LIEES A LA CREATION D'UN DOCUMENT
  108. // *************************************************************************************************************
  109. public function create_doc () {
  110. global $bdd;
  111. $this->pays_contact = "";
  112. // *************************************************
  113. // Type de document et etat
  114. $this->lib_type_doc = $this->LIB_TYPE_DOC;
  115. $this->id_etat_doc = $this->DEFAUT_ID_ETAT;
  116. $this->lib_etat_doc = $this->DEFAUT_LIB_ETAT;
  117. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['id_etat_doc'])) {
  118. $this->id_etat_doc = $GLOBALS['_OPTIONS']['CREATE_DOC']['id_etat_doc'];
  119. }
  120. // *************************************************
  121. // Valeur par défaut des informations du document
  122. $this->check_profils ();
  123. $this->ref_contact = "";
  124. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['ref_contact'])) {
  125. $this->ref_contact = $GLOBALS['_OPTIONS']['CREATE_DOC']['ref_contact'];
  126. }
  127. if ($this->ref_contact) {
  128. // Chargement des informations de ce contact
  129. $this->load_infos_contact ();
  130. }
  131. else {
  132. $this->load_defauts_infos_contact ();
  133. }
  134. if (!$this->app_tarifs) { $this->app_tarifs = "HT"; }
  135. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['code_affaire'])) {
  136. $this->code_affaire = $GLOBALS['_OPTIONS']['CREATE_DOC']['code_affaire'];
  137. }
  138. $this->description = "";
  139. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['description'])) {
  140. $this->description = $GLOBALS['_OPTIONS']['CREATE_DOC']['description'];
  141. }
  142. // *************************************************
  143. // Verification qu'il n'y a pas eu d'erreur
  144. if (count($GLOBALS['_ALERTES'])) {
  145. return false;
  146. }
  147. //**************************************************
  148. //création du code file
  149. $this->code_file = md5(uniqid(rand(), true));
  150. // *************************************************
  151. // Création de la référence
  152. $reference = new reference ($this->DOC_ID_REFERENCE_TAG);
  153. $this->ref_doc = $reference->generer_ref();
  154. $this->echeancier = new document_echeancier($this->getRef_doc(),$this);
  155. // *************************************************
  156. // Insertion dans la base
  157. $bdd->beginTransaction();
  158. $query = "INSERT INTO documents (ref_doc, id_type_doc, id_etat_doc, code_affaire,
  159. ref_contact, nom_contact, ref_adr_contact, adresse_contact, code_postal_contact, ville_contact, id_pays_contact, app_tarifs, description, date_creation_doc, code_file)
  160. VALUES ('".$this->ref_doc."', '".$this->ID_TYPE_DOC."', '".$this->id_etat_doc."', '".$this->code_affaire."',
  161. ".ref_or_null($this->ref_contact).", '".addslashes($this->nom_contact)."',
  162. ".ref_or_null($this->ref_adr_contact).", '".addslashes($this->adresse_contact)."',
  163. '".$this->code_postal_contact."', '".addslashes($this->ville_contact)."',
  164. ".num_or_null($this->id_pays_contact).",
  165. '".$this->app_tarifs."', '".addslashes($this->description)."', NOW(), '".$this->code_file."' ) ";
  166. $bdd->exec ($query);
  167. //conditions de reglement
  168. if (($this->echeancier instanceof document_echeancier)) {
  169. if(!$this->echeancier->exist()){
  170. $this->echeancier->create_from_ref_contact();
  171. }
  172. }
  173. // *************************************************
  174. // Ajout de l'évennement de création
  175. $this->add_event(1);
  176. $bdd->commit();
  177. $GLOBALS['_INFOS']['ref_doc'] = $this->ref_doc;
  178. // *************************************************
  179. // Informations complémentaires
  180. $this->date_creation = date ("Y-m-d H:i:s");
  181. return true;
  182. }
  183. // Charge les informations supplémentaires du contact
  184. protected function load_infos_contact () {
  185. //conditions de reglement
  186. if (($this->echeancier instanceof document_echeancier)) {
  187. if(!$this->echeancier->exist()){
  188. $this->echeancier->create_from_ref_contact();
  189. }
  190. }
  191. // Nom
  192. $this->define_nom_contact ();
  193. // Adresse
  194. $this->define_adresse_contact ();
  195. // Préférences d'affichage du tarif
  196. $this->define_aff_tarif ();
  197. }
  198. protected function load_defauts_infos_contact () {
  199. $this->define_adresse_contact ();
  200. }
  201. protected function load_infos_contact_client () {
  202. global $CLIENT_ID_PROFIL;
  203. if (!is_object($this->contact)) { $this->contact = new contact ($this->ref_contact); }
  204. if (!$this->contact->charger_profiled_infos($CLIENT_ID_PROFIL)) {
  205. // Creation des informations de profil CLIENT
  206. $infos_profil['id_profil'] = $CLIENT_ID_PROFIL;
  207. $this->contact->create_profiled_infos ($infos_profil);
  208. }
  209. }
  210. protected function load_infos_contact_fournisseur () {
  211. global $FOURNISSEUR_ID_PROFIL;
  212. if (!is_object($this->contact)) { $this->contact = new contact ($this->ref_contact); }
  213. if (!$this->contact->charger_profiled_infos($FOURNISSEUR_ID_PROFIL)) {
  214. // Creation des informations de profil FOURNISSEUR
  215. $infos_profil['id_profil'] = $FOURNISSEUR_ID_PROFIL;
  216. $this->contact->create_profiled_infos ($infos_profil);
  217. }
  218. }
  219. // Défini le nom du contact
  220. protected function define_nom_contact () {
  221. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['nom_contact'])) {
  222. $this->nom_contact = $GLOBALS['_OPTIONS']['CREATE_DOC']['nom_contact'];
  223. }
  224. else {
  225. $this->contact = new contact ($this->ref_contact);
  226. $this->nom_contact = $this->contact->getLib_civ_court()." ".$this->contact->getNom();
  227. }
  228. }
  229. // Renvoie l'adresse a utiliser dans le document pour un contact donné
  230. protected function define_adresse_contact () {
  231. global $bdd;
  232. global $DEFAUT_ID_PAYS;
  233. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['ref_adr_contact'])) {
  234. $this->ref_adr_contact = $GLOBALS['_OPTIONS']['CREATE_DOC']['ref_adr_contact'];
  235. }
  236. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['adresse_contact'])) {
  237. $this->adresse_contact = $GLOBALS['_OPTIONS']['CREATE_DOC']['adresse_contact'];
  238. }
  239. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['code_postal_contact'])) {
  240. $this->code_postal_contact = $GLOBALS['_OPTIONS']['CREATE_DOC']['code_postal_contact'];
  241. }
  242. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['ville_contact'])) {
  243. $this->ville_contact = $GLOBALS['_OPTIONS']['CREATE_DOC']['ville_contact'];
  244. }
  245. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['id_pays_contact'])) {
  246. $this->id_pays_contact = $GLOBALS['_OPTIONS']['CREATE_DOC']['id_pays_contact'];
  247. }
  248. if (!$this->ref_adr_contact && !$this->adresse_contact && !$this->code_postal_contact && !$this->ville_contact && !$this->id_pays_contact) {
  249. // Sélection de la première Adresse
  250. $query = "SELECT ref_adresse, text_adresse, code_postal, ville, a.id_pays, p.pays
  251. FROM adresses a
  252. LEFT JOIN pays p ON a.id_pays = p.id_pays
  253. WHERE ref_contact = '".$this->ref_contact."' && ordre = 1 ";
  254. $resultat = $bdd->query ($query);
  255. if (!$a = $resultat->fetchObject()) { return false; }
  256. $this->ref_adr_contact = $a->ref_adresse;
  257. $this->adresse_contact = $a->text_adresse ;
  258. $this->code_postal_contact = $a->code_postal ;
  259. $this->ville_contact = $a->ville ;
  260. $this->id_pays_contact = $a->id_pays ;
  261. $this->pays_contact = $a->pays ;
  262. }
  263. if (!$this->id_pays_contact) {$this->id_pays_contact = $DEFAUT_ID_PAYS;}
  264. return true;
  265. }
  266. // Renvoie l'adresse a utiliser dans le document pour un contact donné
  267. function define_adresse_contact_et_livraison () {
  268. global $bdd;
  269. global $DEFAUT_ID_PAYS;
  270. $adresse_contact_ok = $adresse_livraison_ok = 0;
  271. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['ref_adr_contact'])) {
  272. $this->ref_adr_contact = $GLOBALS['_OPTIONS']['CREATE_DOC']['ref_adr_contact'];
  273. $adresse_contact_ok = 1;
  274. }
  275. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['adresse_contact'])) {
  276. $this->adresse_contact = $GLOBALS['_OPTIONS']['CREATE_DOC']['adresse_contact'];
  277. $adresse_contact_ok = 1;
  278. }
  279. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['code_postal_contact'])) {
  280. $this->code_postal_contact = $GLOBALS['_OPTIONS']['CREATE_DOC']['code_postal_contact'];
  281. $adresse_contact_ok = 1;
  282. }
  283. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['ville_contact'])) {
  284. $this->ville_contact = $GLOBALS['_OPTIONS']['CREATE_DOC']['ville_contact'];
  285. $adresse_contact_ok = 1;
  286. }
  287. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['id_pays_contact'])) {
  288. $this->id_pays_contact = $GLOBALS['_OPTIONS']['CREATE_DOC']['id_pays_contact'];
  289. $adresse_contact_ok = 1;
  290. }
  291. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['ref_adr_livraison'])) {
  292. $this->ref_adr_livraison = $GLOBALS['_OPTIONS']['CREATE_DOC']['ref_adr_livraison'];
  293. $adresse_livraison_ok = 1;
  294. }
  295. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['adresse_livraison'])) {
  296. $this->adresse_livraison = $GLOBALS['_OPTIONS']['CREATE_DOC']['adresse_livraison'];
  297. $adresse_livraison_ok = 1;
  298. }
  299. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['code_postal_livraison'])) {
  300. $this->code_postal_livraison = $GLOBALS['_OPTIONS']['CREATE_DOC']['code_postal_livraison'];
  301. $adresse_livraison_ok = 1;
  302. }
  303. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['ville_livraison'])) {
  304. $this->ville_livraison = $GLOBALS['_OPTIONS']['CREATE_DOC']['ville_livraison'];
  305. $adresse_livraison_ok = 1;
  306. }
  307. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['id_pays_livraison'])) {
  308. $this->id_pays_livraison = $GLOBALS['_OPTIONS']['CREATE_DOC']['id_pays_livraison'];
  309. $adresse_livraison_ok = 1;
  310. }
  311. if (!$adresse_livraison_ok && ($_SESSION['magasin']->getMode_vente() == "VAC")) {
  312. $this->ref_adr_livraison = "NULL";
  313. $this->adresse_livraison = $_SESSION['magasin']->getLib_magasin ();
  314. }
  315. if ( !$adresse_contact_ok || !$adresse_livraison_ok) {
  316. // Sélection des adresses prédéfinies
  317. $query = "SELECT ref_adr_livraison, a1.text_adresse ta1, a1.code_postal cp1, a1.ville v1, a1.id_pays ip1, p1.pays p1,
  318. ref_adr_facturation, a2.text_adresse ta2, a2.code_postal cp2, a2.ville v2, a2.id_pays ip2, p2.pays p2
  319. FROM annu_client ac
  320. LEFT JOIN adresses a1 ON ac.ref_adr_livraison = a1.ref_adresse
  321. LEFT JOIN pays p1 ON a1.id_pays = p1.id_pays
  322. LEFT JOIN adresses a2 ON ac.ref_adr_facturation = a2.ref_adresse
  323. LEFT JOIN pays p2 ON a2.id_pays = p2.id_pays
  324. WHERE ac.ref_contact = '".$this->ref_contact."' ";
  325. $resultat = $bdd->query ($query);
  326. if (!$a = $resultat->fetchObject()) { return false; }
  327. if (!$adresse_contact_ok) {
  328. $this->ref_adr_contact = $a->ref_adr_facturation;
  329. $this->adresse_contact = $a->ta2;
  330. $this->code_postal_contact = $a->cp2 ;
  331. $this->ville_contact = $a->v2 ;
  332. $this->id_pays_contact = $a->ip2 ;
  333. $this->pays_contact = $a->p2 ;
  334. }
  335. if (!$adresse_livraison_ok || ($_SESSION['magasin']->getMode_vente() != "VAC")) {
  336. $this->ref_adr_livraison = $a->ref_adr_livraison;
  337. $this->adresse_livraison = $a->ta1;
  338. $this->code_postal_livraison = $a->cp1 ;
  339. $this->ville_livraison = $a->v1 ;
  340. $this->id_pays_livraison = $a->ip1 ;
  341. $this->pays_livraison = $a->p1 ;
  342. }
  343. }
  344. if (!$this->id_pays_contact) {$this->id_pays_contact = $DEFAUT_ID_PAYS;}
  345. if (!$this->id_pays_livraison) {$this->id_pays_livraison = $DEFAUT_ID_PAYS;}
  346. return true;
  347. }
  348. // Renvoie le texte précis de l'adresse qui sera affiché
  349. protected function define_text_adresse ($text_adresse, $code_postal, $ville, $id_pays, $pays) {
  350. global $DEFAUT_ID_PAYS;
  351. $adresse_contact = $text_adresse;
  352. if ($code_postal || $ville) {
  353. $adresse_contact .= "\n".$code_postal." ".$ville;
  354. }
  355. if ($id_pays && $id_pays != $DEFAUT_ID_PAYS ) {
  356. $adresse_contact .= "\n".$pays;
  357. }
  358. return $adresse_contact;
  359. }
  360. // Renvoie le type d'affichage des tarifs a utiliser (HT ou TTC) pour le document
  361. protected function define_aff_tarif () {
  362. $this->define_client_aff_tarif ();
  363. }
  364. function define_client_aff_tarif () {
  365. global $DEFAUT_APP_TARIFS_CLIENT;
  366. global $CLIENT_ID_PROFIL;
  367. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['app_tarifs'])) {
  368. $this->app_tarifs = $GLOBALS['_OPTIONS']['CREATE_DOC']['app_tarifs'];
  369. }
  370. else {
  371. $tmp = $this->contact->getProfil($CLIENT_ID_PROFIL);
  372. $this->app_tarifs = $tmp->getApp_tarifs();
  373. }
  374. }
  375. function define_fournisseur_aff_tarif () {
  376. global $DEFAUT_APP_TARIFS_FOURNISSEUR;
  377. global $FOURNISSEUR_ID_PROFIL;
  378. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['app_tarifs'])) {
  379. $this->app_tarifs = $GLOBALS['_OPTIONS']['CREATE_DOC']['app_tarifs'];
  380. }
  381. else {
  382. $tmp = $this->contact->getProfil($FOURNISSEUR_ID_PROFIL);
  383. $this->app_tarifs = $tmp->getApp_tarifs();
  384. }
  385. }
  386. // *************************************************************************************************************
  387. // FONCTIONS LIEES A LA MODIFICATION D'UN DOCUMENT
  388. // *************************************************************************************************************
  389. // Changement du contact d'un document
  390. public function maj_contact ($ref_contact) {
  391. global $bdd;
  392. global $DEFAUT_APP_TARIFS_CLIENT;
  393. global $DEFAUT_ID_PAYS;
  394. if ($ref_contact == $this->ref_contact) { return false; }
  395. $old_ref_contact = $this->ref_contact;
  396. $old_nom_contact = $this->nom_contact;
  397. if (!$ref_contact) {$this->ref_contact = "";}
  398. if ($ref_contact) {
  399. $this->ref_contact = $ref_contact;
  400. // Chargement des informations de ce contact
  401. $this->contact = new contact ($ref_contact);
  402. $this->load_infos_contact ();
  403. }
  404. if (!is_object($this->contact) || !$this->contact->getRef_contact()) {
  405. $this->ref_contact = "";
  406. $this->nom_contact = "";
  407. $this->ref_adr_contact = "";
  408. $this->adresse_contact = "";
  409. $this->code_postal_contact = "";
  410. $this->ville_contact = "";
  411. $this->id_pays_contact = $DEFAUT_ID_PAYS;
  412. $this->app_tarifs = $DEFAUT_APP_TARIFS_CLIENT;
  413. }
  414. // *************************************************
  415. // MAJ de la base
  416. $bdd->beginTransaction();
  417. $query = "UPDATE documents
  418. SET ref_contact = ".ref_or_null($this->ref_contact).", nom_contact = '".addslashes($this->nom_contact)."',
  419. ref_adr_contact = ".ref_or_null($this->ref_adr_contact).",
  420. adresse_contact = '".addslashes($this->adresse_contact)."',
  421. code_postal_contact = '".$this->code_postal_contact."',
  422. ville_contact = '".addslashes($this->ville_contact)."',
  423. id_pays_contact = ".num_or_null($this->id_pays_contact).",
  424. app_tarifs = '".$this->app_tarifs."'
  425. WHERE ref_doc = '".$this->ref_doc."' ";
  426. $bdd->exec ($query);
  427. // MAJ du contact pour les règlements de ce document qui ne règlent pas d'autres documents du même type
  428. $query = "SELECT rd.ref_reglement, COUNT(rd.ref_doc) as nb_docs
  429. FROM reglements_docs rd
  430. LEFT JOIN documents d ON d.ref_doc = rd.ref_doc
  431. WHERE rd.ref_reglement IN (
  432. SELECT ref_reglement
  433. FROM reglements_docs rd
  434. WHERE rd.ref_doc = '".$this->ref_doc."'
  435. )
  436. && rd.liaison_valide = '1'
  437. GROUP BY rd.ref_reglement";
  438. $resultat = $bdd->query ($query);
  439. $rgmts_to_update = $rgmts_to_deli = array();
  440. while ($rgmt = $resultat->fetchObject()) {
  441. //echo $rgmt->ref_reglement;
  442. if ($rgmt->nb_docs > 1) { $rgmts_to_deli[] = $rgmt->ref_reglement; }
  443. else { $rgmts_to_update[] = $rgmt->ref_reglement; }
  444. }
  445. if ($rgmts_to_update) {
  446. $list_to_update = "''";
  447. foreach ($rgmts_to_update as $ref_rgmt) { $list_to_update .= ",'".$ref_rgmt."'"; }
  448. $query = "UPDATE reglements SET ref_contact = ".ref_or_null($this->ref_contact)."
  449. WHERE ref_reglement IN (".$list_to_update.")";
  450. $bdd->exec ($query);
  451. }
  452. if ($rgmts_to_deli) {
  453. $list_to_deli = "''";
  454. foreach ($rgmts_to_deli as $ref_rgmt) { $list_to_deli .= ",'".$ref_rgmt."'"; }
  455. $query = "DELETE FROM reglements_docs
  456. WHERE ref_reglement IN (".$list_to_deli.") && ref_doc = '".$this->ref_doc."' ";
  457. $resultat = $bdd->query ($query);
  458. if ($resultat->rowCount()) { $this->check_etat_reglement (); }
  459. }
  460. // Evenement
  461. if ($old_ref_contact) {
  462. $this->add_event(6, "Ancien contact : ".$old_nom_contact." (".$old_ref_contact.")\n Nouveau contact : ".$this->nom_contact." (".$this->ref_contact.")");
  463. }
  464. $bdd->commit();
  465. return true;
  466. }
  467. public function maj_nom_contact ($nom_contact) {
  468. global $bdd;
  469. $this->nom_contact = $nom_contact;
  470. // *************************************************
  471. // MAJ de la base
  472. $query = "UPDATE documents
  473. SET nom_contact = '".addslashes($this->nom_contact)."'
  474. WHERE ref_doc = '".$this->ref_doc."' ";
  475. $bdd->exec ($query);
  476. }
  477. public function maj_adresse_contact ($ref_adresse) {
  478. global $bdd;
  479. global $DEFAUT_ID_PAYS;
  480. if (is_numeric($ref_adresse)) {
  481. $query = "SELECT lib_stock FROM stocks
  482. WHERE id_stock = '".addslashes($ref_adresse)."' ";
  483. $resultat = $bdd->query ($query);
  484. if (!$s = $resultat->fetchObject()) { return false; }
  485. $this->ref_adr_contact = "NULL";
  486. $this->adresse_contact = $s->lib_stock;
  487. $this->code_postal_contact = "";
  488. $this->ville_contact = "";
  489. $this->id_pays_contact = $DEFAUT_ID_PAYS;
  490. $this->pays_contact = "";
  491. if (isset($_SESSION['stocks'][$ref_adresse])) {
  492. $adresse = $_SESSION['stocks'][$ref_adresse]->getAdresse ();
  493. $this->id_pays_contact = $adresse->getId_pays();
  494. }
  495. }
  496. else {
  497. // Sélection de l'Adresse
  498. $query = "SELECT ref_adresse, text_adresse, code_postal, ville, a.id_pays, p.pays
  499. FROM adresses a
  500. LEFT JOIN pays p ON a.id_pays = p.id_pays
  501. WHERE ref_adresse = '".$ref_adresse."' ";
  502. $resultat = $bdd->query ($query);
  503. if (!$a = $resultat->fetchObject()) { return false; }
  504. $this->ref_adr_contact = $a->ref_adresse;
  505. $this->adresse_contact = $a->text_adresse;
  506. $this->code_postal_contact = $a->code_postal;
  507. $this->ville_contact = $a->ville;
  508. $this->id_pays_contact = $a->id_pays;
  509. $this->pays_contact = $a->pays;
  510. }
  511. // *************************************************
  512. // MAJ de la base
  513. $query = "UPDATE documents
  514. SET ref_adr_contact = ".ref_or_null($this->ref_adr_contact).",
  515. adresse_contact = '".addslashes($this->adresse_contact)."',
  516. code_postal_contact = '".($this->code_postal_contact)."',
  517. ville_contact = '".addslashes($this->ville_contact)."',
  518. id_pays_contact = ".num_or_null($this->id_pays_contact)."
  519. WHERE ref_doc = '".$this->ref_doc."' ";
  520. $bdd->exec ($query);
  521. return true;
  522. }
  523. public function maj_text_adresse_contact ($text_adresse) {
  524. global $bdd;
  525. $this->adresse_contact = $text_adresse;
  526. // *************************************************
  527. // MAJ de la base
  528. $query = "UPDATE documents
  529. SET adresse_contact = '".addslashes($this->adresse_contact)."'
  530. WHERE ref_doc = '".$this->ref_doc."' ";
  531. $bdd->exec ($query);
  532. }
  533. public function maj_text_code_postal_contact ($text_code_postal) {
  534. global $bdd;
  535. $this->code_postal_contact = $text_code_postal;
  536. // *************************************************
  537. // MAJ de la base
  538. $query = "UPDATE documents
  539. SET code_postal_contact = '".$this->code_postal_contact."'
  540. WHERE ref_doc = '".$this->ref_doc."' ";
  541. $bdd->exec ($query);
  542. }
  543. public function maj_text_ville_contact ($text_ville) {
  544. global $bdd;
  545. $this->ville_contact = $text_ville;
  546. // *************************************************
  547. // MAJ de la base
  548. $query = "UPDATE documents
  549. SET ville_contact = '".addslashes($this->ville_contact)."'
  550. WHERE ref_doc = '".$this->ref_doc."' ";
  551. $bdd->exec ($query);
  552. }
  553. public function maj_text_id_pays_contact ($text_id_pays) {
  554. global $bdd;
  555. $this->id_pays_contact = $text_id_pays;
  556. // *************************************************
  557. // MAJ de la base
  558. $query = "UPDATE documents
  559. SET id_pays_contact = ".num_or_null($this->id_pays_contact)."
  560. WHERE ref_doc = '".$this->ref_doc."' ";
  561. $bdd->exec ($query);
  562. }
  563. public function maj_adresse_livraison ($ref_adresse) {
  564. global $bdd;
  565. global $DEFAUT_ID_PAYS;
  566. if (is_numeric($ref_adresse)) {
  567. $query = "SELECT lib_stock FROM stocks
  568. WHERE id_stock = '".addslashes($ref_adresse)."' ";$resultat = $bdd->query ($query);
  569. if (!$s = $resultat->fetchObject()) { return false; }
  570. $this->ref_adr_livraison = "NULL";
  571. $this->adresse_livraison = $s->lib_stock;
  572. $this->code_postal_livraison = "" ;
  573. $this->ville_livraison = "" ;
  574. $this->id_pays_livraison = $DEFAUT_ID_PAYS ;
  575. $this->pays_livraison = "" ;
  576. if (isset($_SESSION['stocks'][$ref_adresse])) {
  577. $adresse = $_SESSION['stocks'][$ref_adresse]->getAdresse ();
  578. $this->id_pays_livraison = $adresse->getId_pays();
  579. }
  580. }
  581. else {
  582. // Sélection de l'Adresse
  583. $query = "SELECT ref_adresse, text_adresse, code_postal, ville, a.id_pays, p.pays
  584. FROM adresses a
  585. LEFT JOIN pays p ON a.id_pays = p.id_pays
  586. WHERE ref_adresse = '".$ref_adresse."' ";
  587. $resultat = $bdd->query ($query);
  588. if (!$a = $resultat->fetchObject()) { return false; }
  589. $this->ref_adr_livraison = $a->ref_adresse;
  590. $this->adresse_livraison = $a->text_adresse ;
  591. $this->code_postal_livraison = $a->code_postal ;
  592. $this->ville_livraison = $a->ville ;
  593. $this->id_pays_livraison = $a->id_pays ;
  594. $this->pays_livraison = $a->pays ;
  595. }
  596. // *************************************************
  597. // MAJ de la base
  598. $query = "UPDATE doc_".strtolower($this->CODE_DOC)."
  599. SET ref_adr_livraison = ".ref_or_null($this->ref_adr_livraison).",
  600. adresse_livraison = '".addslashes($this->adresse_livraison)."',
  601. code_postal_livraison = '".$this->code_postal_livraison."',
  602. ville_livraison = '".addslashes($this->ville_livraison)."',
  603. id_pays_livraison = ".num_or_null($this->id_pays_livraison)."
  604. WHERE ref_doc = '".$this->ref_doc."' ";
  605. $bdd->exec ($query);
  606. return true;
  607. }
  608. public function maj_text_adresse_livraison ($text_adresse) {
  609. global $bdd;
  610. $this->adresse_livraison = $text_adresse;
  611. // *************************************************
  612. // MAJ de la base
  613. $query = "UPDATE doc_".strtolower($this->CODE_DOC)."
  614. SET adresse_livraison = '".addslashes($this->adresse_livraison)."'
  615. WHERE ref_doc = '".$this->ref_doc."' ";
  616. $bdd->exec ($query);
  617. return true;
  618. }
  619. public function maj_text_code_postal_livraison ($text_code_postal) {
  620. global $bdd;
  621. $this->code_postal_livraison = $text_code_postal;
  622. // *************************************************
  623. // MAJ de la base
  624. $query = "UPDATE doc_".strtolower($this->CODE_DOC)."
  625. SET code_postal_livraison = '".$this->code_postal_livraison."'
  626. WHERE ref_doc = '".$this->ref_doc."' ";
  627. $bdd->exec ($query);
  628. return true;
  629. }
  630. public function maj_text_ville_livraison ($text_ville) {
  631. global $bdd;
  632. $this->ville_livraison = $text_ville;
  633. // *************************************************
  634. // MAJ de la base
  635. $query = "UPDATE doc_".strtolower($this->CODE_DOC)."
  636. SET ville_livraison = '".addslashes($this->ville_livraison)."'
  637. WHERE ref_doc = '".$this->ref_doc."' ";
  638. $bdd->exec ($query);
  639. return true;
  640. }
  641. public function maj_text_id_pays_livraison ($text_id_pays) {
  642. global $bdd;
  643. $this->id_pays_livraison = $text_id_pays;
  644. // *************************************************
  645. // MAJ de la base
  646. $query = "UPDATE doc_".strtolower($this->CODE_DOC)."
  647. SET id_pays_livraison = ".num_or_null($this->id_pays_livraison)."
  648. WHERE ref_doc = '".$this->ref_doc."' ";
  649. $bdd->exec ($query);
  650. return true;
  651. }
  652. static function maj_description ($ref_doc, $new_description) {
  653. global $bdd;
  654. $query = "UPDATE documents SET description = '".addslashes($new_description)."'
  655. WHERE ref_doc = '".$ref_doc."' ";
  656. $bdd->exec ($query);
  657. // $this->description = $new_description;
  658. }
  659. function maj_app_tarifs ($new_app_tarifs) {
  660. global $bdd;
  661. global $CLIENT_ID_PROFIL;
  662. // Controle
  663. if ($new_app_tarifs != "HT") {
  664. $new_app_tarifs == "TTC";
  665. }
  666. $this->app_tarifs = $new_app_tarifs;
  667. // Maj de la base de données
  668. $query = "UPDATE documents SET app_tarifs = '".$this->app_tarifs."'
  669. WHERE ref_doc = '".$this->ref_doc."' ";
  670. $bdd->exec ($query);
  671. //fonction de mise à jour de l'app_tarif du contact en cas de changement d'app_tarif du document par défaut le profil client (seul les class de doc spéciales fournisseur forceront l'app_tarifs du profil fournisseur
  672. if (!is_object($this->contact)) { $this->contact = new contact ($this->ref_contact); }
  673. if ($this->contact->charger_profiled_infos($CLIENT_ID_PROFIL)) {
  674. $profil_tmp = $this->contact->getProfil($CLIENT_ID_PROFIL);
  675. $profil_tmp->maj_app_tarifs ($this->app_tarifs);
  676. }
  677. }
  678. public function maj_id_stock ($id_stock, $lib_var = "") {
  679. global $bdd;
  680. if (!$lib_var) { $lib_var = "id_stock"; }
  681. // Vérification de l'existence du stock
  682. $found = 0;
  683. foreach ($_SESSION['stocks'] as $stock) {
  684. if ($stock->getId_stock() != $id_stock) { continue; }
  685. $found = 1;
  686. break;
  687. }
  688. if (!$found) {
  689. $GLOBALS['_ALERTES']['bad_'.$lib_var] = 1;
  690. }
  691. // Vérification de la possibilité de changer l'id_stock (expédiant ou recevant)
  692. if (!$this->is_open) {
  693. $GLOBALS['_ALERTES']['doc_is_closed'] = 1;
  694. }
  695. if (count($GLOBALS['_ALERTES'])) {
  696. return false;
  697. }
  698. $this->{$lib_var} = $id_stock;
  699. // *************************************************
  700. // MAJ de la base
  701. $query = "UPDATE doc_".strtolower($this->CODE_DOC)."
  702. SET ".$lib_var." = '".$this->{$lib_var}."'
  703. WHERE ref_doc = '".$this->ref_doc."' ";
  704. $bdd->exec ($query);
  705. // *************************************************
  706. // Retour des informations
  707. $GLOBALS['_INFOS'][$lib_var] = $this->{$lib_var};
  708. return true;
  709. }
  710. // Fusion de documents
  711. public function fusion_doc ($second_ref_doc) {
  712. global $bdd;
  713. if (!isset($second_ref_doc)) { return false;}
  714. $second_document = open_doc($second_ref_doc);
  715. if (!$this->check_allow_fusion ($second_document)) { return false; }
  716. //Début de la fusion
  717. $GLOBALS['_OPTIONS']['FUSION'] = 1;
  718. //insertion de la ligne d'info
  719. $infos['type_of_line'] = "information";
  720. $infos['titre'] = "Contenu issu de la fusion avec ".$second_document->getRef_doc ();
  721. $infos['texte'] = "";
  722. $infos['visible'] = 0;
  723. $this->add_line ($infos);
  724. //copie du contenu
  725. $second_document->copie_content ($this);
  726. //changement des liaisons existantes du second document vers le premier
  727. $query = "UPDATE documents_liaisons SET ref_doc_destination = '".$this->ref_doc."'
  728. WHERE ref_doc_destination = '".$second_document->getRef_doc ()."' ";
  729. $bdd->exec ($query);
  730. $query = "UPDATE documents_liaisons SET ref_doc_source = '".$this->ref_doc."'
  731. WHERE ref_doc_source = '".$second_document->getRef_doc ()."' ";
  732. $bdd->exec ($query);
  733. //liaison non active prouvant la liaison entre les docs
  734. $this->link_from_doc_set_active ($second_document->getRef_doc (), 0) ;
  735. //renvoi des règlements vers le premier document
  736. $query = "UPDATE reglements_docs SET ref_doc = '".$this->ref_doc."'
  737. WHERE ref_doc = '".$second_document->getRef_doc ()."' ";
  738. $bdd->exec ($query);
  739. // Annulation du second document
  740. $second_document->maj_etat_doc ($this->ID_ETAT_ANNULE);
  741. $this->add_event(5, "avec ".$second_document->getRef_doc ());
  742. $second_document->add_event(5, "vers ".$this->ref_doc);
  743. return true;
  744. }
  745. // Liste des documents pouvant être fusionner
  746. public function check_allow_fusion ($second_document) {
  747. return true;
  748. }
  749. // Liste des documents pouvant être fusionner
  750. public function liste_doc_fusion () {
  751. return true;
  752. }
  753. // *************************************************************************************************************
  754. // FONCTIONS LIEES A LA MODIFICATION DE L'ETAT D'UN DOCUMENT
  755. // *************************************************************************************************************
  756. // Changement de l'état du document
  757. final public function maj_etat_doc ($new_etat_doc) {
  758. global $bdd;
  759. if ($this->id_etat_doc == $new_etat_doc) { return false; }
  760. if (!$new_etat_doc = $this->check_maj_etat($new_etat_doc)) { return false; }
  761. // Mise à jour des liaisons avec d'autres documents le cas échéant
  762. if ($new_etat_doc == $this->ID_ETAT_ANNULE) {
  763. $this->maj_etat_liaisons(0);
  764. }
  765. elseif ($this->id_etat_doc == $this->ID_ETAT_ANNULE) {
  766. $this->maj_etat_liaisons(1);
  767. }
  768. // Mise à jour des liaisons avec les règlements le cas échéant
  769. if ($new_etat_doc == $this->ID_ETAT_ANNULE) {
  770. $this->maj_etat_reglements(0);
  771. }
  772. elseif ($this->id_etat_doc == $this->ID_ETAT_ANNULE) {
  773. $this->maj_etat_reglements(1);
  774. }
  775. // Action a effectuer avant toute chose (Controles, etc.)
  776. $this->action_before_maj_etat ($new_etat_doc);
  777. // Sélection du libellé du nouvel état
  778. $query = "SELECT lib_etat_doc FROM documents_etats
  779. WHERE id_etat_doc = '".$new_etat_doc."' ";
  780. $resultat = $bdd->query ($query);
  781. $info = $resultat->fetchObject();
  782. // Changements sur l'objet
  783. $old_etat_doc = $this->id_etat_doc;
  784. $this->id_etat_doc = $new_etat_doc;
  785. $this->lib_etat_doc = $info->lib_etat_doc;
  786. // *************************************************
  787. // Maj dans la BDD
  788. $query = "UPDATE documents SET id_etat_doc = '".$new_etat_doc."'
  789. WHERE ref_doc = '".$this->ref_doc."' ";
  790. $bdd->exec ($query);
  791. // Enregistrement de l'évennement
  792. $id_event_type = 2;
  793. $event = $this->lib_etat_doc;
  794. $this->add_event($id_event_type, $event);
  795. // Action a effectuer apres la mise a jour de l'etat.
  796. $this->action_after_maj_etat ($old_etat_doc);
  797. return true;
  798. }
  799. // Vérification de la possibilité de changer l'état du document
  800. protected function check_maj_etat ($new_etat_doc) {
  801. if (!is_numeric($new_etat_doc)) { return false; }
  802. return $new_etat_doc;
  803. }
  804. // Action avant de changer l'état du document
  805. protected function action_before_maj_etat ($new_etat_doc) {
  806. return true;
  807. }
  808. // Action après de changer l'état du document
  809. protected function action_after_maj_etat ($old_etat_doc) {
  810. return true;
  811. }
  812. protected function maj_etat_liaisons ($active = 1) {
  813. global $bdd;
  814. $query = "UPDATE documents_liaisons SET active = '".$active."'
  815. WHERE ref_doc_source = '".$this->ref_doc."' || ref_doc_destination = '".$this->ref_doc."' ";
  816. $bdd->exec ($query);
  817. return true;
  818. }
  819. // *************************************************************************************************************
  820. // FONCTIONS DE GESTION DU CONTENU
  821. // *************************************************************************************************************
  822. // Chargement du contenu du document
  823. public function charger_contenu () {
  824. global $bdd;
  825. global $CALCUL_TARIFS_NB_DECIMALS;
  826. global $TARIFS_NB_DECIMALES;
  827. global $GESTION_SN;
  828. global $DOC_AFF_QTE_SN;
  829. $query_infos_supp = $this->doc_line_infos_supp ();
  830. // *****************************************
  831. // Chargement du contenu
  832. $this->contenu = $tmp_contenu = array();
  833. $query = "SELECT dl.ref_doc_line, dl.ref_article, dl.lib_article, dl.desc_article,
  834. dl.qte, round(dl.pu_ht,".$CALCUL_TARIFS_NB_DECIMALS.") as pu_ht, dl.remise, dl.tva,
  835. dl.ordre, dl.ref_doc_line_parent, dl.visible, dl.pa_ht, dl.pa_forced,
  836. a.ref_oem, a.ref_interne, a.id_valo, a.valo_indice, a.gestion_sn, ac.modele, a.lot, av.abrev_valo
  837. ".$query_infos_supp['select']."
  838. FROM docs_lines dl
  839. LEFT JOIN articles a ON dl.ref_article = a.ref_article
  840. LEFT JOIN articles_valorisations av ON av.id_valo = a.id_valo
  841. LEFT JOIN art_categs ac ON a.ref_art_categ = ac.ref_art_categ
  842. ".$query_infos_supp['left_join']."
  843. WHERE ref_doc = '".$this->ref_doc."'
  844. ORDER BY ordre ";
  845. $resultat = $bdd->query ($query);
  846. while ($doc_line = $resultat->fetchObject()) {
  847. $doc_line->type_of_line = define_type_of_line($doc_line->ref_article);
  848. $tmp_contenu[] = $doc_line;
  849. }
  850. for ($i=0; $i<count($tmp_contenu); $i++) {
  851. if ($tmp_contenu[$i]->ref_doc_line_parent) { continue; }
  852. $this->contenu[] = $tmp_contenu[$i];
  853. for ($j=0; $j<count($tmp_contenu); $j++) {
  854. if ($tmp_contenu[$j]->ref_doc_line_parent != $tmp_contenu[$i]->ref_doc_line) { continue; }
  855. $this->contenu[] = $tmp_contenu[$j];
  856. }
  857. }
  858. $this->contenu_loaded = true;
  859. // *****************************************
  860. // Calcul des montants HT et TTC, des TVAS et des TAXES
  861. $this->montant_ht = $this->montant_tva = 0;
  862. $this->tvas = array();
  863. for ($i=0; $i<count($this->contenu); $i++) {
  864. if ( $this->contenu[$i]->type_of_line != "article" || !$this->contenu[$i]->visible) {
  865. continue;
  866. }
  867. $this->contenu[$i]->pu_ht = round($this->contenu[$i]->pu_ht, $CALCUL_TARIFS_NB_DECIMALS);
  868. $montant_ht = round ($this->contenu[$i]->pu_ht * $this->contenu[$i]->qte * (1-$this->contenu[$i]->remise/100), $CALCUL_TARIFS_NB_DECIMALS);
  869. $tva = round($montant_ht * ($this->contenu[$i]->tva/100), $TARIFS_NB_DECIMALES) ;
  870. $this->montant_ht += $montant_ht;
  871. $this->montant_tva += $tva;
  872. if (!isset($this->tvas[$this->contenu[$i]->tva])) { $this->tvas[$this->contenu[$i]->tva] = 0; }
  873. $this->tvas[$this->contenu[$i]->tva] += $tva;
  874. }
  875. $this->montant_ttc = $this->montant_ht + $this->montant_tva;
  876. // *****************************************
  877. // Chargement des numéros de serie
  878. if (!$GESTION_SN || !$this->GESTION_SN) { return true; }
  879. $query_sn_infos_supp = $this->doc_line_sn_infos_supp ();
  880. $numeros = array();
  881. $contenu_liste = "";
  882. foreach ($this->contenu as $doc_line) {
  883. if (!$doc_line->gestion_sn) { continue; }
  884. if ($doc_line->gestion_sn == 2) {
  885. unset($GLOBALS['_OPTIONS']['CREATE_DOC']['no_charge_all_sn']);
  886. $GLOBALS['_OPTIONS']['CREATE_DOC']['group_sn'] = 1;
  887. }
  888. if ($contenu_liste) {$contenu_liste .= ",";}
  889. $contenu_liste .= "'".$doc_line->ref_doc_line."'";
  890. }
  891. if (!$contenu_liste) { return true; }
  892. $query = "SELECT dls.ref_doc_line, dls.numero_serie, dls.sn_qte ".$query_sn_infos_supp['select']."
  893. FROM docs_lines_sn dls ".$query_sn_infos_supp['left_join']."
  894. WHERE dls.ref_doc_line IN (".$contenu_liste.")
  895. ORDER BY dls.numero_serie ASC";
  896. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['no_charge_all_sn']) ) {
  897. $query .= " LIMIT ".($DOC_AFF_QTE_SN+1);
  898. }
  899. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['group_sn']) ) {
  900. $query = "SELECT dls.ref_doc_line, dls.numero_serie, dls.sn_qte
  901. FROM docs_lines_sn dls
  902. WHERE dls.ref_doc_line IN (".$contenu_liste.")
  903. ORDER BY dls.numero_serie ASC";
  904. }
  905. $resultat = $bdd->query ($query);
  906. while ($sn = $resultat->fetchObject()) { $numeros[] = $sn; }
  907. // Association des numéros de série au contenu
  908. for ($i=0; $i<count($this->contenu); $i++) {
  909. // Création du tableau de numéros de série
  910. if ($this->contenu[$i]->gestion_sn) { $this->contenu[$i]->sn = array(); }
  911. // Remplissage du tableau
  912. foreach ($numeros as $sn) {
  913. if ($this->contenu[$i]->ref_doc_line != $sn->ref_doc_line) { continue; }
  914. $this->contenu[$i]->sn[] = $sn;
  915. }
  916. }
  917. return true;
  918. }
  919. protected function charger_line ($ref_doc_line) {
  920. global $bdd;
  921. global $GESTION_SN;
  922. global $CALCUL_TARIFS_NB_DECIMALS;
  923. $query_infos_supp = $this->doc_line_infos_supp ();
  924. // Sélection des informations
  925. $query = "SELECT dl.ref_doc_line, dl.ref_article, dl.lib_article, dl.desc_article,
  926. dl.qte, round(dl.pu_ht,".$CALCUL_TARIFS_NB_DECIMALS.") as pu_ht, dl.remise, dl.tva,
  927. dl.ordre, dl.ref_doc_line_parent, dl.visible, dl.pa_ht, dl.pa_forced,
  928. a.ref_oem, a.ref_interne, a.id_valo, a.valo_indice, a.gestion_sn, ac.modele, a.lot, av.abrev_valo
  929. ".$query_infos_supp['select']."
  930. FROM docs_lines dl
  931. LEFT JOIN articles a ON dl.ref_article = a.ref_article
  932. LEFT JOIN articles_valorisations av ON av.id_valo = a.id_valo
  933. LEFT JOIN art_categs ac ON a.ref_art_categ = ac.ref_art_categ
  934. ".$query_infos_supp['left_join']."
  935. WHERE dl.ref_doc_line = '".addslashes($ref_doc_line)."' ";
  936. $bdd->query ($query);
  937. $resultat = $bdd->query ($query);
  938. if (!$doc_line = $resultat->fetchObject()) { return false; }
  939. $doc_line->type_of_line = define_type_of_line($doc_line->ref_article);
  940. // *****************************************
  941. // Chargement des numéros de serie
  942. if (!$GESTION_SN || !$this->GESTION_SN || !$doc_line->gestion_sn) { return $doc_line; }
  943. $query_sn_infos_supp = $this->doc_line_sn_infos_supp ();
  944. $doc_line->sn = array();
  945. $query = "SELECT dls.ref_doc_line, dls.numero_serie ".$query_sn_infos_supp['select']."
  946. FROM docs_lines_sn dls ".$query_sn_infos_supp['left_join']."
  947. WHERE dls.ref_doc_line = '".$ref_doc_line."' ";
  948. if ($doc_line->gestion_sn == 2) {
  949. $query = "SELECT dls.ref_doc_line, dls.numero_serie
  950. FROM docs_lines_sn dls
  951. WHERE dls.ref_doc_line = '".$ref_doc_line."' ";
  952. }
  953. $resultat = $bdd->query ($query);
  954. while ($sn = $resultat->fetchObject()) { $doc_line->sn[] = $sn; }
  955. return $doc_line;
  956. }
  957. // Chargement du contenu "matériel" du document
  958. public function charger_contenu_materiel () {
  959. global $bdd;
  960. global $GESTION_SN;
  961. $query_infos_supp = $this->doc_line_infos_supp ();
  962. // *****************************************
  963. // Chargement du contenu
  964. $this->contenu_materiel = array();
  965. $query = "SELECT dl.ref_doc_line, dl.ref_article, dl.qte, dl.pa_ht, dl.pa_forced, a.gestion_sn
  966. ".$query_infos_supp['select']."
  967. FROM docs_lines dl
  968. LEFT JOIN articles a ON dl.ref_article = a.ref_article
  969. LEFT JOIN art_categs ac ON a.ref_art_categ = ac.ref_art_categ
  970. ".$query_infos_supp['left_join']."
  971. WHERE ref_doc = '".$this->ref_doc."' && a.modele = 'materiel' && a.lot != '2'";
  972. $resultat = $bdd->query ($query);
  973. while ($doc_line = $resultat->fetchObject()) {
  974. $this->contenu_materiel[] = $doc_line;
  975. }
  976. $this->contenu_materiel_loaded = true;
  977. // *****************************************
  978. // Chargement des numéros de serie
  979. if (!$GESTION_SN || !$this->GESTION_SN) { return true; }
  980. $query_sn_infos_supp = $this->doc_line_sn_infos_supp ();
  981. $contenu_liste = "''";
  982. foreach ($this->contenu_materiel as $doc_line) {
  983. if (!$doc_line->gestion_sn) { continue; }
  984. if ($doc_line->gestion_sn == 2) {
  985. unset($GLOBALS['_OPTIONS']['CREATE_DOC']['no_charge_all_sn']);
  986. $GLOBALS['_OPTIONS']['CREATE_DOC']['group_sn'] = 1;
  987. }
  988. if ($contenu_liste) {$contenu_liste .= ",";}
  989. $contenu_liste .= "'".$doc_line->ref_doc_line."'";
  990. }
  991. if (!$contenu_liste) { return true; }
  992. $query = "SELECT dls.ref_doc_line, dls.numero_serie, dls.sn_qte ".$query_sn_infos_supp['select']."
  993. FROM docs_lines_sn dls ".$query_sn_infos_supp['left_join']."
  994. WHERE dls.ref_doc_line IN (".$contenu_liste.") ";
  995. if (isset($GLOBALS['_OPTIONS']['CREATE_DOC']['group_sn']) ) {
  996. $query = "SELECT dls.ref_doc_line, dls.numero_serie, dls.sn_qte
  997. FROM docs_lines_sn dls
  998. WHERE dls.ref_doc_line IN (".$contenu_liste.") ";
  999. }
  1000. $resultat = $bdd->query ($query);
  1001. $numeros = array();
  1002. while ($sn = $resultat->fetchObject()) {
  1003. $numeros[] = $sn;
  1004. }
  1005. // Association des numéros de série au contenu
  1006. for ($i=0; $i<count($this->contenu_materiel); $i++) {
  1007. // Création du tableau de numéros de série
  1008. if ($this->contenu_materiel[$i]->gestion_sn) { $this->contenu_materiel[$i]->sn = array(); }
  1009. // Remplissage du tableau
  1010. foreach ($numeros as $sn) {
  1011. if ($this->contenu_materiel[$i]->ref_doc_line != $sn->ref_doc_line) { continue; }
  1012. $this->contenu_materiel[$i]->sn[] = $sn;
  1013. }
  1014. }
  1015. return true;
  1016. }
  1017. // Chargement du contenu "service_abo" du document
  1018. public function charger_contenu_service_abo () {
  1019. global $bdd;
  1020. global $GESTION_SN;
  1021. $query_infos_supp = $this->doc_line_infos_supp ();
  1022. // *****************************************
  1023. // Chargement du contenu
  1024. $this->contenu_service_abo = array();
  1025. $query = "SELECT dl.ref_doc_line, dl.ref_article, dl.qte, dl.pa_ht, dl.pa_forced, a.gestion_sn
  1026. ".$query_infos_supp['select']."
  1027. FROM docs_lines dl
  1028. LEFT JOIN articles a ON dl.ref_article = a.ref_article
  1029. LEFT JOIN art_categs ac ON a.ref_art_categ = ac.ref_art_categ
  1030. ".$query_infos_supp['left_join']."
  1031. WHERE ref_doc = '".$this->ref_doc."' && a.modele = 'service_abo' && a.lot != '2'";
  1032. $resultat = $bdd->query ($query);
  1033. while ($doc_line = $resultat->fetchObject()) {
  1034. $this->contenu_service_abo[] = $doc_line;
  1035. }
  1036. $this->contenu_service_abo_loaded = true;
  1037. return true;
  1038. }
  1039. // Chargement du contenu "service_conso" du document
  1040. public function charger_contenu_service_conso () {
  1041. global $bdd;
  1042. global $GESTION_SN;
  1043. $query_infos_supp = $this->doc_line_infos_supp ();
  1044. // *****************************************
  1045. // Chargement du contenu
  1046. $this->contenu_service_conso = array();
  1047. $query = "SELECT dl.ref_doc_line, dl.ref_article, dl.qte, dl.pa_ht, dl.pa_forced, a.gestion_sn
  1048. ".$query_infos_supp['select']."
  1049. FROM docs_lines dl
  1050. LEFT JOIN articles a ON dl.ref_article = a.ref_article
  1051. LEFT JOIN art_categs ac ON a.ref_art_categ = ac.ref_art_categ
  1052. ".$query_infos_supp['left_join']."
  1053. WHERE ref_doc = '".$this->ref_doc."' && a.modele = 'service_conso' && a.lot != '2'";
  1054. $resultat = $bdd->query ($query);
  1055. while ($doc_line = $resultat->fetchObject()) {
  1056. $this->contenu_service_conso[] = $doc_line;
  1057. }
  1058. $this->contenu_service_conso_loaded = true;
  1059. return true;
  1060. }
  1061. // Défini les informations supplémentaires a charger avec une ligne de document
  1062. protected function doc_line_infos_supp () {
  1063. $query['select'] = "";
  1064. $query['left_join'] = "";
  1065. return $query;
  1066. }
  1067. // Chargement des informations supplémentaires concernant les numéros de série
  1068. protected function doc_line_sn_infos_supp () {
  1069. $query['select'] = "";
  1070. $query['left_join'] = "";
  1071. return $query;
  1072. }
  1073. // Supprime une ligne de contenu
  1074. public function delete_line ($ref_doc_line) {
  1075. global $bdd;
  1076. // On récupère l'article de la ligne
  1077. $query = "SELECT ref_article FROM docs_lines WHERE ref_doc_line = '".$ref_doc_line."';";
  1078. $res = $bdd->query($query);
  1079. if($enr = $res->fetchObject()){
  1080. $article = new article($enr->ref_article);
  1081. // On supprime les lignes de taxes
  1082. $taxes = $article->getTaxes();
  1083. foreach ($taxes as $taxe) {
  1084. $query = "DELETE FROM docs_lines
  1085. WHERE ref_article = 'TAXE " . $taxe->code_taxe . "'
  1086. AND ref_doc_line_parent = '" . $ref_doc_line . "';";
  1087. $bdd->exec($query);
  1088. }
  1089. }
  1090. $query = "DELETE FROM docs_lines WHERE ref_doc_line = '".$ref_doc_line."' ";
  1091. $bdd->exec ($query);
  1092. return true;
  1093. }
  1094. // Supprime toutes les lignes de contenu
  1095. public function delete_all_line () {
  1096. global $bdd;
  1097. $query = "DELETE FROM docs_lines WHERE ref_doc = '".$this->ref_doc."' ";
  1098. $bdd->exec ($query);
  1099. return true;
  1100. }
  1101. // *************************************************************************************************************
  1102. // FONCTIONS D'AJOUT DU CONTENU
  1103. // *************************************************************************************************************
  1104. // Ajout d'une ligne au document
  1105. public function add_line ($infos) {
  1106. switch($infos['type_of_line']) {
  1107. case "article" :
  1108. return $this->add_line_article ($infos);
  1109. break;
  1110. case "information":
  1111. return $this->add_line_info ($infos);
  1112. break;
  1113. case "soustotal":
  1114. return $this->add_line_ss_total ($infos);
  1115. break;
  1116. case "taxe":
  1117. return $this->add_line_taxe ($infos);
  1118. break;
  1119. }
  1120. }
  1121. // Ajout d'un article depuis le catalogue
  1122. protected function add_line_article ($infos) {
  1123. global $bdd;
  1124. global $GESTION_SN;
  1125. global $DOC_LINE_ID_REFERENCE_TAG;
  1126. global $ASSUJETTI_TVA;
  1127. global $DEVIS_CLIENT_ID_TYPE_DOC;
  1128. global $COMMANDE_CLIENT_ID_TYPE_DOC;
  1129. global $LIVRAISON_CLIENT_ID_TYPE_DOC;
  1130. global $FACTURE_FOURNISSEUR_ID_TYPE_DOC;
  1131. global $INVENTAIRE_ID_TYPE_DOC;
  1132. global $DEFAUT_ID_PAYS;
  1133. // *************************************************
  1134. // Sélection des informations sur l'article
  1135. $ref_article = $infos['ref_article'];
  1136. $article = new article ($ref_article);
  1137. // *************************************************
  1138. // Verification
  1139. if (!$article->getRef_article()) {
  1140. return false;
  1141. }
  1142. $qte = $infos['qte'];
  1143. // Si on est sur un inventaire, on n'affiche pas les compositions internes
  1144. if($article->getLot() == 2 && $this->ID_TYPE_DOC == $INVENTAIRE_ID_TYPE_DOC){
  1145. return false;
  1146. }
  1147. // *************************************************
  1148. // Réception des autres variables
  1149. if (isset($infos['pu_ht'])) { $pu_ht = $infos['pu_ht']; }
  1150. else { $pu_ht = $this->select_article_pu ($article, $qte); }
  1151. if (isset($infos['pa_ht']) && !empty($infos['pa_forced'])) {
  1152. $pa_ht = $infos['pa_ht'];
  1153. }
  1154. else {
  1155. $pa_ht = $article->getPaa_ht();
  1156. }
  1157. if (isset($infos['remise'])) {
  1158. $remise = floatval($infos['remise']); }
  1159. else {
  1160. if(!$res =$this->select_infos_article_pcotation($article, $qte))
  1161. $remise = 0;
  1162. else
  1163. $remise = floatval($res->remise);
  1164. }
  1165. if (isset($infos['ref_doc_line_parent'])) { $ref_doc_line_parent = $infos['ref_doc_line_parent']; }
  1166. else { $ref_doc_line_parent = ""; }
  1167. $visible = 1;
  1168. if ($ref_doc_line_parent || (isset($infos['visible']) && $infos['visible'])) { $visible = 0; }
  1169. // *************************************************
  1170. // Création de la référence et de l'ordre
  1171. $ref_doc_line = $this->create_ref_doc_line ();
  1172. $ordre = $this->new_line_order ($ref_doc_line_parent);
  1173. //gestion du taux de tva
  1174. if(isset($infos['tva']) && is_numeric($infos['tva'])){
  1175. $article_taux_tva = $infos['tva'];
  1176. }else{
  1177. $article_taux_tva = $article->getTva();
  1178. if (!$ASSUJETTI_TVA && ($this->ID_TYPE_DOC == $DEVIS_CLIENT_ID_TYPE_DOC || $this->ID_TYPE_DOC == $COMMANDE_CLIENT_ID_TYPE_DOC || $this->ID_TYPE_DOC == $LIVRAISON_CLIENT_ID_TYPE_DOC || $this->ID_TYPE_DOC == $FACTURE_FOURNISSEUR_ID_TYPE_DOC)) {$article_taux_tva = 0;}
  1179. //mise à zéro du taux de tva si pays client != pays defaut (GESTION TVA INTERNATTIONNAL)
  1180. if (isset($this->id_pays_contact)) {
  1181. if ( $this->id_pays_contact != $DEFAUT_ID_PAYS) {
  1182. $article_taux_tva = 0;

Large files files are truncated, but you can click here to view the full file