/gocart/models/gift_card_model.php

https://bitbucket.org/admin_malppy/malppy · PHP · 126 lines · 95 code · 26 blank · 5 comment · 7 complexity · 5945a9b2654a762878d6fceb0eab2fdf MD5 · raw file

  1. <?php
  2. class Gift_card_model extends CI_Model
  3. {
  4. function __construct()
  5. {
  6. parent::__construct();
  7. }
  8. // check the expiration date and/or balance
  9. function is_valid($card)
  10. {
  11. if($card->activated == 0) return false;
  12. // check for zero balance
  13. if($this->get_balance($card) == 0) return false;
  14. // check expiry date.. not required
  15. if($card->expiry_date!="0000-00-00")
  16. {
  17. $e_date = split("-", $card->expiry_date);
  18. $end = mktime(0,0,0, $e_date[1], (int) $e_date[2] +1 , $e_date[0]); // add a day to account for the end date as the last viable day
  19. $current = time();
  20. if($current > $end) return false;
  21. }
  22. return true;
  23. }
  24. // update the card records
  25. function update_used_card_balances($gc_list)
  26. {
  27. foreach($gc_list as $code=>$card)
  28. {
  29. if(isset($card['amt_used'])) {
  30. $this->db->where('code', $code);
  31. $this->db->set('amount_used', $card['amt_used']);
  32. $this->db->update('gift_cards');
  33. }
  34. }
  35. }
  36. function activate($code)
  37. {
  38. $this->db->where('code', $code);
  39. $this->db->set('activated', '1');
  40. $this->db->update('gift_cards');
  41. }
  42. function delete($id)
  43. {
  44. $this->db->where('id', $id);
  45. $this->db->delete('gift_cards');
  46. }
  47. function get_all_new()
  48. {
  49. $this->db->select('gift_cards.*, orders.status', false);
  50. $this->db->from('gift_cards');
  51. $this->db->join('orders', 'gift_cards.order_number = orders.order_number', 'left');
  52. $this->db->order_by('gift_cards.id', 'DESC');
  53. $res = $this->db->get();
  54. $cards = $res->result_array();
  55. return $cards;
  56. }
  57. function save_card($data)
  58. {
  59. $this->db->insert('gift_cards', $data);
  60. }
  61. function get_balance($card)
  62. {
  63. return (float) $card->beginning_amount - (float) $card->amount_used;
  64. }
  65. function get_gift_card($code)
  66. {
  67. $this->db->where('code', $code);
  68. $res = $this->db->get('gift_cards');
  69. return $res->row();
  70. }
  71. function send_notification($gc_data)
  72. {
  73. $this->load->helper('formatting_helper');
  74. $row = $this->db->where('id', '1')->get('canned_messages')->row_array();
  75. // set replacement values for subject & body
  76. $row['subject'] = str_replace('{from}', $gc_data['from'], $row['subject']);
  77. $row['subject'] = str_replace('{site_name}', $this->config->item('company_name'), $row['subject']);
  78. $row['content'] = str_replace('{code}', $gc_data['code'], $row['content']);
  79. $row['content'] = str_replace('{amount}', format_currency($gc_data['beginning_amount']), $row['content']);
  80. $row['content'] = str_replace('{from}', $gc_data['from'], $row['content']);
  81. $row['content'] = str_replace('{personal_message}', nl2br($gc_data['personal_message']), $row['content']);
  82. $row['content'] = str_replace('{url}', $this->config->item('base_url'), $row['content']);
  83. $row['content'] = str_replace('{site_name}', $this->config->item('company_name'), $row['content']);
  84. $this->load->library('email');
  85. $config['mailtype'] = 'html';
  86. $this->email->initialize($config);
  87. $this->email->from($this->config->item('email'));
  88. $this->email->to($gc_data['to_email']);
  89. $this->email->subject($row['subject']);
  90. $this->email->message($row['content']);
  91. $this->email->send();
  92. }
  93. function is_active($code)
  94. {
  95. $this->db->where('code', $code);
  96. $res = $this->db->get('gift_cards');
  97. $row = $res->row();
  98. return (bool) $row->activated;
  99. }
  100. }