KataoProduct.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 = $rs->getInt(2);
  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. return sprintf('%d (%d / %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', 'align' => 'top'))):'');
  151. }
  152. public function getUnitPriceEuroStr() {
  153. return Utils::formatCurrencyEuro($this->getUnitPriceEuro());
  154. }
  155. public function getIsArchivedStr() {
  156. return $this->getIsArchived()?'Oui':'Non';
  157. }
  158. public function getRealPicture() {
  159. return $this->hasPicture()?$this->getPicture():(wpConfig::hasDefaultProductPicture()?wpConfig::getDefaultProductPicture():'img04.gif');
  160. }
  161. public function hasPicture() {
  162. return '' != $this->getPicture() && is_file(sfConfig::get('sf_web_dir') . $this->getPicture());
  163. }
  164. public function getTotalPriceWithTaxesStr() {
  165. return Utils::formatCurrencyEuro($this->getTotalPriceWithTaxes());
  166. }
  167. public function getMarginStr() {
  168. return sprintf('%s%%', 100 * $this->getMargin());
  169. }
  170. public function getTvaRateStr() {
  171. return sprintf('%s%%', 100 * $this->getTvaRate());
  172. }
  173. public function notifyProductPriceModification() {
  174. return true;
  175. // désactivation temporaire suite au ticket #001525 (supprimer l'envois des modifs de prix au clients)
  176. $criteria = new Criteria();
  177. $criteria->add(KataoCartProductPeer::KATAO_PRODUCT_ID, $this->getId());
  178. $criteria->add(KataoCartPeer::STATUS, array(KataoCart::STATUS_IN_PROGRESS, KataoCart::STATUS_VALIDATED), Criteria::IN);
  179. $criteria->addJoin(KataoCartProductPeer::KATAO_CART_ID, KataoCartPeer::ID);
  180. $criteria->setDistinct();
  181. $katao_carts = KataoCartPeer::doSelectJoinKataoUser($criteria);
  182. if (0 < count($katao_carts)) {
  183. // referer
  184. $katao_user_referer = null;
  185. if (null != $katao_member_referer = $this->getKataoSupplier()->getKataoMemberRelatedByRefererId()) {
  186. $katao_user_referer = $katao_member_referer->getFirstKataoUser();
  187. }
  188. // seo
  189. $katao_user_seo = null;
  190. if (null != $katao_member_seo = wpConfig::getInstance()->getKataoMember()) {
  191. $katao_user_seo = $katao_member_seo->getFirstKataoUser();
  192. }
  193. $mailer = new wpMail();
  194. foreach ($katao_carts as/*(KataoCart)*/ $katao_cart) {
  195. try {
  196. // member
  197. $katao_user = $katao_cart->getKataoUser();
  198. $katao_member = $katao_user->getKataoMember();
  199. $mailer->send(new Swift_Message('KATAO : modification d\'un produit de votre panier', sprintf('Bonjour %s,
  200. Le produit "%s" (réf. %s, prix unit. %s) vient d\'être modifié.
  201. Votre panier a automatiquement été invalidé.
  202. Cliquez sur le lien ci-dessous pour accèder à votre panier :
  203. %smon-panier
  204. A bientôt,
  205. 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'));
  206. $recipients = new Swift_RecipientList();
  207. if (!is_null($katao_user_referer)) {
  208. $recipients->addTo($katao_user_referer->getEmail(), $katao_user_referer->getFullName());
  209. }
  210. if (!is_null($katao_user_seo)) {
  211. $recipients->addTo($katao_user_seo->getEmail(), $katao_user_seo->getFullName());
  212. }
  213. // delegate
  214. if (null != $katao_user_delegate = $katao_member->getKataoNode()->getKataoUser()) {
  215. $recipients->addTo($katao_user_delegate->getEmail(), $katao_user_delegate->getFullName());
  216. }
  217. $mailer->send(new Swift_Message(sprintf('KATAO : modification d\'un produit du panier de %s', $katao_user->getFullName()), sprintf('Bonjour,
  218. Le produit "%s" (réf. %s, prix unit. %s) vient d\'être modifié.
  219. Le panier de cet adhérent a automatiquement été invalidé.
  220. Cliquez sur le lien ci-dessous pour visualiser les demandes :
  221. %sgestion-des-demandes
  222. A bientôt,
  223. L\'équipe KATAO', $this->getName(), $this->getReference(), Utils::formatCurrencyEuro($this->getTotalPriceWithTaxes()), sfConfig::get('app_katao_backend_url'))), $recipients, sfConfig::get('app_mail_from'));
  224. // unvalidate cart
  225. $katao_cart->setStatus(KataoCart::STATUS_IN_PROGRESS);
  226. $katao_cart->save();
  227. }
  228. catch (Exception $e) {
  229. $mailer->disconnect();
  230. }
  231. }
  232. $mailer->disconnect();
  233. }
  234. }
  235. public function getMaxSolAmountStr() {
  236. return Utils::formatCurrencySol($this->getMaxSolAmount());
  237. }
  238. public function isDeletable() {
  239. return (0 == $this->countKataoCartProducts()) && (0 == $this->countKataoOrderProducts()) && (0 == $this->countKataoInvoiceProducts());
  240. }
  241. /**
  242. * KataoProduct::getTotalReceivedQuantity()
  243. *
  244. * @param int $katao_period_id
  245. * @param int $katao_node_id
  246. * @return int
  247. */
  248. public function getTotalReceivedQuantity($katao_period_id, $katao_node_id) {
  249. $return = 0;
  250. $criteria = new Criteria();
  251. $criteria->addSelectColumn(KataoOrderProductPeer::QUANTITY_RECEIVED);
  252. $criteria->addJoin(KataoOrderProductPeer::KATAO_ORDER_ID, KataoOrderPeer::ID);
  253. $criteria->add(KataoOrderProductPeer::KATAO_PRODUCT_ID, $this->getId());
  254. $criteria->add(KataoOrderPeer::KATAO_PERIOD_ID, $katao_period_id);
  255. $criteria->add(KataoOrderPeer::KATAO_NODE_ID, $katao_node_id);
  256. $rs = KataoOrderProductPeer::doSelectRS($criteria);
  257. if ($rs->next()) {
  258. $return = $rs->getInt(1);
  259. }
  260. return $return;
  261. }
  262. /**
  263. * KataoProduct::getTotalDeliveredQuantity()
  264. *
  265. * @param int $katao_period_id
  266. * @param int $katao_node_id
  267. * @return int
  268. */
  269. public function getTotalDeliveredQuantity($katao_invoice_id = false, $katao_period_id, $katao_node_id) {
  270. $return = 0;
  271. $criteria = new Criteria();
  272. $criteria->addSelectColumn(KataoInvoiceProductPeer::QUANTITY_DELIVERED);
  273. $criteria->addJoin(KataoInvoiceProductPeer::KATAO_INVOICE_ID, KataoInvoicePeer::ID);
  274. $criteria->add(KataoInvoiceProductPeer::KATAO_PRODUCT_ID, $this->getId());
  275. if ($katao_invoice_id) {
  276. $criteria->add(KataoInvoicePeer::ID, $katao_invoice_id, Criteria::NOT_EQUAL);
  277. }
  278. $criteria->add(KataoInvoicePeer::KATAO_PERIOD_ID, $katao_period_id);
  279. $criteria->add(KataoInvoicePeer::KATAO_NODE_ID, $katao_node_id);
  280. $rs = KataoInvoiceProductPeer::doSelectRS($criteria);
  281. while ($rs->next()) {
  282. $return += $rs->getInt(1);
  283. }
  284. return $return;
  285. }
  286. public function getUnitPriceEuroCalculated() {
  287. $total_ttc = $this->getTotalPriceWithTaxes();
  288. return $total_ttc / (1 + $this->getMargin()) / (1 + $this->getTvaRate());
  289. }
  290. }