PageRenderTime 68ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/model/payment/laybuy.php

https://gitlab.com/shapcy/opencart
PHP | 362 lines | 282 code | 80 blank | 0 comment | 39 complexity | cd21dd30f51e0091de0de80356634121 MD5 | raw file
  1. <?php
  2. class ModelPaymentLaybuy extends Model {
  3. public function addRevisedTransaction($data = array()) {
  4. $query = $this->db->query("INSERT INTO `" . DB_PREFIX . "laybuy_revise_request` SET `laybuy_transaction_id` = '" . (int)$data['transaction_id'] . "', `type` = '" . $this->db->escape($data['type']) . "', `order_id` = '" . (int)$data['order_id'] . "', `firstname` = '" . $this->db->escape($data['firstname']) . "', `lastname` = '" . $this->db->escape($data['lastname']) . "', `address` = '" . $this->db->escape($data['address']) . "', `suburb` = '" . $this->db->escape($data['suburb']) . "', `state` = '" . $this->db->escape($data['state']) . "', `country` = '" . $this->db->escape($data['country']) . "', `postcode` = '" . $this->db->escape($data['postcode']) . "', `email` = '" . $this->db->escape($data['email']) . "', `amount` = '" . (float)$data['amount'] . "', `currency` = '" . $this->db->escape($data['currency']) . "', `downpayment` = '" . $this->db->escape($data['downpayment']) . "', `months` = '" . (int)$data['months'] . "', `downpayment_amount` = '" . (float)$data['downpayment_amount'] . "', `payment_amounts` = '" . (float)$data['payment_amounts'] . "', `first_payment_due` = '" . $this->db->escape($data['first_payment_due']) . "', `last_payment_due` = '" . $this->db->escape($data['last_payment_due']) . "', `store_id` = '" . (int)$data['store_id'] . "', `status` = '" . (int)$data['status'] . "', `report` = '" . $this->db->escape($data['report']) . "', `transaction` = '" . (int)$data['transaction'] . "', `paypal_profile_id` = '" . $this->db->escape($data['paypal_profile_id']) . "', `laybuy_ref_no` = '" . (int)$data['laybuy_ref_no'] . "', `payment_type` = '" . (int)$data['payment_type'] . "', `date_added` = NOW()");
  5. return $this->db->getLastId();
  6. }
  7. public function getCustomerIdByOrderId($order_id) {
  8. $query = $this->db->query("SELECT `customer_id` FROM `" . DB_PREFIX . "order` WHERE `order_id` = '" . (int)$order_id . "'");
  9. if ($query->num_rows) {
  10. return $query->row['customer_id'];
  11. } else {
  12. return 0;
  13. }
  14. }
  15. public function getInitialPayments() {
  16. $minimum = $this->config->get('laybuy_min_deposit') ? $this->config->get('laybuy_min_deposit') : 20;
  17. $maximum = $this->config->get('laybuy_max_deposit') ? $this->config->get('laybuy_max_deposit') : 50;
  18. $initial_payments = array();
  19. for ($i = $minimum; $i <= $maximum; $i += 10) {
  20. $initial_payments[] = $i;
  21. }
  22. return $initial_payments;
  23. }
  24. public function getMonths() {
  25. $this->load->language('payment/laybuy');
  26. $max_months = $this->config->get('laybuy_max_months');
  27. if (!$max_months) {
  28. $max_months = 3;
  29. }
  30. if ($max_months < 1) {
  31. $max_months = 1;
  32. }
  33. $months = array();
  34. for ($i = 1; $i <= $max_months; $i++) {
  35. $months[] = array(
  36. 'value' => $i,
  37. 'label' => $i . ' ' . (($i > 1) ? $this->language->get('text_months') : $this->language->get('text_month'))
  38. );
  39. }
  40. return $months;
  41. }
  42. public function getPayPalProfileIds() {
  43. $query = $this->db->query("SELECT `paypal_profile_id` FROM `" . DB_PREFIX . "laybuy_transaction` WHERE `status` = '1'");
  44. return $query->rows;
  45. }
  46. public function getRevisedTransaction($id) {
  47. $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "laybuy_revise_request` WHERE `laybuy_revise_request_id` = '" . (int)$id . "'");
  48. return $query->row;
  49. }
  50. public function getRemainingAmount($amount, $downpayment_amount, $payment_amounts, $transaction = 2) {
  51. return $amount - ($downpayment_amount + (((int)$transaction - 2) * $payment_amounts));
  52. }
  53. public function getRevisedTransactions($id) {
  54. $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "laybuy_revise_request` WHERE `laybuy_revise_request_id` = '" . (int)$id . "'");
  55. return $query->rows;
  56. }
  57. public function getStatusLabel($id) {
  58. $statuses = $this->getTransactionStatuses();
  59. foreach ($statuses as $status) {
  60. if ($status['status_id'] == $id && $status['status_name'] != '') {
  61. return $status['status_name'];
  62. break;
  63. }
  64. }
  65. return $id;
  66. }
  67. public function getTransaction($id) {
  68. $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "laybuy_transaction` WHERE `laybuy_transaction_id` = '" . (int)$id . "'");
  69. return $query->row;
  70. }
  71. public function getTransactions($data = array()) {
  72. $sql = "SELECT *, CONCAT(firstname, ' ', lastname) AS `customer` FROM `" . DB_PREFIX . "laybuy_transaction` `lt` WHERE 1 = 1";
  73. $implode = array();
  74. if (!empty($data['filter_order_id'])) {
  75. $implode[] = "`lt`.`order_id` = '" . (int)$data['filter_order_id'] . "'";
  76. }
  77. if (!empty($data['filter_customer'])) {
  78. $implode[] = "CONCAT(firstname, ' ', lastname) LIKE '%" . $this->db->escape($data['filter_customer']) . "%'";
  79. }
  80. if (!empty($data['filter_dp_percent'])) {
  81. $implode[] = "`lt`.`downpayment` = '" . (int)$data['filter_dp_percent'] . "'";
  82. }
  83. if (!empty($data['filter_months'])) {
  84. $implode[] = "`lt`.`months` = '" . (int)$data['filter_months'] . "'";
  85. }
  86. if (!empty($data['filter_status'])) {
  87. $implode[] = "`lt`.`status` = '" . (int)$data['filter_status'] . "'";
  88. }
  89. if (!empty($data['filter_date_added'])) {
  90. $implode[] = "DATE(`lt`.`date_added`) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
  91. }
  92. if ($implode) {
  93. $sql .= " AND " . implode(" AND ", $implode);
  94. }
  95. $sort_data = array(
  96. 'lt.order_id',
  97. 'customer',
  98. 'lt.amount',
  99. 'lt.downpayment',
  100. 'lt.months',
  101. 'lt.downpayment_amount',
  102. 'lt.first_payment_due',
  103. 'lt.last_payment_due',
  104. 'lt.status',
  105. 'lt.date_added'
  106. );
  107. if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
  108. $sql .= " ORDER BY " . $data['sort'];
  109. } else {
  110. $sql .= " ORDER BY order_id";
  111. }
  112. if (isset($data['order']) && ($data['order'] == 'DESC')) {
  113. $sql .= " DESC";
  114. } else {
  115. $sql .= " ASC";
  116. }
  117. if (isset($data['sort']) && $data['sort'] != 'lt.date_added') {
  118. $sql .= ", lt.date_added DESC";
  119. }
  120. if (isset($data['start']) || isset($data['limit'])) {
  121. if ($data['start'] < 0) {
  122. $data['start'] = 0;
  123. }
  124. if ($data['limit'] < 1) {
  125. $data['limit'] = 20;
  126. }
  127. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  128. }
  129. $query = $this->db->query($sql);
  130. return $query->rows;
  131. }
  132. public function getTotalTransactions($data = array()) {
  133. $sql = "SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "laybuy_transaction` `lt` WHERE 1 = 1";
  134. $implode = array();
  135. if (!empty($data['filter_order_id'])) {
  136. $implode[] = "`lt`.`order_id` = '" . (int)$data['filter_order_id'] . "'";
  137. }
  138. if (!empty($data['filter_customer'])) {
  139. $implode[] = "CONCAT(firstname, ' ', lastname) LIKE '%" . $this->db->escape($data['filter_customer']) . "%'";
  140. }
  141. if (!empty($data['filter_dp_percent'])) {
  142. $implode[] = "`lt`.`downpayment` = '" . (int)$data['filter_dp_percent'] . "'";
  143. }
  144. if (!empty($data['filter_months'])) {
  145. $implode[] = "`lt`.`months` = '" . (int)$data['filter_months'] . "'";
  146. }
  147. if (!empty($data['filter_status'])) {
  148. $implode[] = "`lt`.`status` = '" . (int)$data['filter_status'] . "'";
  149. }
  150. if (!empty($data['filter_date_added'])) {
  151. $implode[] = "DATE(`lt`.`date_added`) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
  152. }
  153. if ($implode) {
  154. $sql .= " AND " . implode(" AND ", $implode);
  155. }
  156. $query = $this->db->query($sql);
  157. return $query->row['total'];
  158. }
  159. public function getTransactionByLayBuyRefId($laybuy_ref_id) {
  160. $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "laybuy_transaction` WHERE `laybuy_ref_no` = '" . (int)$laybuy_ref_id . "'");
  161. return $query->row;
  162. }
  163. public function getTransactionByOrderId($order_id) {
  164. $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "laybuy_transaction` WHERE `order_id` = '" . (int)$order_id . "' ORDER BY `laybuy_ref_no` DESC LIMIT 1");
  165. return $query->row;
  166. }
  167. public function getTransactionStatuses() {
  168. $this->load->language('payment/laybuy');
  169. $transaction_statuses = array(
  170. array(
  171. 'status_id' => 1,
  172. 'status_name' => $this->language->get('text_status_1')
  173. ),
  174. array(
  175. 'status_id' => 5,
  176. 'status_name' => $this->language->get('text_status_5')
  177. ),
  178. array(
  179. 'status_id' => 7,
  180. 'status_name' => $this->language->get('text_status_7')
  181. ),
  182. array(
  183. 'status_id' => 50,
  184. 'status_name' => $this->language->get('text_status_50')
  185. ),
  186. array(
  187. 'status_id' => 51,
  188. 'status_name' => $this->language->get('text_status_51')
  189. )
  190. );
  191. return $transaction_statuses;
  192. }
  193. public function install() {
  194. $this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "laybuy_transaction` (
  195. `laybuy_transaction_id` int(11) NOT NULL AUTO_INCREMENT,
  196. `order_id` int(11) NOT NULL DEFAULT '0',
  197. `firstname` varchar(32) NOT NULL DEFAULT '',
  198. `lastname` varchar(32) NOT NULL DEFAULT '',
  199. `address` text,
  200. `suburb` varchar(128) NOT NULL DEFAULT '',
  201. `state` varchar(128) NOT NULL DEFAULT '',
  202. `country` varchar(32) NOT NULL DEFAULT '',
  203. `postcode` varchar(10) NOT NULL DEFAULT '',
  204. `email` varchar(96) NOT NULL DEFAULT '',
  205. `amount` double NOT NULL,
  206. `currency` varchar(5) NOT NULL,
  207. `downpayment` double NOT NULL,
  208. `months` int(11) NOT NULL,
  209. `downpayment_amount` double NOT NULL,
  210. `payment_amounts` double NOT NULL,
  211. `first_payment_due` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  212. `last_payment_due` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  213. `store_id` int(11) NOT NULL DEFAULT '0',
  214. `status` int(11) NOT NULL DEFAULT '1',
  215. `report` text,
  216. `transaction` int(11) NOT NULL DEFAULT '2',
  217. `paypal_profile_id` varchar(250) NOT NULL DEFAULT '',
  218. `laybuy_ref_no` int(11) NOT NULL DEFAULT '0',
  219. `date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  220. PRIMARY KEY (`laybuy_transaction_id`)
  221. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci");
  222. $this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "laybuy_revise_request` (
  223. `laybuy_revise_request_id` int(11) NOT NULL AUTO_INCREMENT,
  224. `laybuy_transaction_id` int(11) DEFAULT '0',
  225. `type` varchar(250) NOT NULL DEFAULT '',
  226. `order_id` int(11) NOT NULL DEFAULT '0',
  227. `firstname` varchar(32) NOT NULL DEFAULT '',
  228. `lastname` varchar(32) NOT NULL DEFAULT '',
  229. `address` text,
  230. `suburb` varchar(128) NOT NULL DEFAULT '',
  231. `state` varchar(128) NOT NULL DEFAULT '',
  232. `country` varchar(32) NOT NULL DEFAULT '',
  233. `postcode` varchar(10) NOT NULL DEFAULT '',
  234. `email` varchar(96) NOT NULL DEFAULT '',
  235. `amount` double NOT NULL,
  236. `currency` varchar(5) NOT NULL,
  237. `downpayment` double NOT NULL,
  238. `months` int(11) NOT NULL,
  239. `downpayment_amount` double NOT NULL,
  240. `payment_amounts` double NOT NULL,
  241. `first_payment_due` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  242. `last_payment_due` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  243. `store_id` int(11) NOT NULL DEFAULT '0',
  244. `status` int(11) NOT NULL DEFAULT '1',
  245. `report` text,
  246. `transaction` int(11) NOT NULL DEFAULT '2',
  247. `paypal_profile_id` varchar(250) NOT NULL DEFAULT '',
  248. `laybuy_ref_no` int(11) NOT NULL DEFAULT '0',
  249. `payment_type` tinyint(1) NOT NULL DEFAULT '1',
  250. `date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  251. PRIMARY KEY (`laybuy_revise_request_id`)
  252. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci");
  253. $this->load->model('extension/event');
  254. $this->model_extension_event->addEvent('laybuy', 'catalog/model/checkout/order/deleteOrder/after', 'payment/laybuy/deleteOrder');
  255. }
  256. public function log($data, $step = 6) {
  257. if ($this->config->get('laybuy_logging')) {
  258. $backtrace = debug_backtrace();
  259. $log = new Log('laybuy.log');
  260. $log->write('(' . $backtrace[$step]['class'] . '::' . $backtrace[$step]['function'] . ') - ' . $data);
  261. }
  262. }
  263. public function uninstall() {
  264. $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "laybuy_transaction`");
  265. $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "laybuy_revise_request`");
  266. $this->load->model('extension/event');
  267. $this->model_extension_event->deleteEvent('laybuy');
  268. }
  269. public function updateOrderStatus($order_id, $order_status_id, $comment) {
  270. $this->db->query("UPDATE `" . DB_PREFIX . "order` SET `order_status_id` = '" . (int)$order_status_id . "', `date_modified` = NOW() WHERE `order_id` = '" . (int)$order_id . "'");
  271. $this->db->query("INSERT INTO `" . DB_PREFIX . "order_history` SET `order_id` = '" . (int)$order_id . "', `order_status_id` = '" . (int)$order_status_id . "', `notify` = '0', `comment` = '" . $this->db->escape($comment) . "', `date_added` = NOW()");
  272. }
  273. public function updateRevisedTransaction($id, $data = array()) {
  274. $this->db->query("UPDATE `" . DB_PREFIX . "laybuy_revise_request` SET `laybuy_transaction_id` = '" . (int)$data['transaction_id'] . "', `type` = '" . $this->db->escape($data['type']) . "', `order_id` = '" . (int)$data['order_id'] . "', `firstname` = '" . $this->db->escape($data['firstname']) . "', `lastname` = '" . $this->db->escape($data['lastname']) . "', `address` = '" . $this->db->escape($data['address']) . "', `suburb` = '" . $this->db->escape($data['suburb']) . "', `state` = '" . $this->db->escape($data['state']) . "', `country` = '" . $this->db->escape($data['country']) . "', `postcode` = '" . $this->db->escape($data['postcode']) . "', `email` = '" . $this->db->escape($data['email']) . "', `amount` = '" . (float)$data['amount'] . "', `currency` = '" . $this->db->escape($data['currency']) . "', `downpayment` = '" . $this->db->escape($data['downpayment']) . "', `months` = '" . (int)$data['months'] . "', `downpayment_amount` = '" . (float)$data['downpayment_amount'] . "', `payment_amounts` = '" . (float)$data['payment_amounts'] . "', `first_payment_due` = '" . $this->db->escape($data['first_payment_due']) . "', `last_payment_due` = '" . $this->db->escape($data['last_payment_due']) . "', `store_id` = '" . (int)$data['store_id'] . "', `status` = '" . (int)$data['status'] . "', `report` = '" . $this->db->escape($data['report']) . "', `transaction` = '" . (int)$data['transaction'] . "', `paypal_profile_id` = '" . $this->db->escape($data['paypal_profile_id']) . "', `laybuy_ref_no` = '" . (int)$data['laybuy_ref_no'] . "', `payment_type` = '" . (int)$data['payment_type'] . "', `date_added` = NOW() WHERE `laybuy_revise_request_id` = '" . (int)$id . "'");
  275. }
  276. public function updateTransaction($id, $status, $report, $transaction) {
  277. $this->db->query("UPDATE `" . DB_PREFIX . "laybuy_transaction` SET `status` = '" . (int)$status . "', `report` = '" . $this->db->escape($report) . "', `transaction` = '" . (int)$transaction . "' WHERE `laybuy_transaction_id` = '" . (int)$id . "'");
  278. }
  279. public function updateTransactionStatus($id, $status) {
  280. $this->db->query("UPDATE `" . DB_PREFIX . "laybuy_transaction` SET `status` = '" . (int)$status . "' WHERE `laybuy_transaction_id` = '" . (int)$id . "'");
  281. }
  282. }