Guest User

Untitled

a guest
Jun 4th, 2018
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.77 KB | None | 0 0
  1. <?php
  2. // +---------------------------------------------------------------------------+
  3. // | This file is part of the Riobel project. |
  4. // | Copyright (C) Lemieux Bedard communications |
  5. // | |
  6. // | For the full copyright and license information, please view the LICENSE |
  7. // | file that was distributed with this source code. |
  8. // +---------------------------------------------------------------------------+
  9.  
  10. /**
  11. * RiobelRetailerModel handle the retailers.
  12. * @package core
  13. * @subpackage model
  14. * @author Jean-Philippe Dery (jean-philippe.dery@hotmail.com)
  15. * @copyright Lemieux Bedard communications (info@lemieuxbedard.com)
  16. * @since 1.0.0
  17. * @version 1.0.0
  18. */
  19. class RiobelRetailerModel extends RiobelBaseModel implements AgaviISingletonModel
  20. {
  21. /**
  22. * Find all the retailer provinces.
  23. * @param string The order.
  24. * @param int The limit.
  25. * @param int The offset.
  26. * @return object The retailer province.
  27. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  28. * @since 1.0.0
  29. */
  30. public function findAllRetailerProvinces($order = null, $limit = null, $offset = null)
  31. {
  32. $doctrineQuery = Doctrine_Query::create();
  33. $doctrineQuery->from("RiobelRetailerProvince rp");
  34. if ($order) $doctrineQuery->orderby($order);
  35. if ($limit) $doctrineQuery->limit($limit);
  36. if ($offset) $doctrineQuery->offset($offset);
  37. return $doctrineQuery->execute();
  38. }
  39.  
  40. /**
  41. * Find all the retailer cities.
  42. * @param string The order.
  43. * @param int The limit.
  44. * @param int The offset.
  45. * @return object The retailer cities.
  46. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  47. * @since 1.0.0
  48. */
  49. public function findAllRetailerCities($order = null, $limit = null, $offset = null)
  50. {
  51. $doctrineQuery = Doctrine_Query::create();
  52. $doctrineQuery->from("RiobelRetailerCity rc");
  53. $doctrineQuery->where("EXISTS (SELECT * FROM RiobelRetailer r WHERE r.city_id = rc.id)");
  54. if ($order) $doctrineQuery->orderby($order);
  55. if ($limit) $doctrineQuery->limit($limit);
  56. if ($offset) $doctrineQuery->offset($offset);
  57. return $doctrineQuery->execute();
  58. }
  59.  
  60. /**
  61. * Find the
  62. * @param int The province.
  63. * @param string The order.
  64. * @param int The limit.
  65. * @param int The offset.
  66. * @return object The retailer cities.
  67. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  68. * @since 1.0.0
  69. */
  70. public function findRetailerProvinces($order = null, $limit = null, $offset = null)
  71. {
  72. // TODO: Make sure this uses only the province where we have a retailer
  73. $doctrineQuery = Doctrine_Query::create();
  74. $doctrineQuery->from("RiobelRetailerProvince rp");
  75. if ($order) $doctrineQuery->orderby($order);
  76. if ($limit) $doctrineQuery->limit($limit);
  77. if ($offset) $doctrineQuery->offset($offset);
  78. return $doctrineQuery->execute();
  79. }
  80.  
  81. /**
  82. * Find all the retailer cities in a given province.
  83. * @param int The province.
  84. * @param string The order.
  85. * @param int The limit.
  86. * @param int The offset.
  87. * @return object The retailer cities.
  88. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  89. * @since 1.0.0
  90. */
  91. public function findRetailerCitiesByProvince($provinceId, $order = null, $limit = null, $offset = null)
  92. {
  93. $doctrineQuery = Doctrine_Query::create();
  94. $doctrineQuery->from("RiobelRetailerCity rc");
  95. $doctrineQuery->addWhere("rc.province_id = ?", array($provinceId));
  96. $doctrineQuery->addWhere("EXISTS (SELECT * FROM RiobelRetailer r WHERE r.city_id = rc.id)");
  97. if ($order) $doctrineQuery->orderby($order);
  98. if ($limit) $doctrineQuery->limit($limit);
  99. if ($offset) $doctrineQuery->offset($offset);
  100. return $doctrineQuery->execute();
  101. }
  102.  
  103. /**
  104. * Find all the retailers from a city in a province.
  105. * @param int The province.
  106. * @param int The city.
  107. * @param string The order.
  108. * @param int The limit.
  109. * @param int The offset.
  110. * @return object The retailer cities.
  111. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  112. * @since 1.0.0
  113. */
  114. public function findRetailersByProvinceAndCity($provinceId, $cityId, $order = null, $limit = null, $offset = null)
  115. {
  116. $doctrineQuery = Doctrine_Query::create();
  117. $doctrineQuery->from("RiobelRetailer r");
  118. $doctrineQuery->where("r.province_id = ? AND r.city_id = ?");
  119. if ($order) $doctrineQuery->orderby($order);
  120. if ($limit) $doctrineQuery->limit($limit);
  121. if ($offset) $doctrineQuery->offset($offset);
  122. return $doctrineQuery->execute(array($provinceId, $cityId));
  123. }
  124.  
  125. /**
  126. * Create a new retailer.
  127. * @param int The province id.
  128. * @param int The city id.
  129. * @param array The retailer data.
  130. * @param string The original id.
  131. * @return void
  132. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  133. * @since 1.0.0
  134. */
  135. public function createRetailer($provinceId, $cityId, array $attributes, $originalId = '')
  136. {
  137. $retailer = new RiobelRetailer();
  138. $retailer->fromArray($attributes);
  139. $retailer['original_id'] = $originalId;
  140. $retailer['province_id'] = $provinceId;
  141. $retailer['city_id'] = $cityId;
  142. $retailer->save();
  143. return $retailer;
  144. }
  145.  
  146. /**
  147. * Create a new retailer province.
  148. * @param array The attributes.
  149. * @param string The original id.
  150. * @param object The connection to the previous database.
  151. * @return void
  152. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  153. * @since 1.0.0
  154. */
  155. public function createRetailerProvince(array $attributes, $originalId = '')
  156. {
  157. $retailerProvince = new RiobelRetailerProvince();
  158. $retailerProvince->fromArray($attributes);
  159. $retailerProvince['original_id'] = $originalId;
  160. $retailerProvince->save();
  161. return $retailerProvince;
  162. }
  163.  
  164. /**
  165. * Create a new retailer city.
  166. * @param int The province id.
  167. * @param array The attributes.
  168. * @param string The original id.
  169. * @param object The connection to the previous database.
  170. * @return void
  171. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  172. * @since 1.0.0
  173. */
  174. public function createRetailerCity($provinceId, array $attributes, $originalId = '')
  175. {
  176. $retailerCity = new RiobelRetailerCity();
  177. $retailerCity->fromArray($attributes);
  178. $retailerCity['original_id'] = $originalId;
  179. $retailerCity['province_id'] = $provinceId;
  180. $retailerCity->save();
  181. return $retailerCity;
  182. }
  183.  
  184. // ----------------------------------------------------------------------------
  185. // Original Record Finders
  186. // ----------------------------------------------------------------------------
  187.  
  188. /**
  189. * Find a retailer province by it's original id.
  190. * @param string The original id.
  191. * @return object The retailer province.
  192. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  193. * @since 1.0.0
  194. */
  195. public function findRetailerProvinceByOriginalId($originalId)
  196. {
  197. $doctrineQuery = Doctrine_Query::create();
  198. $doctrineQuery->from("RiobelRetailerProvince rp");
  199. $doctrineQuery->where("original_id = ?");
  200. return $doctrineQuery->fetchOne(array($originalId));
  201. }
  202.  
  203. /**
  204. * Find a retailer city by it's original id.
  205. * @param string The original id.
  206. * @return object The retailer city.
  207. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  208. * @since 1.0.0
  209. */
  210. public function findRetailerCityByOriginalId($originalId)
  211. {
  212. $doctrineQuery = Doctrine_Query::create();
  213. $doctrineQuery->from("RiobelRetailerCity rc");
  214. $doctrineQuery->where("original_id = ?");
  215. return $doctrineQuery->fetchOne(array($originalId));
  216. }
  217.  
  218. // ----------------------------------------------------------------------------
  219. // Importation Methods
  220. // ----------------------------------------------------------------------------
  221.  
  222. /**
  223. * Import all the retailers, retailer provinces, retailer cities from the
  224. * previous database.
  225. * @param object The connection to the previous database.
  226. * @return void
  227. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  228. * @since 1.0.0
  229. */
  230. public function import($conn)
  231. {
  232. $this->importRetailerProvinces($conn);
  233. $this->importRetailerCities($conn);
  234. $this->importRetailers($conn);
  235. }
  236.  
  237. /**
  238. * Import all retailer provinces from the previous database.
  239. * @param object The connection to the previous database.
  240. * @return void
  241. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  242. * @since 1.0.0
  243. */
  244. protected function importRetailerProvinces($conn)
  245. {
  246. Doctrine_Manager::connection()->getDbh()->query('TRUNCATE TABLE riobel_retailer_province')->execute();
  247.  
  248. $sql = "SELECT *
  249. FROM rioprovinces p
  250. WHERE ProvDescriptA <> '' AND
  251. ProvDescript <> '' AND (
  252. (SELECT COUNT(IDRioClientAdresse)
  253. FROM rioclientadresse ca
  254. WHERE ca.IDRioProvinces = p.IDRioProvinces) > 0
  255. )
  256. ORDER BY ProvDescript";
  257.  
  258. $rows = $conn->query($sql)->fetchall();
  259. foreach ($rows as $row) {
  260.  
  261. $originalId = $row['IDRioProvinces'];
  262. $nameEn = $row['ProvDescriptA'];
  263. $nameFr = $row['ProvDescript'];
  264. $code = $row['ProvABBR'];
  265.  
  266. $attributes = array(
  267. 'name_en' => $nameEn,
  268. 'name_fr' => $nameFr,
  269. 'code' => $code
  270. );
  271.  
  272. $this->createRetailerProvince($attributes, $originalId);
  273. }
  274. }
  275.  
  276. /**
  277. * Import all the retailer cities from the previous database.
  278. * @param object The connection to the previous database.
  279. * @return void
  280. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  281. * @since 1.0.0
  282. */
  283. protected function importRetailerCities($conn)
  284. {
  285. Doctrine_Manager::connection()->getDbh()->query('TRUNCATE TABLE riobel_retailer_city')->execute();
  286.  
  287. $sql = "SELECT DISTINCT(Ville), IDRioClientAdresse, IDRioProvinces
  288. FROM rioclientadresse
  289. GROUP BY Ville
  290. ORDER BY Ville";
  291.  
  292. $rows = $conn->query($sql)->fetchall();
  293. foreach ($rows as $row) {
  294.  
  295. $provinceId = $row['IDRioProvinces'];
  296. $originalId = $row['Ville'];
  297. $nameEn = $row['Ville'];
  298. $nameFr = $row['Ville'];
  299.  
  300. $province = $this->findRetailerProvinceByOriginalId($provinceId);
  301. if ($province == null) {
  302. continue;
  303. }
  304.  
  305. $attributes = array(
  306. 'name_en' => $nameEn,
  307. 'name_fr' => $nameFr
  308. );
  309.  
  310. $this->createRetailerCity($province['id'], $attributes, $originalId);
  311. }
  312. }
  313.  
  314. /**
  315. * Import all the retailers from the previous database.
  316. * @param object The connection to the previous database.
  317. * @return void
  318. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  319. * @since 1.0.0
  320. */
  321. protected function importRetailers($conn)
  322. {
  323. Doctrine_Manager::connection()->getDbh()->query('TRUNCATE TABLE riobel_retailer')->execute();
  324.  
  325. $sql = "SELECT *
  326. FROM rioclientadresse ca
  327. INNER JOIN rioclient c
  328. ON ca.IDRioClient = c.IDRioClient
  329. WHERE cliBInactif = 0";
  330.  
  331. $rows = $conn->query($sql)->fetchall();
  332. foreach ($rows as $row) {
  333.  
  334. $originalId = $row['IDRioClientAdresse'];
  335. $provinceId = $row['IDRioProvinces'];
  336. $cityId = $row['Ville'];
  337. $name = $row['NomDeLaLocalisation'];
  338. $address = $row['Adresse1'];
  339. $phone = $row['cliTelephone'];
  340. $extension = $row['extension'];
  341. $fax = $row['Fax'];
  342.  
  343. $province = $this->findRetailerProvinceByOriginalId($provinceId);
  344. if ($province == null) {
  345. continue;
  346. }
  347.  
  348. $city = $this->findRetailerCityByOriginalId($cityId);
  349. if ($city == null) {
  350. continue;
  351. }
  352.  
  353. $attributes = array(
  354. 'name' => $name,
  355. 'address' => $address,
  356. 'phone' => $phone,
  357. 'extension' => $extension,
  358. 'fax' => $fax
  359. );
  360.  
  361. $this->createRetailer($province['id'], $city['id'], $attributes, $originalId);
  362. }
  363. }
  364. }
Add Comment
Please, Sign In to add comment