/application/models/Products.php

https://github.com/r1zib/salesforce · PHP · 157 lines · 96 code · 33 blank · 28 comment · 13 complexity · 6a18ab399360a86ce7dc7d4e3dab3d69 MD5 · raw file

  1. <?php
  2. class Application_Model_Products
  3. {
  4. /* permet de recherche en base locale ou directement dans salesfores */
  5. private $rech_db = true;
  6. function __construct() {
  7. }
  8. /*
  9. * Recherche des information d'une Opportunitée
  10. * @param string id
  11. * @param string lstCol liste des champs
  12. * @return un tableau de la liste des enregistrements
  13. */
  14. function find ($id) {
  15. $info = array();
  16. if ($this->rech_db) {
  17. $info = $this->find_db($id);
  18. } else {
  19. $info = $this->find_salesforce($id);
  20. }
  21. if (isset($info['UnitPrice'])) {
  22. /* Formatage de prix */
  23. $prix = $info['UnitPrice'];
  24. $info['UnitPrice'] = $this->montant($prix);
  25. $info['UnitPrice_ttc'] = $this->montant_ttc($prix);
  26. }
  27. $info['pdf'] = Zend_Json::prettyPrint(Zend_Json::encode($info));
  28. return $info;
  29. }
  30. function find_salesforce ($id) {
  31. /* la liste des colonnes provient du fichier de application.ini ou celui du login */
  32. $config = Azeliz_Registreconfig::getInstance()->getConfig();
  33. $lstCol = $config->product->Product2;
  34. $where = "Id='".$id."'";
  35. $vue = $this->fetchAll($lstCol,'',$where);
  36. return $vue['products'][0];
  37. }
  38. function find_db ($id) {
  39. /* la liste des colonnes provient du fichier de application.ini ou celui du login */
  40. $config = Azeliz_Registreconfig::getInstance()->getConfig();
  41. $colProduct2 = $config->product->Product2;
  42. $colPricebook = 'Name,Description,IsStandard,Id';
  43. $colPricebookEntry = 'UnitPrice,Pricebook2Id';
  44. /* Recherche du prix standard */
  45. $product = new Application_Model_Product2Mapper();
  46. $produit = $product->findDetail($id, $colProduct2, $colPricebook, $colPricebookEntry);
  47. return $produit;
  48. }
  49. /*
  50. * Recherche des information d'une Opportunitée
  51. * @param string id
  52. * @param string lstCol liste des champs
  53. * @return un tableau de la liste des enregistrements
  54. */
  55. function fetchAll ($lstCol='Name,ProductCode,Id', $where ='') {
  56. if ($this->rech_db) {
  57. return $this->fetchAll_db($lstCol,$where);
  58. } else {
  59. return $this->fetchAll_salesforce($lstCol,$where);
  60. }
  61. }
  62. function fetchAll_db ($lstCol='Name,ProductCode,Id', $where="") {
  63. $product = new Application_Model_Product2Mapper();
  64. $vue['products'] = $product->fetchAll($lstCol,$where);
  65. if (!is_array($vue['products'])) {
  66. /* pb de query */
  67. echo $vue['products'];
  68. }
  69. $vue['cols'] = explode(',', $lstCol.',UnitPrice');
  70. $vue['tps'] = 0;
  71. return $vue;
  72. }
  73. function fetchAll_salesforce ($lstCol='Name,ProductCode,Id', $where="") {
  74. $sales = Application_Model_SalesforceConnect::getInstance();
  75. $vue = array();
  76. $time_start = $sales->microtime_float();
  77. $vue['products'] = $sales->query('Product2', $lstCol, $where);
  78. if (!is_array($vue['products'])) {
  79. /* pb de query */
  80. echo $vue['products'];
  81. }
  82. $time_end = $sales->microtime_float();
  83. $vue['cols'] = explode(',', $lstCol);
  84. $vue['tps'] = $time_end - $time_start;
  85. $colsPrice = 'Name,Description,IsStandard,Id';
  86. $vue['Pricebooks'] = $sales->query('Pricebook2',$colsPrice);
  87. $vue['colsPrice'] = explode(',', $colsPrice);
  88. /* Quel est le pricebook sélectionné ?
  89. *
  90. */
  91. $pricebookid = "";
  92. if ($pricebookid == "") {
  93. /* on prend le 1er de la liste */
  94. if (count($vue['Pricebooks'])>0) $pricebookid = @$vue['Pricebooks'][0]['Id'];
  95. }
  96. /*recherche des prix */
  97. for ($i=0;$i<count($vue['products']); $i++) {
  98. $id = $vue['products'][$i]['Id'];
  99. $sel = "Product2Id='".$id."' and Pricebook2Id = '".$pricebookid."'";
  100. $info = $sales->query('PricebookEntry','UnitPrice,Pricebook2Id', $sel);
  101. if (isset($info[0])) {
  102. $vue['products'][$i]['UnitPrice'] = $info[0]['UnitPrice'];
  103. }
  104. foreach ($info as $pricebookEntry) {
  105. $vue['products'][$i]['UnitPrice'] = $pricebookEntry['UnitPrice'];
  106. }
  107. /* affichage des images */
  108. if (isset($vue['products'][$i]['image1__c'])) {
  109. $vue['products'][$i]['image1__c'] = '<img src="'.$vue['products'][$i]['image1__c'].'" height="100" width="100"';
  110. }
  111. }
  112. $vue['cols'] = explode(',', $lstCol.',UnitPrice');
  113. return $vue;
  114. }
  115. /*
  116. * Calcul du montant en TTC
  117. */
  118. function montant_ttc ($mt, $taxe = 1.196) {
  119. return number_format(round(floatval($mt) *1.196,2),2,',','.');
  120. }
  121. function montant ($mt) {
  122. return number_format($mt,2,',','.');
  123. }
  124. }