/admin/includes/classes/campaigns.php

https://bitbucket.org/flth/xtcm · PHP · 355 lines · 224 code · 94 blank · 37 comment · 32 complexity · 0afa9c602c03c0455e4b71335ef98181 MD5 · raw file

  1. <?php
  2. /* -----------------------------------------------------------------------------------------
  3. $Id: campaigns.php 1180 2007-04-23 11:13:00Z Hetfield $
  4. XT-Commerce - community made shopping
  5. http://www.xt-commerce.com
  6. Copyright (c) 2005 XT-Commerce
  7. -----------------------------------------------------------------------------------------
  8. based on:
  9. (c) 2000-2001 The Exchange Project (earlier name of osCommerce)
  10. (c) 2002-2003 osCommerce coding standards; www.oscommerce.com
  11. Released under the GNU General Public License
  12. -----------------------------------------------------------------------------------------
  13. Third Party contribution:
  14. (c) 2007 Hits for weekly, monthly and yearly for campaigns-report by Hetfield
  15. Released under the GNU General Public License
  16. ---------------------------------------------------------------------------------------*/
  17. class campaigns {
  18. function campaigns(& $get_array) {
  19. global $currencies;
  20. if (count($get_array) == 9) {
  21. $this->startD = $get_array['startD'];
  22. $this->startM = $get_array['startM'];
  23. $this->startY = $get_array['startY'];
  24. $this->startDate = mktime(0, 0, 0, $this->startM, $this->startD, $this->startY);
  25. $this->endD = $get_array['endD'];
  26. $this->endM = $get_array['endM'];
  27. $this->endY = $get_array['endY'];
  28. $this->endDate = mktime(0, 0, 0, $this->endM, $this->endD, $this->endY);
  29. $this->status = $get_array['status'];
  30. $this->campaign = $get_array['campaign'];
  31. $this->campaigns = $this->getCampaigns();
  32. if ($get_array['campaign'] == "0") {
  33. $this->SelectArray = $this->campaigns;
  34. } else {
  35. $this->SelectArray = $this->getSelectedCampaign();
  36. }
  37. $this->type = $get_array['report'];
  38. $this->result = array ();
  39. $this->total = array ();
  40. // query data
  41. $this->counter = 0;
  42. $this->counterCMP = 0;
  43. // tŠglich
  44. $this->getTotalLeads();
  45. $this->getTotalSells();
  46. for ($n = 0; $n < count($this->SelectArray); $n ++) {
  47. $this->campaign = $this->SelectArray[$n]['id'];
  48. $this->result[$this->counterCMP]['id'] = $this->campaign;
  49. $this->result[$this->counterCMP]['text'] = $this->camp[$this->campaign];
  50. switch ($this->type) {
  51. // yearly
  52. case 1 :
  53. $start = $this->startDate;
  54. while ($start <= $this->endDate) {
  55. $end = mktime(0, 0, 0, date("m", $start), date("d", $start), date("Y", $start) + 1);
  56. // get Leads
  57. $this->getLeads($start, $end, $this->type);
  58. // get Sells
  59. $this->getSells($start, $end, $this->type);
  60. $this->getHits($start, $end, $this->type);
  61. $start = $end;
  62. $this->counter++;
  63. }
  64. break;
  65. // monthly
  66. case 2 :
  67. $start = $this->startDate;
  68. while ($start <= $this->endDate) {
  69. $end = mktime(0, 0, 0, date("m", $start) + 1, date("d", $start), date("Y", $start));
  70. // get Leads
  71. $this->getLeads($start, $end, $this->type);
  72. // get Sells
  73. $this->getSells($start, $end, $this->type);
  74. $this->getHits($start, $end, $this->type);
  75. $start = $end;
  76. $this->counter++;
  77. }
  78. break;
  79. // weekly
  80. case 3 :
  81. $start = $this->startDate;
  82. while ($start <= $this->endDate) {
  83. $end = mktime(0, 0, 0, date("m", $start), date("d", $start) + 7, date("Y", $start));
  84. // get Leads
  85. $this->getLeads($start, $end, $this->type);
  86. // get Sells
  87. $this->getSells($start, $end, $this->type);
  88. $this->getHits($start, $end, $this->type);
  89. $start = $end;
  90. $this->counter++;
  91. }
  92. break;
  93. // daily
  94. case 4 :
  95. $start = $this->startDate;
  96. while ($start <= $this->endDate) {
  97. $end = mktime(0, 0, 0, date("m", $start), date("d", $start) + 1, date("Y", $start));
  98. // get Leads
  99. $this->getLeads($start, '', $this->type);
  100. // get Sells
  101. $this->getSells($start, '', $this->type);
  102. $this->getHits($start, '', $this->type);
  103. $start = $end;
  104. $this->counter++;
  105. }
  106. break;
  107. }
  108. $this->counter = 0;
  109. $this->counterCMP++;
  110. }
  111. // $this->printResult();
  112. $this->total['sum_plain'] = $this->total['sum'];
  113. $this->total['sum'] = $currencies->format($this->total['sum']);
  114. }
  115. }
  116. function getCampaigns() {
  117. $campaign = array ();
  118. $campaign_query = "SELECT * FROM ".TABLE_CAMPAIGNS;
  119. $campaign_query = xtc_db_query($campaign_query);
  120. while ($campaign_data = xtc_db_fetch_array($campaign_query)) {
  121. $campaign[] = array ('id' => $campaign_data['campaigns_refID'], 'text' => $campaign_data['campaigns_name']);
  122. $this->camp[$campaign_data['campaigns_refID']] = $campaign_data['campaigns_name'];
  123. }
  124. return $campaign;
  125. }
  126. function getSelectedCampaign() {
  127. $campaign = array ();
  128. $campaign_query = "SELECT * FROM ".TABLE_CAMPAIGNS." WHERE campaigns_refID='".$this->campaign."'";
  129. $campaign_query = xtc_db_query($campaign_query);
  130. while ($campaign_data = xtc_db_fetch_array($campaign_query)) {
  131. $campaign[] = array ('id' => $campaign_data['campaigns_refID'], 'text' => $campaign_data['campaigns_name']);
  132. }
  133. return $campaign;
  134. }
  135. function getTotalLeads() {
  136. $end = mktime(0, 0, 0, date("m", $this->endDate), date("d", $this->endDate) + 1, date("Y", $this->endDate));
  137. $selection = " and ci.customers_info_date_account_created>'".xtc_db_input(date("Y-m-d", $this->startDate))."'"." and ci.customers_info_date_account_created<'".xtc_db_input(date("Y-m-d", $end))."'";
  138. $lead_query = "SELECT count(*) as leads FROM ".TABLE_CUSTOMERS." c, ".TABLE_CUSTOMERS_INFO." ci WHERE c.customers_id=ci.customers_info_id".$selection;
  139. $lead_query = xtc_db_query($lead_query);
  140. $lead_data = xtc_db_fetch_array($lead_query);
  141. $this->total['leads'] = $lead_data['leads'];
  142. }
  143. function getTotalSells() {
  144. $end = mktime(0, 0, 0, date("m", $this->endDate), date("d", $this->endDate) + 1, date("Y", $this->endDate));
  145. $selection = " and o.date_purchased>'".xtc_db_input(date("Y-m-d", $this->startDate))."'"." and o.date_purchased<'".xtc_db_input(date("Y-m-d", $end))."'";
  146. $status = "";
  147. if ($this->status > 0)
  148. $status = " and o.orders_status='".$this->status."'";
  149. $sale_query = "SELECT count(*) as sells, SUM(ot.value/o.currency_value) as Summe FROM ".TABLE_ORDERS." o, ".TABLE_ORDERS_TOTAL." ot WHERE o.orders_id=ot.orders_id and ot.class='ot_total'".$selection.$status;
  150. $sale_query = xtc_db_query($sale_query);
  151. $sale_data = xtc_db_fetch_array($sale_query);
  152. $this->total['sells'] = $sale_data['sells'];
  153. $this->total['sum'] = $sale_data['Summe'];
  154. }
  155. function getSells($date_start, $date_end = '', $type) {
  156. global $currencies;
  157. switch ($type) {
  158. case 1 :
  159. case 2 :
  160. case 3 :
  161. $selection = " and o.date_purchased>'".xtc_db_input(date("Y-m-d", $date_start))."'"." and o.date_purchased<'".xtc_db_input(date("Y-m-d", $date_end))."'";
  162. break;
  163. // daily
  164. case 4 :
  165. $end = mktime(0, 0, 0, date("m", $date_start), date("d", $date_start) + 1, date("Y", $date_start));
  166. $selection = " and o.date_purchased>'".xtc_db_input(date("Y-m-d", $date_start))."'"." and o.date_purchased<'".xtc_db_input(date("Y-m-d", $end))."'";
  167. break;
  168. }
  169. $status = "";
  170. if ($this->status > 0)
  171. $status = " and o.orders_status='".$this->status."'";
  172. $sell_query = "SELECT count(*) as sells, SUM(ot.value/o.currency_value) as Summe FROM ".TABLE_ORDERS." o, ".TABLE_ORDERS_TOTAL." ot WHERE o.orders_id=ot.orders_id and ot.class='ot_total' and o.conversion_type='1' and o.refferers_id='".$this->campaign."'".$selection.$status;
  173. $sell_query = xtc_db_query($sell_query);
  174. $sell_data = xtc_db_fetch_array($sell_query);
  175. $late_sell_query = "SELECT count(*) as sells, SUM(ot.value/o.currency_value) as Summe FROM ".TABLE_ORDERS." o, ".TABLE_ORDERS_TOTAL." ot WHERE o.orders_id=ot.orders_id and ot.class='ot_total' and o.conversion_type='2' and o.refferers_id='".$this->campaign."'".$selection.$status;
  176. $late_sell_query = xtc_db_query($late_sell_query);
  177. $late_sell_data = xtc_db_fetch_array($late_sell_query);
  178. $this->result[$this->counterCMP]['result'][$this->counter]['sells'] = $sell_data['sells'];
  179. $this->result[$this->counterCMP]['result'][$this->counter]['sum'] = $currencies->format(($sell_data['Summe']+$late_sell_data['Summe']));
  180. $this->result[$this->counterCMP]['sells_s'] += $sell_data['sells'];
  181. $this->result[$this->counterCMP]['sum_s'] += ($sell_data['Summe']+$late_sell_data['Summe']);
  182. if ($this->total['sells'] == 0) {
  183. $this->result[$this->counterCMP]['result'][$this->counter]['sells_p'] = 0;
  184. $this->result[$this->counterCMP]['result'][$this->counter]['late_sells_p'] = 0;
  185. $this->result[$this->counterCMP]['result'][$this->counter]['sum_p'] = 0;
  186. } else {
  187. $this->result[$this->counterCMP]['result'][$this->counter]['sells_p'] = $sell_data['sells'] / $this->total['sells'] * 100;
  188. $this->result[$this->counterCMP]['result'][$this->counter]['late_sells_p'] = round($late_sell_data['sells'] / $this->total['sells'] * 100,2);
  189. $this->result[$this->counterCMP]['result'][$this->counter]['sum_p'] = round(($sell_data['Summe']+$late_sell_data['Summe'])/$this->total['sum']*100,2);
  190. }
  191. $this->result[$this->counterCMP]['result'][$this->counter]['late_sells'] = $late_sell_data['sells'];
  192. $this->result[$this->counterCMP]['late_sells_s'] += $late_sell_data['sells'];
  193. }
  194. function getLeads($date_start, $date_end = '', $type) {
  195. switch ($type) {
  196. case 1 :
  197. case 2 :
  198. case 3 :
  199. $selection = " and ci.customers_info_date_account_created>'".xtc_db_input(date("Y-m-d", $date_start))."'"." and ci.customers_info_date_account_created<'".xtc_db_input(date("Y-m-d", $date_end))."'";
  200. break;
  201. case 4 :
  202. $end = mktime(0, 0, 0, date("m", $date_start), date("d", $date_start) + 1, date("Y", $date_start));
  203. $selection = " and ci.customers_info_date_account_created>'".xtc_db_input(date("Y-m-d", $date_start))."'"." and ci.customers_info_date_account_created<'".xtc_db_input(date("Y-m-d", $end))."'";
  204. break;
  205. }
  206. // select leads
  207. $lead_query = "SELECT count(*) as leads FROM ".TABLE_CUSTOMERS." c, ".TABLE_CUSTOMERS_INFO." ci WHERE c.customers_id=ci.customers_info_id AND c.refferers_id='".$this->campaign."'".$selection;
  208. $lead_query = xtc_db_query($lead_query);
  209. $lead_data = xtc_db_fetch_array($lead_query);
  210. $this->result[$this->counterCMP]['result'][$this->counter]['range'] = $this->getDateFormat($date_start, $date_end);
  211. $this->result[$this->counterCMP]['result'][$this->counter]['leads'] = $lead_data['leads'];
  212. $this->result[$this->counterCMP]['leads_s'] += $lead_data['leads'];
  213. if ($this->total['leads'] == 0) {
  214. $this->result[$this->counterCMP]['result'][$this->counter]['leads_p'] = 0;
  215. } else {
  216. $this->result[$this->counterCMP]['result'][$this->counter]['leads_p'] = $lead_data['leads'] / $this->total['leads'] * 100;
  217. }
  218. }
  219. function getHits($date_start, $date_end = '', $type) {
  220. switch ($type) {
  221. case 1 :
  222. case 2 :
  223. case 3 :
  224. $selection = " and time>'".xtc_db_input(date("Y-m-d", $date_start))."'"." and time <'".xtc_db_input(date("Y-m-d", $date_end))."'";
  225. break;
  226. case 4 :
  227. $end = mktime(0, 0, 0, date("m", $date_start), date("d", $date_start) + 1, date("Y", $date_start));
  228. $selection = " and time>'".xtc_db_input(date("Y-m-d", $date_start))."'"." and time<'".xtc_db_input(date("Y-m-d", $end))."'";
  229. break;
  230. }
  231. // select leads
  232. $hits_query = "SELECT count(*) as hits FROM ".TABLE_CAMPAIGNS_IP." WHERE campaign='".$this->campaign."'".$selection;
  233. $hits_query = xtc_db_query($hits_query);
  234. $hits_data = xtc_db_fetch_array($hits_query);
  235. $this->result[$this->counterCMP]['result'][$this->counter]['hits'] = $hits_data['hits'];
  236. $this->result[$this->counterCMP]['hits_s'] += $hits_data['hits'];
  237. if ($this->total['hits'] == 0) {
  238. $this->result[$this->counterCMP]['result'][$this->counter]['hits_s'] = 0;
  239. } else {
  240. $this->result[$this->counterCMP]['result'][$this->counter]['hits_s'] = $hits_data['hits'] / $this->total['hits'] * 100;
  241. }
  242. }
  243. function getDateFormat($date_from, $date_to) {
  244. if ($date_from != $date_to && $date_to != '') {
  245. return date(DATE_FORMAT, $date_from).'-'.date(DATE_FORMAT, $date_to);
  246. } else {
  247. return date(DATE_FORMAT, $date_from);
  248. }
  249. }
  250. function printResult() {
  251. echo '<pre>';
  252. print_r($this->result);
  253. print_r($this->total);
  254. echo '</pre>';
  255. }
  256. }
  257. ?>