PageRenderTime 27ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/Sales/Setup/Update/Release2.php

https://github.com/testruby/Tine-2.0-Open-Source-Groupware-and-CRM
PHP | 246 lines | 180 code | 20 blank | 46 comment | 3 complexity | 7bfa319895b05c5a1f9c1d6dc2101e2d MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0
  4. *
  5. * @package Sales
  6. * @license http://www.gnu.org/licenses/agpl.html AGPL3
  7. * @copyright Copyright (c) 2009-2010 Metaways Infosystems GmbH (http://www.metaways.de)
  8. * @author Philipp Schuele <p.schuele@metaways.de>
  9. */
  10. class Sales_Setup_Update_Release2 extends Setup_Update_Abstract
  11. {
  12. /**
  13. * renamed erp to sales
  14. *
  15. */
  16. public function update_0()
  17. {
  18. $this->renameTable('erp_numbers', 'sales_numbers');
  19. $this->renameTable('erp_contracts', 'sales_contracts');
  20. $this->setApplicationVersion('Sales', '2.1');
  21. }
  22. /**
  23. * - create sales product table
  24. * - copy products from metacrm_products to new table
  25. *
  26. */
  27. public function update_1()
  28. {
  29. $tableDefinition = '
  30. <table>
  31. <name>sales_products</name>
  32. <version>1</version>
  33. <declaration>
  34. <field>
  35. <name>id</name>
  36. <type>text</type>
  37. <length>40</length>
  38. <notnull>true</notnull>
  39. </field>
  40. <field>
  41. <name>name</name>
  42. <type>text</type>
  43. <length>255</length>
  44. <notnull>true</notnull>
  45. </field>
  46. <field>
  47. <name>description</name>
  48. <type>text</type>
  49. <notnull>false</notnull>
  50. </field>
  51. <field>
  52. <name>price</name>
  53. <type>float</type>
  54. <notnull>false</notnull>
  55. </field>
  56. <field>
  57. <name>created_by</name>
  58. <type>text</type>
  59. <length>40</length>
  60. </field>
  61. <field>
  62. <name>creation_time</name>
  63. <type>datetime</type>
  64. </field>
  65. <field>
  66. <name>last_modified_by</name>
  67. <type>text</type>
  68. <length>40</length>
  69. </field>
  70. <field>
  71. <name>last_modified_time</name>
  72. <type>datetime</type>
  73. </field>
  74. <field>
  75. <name>is_deleted</name>
  76. <type>boolean</type>
  77. <default>false</default>
  78. </field>
  79. <field>
  80. <name>deleted_by</name>
  81. <type>text</type>
  82. <length>40</length>
  83. </field>
  84. <field>
  85. <name>deleted_time</name>
  86. <type>datetime</type>
  87. </field>
  88. <index>
  89. <name>id</name>
  90. <primary>true</primary>
  91. <field>
  92. <name>id</name>
  93. </field>
  94. </index>
  95. </declaration>
  96. </table>
  97. ';
  98. $table = Setup_Backend_Schema_Table_Factory::factory('Xml', $tableDefinition);
  99. $this->_backend->createTable($table);
  100. Tinebase_Application::getInstance()->addApplicationTable(
  101. Tinebase_Application::getInstance()->getApplicationByName('Sales'),
  102. 'sales_products',
  103. 1
  104. );
  105. // check if crm is installed first
  106. if (Setup_Controller::getInstance()->isInstalled('Crm')) {
  107. // add products from crm
  108. $select = $this->_db->select()
  109. ->from(SQL_TABLE_PREFIX . 'metacrm_products');
  110. $stmt = $this->_db->query($select);
  111. $queryResult = $stmt->fetchAll();
  112. if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . print_r($queryResult, TRUE));
  113. // insert values into products table
  114. $productsBackend = new Tinebase_Backend_Sql(array(
  115. 'modelName' => 'Sales_Model_Product',
  116. 'tableName' => 'sales_products',
  117. ));
  118. foreach ($queryResult as $row) {
  119. $products = new Sales_Model_Product(array(
  120. 'id' => $row['id'],
  121. 'name' => $row['productsource'],
  122. 'price' => $row['price'],
  123. ));
  124. $productsBackend->create($products);
  125. }
  126. }
  127. $this->setApplicationVersion('Sales', '2.2');
  128. }
  129. /**
  130. * - use relations to save lead products
  131. * - remove old crm products tables
  132. *
  133. * @return void
  134. */
  135. public function update_2()
  136. {
  137. if (Setup_Controller::getInstance()->isInstalled('Crm')) {
  138. // get linked products
  139. $select = $this->_db->select()
  140. ->from(SQL_TABLE_PREFIX . 'metacrm_leads_products');
  141. $stmt = $this->_db->query($select);
  142. $queryResult = $stmt->fetchAll();
  143. //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . print_r($queryResult, TRUE));
  144. // insert values into relations table
  145. $relationsBackend = new Tinebase_Relation_Backend_Sql();
  146. foreach ($queryResult as $row) {
  147. $relation = new Tinebase_Model_Relation( array(
  148. 'own_model' => 'Crm_Model_Lead',
  149. 'own_backend' => 'Sql',
  150. 'own_id' => $row['lead_id'],
  151. 'own_degree' => Tinebase_Model_Relation::DEGREE_SIBLING,
  152. 'type' => 'PRODUCT',
  153. 'related_model' => 'Sales_Model_Product',
  154. 'related_backend' => 'Sql',
  155. 'related_id' => $row['product_id'],
  156. 'remark' => Zend_Json::encode(array(
  157. 'description' => $row['product_desc'],
  158. 'price' => $row['product_price'],
  159. 'quantity' => 1,
  160. ))
  161. ));
  162. try {
  163. $relationsBackend->addRelation($relation);
  164. } catch (Zend_Db_Statement_Exception $zdse) {
  165. Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
  166. . ' Found duplicate, increasing quantity (' . $zdse->getMessage() . ')');
  167. // increase quantity
  168. $updateRelation = $relationsBackend->search(new Tinebase_Model_RelationFilter(array(
  169. array('field' => 'own_id', 'operator' => 'equals', 'value' => $relation->own_id),
  170. array('field' => 'related_id', 'operator' => 'equals', 'value' => $relation->related_id),
  171. array('field' => 'related_model', 'operator' => 'equals', 'value' => 'Sales_Model_Product'),
  172. )))->getFirstRecord();
  173. $remark = $updateRelation->remark;
  174. $remark['quantity'] = $remark['quantity'] + 1;
  175. $updateRelation->remark = $remark;
  176. //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($updateRelation->toArray(), TRUE));
  177. $relationsBackend->updateRelation($updateRelation);
  178. }
  179. }
  180. // drop table metacrm_leadsproducts and metacrm_products
  181. $this->dropTable('metacrm_leads_products');
  182. $this->dropTable('metacrm_products');
  183. }
  184. $this->setApplicationVersion('Sales', '2.3');
  185. }
  186. /**
  187. * add manufacturer and category to products
  188. *
  189. * @return void
  190. */
  191. public function update_3()
  192. {
  193. $newFields = array('manufacturer', 'category');
  194. foreach ($newFields as $fieldName) {
  195. $field = '<field>
  196. <name>' . $fieldName . '</name>
  197. <type>text</type>
  198. <length>255</length>
  199. <notnull>false</notnull>
  200. </field>';
  201. $declaration = new Setup_Backend_Schema_Field_Xml($field);
  202. $this->_backend->addCol('sales_products', $declaration);
  203. }
  204. $this->setApplicationVersion('Sales', '2.4');
  205. $this->setTableVersion('sales_products', '2');
  206. }
  207. /**
  208. * erp was renamed to sales -> update models in relations
  209. *
  210. * @return void
  211. */
  212. public function update_4()
  213. {
  214. $this->_db->query("update " . SQL_TABLE_PREFIX . "relations set own_model='Sales_Model_Contract' where own_model='Erp_Model_Contract'");
  215. $this->_db->query("update " . SQL_TABLE_PREFIX . "relations set related_model='Sales_Model_Contract' where related_model='Erp_Model_Contract'");
  216. $this->setApplicationVersion('Sales', '2.5');
  217. }
  218. /**
  219. * update to 3.0
  220. * @return void
  221. */
  222. public function update_5()
  223. {
  224. $this->setApplicationVersion('Sales', '3.0');
  225. }
  226. }