KataoSupplier.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. class KataoSupplier extends BaseKataoSupplier {
  3. public function save($con = null) {
  4. if ('' == $this->getUrlIdentifier()) {
  5. $this->setUrlIdentifier(Catalyz::normalizeUrlIdentifier($this->getName()));
  6. }
  7. $this->initGmapCoordinates();
  8. $this->initAccountingCode();
  9. parent::save($con);
  10. }
  11. public function __toString() {
  12. return $this->getName();
  13. }
  14. public function getCompleteAdress() {
  15. $result = '';
  16. if ($this->getAddress1()) {
  17. $result .= ' ' . $this->getAddress1() ;
  18. }
  19. if ($this->getAddress2()) {
  20. $result .= ' ' . $this->getAddress2() ;
  21. }
  22. $result .= ' ' . $this->getZip() ;
  23. $result .= ' ' . $this->getCity() ;
  24. return $result;
  25. }
  26. public function getFullAdress() {
  27. $return = $this->getAddress1();
  28. if ('' != $this->getAddress2()) {
  29. $return .= "\n" . $this->getAddress2();
  30. }
  31. $return .= "\n";
  32. $return .= $this->getZip() ;
  33. $return .= ' ' . $this->getCity() ;
  34. return $return;
  35. }
  36. public function getMemberLink($with_link = false) {
  37. $result = '';
  38. if (null != $katao_member = $this->getKataoMemberRelatedByMemberId()) {
  39. if ($with_link) {
  40. $result = link_to($katao_member->getFullName(), '@katao_member_show?id=' . $katao_member->getId());
  41. } else {
  42. $result = $katao_member->getFullName();
  43. }
  44. }
  45. return $result;
  46. }
  47. public function getRefererLink($with_link = false) {
  48. $result = '';
  49. if (null != $katao_member = $this->getKataoMemberRelatedByRefererId()) {
  50. if ($with_link) {
  51. $result = link_to($katao_member->getFullName(), '@katao_member_show?id=' . $katao_member->getId());
  52. } else {
  53. $result = $katao_member->getFullName();
  54. }
  55. }
  56. return $result;
  57. }
  58. public function initGmapCoordinates($force = false) {
  59. if ($this->isNew() || $force) {
  60. $lat_lng = Utils::getGmapLatLng($this->getCompleteAdress(), $this->getZip() . ' ' . $this->getCity());
  61. $this->setGmapLat($lat_lng['lat']);
  62. $this->setGmapLng($lat_lng['lng']);
  63. }
  64. }
  65. public function initAccountingCode() {
  66. if ('' == $this->getAccountingCode()) {
  67. $index = 0;
  68. $accounting_code = sprintf('401%s', strtoupper(substr($this->getName(), 0, 3)));
  69. $accounting_code_sol = sprintf('402%s', strtoupper(substr($this->getName(), 0, 3)));
  70. while (0 < KataoSupplierPeer::countByAccountingCode($accounting_code)) {
  71. $index++;
  72. $accounting_code = sprintf('401%s%d', strtoupper(substr($this->getName(), 0, 2)), $index);
  73. $accounting_code_sol = sprintf('402%s%d', strtoupper(substr($this->getName(), 0, 2)), $index);
  74. }
  75. $this->setAccountingCode($accounting_code);
  76. $this->setAccountingCodeSol($accounting_code_sol);
  77. }
  78. }
  79. public function getNodesStr() {
  80. $return = array();
  81. foreach ($this->getKataoSupplierNodesJoinKataoNode() as/*(KataoSupplierNode)*/ $katao_supplier_node) {
  82. $katao_node = $katao_supplier_node->getKataoNode();
  83. $return[] = $katao_node->getCity();
  84. }
  85. return implode(', ', $return);
  86. }
  87. public function getTypeStr($display_only_number = false) {
  88. $return = 'National';
  89. if (!$this->getIsGlobal()) {
  90. $return = 'Local';
  91. if (!$display_only_number && '' != $nodes = $this->getNodesStr()) {
  92. $return .= sprintf(' (%s)', $nodes);
  93. } elseif ($display_only_number && 0 < $nodes_count = $this->countKataoSupplierNodes()) {
  94. $return .= sprintf(' (%d noeud%s)', $nodes_count, (1 < $nodes_count)?'s':'');
  95. }
  96. }
  97. return $return;
  98. }
  99. /**
  100. * KataoSupplier::isVisible()
  101. *
  102. * @return boolean
  103. */
  104. public function isVisible() {
  105. $visible_suppliers = KataoSupplierPeer::getAllVisible();
  106. return !empty($visible_suppliers[$this->getId()]);
  107. }
  108. /**
  109. * KataoMember::getFirstKataoUser()
  110. *
  111. * @return KataoUser
  112. */
  113. public function getFirstKataoUser() {
  114. $katao_users = $this->getKataoUsers();
  115. return array_shift($katao_users);
  116. }
  117. public function getStatusStr() {
  118. return $this->getFirstKataoUser()->getStatusStr();
  119. }
  120. /**
  121. * KataoSupplier::isLocalToNode()
  122. *
  123. * @param int $katao_node_id
  124. * @return boolean
  125. */
  126. public function isLocalToNode($katao_node_id) {
  127. $criteria = new Criteria();
  128. $criteria->add(KataoSupplierNodePeer::KATAO_SUPPLIER_ID, $this->getId());
  129. $criteria->add(KataoSupplierNodePeer::KATAO_NODE_ID, $katao_node_id);
  130. return 1 == KataoSupplierNodePeer::doCount($criteria);
  131. }
  132. public function isDeletable() {
  133. return (0 == $this->countKataoProducts()) && (0 == $this->countKataoOrders());
  134. }
  135. public function getCAEuroStr() {
  136. $return = 0;
  137. $criteria = new Criteria();
  138. $criteria->addSelectColumn(KataoSupplierInvoicePeer::ID);
  139. $criteria->addAsColumn('sum_euro', sprintf('SUM(%s)', KataoSupplierInvoicePeer::TOTAL_HT));
  140. $criteria->add(KataoSupplierInvoicePeer::KATAO_SUPPLIER_ID, $this->getId());
  141. $criteria->add(KataoSupplierInvoicePeer::PAYMENT_DATE, null, Criteria::NOT_EQUAL);
  142. $criteria->addGroupByColumn(KataoSupplierInvoicePeer::ID);
  143. $rs = KataoSupplierInvoicePeer::doSelectRS($criteria);
  144. if ($rs->next()) {
  145. $return = $rs->getFloat(2);
  146. }
  147. return Utils::formatCurrencyEuro($return);
  148. }
  149. public function getCASolStr() {
  150. $return = 0;
  151. $criteria = new Criteria();
  152. $criteria->addSelectColumn(KataoSupplierInvoicePeer::ID);
  153. $criteria->addAsColumn('sum_sol', sprintf('SUM(%s)', KataoSupplierInvoicePeer::PAYMENT_AMOUNT_SOL));
  154. $criteria->add(KataoSupplierInvoicePeer::KATAO_SUPPLIER_ID, $this->getId());
  155. $criteria->add(KataoSupplierInvoicePeer::PAYMENT_DATE, null, Criteria::NOT_EQUAL);
  156. $criteria->addGroupByColumn(KataoSupplierInvoicePeer::ID);
  157. $rs = KataoSupplierInvoicePeer::doSelectRS($criteria);
  158. if ($rs->next()) {
  159. $return = $rs->getInt(2);
  160. }
  161. return Utils::formatCurrencySol($return);
  162. }
  163. }