KataoProduct.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. class KataoProduct extends BaseKataoProduct {
  3. public function __construct() {
  4. $this->setMargin(wpConfig::getDefaultProductMargin() / 100);
  5. }
  6. public function save($con = null) {
  7. if ('' == $this->getUrlIdentifier()) {
  8. $this->setUrlIdentifier(Catalyz::normalizeUrlIdentifier($this->getName()));
  9. }
  10. if ('' == $this->getAccountingCodePurchase()) {
  11. $this->setAccountingCodePurchase(wpConfig::getAccountingCodePrefixPurchase() . $this->getKataoProductFamily()->getKataoProductCategory()->getAccountingCodeSuffix());
  12. }
  13. if ('' == $this->getAccountingCodeSell()) {
  14. $this->setAccountingCodeSell(wpConfig::getAccountingCodePrefixSell() . $this->getKataoProductFamily()->getKataoProductCategory()->getAccountingCodeSuffix());
  15. }
  16. if (!$this->isNew() && ($this->isColumnModified(KataoProductPeer::UNIT_PRICE_EURO) || $this->isColumnModified(KataoProductPeer::MARGIN) || $this->isColumnModified(KataoProductPeer::TVA_RATE))) {
  17. $this->notifyProductPriceModification();
  18. }
  19. parent::save($con);
  20. }
  21. public function delete($con = null) {
  22. parent::delete($con);
  23. if (is_file(sfConfig::get('sf_web_dir') . $this->getPicture())) {
  24. unlink(sfConfig::get('sf_web_dir') . $this->getPicture());
  25. }
  26. }
  27. public function getTotalPriceWithoutTaxes() {
  28. return round($this->getUnitPriceEuro() * (1 + $this->getMargin()), 2);
  29. }
  30. public function getTotalPriceWithTaxes() {
  31. return round($this->getUnitPriceEuro() * (1 + $this->getMargin()) * (1 + $this->getTvaRate()), 2);
  32. }
  33. public function isCartable() {
  34. $return = true;
  35. $return &= in_array($this->getId(), sfContext::getInstance()->getUser()->getCartableProducts());
  36. $return &= sfContext::getInstance()->getUser()->getActivePeriod()->getOrderEndedAt('Y-m-d') >= date('Y-m-d');
  37. return $return;
  38. }
  39. public function getCompletedPackages($katao_period_id, $katao_node_id, $with_adjusted_quantity = false) {
  40. return $this->getMinOrderNumber()?floor($this->getOrderedProductQuantity($katao_period_id, $katao_node_id, $with_adjusted_quantity) / $this->getMinOrderNumber()):0;
  41. }
  42. public function getLastIncompletePackageQuantity($katao_period_id, $katao_node_id, $with_adjusted_quantity = false) {
  43. $ordered_product_quantity = $this->getOrderedProductQuantity($katao_period_id, $katao_node_id, $with_adjusted_quantity);
  44. $min_order_number = $this->getMinOrderNumber();
  45. return $min_order_number?($ordered_product_quantity - $min_order_number * floor($ordered_product_quantity / $min_order_number)):0;
  46. }
  47. protected function getOrderedProductQuantity($katao_period_id, $katao_node_id, $with_adjusted_quantity = false) {
  48. $return = 0;
  49. $criteria = new Criteria();
  50. $criteria->addSelectColumn(KataoCartProductPeer::KATAO_PRODUCT_ID);
  51. if ($with_adjusted_quantity) {
  52. $criteria->addAsColumn('quantity', sprintf('SUM(%s)', KataoCartProductPeer::QUANTITY_ADJUSTED));
  53. } else {
  54. $criteria->addAsColumn('quantity', sprintf('SUM(%s)', KataoCartProductPeer::QUANTITY));
  55. }
  56. $criteria->addJoin(KataoCartPeer::ID, KataoCartProductPeer::KATAO_CART_ID);
  57. $criteria->add(KataoCartPeer::STATUS, array(KataoCart::STATUS_IN_PROGRESS, KataoCart::STATUS_VALIDATED), Criteria::IN);
  58. $criteria->add(KataoCartPeer::KATAO_PERIOD_ID, $katao_period_id);
  59. $criteria->add(KataoCartPeer::KATAO_NODE_ID, $katao_node_id);
  60. $criteria->add(KataoCartProductPeer::KATAO_PRODUCT_ID, $this->getId());
  61. $criteria->addGroupByColumn(KataoCartProductPeer::KATAO_PRODUCT_ID);
  62. $rs = KataoProductPeer::doSelectRS($criteria);
  63. if ($rs->next()) {
  64. $return = number_format($rs->getFloat(2), 3);
  65. }
  66. return $return;
  67. }
  68. /**
  69. * KataoProduct::getUrlDetails()
  70. *
  71. * @param string $katao_product_category
  72. * @param string $katao_product_family
  73. * @return string
  74. */
  75. public function getUrlDetails($katao_product_category = '', $katao_product_family = '') {
  76. if ('' == $katao_product_category || '' == $katao_product_family) {
  77. $criteria = new Criteria();
  78. $criteria->addSelectColumn(KataoProductFamilyPeer::URL_IDENTIFIER);
  79. $criteria->addSelectColumn(KataoProductCategoryPeer::URL_IDENTIFIER);
  80. $criteria->addJoin(KataoProductPeer::KATAO_PRODUCT_FAMILY_ID, KataoProductFamilyPeer::ID);
  81. $criteria->addJoin(KataoProductFamilyPeer::KATAO_PRODUCT_CATEGORY_ID, KataoProductCategoryPeer::ID);
  82. $criteria->add(KataoProductPeer::URL_IDENTIFIER, $this->getUrlIdentifier());
  83. $criteria->setLimit(1);
  84. $rs = KataoProductPeer::doSelectRS($criteria);
  85. if ($rs->next()) {
  86. $katao_product_category = $rs->getString(2);
  87. $katao_product_family = $rs->getString(1);
  88. }
  89. }
  90. return url_for('@katao_boutique_produit?category=' . $katao_product_category . '&family=' . $katao_product_family . '&product=' . $this->getUrlIdentifier());
  91. }
  92. public function getCategoryLink($with_link = false) {
  93. $return = '';
  94. $criteria = new Criteria();
  95. $criteria->addSelectColumn(KataoProductCategoryPeer::ID);
  96. $criteria->addSelectColumn(KataoProductCategoryPeer::NAME);
  97. $criteria->addJoin(KataoProductPeer::KATAO_PRODUCT_FAMILY_ID, KataoProductFamilyPeer::ID);
  98. $criteria->addJoin(KataoProductFamilyPeer::KATAO_PRODUCT_CATEGORY_ID, KataoProductCategoryPeer::ID);
  99. $criteria->add(KataoProductPeer::ID, $this->getId());
  100. $criteria->setLimit(1);
  101. $rs = KataoProductCategoryPeer::doSelectRS($criteria);
  102. if ($rs->next()) {
  103. if ($with_link) {
  104. $return = link_to($rs->getString(2), '@katao_product_category_show?id=' . $rs->getInt(1));
  105. } else {
  106. $return = $rs->getString(2);
  107. }
  108. }
  109. return $return;
  110. }
  111. public function getFamilyLink($with_link = false) {
  112. $return = '';
  113. $criteria = new Criteria();
  114. $criteria->addSelectColumn(KataoProductFamilyPeer::ID);
  115. $criteria->addSelectColumn(KataoProductFamilyPeer::NAME);
  116. $criteria->addJoin(KataoProductPeer::KATAO_PRODUCT_FAMILY_ID, KataoProductFamilyPeer::ID);
  117. $criteria->add(KataoProductPeer::ID, $this->getId());
  118. $criteria->setLimit(1);
  119. $rs = KataoProductFamilyPeer::doSelectRS($criteria);
  120. if ($rs->next()) {
  121. if ($with_link) {
  122. $return = link_to($rs->getString(2), '@katao_product_family_show?id=' . $rs->getInt(1));
  123. } else {
  124. $return = $rs->getString(2);
  125. }
  126. }
  127. return $return;
  128. }
  129. public function getSupplierName() {
  130. $result = '';
  131. if (null != $katao_supplier = $this->getKataoSupplier()) {
  132. $result = $katao_supplier->getName();
  133. }
  134. return $result;
  135. }
  136. public function getSupplierLink($with_link = false) {
  137. $result = '';
  138. if (null != $katao_supplier = $this->getKataoSupplier()) {
  139. if ($with_link) {
  140. $result = link_to($katao_supplier->getName(), '@katao_supplier_show?id=' . $katao_supplier->getId());
  141. } else {
  142. $result = link_to($katao_supplier->getName(), sprintf('%sles-partenaires/%s', sfConfig::get('app_katao_frontend_url'), $katao_supplier->getUrlIdentifier()), array('target' => '_blank'));
  143. }
  144. }
  145. return $result;
  146. }
  147. public function getOrderDetails($katao_period_id, $katao_node_id, $with_adjusted_quantity = false) {
  148. $completed_packages = $this->getCompletedPackages($katao_period_id, $katao_node_id, $with_adjusted_quantity);
  149. $last_incomplete_package = $this->getLastIncompletePackageQuantity($katao_period_id, $katao_node_id, $with_adjusted_quantity);
  150. $format_quantity = $this->getAuthorizeDecimal()?'%.3f':'%d';
  151. return sprintf('%d ('.$format_quantity.' / %d)%s', $completed_packages, $last_incomplete_package, $this->getMinOrderNumber(), ($completed_packages || $last_incomplete_package)?sprintf(' %s', image_tag($last_incomplete_package?'actions/bullet_red.png':'actions/bullet_green.png', array('alt' => $last_incomplete_package?'Colis incomplet':'Colis complet', 'title' => $last_incomplete_package?'Colis incomplet':'Colis complet', 'align' => 'top'))):'');
  152. }
  153. public function getUnitPriceEuroStr() {
  154. return Utils::formatCurrencyEuro($this->getUnitPriceEuro());
  155. }
  156. public function getIsArchivedStr() {
  157. return $this->getIsArchived()?'Oui':'Non';
  158. }
  159. public function getAuthorizeDecimalStr() {
  160. return $this->getAuthorizeDecimal()?'Oui':'Non';
  161. }
  162. public function getRealPicture() {
  163. return $this->hasPicture()?$this->getPicture():(wpConfig::hasDefaultProductPicture()?wpConfig::getDefaultProductPicture():'img04.gif');
  164. }
  165. public function hasPicture() {
  166. return '' != $this->getPicture() && is_file(sfConfig::get('sf_web_dir') . $this->getPicture());
  167. }
  168. public function getTotalPriceWithTaxesStr() {
  169. return Utils::formatCurrencyEuro($this->getTotalPriceWithTaxes());
  170. }
  171. public function getMarginStr() {
  172. return sprintf('%s%%', 100 * $this->getMargin());
  173. }
  174. public function getTvaRateStr() {
  175. return sprintf('%s%%', 100 * $this->getTvaRate());
  176. }
  177. public function notifyProductPriceModification() {
  178. return true;
  179. // désactivation temporaire suite au ticket #001525 (supprimer l'envois des modifs de prix au clients)
  180. $criteria = new Criteria();
  181. $criteria->add(KataoCartProductPeer::KATAO_PRODUCT_ID, $this->getId());
  182. $criteria->add(KataoCartPeer::STATUS, array(KataoCart::STATUS_IN_PROGRESS, KataoCart::STATUS_VALIDATED), Criteria::IN);
  183. $criteria->addJoin(KataoCartProductPeer::KATAO_CART_ID, KataoCartPeer::ID);
  184. $criteria->setDistinct();
  185. $katao_carts = KataoCartPeer::doSelectJoinKataoUser($criteria);
  186. if (0 < count($katao_carts)) {
  187. // referer
  188. $katao_user_referer = null;
  189. if (null != $katao_member_referer = $this->getKataoSupplier()->getKataoMemberRelatedByRefererId()) {
  190. $katao_user_referer = $katao_member_referer->getFirstKataoUser();
  191. }
  192. // seo
  193. $katao_user_seo = null;
  194. if (null != $katao_member_seo = wpConfig::getInstance()->getKataoMember()) {
  195. $katao_user_seo = $katao_member_seo->getFirstKataoUser();
  196. }
  197. $mailer = new wpMail();
  198. foreach ($katao_carts as/*(KataoCart)*/ $katao_cart) {
  199. try {
  200. // member
  201. $katao_user = $katao_cart->getKataoUser();
  202. $katao_member = $katao_user->getKataoMember();
  203. $mailer->send(new Swift_Message('KATAO : modification d\'un produit de votre panier', sprintf('Bonjour %s,
  204. Le produit "%s" (réf. %s, prix unit. %s) vient d\'être modifié.
  205. Votre panier a automatiquement été invalidé.
  206. Cliquez sur le lien ci-dessous pour accèder à votre panier :
  207. %smon-panier
  208. A bientôt,
  209. L\'équipe KATAO', $katao_user->getFullName(), $this->getName(), $this->getReference(), Utils::formatCurrencyEuro($this->getTotalPriceWithTaxes()), sfConfig::get('app_katao_frontend_url'))), new Swift_Address($katao_user->getEmail(), $katao_user->getFullName()), sfConfig::get('app_mail_from'));
  210. $recipients = new Swift_RecipientList();
  211. if (!is_null($katao_user_referer)) {
  212. $recipients->addTo($katao_user_referer->getEmail(), $katao_user_referer->getFullName());
  213. }
  214. if (!is_null($katao_user_seo)) {
  215. $recipients->addTo($katao_user_seo->getEmail(), $katao_user_seo->getFullName());
  216. }
  217. // delegate
  218. if (null != $katao_user_delegate = $katao_member->getKataoNode()->getKataoUser()) {
  219. $recipients->addTo($katao_user_delegate->getEmail(), $katao_user_delegate->getFullName());
  220. }
  221. $mailer->send(new Swift_Message(sprintf('KATAO : modification d\'un produit du panier de %s', $katao_user->getFullName()), sprintf('Bonjour,
  222. Le produit "%s" (réf. %s, prix unit. %s) vient d\'être modifié.
  223. Le panier de cet adhérent a automatiquement été invalidé.
  224. Cliquez sur le lien ci-dessous pour visualiser les demandes :
  225. %sgestion-des-demandes
  226. A bientôt,
  227. L\'équipe KATAO', $this->getName(), $this->getReference(), Utils::formatCurrencyEuro($this->getTotalPriceWithTaxes()), sfConfig::get('app_katao_backend_url'))), $recipients, sfConfig::get('app_mail_from'));
  228. // unvalidate cart
  229. $katao_cart->setStatus(KataoCart::STATUS_IN_PROGRESS);
  230. $katao_cart->save();
  231. }
  232. catch (Exception $e) {
  233. $mailer->disconnect();
  234. }
  235. }
  236. $mailer->disconnect();
  237. }
  238. }
  239. public function getMaxSolAmountStr() {
  240. return Utils::formatCurrencySol($this->getMaxSolAmount());
  241. }
  242. public function isDeletable() {
  243. return (0 == $this->countKataoCartProducts()) && (0 == $this->countKataoOrderProducts()) && (0 == $this->countKataoInvoiceProducts());
  244. }
  245. /**
  246. * KataoProduct::getTotalReceivedQuantity()
  247. *
  248. * @param int $katao_period_id
  249. * @param int $katao_node_id
  250. * @return float
  251. */
  252. public function getTotalReceivedQuantity($katao_period_id, $katao_node_id) {
  253. $return = 0;
  254. $criteria = new Criteria();
  255. $criteria->addSelectColumn(KataoOrderProductPeer::QUANTITY_RECEIVED);
  256. $criteria->addJoin(KataoOrderProductPeer::KATAO_ORDER_ID, KataoOrderPeer::ID);
  257. $criteria->add(KataoOrderProductPeer::KATAO_PRODUCT_ID, $this->getId());
  258. $criteria->add(KataoOrderPeer::KATAO_PERIOD_ID, $katao_period_id);
  259. $criteria->add(KataoOrderPeer::KATAO_NODE_ID, $katao_node_id);
  260. $rs = KataoOrderProductPeer::doSelectRS($criteria);
  261. if ($rs->next()) {
  262. $return = number_format($rs->getFloat(1), 3);
  263. }
  264. return $return;
  265. }
  266. /**
  267. * KataoProduct::getTotalDeliveredQuantity()
  268. *
  269. * @param int $katao_period_id
  270. * @param int $katao_node_id
  271. * @return float
  272. */
  273. public function getTotalDeliveredQuantity($katao_invoice_id = false, $katao_period_id, $katao_node_id) {
  274. $return = 0;
  275. $criteria = new Criteria();
  276. $criteria->addSelectColumn(KataoInvoiceProductPeer::QUANTITY_DELIVERED);
  277. $criteria->addJoin(KataoInvoiceProductPeer::KATAO_INVOICE_ID, KataoInvoicePeer::ID);
  278. $criteria->add(KataoInvoiceProductPeer::KATAO_PRODUCT_ID, $this->getId());
  279. if ($katao_invoice_id) {
  280. $criteria->add(KataoInvoicePeer::ID, $katao_invoice_id, Criteria::NOT_EQUAL);
  281. }
  282. $criteria->add(KataoInvoicePeer::KATAO_PERIOD_ID, $katao_period_id);
  283. $criteria->add(KataoInvoicePeer::KATAO_NODE_ID, $katao_node_id);
  284. $rs = KataoInvoiceProductPeer::doSelectRS($criteria);
  285. while ($rs->next()) {
  286. $return += number_format($rs->getFloat(1), 3);
  287. }
  288. return $return;
  289. }
  290. public function getUnitPriceEuroCalculated() {
  291. $total_ttc = $this->getTotalPriceWithTaxes();
  292. return $total_ttc / (1 + $this->getMargin()) / (1 + $this->getTvaRate());
  293. }
  294. }