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

/src/azora.local/domain/product.php

https://gitlab.com/nvtdn2006/azora
PHP | 413 lines | 318 code | 95 blank | 0 comment | 14 complexity | 6828560999fe0f2d0d1e062a52ae8963 MD5 | raw file
  1. <?php
  2. class Product
  3. {
  4. function getAllProductInfo() {
  5. $result = null;
  6. Loader::Load('Database');
  7. $dbh = Database::getInstance()->getConnection();
  8. $sth = $dbh->prepare('SELECT p.*, cu.username as created_username, mu.username as updated_username, pb.product_balance FROM products p
  9. LEFT JOIN users cu ON p.created_by = cu.user_id
  10. LEFT JOIN users mu ON p.updated_by = mu.user_id
  11. LEFT JOIN product_balance pb ON p.product_id = pb.product_id
  12. ');
  13. $sth->execute();
  14. $result = $sth->fetchAll(PDO::FETCH_ASSOC);
  15. $sth = null;
  16. $dbh = null;
  17. return $result;
  18. }
  19. function getActiveProducts() {
  20. $products = null;
  21. Loader::Load('Database');
  22. $dbh = Database::getInstance()->getConnection();
  23. $sth = $dbh->prepare('SELECT product_id FROM products WHERE product_status = 1 ORDER BY created_dt DESC');
  24. $sth->execute();
  25. $result = $sth->fetchAll(PDO::FETCH_ASSOC);
  26. $sth = null;
  27. $dbh = null;
  28. if (isset($result) && count($result) > 0) {
  29. $products = array();
  30. foreach($result as $handle) {
  31. array_push($products, $this->getProduct($handle['product_id']));
  32. }
  33. }
  34. return $products;
  35. }
  36. function getLatestProducts($limit = 10) {
  37. $products = null;
  38. Loader::Load('Database');
  39. $dbh = Database::getInstance()->getConnection();
  40. $sth = $dbh->prepare('SELECT product_id FROM products WHERE product_status = 1 ORDER BY created_dt DESC LIMIT 0, '.$limit);
  41. $sth->execute();
  42. $result = $sth->fetchAll(PDO::FETCH_ASSOC);
  43. $sth = null;
  44. $dbh = null;
  45. if (isset($result) && count($result) > 0) {
  46. $products = array();
  47. foreach($result as $handle) {
  48. array_push($products, $this->getProduct($handle['product_id']));
  49. }
  50. }
  51. return $products;
  52. }
  53. function getProductsByCategory($category_id) {
  54. $products = null;
  55. Loader::Load('Database');
  56. $dbh = Database::getInstance()->getConnection();
  57. $sth = $dbh->prepare('SELECT DISTINCT p.product_id FROM products p LEFT JOIN product_categories pc ON p.product_id = pc.product_id WHERE p.product_status = 1 AND pc.category_id = '. $category_id .' ORDER BY created_dt DESC');
  58. $sth->execute();
  59. $result = $sth->fetchAll(PDO::FETCH_ASSOC);
  60. $sth = null;
  61. $dbh = null;
  62. if (isset($result) && count($result) > 0) {
  63. $products = array();
  64. foreach($result as $handle) {
  65. array_push($products, $this->getProduct($handle['product_id']));
  66. }
  67. }
  68. return $products;
  69. }
  70. function getProduct($product_id) {
  71. $result = array();
  72. Loader::Load('Database');
  73. $dbh = Database::getInstance()->getConnection();
  74. $sth = $dbh->prepare('SELECT p.*, cu.username as created_username, mu.username as updated_username FROM products p
  75. LEFT JOIN users cu ON p.created_by = cu.user_id
  76. LEFT JOIN users mu ON p.updated_by = mu.user_id
  77. WHERE p.product_id = :product_id');
  78. $sth->bindParam(':product_id', $product_id);
  79. $sth->execute();
  80. $result['product'] = $sth->fetch(PDO::FETCH_ASSOC);
  81. $sth = $dbh->prepare('SELECT * FROM product_balance p
  82. WHERE p.product_id = :product_id');
  83. $sth->bindParam(':product_id', $product_id);
  84. $sth->execute();
  85. $result['product_balance'] = $sth->fetch(PDO::FETCH_ASSOC);
  86. $sth = $dbh->prepare('SELECT p.*, c.category_name FROM product_categories p
  87. INNER JOIN categories c ON p.category_id = c.category_id
  88. WHERE p.product_id = :product_id');
  89. $sth->bindParam(':product_id', $product_id);
  90. $sth->execute();
  91. $result['product_categories'] = $sth->fetchAll(PDO::FETCH_ASSOC);
  92. $sth = $dbh->prepare('SELECT * FROM product_images p
  93. WHERE p.product_id = :product_id');
  94. $sth->bindParam(':product_id', $product_id);
  95. $sth->execute();
  96. $result['product_images'] = $sth->fetchAll(PDO::FETCH_ASSOC);
  97. $sth = null;
  98. $dbh = null;
  99. return $result;
  100. }
  101. function isExistProductCode($product_code, $product_id) {
  102. $result = null;
  103. Loader::Load('Database');
  104. $dbh = Database::getInstance()->getConnection();
  105. $sth = $dbh->prepare('SELECT 1 FROM products p
  106. WHERE p.product_code = :product_code AND p.product_id <> :product_id');
  107. $sth->bindParam(':product_code', $product_code);
  108. $sth->bindParam(':product_id', $product_id);
  109. $sth->execute();
  110. $result = ($sth->rowCount() > 0);
  111. $sth = null;
  112. $dbh = null;
  113. return $result;
  114. }
  115. function saveProduct($product) {
  116. $result = false;
  117. Loader::Load('Database');
  118. $dbh = Database::getInstance()->getConnection();
  119. try {
  120. $product_id = 0;
  121. $dbh->beginTransaction();
  122. $sth = $dbh->prepare('INSERT INTO products (product_code, product_name, product_description, product_status, product_cost, product_points, created_dt, created_by, updated_dt, updated_by) VALUES
  123. (:product_code, :product_name, :product_description, :product_status, :product_cost, :product_points, NOW(), :created_by, NOW(), :updated_by)');
  124. $sth->bindParam(':product_code', $product['product']['product_code']);
  125. $sth->bindParam(':product_name', $product['product']['product_name']);
  126. $sth->bindParam(':product_description', $product['product']['product_description']);
  127. $sth->bindParam(':product_status', $product['product']['product_status']);
  128. $sth->bindParam(':product_cost', $product['product']['product_cost']);
  129. $sth->bindParam(':product_points', $product['product']['product_points']);
  130. $sth->bindParam(':created_by', $product['product']['created_by']);
  131. $sth->bindParam(':updated_by', $product['product']['updated_by']);
  132. $sth->execute();
  133. $product_id = $dbh->lastInsertId('product_id');
  134. $sth = $dbh->prepare('INSERT INTO product_balance (product_id, product_balance) VALUES
  135. (:product_id, :product_balance)');
  136. $sth->bindParam(':product_id', $product_id);
  137. $sth->bindParam(':product_balance', $product['product_balance']['product_balance']);
  138. $sth->execute();
  139. foreach ($product['product_categories'] as $productcategory) {
  140. $sth = $dbh->prepare('INSERT INTO product_categories (product_id, category_id) VALUES
  141. (:product_id, :category_id)');
  142. $sth->bindParam(':product_id', $product_id);
  143. $sth->bindParam(':category_id', $productcategory['category_id']);
  144. $sth->execute();
  145. }
  146. foreach ($product['product_images'] as $productimage) {
  147. $sth = $dbh->prepare('INSERT INTO product_images (product_id, image_name, image_type, sys_file_name) VALUES
  148. (:product_id, :image_name, :image_type, :sys_file_name)');
  149. $sth->bindParam(':product_id', $product_id);
  150. $sth->bindParam(':image_name', $productimage['image_name']);
  151. $sth->bindParam(':image_type', $productimage['image_type']);
  152. $sth->bindParam(':sys_file_name', $productimage['sys_file_name']);
  153. $sth->execute();
  154. }
  155. $dbh->commit();
  156. $result = true;
  157. } catch(PDOException $e) {
  158. $dbh->rollback();
  159. throw new Exception($e->getMessage());
  160. }
  161. $sth = null;
  162. $dbh = null;
  163. return $result;
  164. }
  165. function updateProduct($product) {
  166. $result = false;
  167. Loader::Load('Database');
  168. $dbh = Database::getInstance()->getConnection();
  169. try {
  170. $product_id = $product['product']['product_id'];
  171. $filesTBD = array();
  172. $dbh->beginTransaction();
  173. $sth = $dbh->prepare('UPDATE products SET product_code = :product_code, product_name = :product_name, product_description = :product_description,
  174. product_status = :product_status, product_cost = :product_cost, product_points = :product_points, updated_dt = NOW(), updated_by = :updated_by WHERE product_id = :product_id');
  175. $sth->bindParam(':product_code', $product['product']['product_code']);
  176. $sth->bindParam(':product_name', $product['product']['product_name']);
  177. $sth->bindParam(':product_description', $product['product']['product_description']);
  178. $sth->bindParam(':product_status', $product['product']['product_status']);
  179. $sth->bindParam(':product_cost', $product['product']['product_cost']);
  180. $sth->bindParam(':product_points', $product['product']['product_points']);
  181. $sth->bindParam(':updated_by', $product['product']['updated_by']);
  182. $sth->bindParam(':product_id', $product_id);
  183. $sth->execute();
  184. $sth = $dbh->prepare('UPDATE product_balance SET product_balance = :product_balance WHERE
  185. product_id = :product_id');
  186. $sth->bindParam(':product_balance', $product['product_balance']['product_balance']);
  187. $sth->bindParam(':product_id', $product_id);
  188. $sth->execute();
  189. $sth = $dbh->prepare('DELETE FROM product_categories WHERE
  190. product_id = :product_id');
  191. $sth->bindParam(':product_id', $product_id);
  192. $sth->execute();
  193. foreach ($product['product_categories'] as $productcategory) {
  194. $sth = $dbh->prepare('INSERT INTO product_categories (product_id, category_id) VALUES
  195. (:product_id, :category_id)');
  196. $sth->bindParam(':product_id', $product_id);
  197. $sth->bindParam(':category_id', $productcategory['category_id']);
  198. $sth->execute();
  199. }
  200. foreach ($product['product_imagesTBD'] as $productremovedimage) {
  201. $sth = $dbh->prepare('SELECT * FROM product_images WHERE
  202. product_id = :product_id AND image_type = :image_type');
  203. $sth->bindParam(':product_id', $product_id);
  204. $sth->bindParam(':image_type', $productremovedimage);
  205. $sth->execute();
  206. if ($sth->rowCount() > 0) {
  207. $imgTBD = $sth->fetch(PDO::FETCH_ASSOC);
  208. $sth = $dbh->prepare('DELETE FROM product_images WHERE
  209. product_image_id = :product_image_id');
  210. $sth->bindParam(':product_image_id', $imgTBD['product_image_id']);
  211. $sth->execute();
  212. array_push($filesTBD, dirname(__FILE__).'/images/products/'.$imgTBD['sys_file_name']);
  213. }
  214. }
  215. foreach ($product['product_images'] as $productimage) {
  216. $sth = $dbh->prepare('SELECT * FROM product_images WHERE
  217. product_id = :product_id AND image_type = :image_type');
  218. $sth->bindParam(':product_id', $product_id);
  219. $sth->bindParam(':image_type', $productimage['image_type']);
  220. $sth->execute();
  221. if ($sth->rowCount() > 0) {
  222. $imgTBD = $sth->fetch(PDO::FETCH_ASSOC);
  223. $sth = $dbh->prepare('DELETE FROM product_images WHERE
  224. product_image_id = :product_image_id');
  225. $sth->bindParam(':product_image_id', $imgTBD['product_image_id']);
  226. $sth->execute();
  227. array_push($filesTBD, dirname(__FILE__).'/images/products/'.$imgTBD['sys_file_name']);
  228. }
  229. if ($productimage['sys_file_name'] != '') {
  230. $sth = $dbh->prepare('INSERT INTO product_images (product_id, image_name, image_type, sys_file_name) VALUES
  231. (:product_id, :image_name, :image_type, :sys_file_name)');
  232. $sth->bindParam(':product_id', $product_id);
  233. $sth->bindParam(':image_name', $productimage['image_name']);
  234. $sth->bindParam(':image_type', $productimage['image_type']);
  235. $sth->bindParam(':sys_file_name', $productimage['sys_file_name']);
  236. $sth->execute();
  237. }
  238. }
  239. $dbh->commit();
  240. foreach($filesTBD as $fileTBD)
  241. unlink($fileTBD);
  242. $result = true;
  243. } catch(PDOException $e) {
  244. $dbh->rollback();
  245. throw new Exception($e->getMessage());
  246. }
  247. $sth = null;
  248. $dbh = null;
  249. return $result;
  250. }
  251. function deleteProduct($products) {
  252. $result = false;
  253. Loader::Load('Database');
  254. $dbh = Database::getInstance()->getConnection();
  255. try {
  256. $filesTBD = array();
  257. $product_ids = implode(',', $products);
  258. $dbh->beginTransaction();
  259. $sth = $dbh->prepare('SELECT * FROM product_images WHERE product_id IN (' . $product_ids . ')');
  260. $sth->execute();
  261. $images = $sth->fetchAll(PDO::FETCH_ASSOC);
  262. foreach($images as $image) {
  263. array_push($filesTBD, dirname(__FILE__).'/images/products/'.$image['sys_file_name']);
  264. }
  265. $sth = $dbh->prepare('DELETE FROM product_images WHERE product_id IN (' . $product_ids . ')');
  266. $sth->execute();
  267. $sth = $dbh->prepare('DELETE FROM product_categories WHERE product_id IN (' . $product_ids . ')');
  268. $sth->execute();
  269. $sth = $dbh->prepare('DELETE FROM product_balance WHERE product_id IN (' . $product_ids . ')');
  270. $sth->execute();
  271. $sth = $dbh->prepare('DELETE FROM products WHERE product_id IN (' . $product_ids . ')');
  272. $sth->execute();
  273. $dbh->commit();
  274. foreach($filesTBD as $fileTBD)
  275. unlink($fileTBD);
  276. $result = true;
  277. } catch(PDOException $e) {
  278. $dbh->rollback();
  279. throw new Exception($e->getMessage());
  280. }
  281. $sth = null;
  282. $dbh = null;
  283. return $result;
  284. }
  285. function updateAllProductPointValue($newrate, $rounding) {
  286. $result = false;
  287. Loader::Load('Database');
  288. $dbh = Database::getInstance()->getConnection();
  289. try {
  290. $products = $this->getAllProductInfo();
  291. if ($products) {
  292. $dbh->beginTransaction();
  293. foreach ($products as $product) {
  294. $points = 0;
  295. if ($rounding == 0)
  296. $points = floor($product['product_cost'] / $newrate);
  297. else {
  298. $points = round($product['product_cost'] / $newrate);
  299. }
  300. $sth = $dbh->prepare('UPDATE products SET product_points = :product_points WHERE
  301. product_id = :product_id');
  302. $sth->bindParam(':product_points', $points);
  303. $sth->bindParam(':product_id', $product['product_id']);
  304. $sth->execute();
  305. }
  306. $dbh->commit();
  307. $result = true;
  308. }
  309. } catch(PDOException $e) {
  310. $dbh->rollback();
  311. throw new Exception($e->getMessage());
  312. }
  313. $sth = null;
  314. $dbh = null;
  315. return $result;
  316. }
  317. }
  318. ?>