laybuy.php (15309B)
1 <?php 2 class ModelExtensionPaymentLaybuy 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 6 return $this->db->getLastId(); 7 } 8 9 public function getCustomerIdByOrderId($order_id) { 10 $query = $this->db->query("SELECT `customer_id` FROM `" . DB_PREFIX . "order` WHERE `order_id` = '" . (int)$order_id . "'"); 11 12 if ($query->num_rows) { 13 return $query->row['customer_id']; 14 } else { 15 return 0; 16 } 17 } 18 19 public function getInitialPayments() { 20 $minimum = $this->config->get('payment_laybuy_min_deposit') ? $this->config->get('payment_laybuy_min_deposit') : 20; 21 22 $maximum = $this->config->get('payment_laybuy_max_deposit') ? $this->config->get('payment_laybuy_max_deposit') : 50; 23 24 $initial_payments = array(); 25 26 for ($i = $minimum; $i <= $maximum; $i += 10) { 27 $initial_payments[] = $i; 28 } 29 30 return $initial_payments; 31 } 32 33 public function getMonths() { 34 $this->load->language('extension/payment/laybuy'); 35 36 $max_months = $this->config->get('payment_laybuy_max_months'); 37 38 if (!$max_months) { 39 $max_months = 3; 40 } 41 42 if ($max_months < 1) { 43 $max_months = 1; 44 } 45 46 $months = array(); 47 48 for ($i = 1; $i <= $max_months; $i++) { 49 $months[] = array( 50 'value' => $i, 51 'label' => $i . ' ' . (($i > 1) ? $this->language->get('text_months') : $this->language->get('text_month')) 52 ); 53 } 54 55 return $months; 56 } 57 58 public function getPayPalProfileIds() { 59 $query = $this->db->query("SELECT `paypal_profile_id` FROM `" . DB_PREFIX . "laybuy_transaction` WHERE `status` = '1'"); 60 61 return $query->rows; 62 } 63 64 public function getRevisedTransaction($id) { 65 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "laybuy_revise_request` WHERE `laybuy_revise_request_id` = '" . (int)$id . "'"); 66 67 return $query->row; 68 } 69 70 public function getRemainingAmount($amount, $downpayment_amount, $payment_amounts, $transaction = 2) { 71 return $amount - ($downpayment_amount + (((int)$transaction - 2) * $payment_amounts)); 72 } 73 74 public function getRevisedTransactions($id) { 75 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "laybuy_revise_request` WHERE `laybuy_revise_request_id` = '" . (int)$id . "'"); 76 77 return $query->rows; 78 } 79 80 public function getStatusLabel($id) { 81 $statuses = $this->getTransactionStatuses(); 82 83 foreach ($statuses as $status) { 84 if ($status['status_id'] == $id && $status['status_name'] != '') { 85 return $status['status_name']; 86 87 break; 88 } 89 } 90 91 return $id; 92 } 93 94 public function getTransaction($id) { 95 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "laybuy_transaction` WHERE `laybuy_transaction_id` = '" . (int)$id . "'"); 96 97 return $query->row; 98 } 99 100 public function getTransactions($data = array()) { 101 $sql = "SELECT *, CONCAT(firstname, ' ', lastname) AS `customer` FROM `" . DB_PREFIX . "laybuy_transaction` `lt` WHERE 1 = 1"; 102 103 $implode = array(); 104 105 if (!empty($data['filter_order_id'])) { 106 $implode[] = "`lt`.`order_id` = '" . (int)$data['filter_order_id'] . "'"; 107 } 108 109 if (!empty($data['filter_customer'])) { 110 $implode[] = "CONCAT(firstname, ' ', lastname) LIKE '%" . $this->db->escape($data['filter_customer']) . "%'"; 111 } 112 113 if (!empty($data['filter_dp_percent'])) { 114 $implode[] = "`lt`.`downpayment` = '" . (int)$data['filter_dp_percent'] . "'"; 115 } 116 117 if (!empty($data['filter_months'])) { 118 $implode[] = "`lt`.`months` = '" . (int)$data['filter_months'] . "'"; 119 } 120 121 if (!empty($data['filter_status'])) { 122 $implode[] = "`lt`.`status` = '" . (int)$data['filter_status'] . "'"; 123 } 124 125 if (!empty($data['filter_date_added'])) { 126 $implode[] = "DATE(`lt`.`date_added`) = DATE('" . $this->db->escape($data['filter_date_added']) . "')"; 127 } 128 129 if ($implode) { 130 $sql .= " AND " . implode(" AND ", $implode); 131 } 132 133 $sort_data = array( 134 'lt.order_id', 135 'customer', 136 'lt.amount', 137 'lt.downpayment', 138 'lt.months', 139 'lt.downpayment_amount', 140 'lt.first_payment_due', 141 'lt.last_payment_due', 142 'lt.status', 143 'lt.date_added' 144 ); 145 146 if (isset($data['sort']) && in_array($data['sort'], $sort_data)) { 147 $sql .= " ORDER BY " . $data['sort']; 148 } else { 149 $sql .= " ORDER BY order_id"; 150 } 151 152 if (isset($data['order']) && ($data['order'] == 'DESC')) { 153 $sql .= " DESC"; 154 } else { 155 $sql .= " ASC"; 156 } 157 158 if (isset($data['sort']) && $data['sort'] != 'lt.date_added') { 159 $sql .= ", lt.date_added DESC"; 160 } 161 162 if (isset($data['start']) || isset($data['limit'])) { 163 if ($data['start'] < 0) { 164 $data['start'] = 0; 165 } 166 167 if ($data['limit'] < 1) { 168 $data['limit'] = 20; 169 } 170 171 $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit']; 172 } 173 174 $query = $this->db->query($sql); 175 176 return $query->rows; 177 } 178 179 public function getTotalTransactions($data = array()) { 180 $sql = "SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "laybuy_transaction` `lt` WHERE 1 = 1"; 181 182 $implode = array(); 183 184 if (!empty($data['filter_order_id'])) { 185 $implode[] = "`lt`.`order_id` = '" . (int)$data['filter_order_id'] . "'"; 186 } 187 188 if (!empty($data['filter_customer'])) { 189 $implode[] = "CONCAT(firstname, ' ', lastname) LIKE '%" . $this->db->escape($data['filter_customer']) . "%'"; 190 } 191 192 if (!empty($data['filter_dp_percent'])) { 193 $implode[] = "`lt`.`downpayment` = '" . (int)$data['filter_dp_percent'] . "'"; 194 } 195 196 if (!empty($data['filter_months'])) { 197 $implode[] = "`lt`.`months` = '" . (int)$data['filter_months'] . "'"; 198 } 199 200 if (!empty($data['filter_status'])) { 201 $implode[] = "`lt`.`status` = '" . (int)$data['filter_status'] . "'"; 202 } 203 204 if (!empty($data['filter_date_added'])) { 205 $implode[] = "DATE(`lt`.`date_added`) = DATE('" . $this->db->escape($data['filter_date_added']) . "')"; 206 } 207 208 if ($implode) { 209 $sql .= " AND " . implode(" AND ", $implode); 210 } 211 212 $query = $this->db->query($sql); 213 214 return $query->row['total']; 215 } 216 217 public function getTransactionByLayBuyRefId($laybuy_ref_id) { 218 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "laybuy_transaction` WHERE `laybuy_ref_no` = '" . (int)$laybuy_ref_id . "'"); 219 220 return $query->row; 221 } 222 223 public function getTransactionByOrderId($order_id) { 224 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "laybuy_transaction` WHERE `order_id` = '" . (int)$order_id . "' ORDER BY `laybuy_ref_no` DESC LIMIT 1"); 225 226 return $query->row; 227 } 228 229 public function getTransactionStatuses() { 230 $this->load->language('extension/payment/laybuy'); 231 232 $transaction_statuses = array( 233 array( 234 'status_id' => 1, 235 'status_name' => $this->language->get('text_status_1') 236 ), 237 array( 238 'status_id' => 5, 239 'status_name' => $this->language->get('text_status_5') 240 ), 241 array( 242 'status_id' => 7, 243 'status_name' => $this->language->get('text_status_7') 244 ), 245 array( 246 'status_id' => 50, 247 'status_name' => $this->language->get('text_status_50') 248 ), 249 array( 250 'status_id' => 51, 251 'status_name' => $this->language->get('text_status_51') 252 ) 253 ); 254 255 return $transaction_statuses; 256 } 257 258 public function install() { 259 $this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "laybuy_transaction` ( 260 `laybuy_transaction_id` int(11) NOT NULL AUTO_INCREMENT, 261 `order_id` int(11) NOT NULL DEFAULT '0', 262 `firstname` varchar(32) NOT NULL DEFAULT '', 263 `lastname` varchar(32) NOT NULL DEFAULT '', 264 `address` text, 265 `suburb` varchar(128) NOT NULL DEFAULT '', 266 `state` varchar(128) NOT NULL DEFAULT '', 267 `country` varchar(32) NOT NULL DEFAULT '', 268 `postcode` varchar(10) NOT NULL DEFAULT '', 269 `email` varchar(96) NOT NULL DEFAULT '', 270 `amount` double NOT NULL, 271 `currency` varchar(5) NOT NULL, 272 `downpayment` double NOT NULL, 273 `months` int(11) NOT NULL, 274 `downpayment_amount` double NOT NULL, 275 `payment_amounts` double NOT NULL, 276 `first_payment_due` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 277 `last_payment_due` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 278 `store_id` int(11) NOT NULL DEFAULT '0', 279 `status` int(11) NOT NULL DEFAULT '1', 280 `report` text, 281 `transaction` int(11) NOT NULL DEFAULT '2', 282 `paypal_profile_id` varchar(250) NOT NULL DEFAULT '', 283 `laybuy_ref_no` int(11) NOT NULL DEFAULT '0', 284 `date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 285 PRIMARY KEY (`laybuy_transaction_id`) 286 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci"); 287 288 $this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "laybuy_revise_request` ( 289 `laybuy_revise_request_id` int(11) NOT NULL AUTO_INCREMENT, 290 `laybuy_transaction_id` int(11) DEFAULT '0', 291 `type` varchar(250) NOT NULL DEFAULT '', 292 `order_id` int(11) NOT NULL DEFAULT '0', 293 `firstname` varchar(32) NOT NULL DEFAULT '', 294 `lastname` varchar(32) NOT NULL DEFAULT '', 295 `address` text, 296 `suburb` varchar(128) NOT NULL DEFAULT '', 297 `state` varchar(128) NOT NULL DEFAULT '', 298 `country` varchar(32) NOT NULL DEFAULT '', 299 `postcode` varchar(10) NOT NULL DEFAULT '', 300 `email` varchar(96) NOT NULL DEFAULT '', 301 `amount` double NOT NULL, 302 `currency` varchar(5) NOT NULL, 303 `downpayment` double NOT NULL, 304 `months` int(11) NOT NULL, 305 `downpayment_amount` double NOT NULL, 306 `payment_amounts` double NOT NULL, 307 `first_payment_due` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 308 `last_payment_due` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 309 `store_id` int(11) NOT NULL DEFAULT '0', 310 `status` int(11) NOT NULL DEFAULT '1', 311 `report` text, 312 `transaction` int(11) NOT NULL DEFAULT '2', 313 `paypal_profile_id` varchar(250) NOT NULL DEFAULT '', 314 `laybuy_ref_no` int(11) NOT NULL DEFAULT '0', 315 `payment_type` tinyint(1) NOT NULL DEFAULT '1', 316 `date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 317 PRIMARY KEY (`laybuy_revise_request_id`) 318 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci"); 319 320 $this->load->model('setting/event'); 321 322 $this->model_setting_event->addEvent('laybuy', 'catalog/model/checkout/order/deleteOrder/after', 'extension/payment/laybuy/deleteOrder'); 323 } 324 325 public function log($data, $step = 6) { 326 if ($this->config->get('payment_laybuy_logging')) { 327 $backtrace = debug_backtrace(); 328 329 $log = new Log('laybuy.log'); 330 331 $log->write('(' . $backtrace[$step]['class'] . '::' . $backtrace[$step]['function'] . ') - ' . $data); 332 } 333 } 334 335 public function uninstall() { 336 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "laybuy_transaction`"); 337 338 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "laybuy_revise_request`"); 339 340 $this->load->model('setting/event'); 341 342 $this->model_setting_event->deleteEventByCode('laybuy'); 343 } 344 345 public function updateOrderStatus($order_id, $order_status_id, $comment) { 346 $this->db->query("UPDATE `" . DB_PREFIX . "order` SET `order_status_id` = '" . (int)$order_status_id . "', `date_modified` = NOW() WHERE `order_id` = '" . (int)$order_id . "'"); 347 348 $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()"); 349 } 350 351 public function updateRevisedTransaction($id, $data = array()) { 352 $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 . "'"); 353 } 354 355 public function updateTransaction($id, $status, $report, $transaction) { 356 $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 . "'"); 357 } 358 359 public function updateTransactionStatus($id, $status) { 360 $this->db->query("UPDATE `" . DB_PREFIX . "laybuy_transaction` SET `status` = '" . (int)$status . "' WHERE `laybuy_transaction_id` = '" . (int)$id . "'"); 361 } 362 }