KataoPeriod.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. class KataoPeriod extends BaseKataoPeriod {
  3. const STATUS_DRAFT = 1;
  4. const STATUS_ACTIVE = 2;
  5. const STATUS_ARCHIVED = 3;
  6. public function __toString() {
  7. return $this->getName();
  8. }
  9. public function getBeginAtStr() {
  10. return CatalyzDate::formatShort($this->getBeginAt(null));
  11. }
  12. public function getFinishAtStr() {
  13. return CatalyzDate::formatShort($this->getFinishAt(null));
  14. }
  15. public function getOrderEndedAtStr() {
  16. return CatalyzDate::formatShort($this->getOrderEndedAt(null));
  17. }
  18. public function getStatusStr() {
  19. return KataoPeriodPeer::getStatusStr($this->getStatus());
  20. }
  21. public function updateAvailableProducts($serialized_suppliers_data = '', $product_ids = array(), $con = null) {
  22. $criteria = new Criteria();
  23. $criteria->add(KataoPeriodSupplierPeer::KATAO_PERIOD_ID, $this->getId());
  24. KataoPeriodSupplierPeer::doDelete($criteria, $con);
  25. $suppliers_data = Utils::parseOrderedData($serialized_suppliers_data);
  26. if (!empty($suppliers_data['middle_col'])) {
  27. foreach ($suppliers_data['middle_col'] as $supplier_id) {
  28. $katao_period_supplier = new KataoPeriodSupplier();
  29. $katao_period_supplier->setKataoPeriod($this);
  30. $katao_period_supplier->setKataoSupplierId(Utils::sanitizeSupplierId($supplier_id));
  31. $katao_period_supplier->setIncludeAllProducts(true);
  32. $katao_period_supplier->save($con);
  33. }
  34. }
  35. if (!empty($suppliers_data['right_col'])) {
  36. foreach ($suppliers_data['right_col'] as $supplier_id) {
  37. $katao_period_supplier = new KataoPeriodSupplier();
  38. $katao_period_supplier->setKataoPeriod($this);
  39. $katao_period_supplier->setKataoSupplierId(Utils::sanitizeSupplierId($supplier_id));
  40. $katao_period_supplier->setIncludeAllProducts(false);
  41. $katao_period_supplier->save($con);
  42. }
  43. }
  44. $criteria = new Criteria();
  45. $criteria->add(KataoPeriodProductPeer::KATAO_PERIOD_ID, $this->getId());
  46. KataoPeriodProductPeer::doDelete($criteria, $con);
  47. if (0 < count($product_ids)) {
  48. foreach ($product_ids as $product_id) {
  49. $katao_period_product = new KataoPeriodProduct();
  50. $katao_period_product->setKataoPeriod($this);
  51. $katao_period_product->setKataoProductId((int)$product_id);
  52. $katao_period_product->save($con);
  53. }
  54. }
  55. }
  56. public function activate() {
  57. try {
  58. $con = Propel::getConnection();
  59. $con->begin();
  60. /* archive previous active period */
  61. $criteria_update = new Criteria();
  62. $criteria_update->add(KataoPeriodPeer::STATUS, self::STATUS_ARCHIVED);
  63. $criteria_where = new Criteria();
  64. $criteria_where->add(KataoPeriodPeer::STATUS, self::STATUS_ACTIVE);
  65. BasePeer::doUpdate($criteria_where, $criteria_update, $con);
  66. /* activate current period */
  67. $this->setStatus(self::STATUS_ACTIVE);
  68. $this->save($con);
  69. $katao_period_suppliers = $this->getKataoPeriodSuppliers();
  70. $katao_period_products = $this->getKataoPeriodProducts();
  71. /* create available products for current period and for each node
  72. with global suppliers and local suppliers available for these nodes */
  73. foreach (KataoNodePeer::getAll() as/*(KataoNode)*/ $katao_node) {
  74. foreach ($katao_period_suppliers as/*(KataoPeriodSupplier)*/ $katao_period_supplier) {
  75. $katao_node_suppplier = new KataoNodeSupplier();
  76. $katao_node_suppplier->setKataoNode($katao_node);
  77. $katao_node_suppplier->setKataoPeriod($this);
  78. $katao_node_suppplier->setKataoSupplierId($katao_period_supplier->getKataoSupplierId());
  79. $katao_node_suppplier->setIncludeAllProducts($katao_period_supplier->getIncludeAllProducts());
  80. $katao_node_suppplier->save($con);
  81. }
  82. foreach ($katao_period_products as/*(KataoPeriodProduct)*/ $katao_period_product) {
  83. $katao_node_product = new KataoNodeProduct();
  84. $katao_node_product->setKataoNode($katao_node);
  85. $katao_node_product->setKataoPeriod($this);
  86. $katao_node_product->setKataoProductId($katao_period_product->getKataoProductId());
  87. $katao_node_product->save($con);
  88. }
  89. $criteria = new Criteria();
  90. $criteria->addJoin(KataoSupplierPeer::ID, KataoUserPeer::KATAO_SUPPLIER_ID);
  91. $criteria->add(KataoSupplierPeer::IS_GLOBAL, false);
  92. $criteria->add(KataoUserPeer::STATUS, KataoUser::STATUS_ACTIVE);
  93. foreach ($katao_node->getKataoSupplierNodesJoinKataoSupplier($criteria) as/*(KataoSupplierNode)*/ $katao_supplier_node) {
  94. $katao_node_suppplier = new KataoNodeSupplier();
  95. $katao_node_suppplier->setKataoNode($katao_node);
  96. $katao_node_suppplier->setKataoPeriod($this);
  97. $katao_node_suppplier->setKataoSupplierId($katao_supplier_node->getKataoSupplierId());
  98. $katao_node_suppplier->setIncludeAllProducts(true);
  99. $katao_node_suppplier->save($con);
  100. }
  101. }
  102. /* switch members with new nodes */
  103. $criteria = new Criteria();
  104. $criteria->add(KataoMemberPeer::NEXT_KATAO_NODE_ID, null, Criteria::NOT_EQUAL);
  105. foreach (KataoMemberPeer::doSelect($criteria) as/*(KataoMember)*/ $katao_member) {
  106. $katao_member_node = new KataoMemberNode();
  107. $katao_member_node->setKataoMember($katao_member);
  108. $katao_member_node->setKataoNodeId($katao_member->getKataoNodeId());
  109. $katao_member_node->save($con);
  110. $katao_member->setKataoNodeId($katao_member->getNextKataoNodeId());
  111. $katao_member->setNextKataoNodeId(null);
  112. $katao_member->save($con);
  113. }
  114. $con->commit();
  115. }
  116. catch (PropelException $e) {
  117. $con->rollback();
  118. throw $e;
  119. }
  120. }
  121. public function getKataoNodeIdsForSupplierOrders() {
  122. $return = $this->initKataoNodeIdsForOrdersAndInvoices();
  123. $criteria = new Criteria();
  124. $criteria->addSelectColumn(KataoOrderPeer::KATAO_NODE_ID);
  125. $criteria->add(KataoOrderPeer::KATAO_PERIOD_ID, $this->getId());
  126. $criteria->add(KataoOrderPeer::STATUS, array(KataoOrder::STATUS_GENERATED, KataoOrder::STATUS_ADJUSTED), Criteria::NOT_IN);
  127. $criteria->setDistinct();
  128. $rs = KataoOrderPeer::doSelectRS($criteria);
  129. while ($rs->next()) {
  130. unset($return[$rs->getInt(1)]['available']);
  131. }
  132. return $return;
  133. }
  134. public function getKataoNodeIdsForMemberInvoices() {
  135. $return = $this->initKataoNodeIdsForOrdersAndInvoices();
  136. $criteria = new Criteria();
  137. $criteria->addSelectColumn(KataoInvoicePeer::KATAO_NODE_ID);
  138. $criteria->add(KataoInvoicePeer::KATAO_PERIOD_ID, $this->getId());
  139. $criteria->setDistinct();
  140. $rs = KataoInvoicePeer::doSelectRS($criteria);
  141. while ($rs->next()) {
  142. unset($return[$rs->getInt(1)]['available']);
  143. }
  144. return $return;
  145. }
  146. protected function initKataoNodeIdsForOrdersAndInvoices() {
  147. $return = array();
  148. $criteria = new Criteria();
  149. $criteria->addSelectColumn(KataoNodePeer::ID);
  150. $criteria->addSelectColumn(KataoNodePeer::CITY);
  151. $criteria->addJoin(KataoNodePeer::ID, KataoCartPeer::KATAO_NODE_ID);
  152. $criteria->add(KataoCartPeer::KATAO_PERIOD_ID, $this->getId());
  153. $criteria->setDistinct();
  154. $rs = KataoNodePeer::doSelectRS($criteria);
  155. while ($rs->next()) {
  156. $return[$rs->getInt(1)] = array('city' => $rs->getString(2), 'available' => true);
  157. }
  158. return $return;
  159. }
  160. public function isDeletable() {
  161. return (0 == $this->countKataoCarts()) && (0 == $this->countKataoOrders()) && (0 == $this->countKataoInvoices());
  162. }
  163. public function makePeriodCopy() {
  164. try {
  165. $con = Propel::getConnection();
  166. $con->begin();
  167. $added_year = 1;
  168. while (KataoPeriodPeer::hasConflictedPeriodDates(strtotime('+' . $added_year . ' year', $this->getBeginAt(null)), strtotime('+' . $added_year . ' year', $this->getFinishAt(null)))) {
  169. $added_year++;
  170. }
  171. $name = sprintf('%s (2)', $this->getName());
  172. if (false !== strpos($this->getName(), $this->getBeginAt('Y'))) {
  173. $name = str_replace($this->getBeginAt('Y'), (int)$this->getBeginAt('Y') + $added_year, $this->getName());
  174. }
  175. $this_copy = new KataoPeriod();
  176. $this_copy->setName($name);
  177. $this_copy->setBeginAt(strtotime('+' . $added_year . ' year', $this->getBeginAt(null)));
  178. $this_copy->setFinishAt(strtotime('+' . $added_year . ' year', $this->getFinishAt(null)));
  179. $this_copy->setOrderEndedAt(strtotime('+' . $added_year . ' year', $this->getOrderEndedAt(null)));
  180. $this_copy->setStatus(self::STATUS_DRAFT);
  181. $this_copy->setExported(false);
  182. $this_copy->save($con);
  183. foreach($this->getKataoPeriodProducts() as/*(KataoPeriodProduct)*/ $katao_period_product) {
  184. $katao_period_product_copy = new KataoPeriodProduct();
  185. $katao_period_product_copy->setKataoPeriod($this_copy);
  186. $katao_period_product_copy->setKataoProductId($katao_period_product->getKataoProductId());
  187. $katao_period_product_copy->save($con);
  188. }
  189. foreach($this->getKataoPeriodSuppliers() as/*(KataoPeriodSupplier)*/ $katao_period_supplier) {
  190. $katao_period_supplier_copy = new KataoPeriodSupplier();
  191. $katao_period_supplier_copy->setKataoPeriod($this_copy);
  192. $katao_period_supplier_copy->setKataoSupplierId($katao_period_supplier->getKataoSupplierId());
  193. $katao_period_supplier_copy->setIncludeAllProducts($katao_period_supplier->getIncludeAllProducts());
  194. $katao_period_supplier_copy->save($con);
  195. }
  196. $con->commit();
  197. return $this_copy->getId();
  198. }
  199. catch (PropelException $e) {
  200. $con->rollback();
  201. throw $e;
  202. }
  203. }
  204. }