PageRenderTime 91ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/local/Mycode/Function/Helper/Data.php

https://bitbucket.org/acidel/buykoala
PHP | 1452 lines | 1045 code | 316 blank | 91 comment | 85 complexity | d97179a455708430daab24160375167f MD5 | raw file
  1. <?php
  2. class Mycode_Function_Helper_Data extends Mage_Core_Helper_Abstract
  3. {
  4. public function weekly_detailed_report_v2(){
  5. $day = new DateTime();
  6. $day->modify( "-7 day" );
  7. $fromDate = $day->format("Y-m-d 00:00:00");
  8. $weekday = "Saturday";
  9. if (date('l') == "$weekday")
  10. {
  11. $week = strtotime("last $weekday");
  12. }
  13. else
  14. {
  15. $week = strtotime("last $weekday-1 week");
  16. }
  17. $startdate = (date("Y-m-d 00:00:00", $week));
  18. $enddate = date("Y-m-d 00:00:00", $week + 7*86400);
  19. // $sat= date("Y-m-d 00:00:00", mktime(0,0,0,9,22,2012));
  20. //$start = date("Y-m-d 00:00:00", mktime(0,0,0,9,15,2012));
  21. $sql = "SELECT DISTINCT order_id FROM sales_flat_order_item where created_at<'$enddate' and created_at>'$startdate'";
  22. //echo $sql;die();
  23. // $sql = "SELECT DISTINCT order_id FROM sales_flat_order_item where created_at>'$fromDate'";
  24. $orders = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql);
  25. foreach ($orders as $neworder)
  26. {
  27. $order_id = $neworder['order_id'];
  28. $order = Mage::getModel('sales/order')->load($order_id);
  29. $date = $order->getCreatedAt();
  30. $_shippingAddress = $order->getBillingAddress();
  31. $date = "".date("d/m/Y",strtotime($date))."";
  32. $fname = $order['customer_firstname'];
  33. $lname = $order['customer_lastname'];
  34. $purchase_date =Mage::app()->getLocale()->date(strtotime($order['updated_at']), null, null, false)->toString('dd/MM/yyyy');
  35. $city = $_shippingAddress->getCity();
  36. $address = $_shippingAddress->getStreetFull();
  37. $state = $_shippingAddress->getRegion() ;
  38. $postcode = $_shippingAddress->getPostcode();
  39. $phone = $_shippingAddress->getTelephone();
  40. $country_code = $_shippingAddress->getCountry();
  41. $country=Mage::app()->getLocale()->getCountryTranslation($country_code);
  42. $items = $order->getAllItems();
  43. $itemcount=count($items);
  44. //echo $itemcount;die();
  45. $name=array();
  46. $unitPrice=array();
  47. $sku=array();
  48. $ids=array();
  49. $qty=array();
  50. foreach ($items as $itemId => $item)
  51. {
  52. $ids=$item->getProductId();
  53. //echo $item->getStatus();
  54. $sql = "SELECT merchant_id FROM groupdeals WHERE product_id = $ids";
  55. $res = Mage::getSingleton('core/resource')->getConnection('core_read')->fetchOne($sql);
  56. if ($res && $res!="")
  57. {
  58. $item_id = $item->getId();
  59. $_prod = Mage::getModel('catalog/product')->load($ids);
  60. if ($_prod->getData('commission_percent') > 0)
  61. {
  62. $commis_percent = $_prod->getData('commission_percent');//commission_percent
  63. }
  64. else
  65. {
  66. $commis_percent = 0;
  67. }
  68. if ($_prod->getData('commission_fixed') > 0)
  69. {
  70. $commis_fixed = $_prod->getData('commission_fixed');//commission_fixed
  71. }
  72. else
  73. {
  74. $commis_fixed = 0;
  75. }
  76. if ($_prod->getData('aammount') > 0)
  77. {
  78. $additional_ammount = $_prod->getData('aammount');//aammount
  79. }
  80. else
  81. {
  82. $additional_ammount = 0;
  83. }
  84. //echo "($ids, $res)";
  85. $sql = "SELECT twitter FROM groupdeals_merchants WHERE merchants_id = $res";
  86. $freq = Mage::getSingleton('core/resource')->getConnection('core_read')->fetchOne($sql);
  87. // echo "$freq </br>";
  88. $sql = "SELECT product_options FROM sales_flat_order_item WHERE product_id = $ids AND order_id = $order_id and item_id = $item_id";
  89. $opts = unserialize(Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchOne($sql));
  90. $sql = "SELECT coupon_code FROM groupdeals_coupons WHERE order_item_id = $item_id";
  91. $code = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql);
  92. unset($coupon);
  93. foreach ($code as $value)
  94. {
  95. $coupon.="".$value["coupon_code"]."-";
  96. }
  97. $coupon = substr($coupon,0,-1);
  98. unset ($variable);
  99. foreach ($opts['options'] as $option)
  100. {
  101. $variable[] = "".$option['label'].": ".$option['value']."";
  102. }
  103. $prodname = $item->getName();
  104. $commission = (100 - $commis_percent) / 100;
  105. if ($commission != 1)
  106. {
  107. $unitPrice = ($item->getBasePriceInclTax()- $additional_ammount) * $commission;
  108. }
  109. else
  110. {
  111. $unitPrice = ($item->getBasePriceInclTax() - $additional_ammount) - $commis_fixed;
  112. }
  113. $sku=$item->getSku();
  114. $qty=$item->getQtyOrdered();
  115. //echo $freq;die();
  116. if ($res > 0 && $freq == '0||1')//weekely report
  117. {
  118. $searchArray = array("\r\n", ",", "\n");
  119. $replaceArray = array(" ", "-", " ");
  120. $products_row[$ids][] = array($order->getIncrementId(), $date, $coupon, $fname, $lname, str_replace($searchArray, $replaceArray, $address),
  121. $city, $state, $postcode, $country, $phone, $prodname, $ids, "$".($item->getBasePriceInclTax()-$additional_ammount)."", $qty, $variable[0], $variable[1]);
  122. $report_summery[$ids]['qty']+= $qty;
  123. $report_summery[$ids]['revenue']+= ($qty * ($item->getBasePriceInclTax()-$additional_ammount));
  124. $report_summery[$ids]['merchant']+= ($qty * $unitPrice);
  125. $merchant[$ids] = $res;
  126. $product_name[$ids] = $prodname;
  127. }
  128. }
  129. }
  130. }
  131. //echo "blah";die();
  132. $header = array('Order ID','Transaction Date', 'Coupon Code','First Name','Last Name','Address','City','State','Postcode','Country','Phone Number','Product Name', 'Product Id', 'Unit Price ($)', 'Qty Sold', 'Option 1','Option 2');
  133. //echo "fdsjkhg";die();
  134. //echo "<pre>"; print_r($products_row);die();
  135. foreach ($products_row as $key=>$value)
  136. {
  137. unset ($data);
  138. //echo $key;die();
  139. $data[] = $header;
  140. foreach ($value as $newvalue)
  141. {
  142. $data[] = $newvalue;
  143. }
  144. $data[]=array("","");
  145. $data[]=array("","");
  146. $data[]=array("Total Items",$report_summery[$key]['qty']);
  147. $data[]=array("Total Revenue($)","$".$report_summery[$key]['revenue']."");
  148. $data[]=array("Total Commission($)","$".($report_summery[$key]['revenue'] - $report_summery[$key]['merchant'])."");
  149. $data[]=array("Total payable to merchant($)","$".$report_summery[$key]['merchant']."");
  150. $this->create_pdf($data);
  151. //echo "<pre>"; print_r($data);die();
  152. $mage_csv = new Varien_File_Csv();
  153. $file_path = '/tmp/sample.csv';
  154. //echo ($merchant[$key]);die();
  155. $sql = "SELECT email, name FROM groupdeals_merchants WHERE merchants_id = ".$merchant[$key]."";
  156. //echo $sql;die();
  157. $email_add = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql);
  158. //echo "<pre>";print_r($email_add);die();
  159. $email_ser = explode('||', $email_add[0]['email']);
  160. $email = $email_ser[1];
  161. $name_ser = explode('||', $email_add[0]['name']);
  162. $name = $name_ser[1];
  163. //echo $name;echo $email;die();
  164. $mage_csv->saveData($file_path, $data);
  165. try
  166. {
  167. $today = date("d-m-Y", Mage::getModel('core/date')->timestamp(time()));
  168. $mail = new Zend_Mail();
  169. $mail->setFrom("deals@ikoala.com.au","iKoala Team");
  170. $mail->addTo($email,$name);//$email
  171. $mail->addCc('merchantsalesreports@ikoala.com.au','ikOala');//$email
  172. $mail->addCc('accounts@ikoala.com.au','ikOala');//$email
  173. $mail->setSubject("Weekly Reports");
  174. $mail->setBodyHtml("Dear $name, <br/><br/> Please find attached the weekly reports of your products. <br/><br/>Sincerly,<br/>iKoala Team"); // here u also use setBodyText options.
  175. $fileContents = file_get_contents($file_path);
  176. $file = $mail->createAttachment($fileContents);
  177. $file->filename = "".$name."_sales_reports_".$product_name[$key]."_".$today.".csv";
  178. $fileContents = file_get_contents('/tmp/test1.pdf');
  179. $file = $mail->createAttachment($fileContents);
  180. $file->filename = "".$name."_sales_reports_".$product_name[$key]."_".$today.".pdf";
  181. $mail->send();
  182. //echo "done";die();
  183. }
  184. catch(Exception $e)
  185. {
  186. echo $e->getMassage();
  187. }
  188. }
  189. }
  190. /////////////////////////////////////////
  191. public function daily_detailed_report_v2(){
  192. $day = new DateTime();
  193. $day->modify( "-1 day" );
  194. $fromDate = $day->format("Y-m-d 00:00:00");
  195. $sql = "SELECT DISTINCT order_id FROM sales_flat_order_item where created_at>'$fromDate'";
  196. $orders = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql);
  197. foreach ($orders as $neworder)
  198. {
  199. $order_id = $neworder['order_id'];
  200. $order = Mage::getModel('sales/order')->load($order_id);
  201. $date = $order->getCreatedAt();
  202. $_shippingAddress = $order->getBillingAddress();
  203. $date = "".date("d/m/Y",strtotime($date))."";
  204. //$address = Mage::getModel('sales/order_address')->load($order_id);
  205. //echo "pre"; print_r($_shippingAddress->getCity());die();
  206. $fname = $order['customer_firstname'];
  207. $lname = $order['customer_lastname'];
  208. $purchase_date =Mage::app()->getLocale()->date(strtotime($order['updated_at']), null, null, false)->toString('dd/MM/yyyy');
  209. $city = $_shippingAddress->getCity();
  210. $address = $_shippingAddress->getStreetFull();
  211. $state = $_shippingAddress->getRegion() ;
  212. $postcode = $_shippingAddress->getPostcode();
  213. $phone = $_shippingAddress->getTelephone();
  214. $country_code = $_shippingAddress->getCountry();
  215. $country=Mage::app()->getLocale()->getCountryTranslation($country_code);
  216. //echo $address;die();
  217. //echo "<pre>";print_r($state);die();
  218. $items = $order->getAllItems();
  219. //$options = $order->getItemOptions();
  220. // echo "<pre>";print_r($options);die();
  221. $itemcount=count($items);
  222. $name=array();
  223. $unitPrice=array();
  224. $sku=array();
  225. $ids=array();
  226. $qty=array();
  227. foreach ($items as $itemId => $item)
  228. {
  229. $ids=$item->getProductId();
  230. $item_id = $item->getId();
  231. $_prod = Mage::getModel('catalog/product')->load($ids);
  232. if ($_prod->getData('commission_percent') > 0)
  233. {
  234. $commis_percent = $_prod->getData('commission_percent');//commission_percent
  235. }
  236. else
  237. {
  238. $commis_percent = 0;
  239. }
  240. if ($_prod->getData('commission_fixed') > 0)
  241. {
  242. $commis_fixed = $_prod->getData('commission_fixed');//commission_fixed
  243. }
  244. else
  245. {
  246. $commis_fixed = 0;
  247. }
  248. if ($_prod->getData('aammount') > 0)
  249. {
  250. $additional_ammount = $_prod->getData('aammount');//aammount
  251. }
  252. else
  253. {
  254. $additional_ammount = 0;
  255. }
  256. $sql = "SELECT merchant_id FROM groupdeals WHERE product_id = $ids";
  257. $res = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchOne($sql);
  258. $sql = "SELECT twitter FROM groupdeals_merchants WHERE merchants_id = $res";
  259. $freq = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchOne($sql);
  260. $sql = "SELECT product_options FROM sales_flat_order_item WHERE product_id = $ids AND order_id = $order_id and item_id = $item_id";
  261. $opts = unserialize(Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchOne($sql));
  262. $sql = "SELECT coupon_code FROM groupdeals_coupons WHERE order_item_id = $item_id";
  263. $code = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql);
  264. unset($coupon);
  265. foreach ($code as $value)
  266. {
  267. $coupon.="".$value["coupon_code"]."-";
  268. }
  269. $coupon = substr($coupon,0,-1);
  270. unset ($variable);
  271. foreach ($opts['options'] as $option)
  272. {
  273. //echo "<pre>";print_r($option);die();
  274. $variable[] = "".$option['label'].": ".$option['value']."";
  275. }
  276. /*if ($ids == 73)
  277. {
  278. echo "<pre>";print_r($variable[1]);die();
  279. }*/
  280. $prodname = $item->getName();
  281. $commission = (100 - $commis_percent) / 100;
  282. if ($commission != 1)
  283. {
  284. $unitPrice = ($item->getBasePriceInclTax() - $additional_ammount) * $commission;
  285. }
  286. else
  287. {
  288. $unitPrice = ($item->getBasePriceInclTax() - $additional_ammount) - $commis_fixed;
  289. }
  290. $sku=$item->getSku();
  291. $qty=$item->getQtyOrdered();
  292. //echo $freq;die();
  293. if ($res > 0 && $freq == '0||2')//daily report
  294. {
  295. //$products_row[$ids][] = array('Order ID', 'Coupon Code','First Name','Last Name','Address','City','State','Postcode','Phone Number','Product Name', 'Product Id', 'Unit Price ($)', 'Qty Sold', 'Option 1','Option 2');
  296. $searchArray = array("\r\n", ",", "\n");
  297. $replaceArray = array(" ", "-", " ");
  298. $products_row[$ids][] = array($order->getIncrementId(),$date, $coupon, $fname, $lname, str_replace($searchArray, $replaceArray, $address),
  299. $city, $state, $postcode, $phone, $prodname, $ids, "$".($item->getBasePriceInclTax() - $additional_ammount)."", $qty, $variable[0], $variable[1]);
  300. $report_summery[$ids]['qty']+= $qty;
  301. $report_summery[$ids]['revenue']+= ($qty * ($item->getBasePriceInclTax() - $additional_ammount));
  302. $report_summery[$ids]['merchant']+= ($qty * $unitPrice);
  303. $merchant[$ids] = $res;
  304. $product_name[$ids] = $prodname;
  305. }
  306. }
  307. }
  308. $header = array('Order ID','Transaction Date', 'Coupon Code','First Name','Last Name','Address','City','State','Postcode','Phone Number','Product Name', 'Product Id', 'Unit Price ($)', 'Qty Sold', 'Option 1','Option 2');
  309. //echo "fdsjkhg";die();
  310. //echo "<pre>"; print_r($products_row);die();
  311. foreach ($products_row as $key=>$value)
  312. {
  313. unset ($data);
  314. //echo $key;die();
  315. $data[] = $header;
  316. foreach ($value as $newvalue)
  317. {
  318. $data[] = $newvalue;
  319. }
  320. $data[]=array("","");
  321. $data[]=array("","");
  322. $data[]=array("Total Items",$report_summery[$key]['qty']);
  323. $data[]=array("Total Revenue($)","$".$report_summery[$key]['revenue']."");
  324. $data[]=array("Total Commission($)","$".($report_summery[$key]['revenue'] - $report_summery[$key]['merchant'])."");
  325. $data[]=array("Total payable to merchant($)","$".$report_summery[$key]['merchant']."");
  326. //echo "<pre>"; print_r($data);die();
  327. $mage_csv = new Varien_File_Csv();
  328. $file_path = '/tmp/sample.csv';
  329. //echo ($merchant[$key]);die();
  330. $sql = "SELECT email, name FROM groupdeals_merchants WHERE merchants_id = ".$merchant[$key]."";
  331. //echo $sql;die();
  332. $email_add = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql);
  333. //echo "<pre>";print_r($email_add);die();
  334. $email_ser = explode('||', $email_add[0]['email']);
  335. $email = $email_ser[1];
  336. $name_ser = explode('||', $email_add[0]['name']);
  337. $name = $name_ser[1];
  338. //echo $name;echo $email;die();
  339. $mage_csv->saveData($file_path, $data);
  340. try
  341. {
  342. $today = date("d-m-Y", Mage::getModel('core/date')->timestamp(time()));
  343. $mail = new Zend_Mail();
  344. $mail->setFrom("deals@ikoala.com.au","iKoala Team");
  345. $mail->addTo($email,$name);//$email
  346. $mail->addCc('merchantsalesreports@ikoala.com.au','ikOala');//$email
  347. $mail->addCc('accounts@ikoala.com.au','ikOala');//$email
  348. $mail->setSubject("Daily Reports");
  349. $mail->setBodyHtml("Dear $name, <br/><br/> Please find attached the daily reports of your products. <br/><br/>Sincerly,<br/>iKoala Team"); // here u also use setBodyText options.
  350. $fileContents = file_get_contents($file_path);
  351. $file = $mail->createAttachment($fileContents);
  352. $file->filename = "".$name."_sales_reports_".$product_name[$key]."_".$today.".csv";
  353. $mail->send();
  354. }
  355. catch(Exception $e)
  356. {
  357. echo $e->getMassage();
  358. }
  359. }
  360. }
  361. /////////////////////////////////////////
  362. public function weekly_detailed_report(){
  363. $day = new DateTime();
  364. $day->modify( "-7 day" );
  365. $fromDate = $day->format("Y-m-d 00:00:00");
  366. $sql = "SELECT DISTINCT order_id FROM sales_flat_order_item where created_at>'$fromDate'";
  367. $orders = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql);
  368. foreach ($orders as $neworder)
  369. {
  370. $order_id = $neworder['order_id'];
  371. $order = Mage::getModel('sales/order')->load($order_id);
  372. $_shippingAddress = $order->getBillingAddress();
  373. //$address = Mage::getModel('sales/order_address')->load($order_id);
  374. //echo "pre"; print_r($_shippingAddress->getCity());die();
  375. $fname = $order['customer_firstname'];
  376. $lname = $order['customer_lastname'];
  377. $purchase_date =Mage::app()->getLocale()->date(strtotime($order['updated_at']), null, null, false)->toString('dd/MM/yyyy');
  378. $city = $_shippingAddress->getCity();
  379. $address = $_shippingAddress->getStreetFull();
  380. $state = $_shippingAddress->getRegion() ;
  381. $postcode = $_shippingAddress->getPostcode();
  382. $phone = $_shippingAddress->getTelephone();
  383. //echo $address;die();
  384. //echo "<pre>";print_r($state);die();
  385. $items = $order->getAllItems();
  386. //$options = $order->getItemOptions();
  387. // echo "<pre>";print_r($options);die();
  388. $itemcount=count($items);
  389. $name=array();
  390. $unitPrice=array();
  391. $sku=array();
  392. $ids=array();
  393. $qty=array();
  394. foreach ($items as $itemId => $item)
  395. {
  396. $ids=$item->getProductId();
  397. $item_id = $item->getId();
  398. $_prod = Mage::getModel('catalog/product')->load($ids);
  399. if ($_prod->getData('commission_percent') > 0)
  400. {
  401. $commis_percent = $_prod->getData('commission_percent');//commission_percent
  402. }
  403. else
  404. {
  405. $commis_percent = 0;
  406. }
  407. if ($_prod->getData('commission_fixed') > 0)
  408. {
  409. $commis_fixed = $_prod->getData('commission_fixed');//commission_fixed
  410. }
  411. else
  412. {
  413. $commis_fixed = 0;
  414. }
  415. if ($_prod->getData('aammount') > 0)
  416. {
  417. $additional_ammount = $_prod->getData('aammount');//aammount
  418. }
  419. else
  420. {
  421. $additional_ammount = 0;
  422. }
  423. $sql = "SELECT merchant_id FROM groupdeals WHERE product_id = $ids";
  424. $res = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchOne($sql);
  425. $sql = "SELECT twitter FROM groupdeals_merchants WHERE merchants_id = $res";
  426. $freq = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchOne($sql);
  427. $sql = "SELECT product_options FROM sales_flat_order_item WHERE product_id = $ids AND order_id = $order_id and item_id = $item_id";
  428. $opts = unserialize(Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchOne($sql));
  429. $sql = "SELECT coupon_code FROM groupdeals_coupons WHERE order_item_id = $item_id";
  430. $coupon = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchOne($sql);
  431. unset ($variable);
  432. foreach ($opts['options'] as $option)
  433. {
  434. //echo "<pre>";print_r($option);die();
  435. $variable[] = "".$option['label'].": ".$option['value']."";
  436. }
  437. /*if ($ids == 73)
  438. {
  439. echo "<pre>";print_r($variable[1]);die();
  440. }*/
  441. $name = $item->getName();
  442. $commission = (100 - $commis_percent) / 100;
  443. if ($commission != 1)
  444. {
  445. $unitPrice = ($item->getPrice() - $additional_ammount) * $commission;
  446. }
  447. else
  448. {
  449. $unitPrice = ($item->getPrice() - $additional_ammount) - $commis_fixed;
  450. }
  451. $sku=$item->getSku();
  452. $qty=$item->getQtyOrdered();
  453. if ($res > 0 && $freq == '0||1')//weekely report
  454. {
  455. $product_order_details[$res][$item_id]['order'] = $order->getIncrementId();
  456. $product_order_details[$res][$item_id]['coupon'] = $coupon;
  457. $product_order_details[$res][$item_id]['fname'] = $fname;
  458. $product_order_details[$res][$item_id]['lname'] = $lname;
  459. $product_order_details[$res][$item_id]['address'] = str_replace(",","-",$address);
  460. $product_order_details[$res][$item_id]['city'] = $city;
  461. $product_order_details[$res][$item_id]['state'] = $state;
  462. $product_order_details[$res][$item_id]['pcode'] = $postcode;
  463. $product_order_details[$res][$item_id]['phone'] = $phone;
  464. $product_order_details[$res][$item_id]['option1'] = $variable[0];
  465. $product_order_details[$res][$item_id]['option2'] = $variable[1];
  466. $product_order_details[$res][$item_id]['name'] = $name;
  467. $product_order_details[$res][$item_id]['ids'] = $ids;
  468. $product_order_details[$res][$item_id]['unitPrice'] = $item->getPrice();
  469. $product_order_details[$res][$item_id]['qty'] += $qty;
  470. $product_order_details[$res][$item_id]['total_merchant'] += ($qty * $unitPrice);
  471. $product_order_details[$res][$item_id]['total_rev'] += ($qty * $item->getPrice());
  472. $product_order_details[$res][$item_id]['aa'] = ($qty * $additional_ammount);
  473. $product_order_details[$res][$item_id]['com'] = ($commis_percent);
  474. $product_order_details[$res][$item_id]['com_fixed'] = ($qty * $commis_fixed);
  475. $report_summery[$res]['qty']+= $qty;
  476. $report_summery[$res]['revenue']+= ($qty * $item->getPrice());
  477. $report_summery[$res]['merchant']+= ($qty * $unitPrice);
  478. }
  479. }
  480. }
  481. $mage_csv = new Varien_File_Csv();
  482. $file_path = '/tmp/sample.csv';
  483. foreach($product_order_details as $merch_id=>$arr)
  484. {
  485. unset($products_row);
  486. $products_row[] = array('Order ID', 'Coupon Code','First Name','Last Name','Address','City','State','Postcode','Phone Number','Product Name', 'Product Id', 'Unit Price ($)', 'Qty Sold', 'Option 1','Option 2', 'Total Revenue($)', 'Additional Ammount ($)', 'Commission Rate(%)','Commission Rate(fixed)', 'Net Payable to Merchant($)', 'Net Revenue iKoala($)');
  487. foreach ($arr as $key=>$value)
  488. {
  489. $data = array();
  490. $data[] = $value['order'];
  491. $data[] = $value['coupon'];
  492. $data[] = $value['fname'];
  493. $data[] = $value['lname'];
  494. $data[] = $value['address'];
  495. $data[] = $value['city'];
  496. $data[] = $value['state'];
  497. $data[] = $value['pcode'];
  498. $data[] = $value['phone'];
  499. $data[] = $value['name'];
  500. $data[] = $value['ids'];
  501. $data[] = $value['unitPrice'];
  502. $data[] = $value['qty'];
  503. $data[] = $value['option1'];
  504. $data[] = $value['option2'];
  505. $data[] = $value['total_rev'];
  506. $data[] = $value['aa'];
  507. $data[] = $value['com'];
  508. $data[] = $value['com_fixed'];
  509. $data[] = $value['total_merchant'];
  510. $data[] = $value['total_rev'] - $value['total_merchant'];
  511. $products_row[] = $data;
  512. }
  513. $products_row[]=array("","");
  514. $products_row[]=array("","");
  515. $products_row[]=array("Total Items",$report_summery[$merch_id]['qty']);
  516. $products_row[]=array("Total Revenue($)",$report_summery[$merch_id]['revenue']);
  517. $products_row[]=array("Total Commission($)",($report_summery[$merch_id]['revenue'] - $report_summery[$merch_id]['merchant']));
  518. $products_row[]=array("Total payable to merchant($)",$report_summery[$merch_id]['merchant']);
  519. $sql = "SELECT email, name FROM groupdeals_merchants WHERE merchants_id = $merch_id";
  520. $email_add = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql);
  521. //echo "<pre>";print_r($email_add);die();
  522. $email_ser = explode('||', $email_add[0]['email']);
  523. $email = $email_ser[1];
  524. $name_ser = explode('||', $email_add[0]['name']);
  525. $name = $name_ser[1];
  526. //echo $name;echo $email;die();
  527. $mage_csv->saveData($file_path, $products_row);
  528. try
  529. {
  530. $today = date("d-m-Y", Mage::getModel('core/date')->timestamp(time()));
  531. $mail = new Zend_Mail();
  532. $mail->setFrom("deals@ikoala.com.au","iKoala Team");
  533. $mail->addTo($email,$name);//$email
  534. $mail->addCc('merchantsalesreports@ikoala.com.au','ikOala');//$email
  535. $mail->addCc('accounts@ikoala.com.au','ikOala');//$email
  536. $mail->setSubject("Daily Reports");
  537. $mail->setBodyHtml("Dear $name, <br/><br/> Please find attached the daily reports of your products. <br/><br/>Sincerly,<br/>iKoala Team"); // here u also use setBodyText options.
  538. $fileContents = file_get_contents($file_path);
  539. $file = $mail->createAttachment($fileContents);
  540. $file->filename = "daily_reports_$today.csv";
  541. $mail->send();
  542. }
  543. catch(Exception $e)
  544. {
  545. echo $e->getMassage();
  546. }
  547. }
  548. }
  549. public function daily_detailed_report(){
  550. $day = new DateTime();
  551. $day->modify( "-1 day" );
  552. $fromDate = $day->format("Y-m-d 00:00:00");
  553. $sql = "SELECT DISTINCT order_id FROM sales_flat_order_item where created_at>'$fromDate'";
  554. $orders = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql);
  555. foreach ($orders as $neworder)
  556. {
  557. $order_id = $neworder['order_id'];
  558. $order = Mage::getModel('sales/order')->load($order_id);
  559. $_shippingAddress = $order->getBillingAddress();
  560. //$address = Mage::getModel('sales/order_address')->load($order_id);
  561. //echo "pre"; print_r($_shippingAddress->getCity());die();
  562. $fname = $order['customer_firstname'];
  563. $lname = $order['customer_lastname'];
  564. $purchase_date =Mage::app()->getLocale()->date(strtotime($order['updated_at']), null, null, false)->toString('dd/MM/yyyy');
  565. $city = $_shippingAddress->getCity();
  566. $address = $_shippingAddress->getStreetFull();
  567. $state = $_shippingAddress->getRegion() ;
  568. $postcode = $_shippingAddress->getPostcode();
  569. $phone = $_shippingAddress->getTelephone();
  570. //echo $address;die();
  571. //echo "<pre>";print_r($state);die();
  572. $items = $order->getAllItems();
  573. //$options = $order->getItemOptions();
  574. // echo "<pre>";print_r($options);die();
  575. $itemcount=count($items);
  576. $name=array();
  577. $unitPrice=array();
  578. $sku=array();
  579. $ids=array();
  580. $qty=array();
  581. foreach ($items as $itemId => $item)
  582. {
  583. $ids=$item->getProductId();
  584. $item_id = $item->getId();
  585. $_prod = Mage::getModel('catalog/product')->load($ids);
  586. if ($_prod->getData('commission_percent') > 0)
  587. {
  588. $commis_percent = $_prod->getData('commission_percent');//commission_percent
  589. }
  590. else
  591. {
  592. $commis_percent = 0;
  593. }
  594. if ($_prod->getData('commission_fixed') > 0)
  595. {
  596. $commis_fixed = $_prod->getData('commission_fixed');//commission_fixed
  597. }
  598. else
  599. {
  600. $commis_fixed = 0;
  601. }
  602. if ($_prod->getData('aammount') > 0)
  603. {
  604. $additional_ammount = $_prod->getData('aammount');//aammount
  605. }
  606. else
  607. {
  608. $additional_ammount = 0;
  609. }
  610. $sql = "SELECT merchant_id FROM groupdeals WHERE product_id = $ids";
  611. $res = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchOne($sql);
  612. $sql = "SELECT twitter FROM groupdeals_merchants WHERE merchants_id = $res";
  613. $freq = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchOne($sql);
  614. $sql = "SELECT product_options FROM sales_flat_order_item WHERE product_id = $ids AND order_id = $order_id and item_id = $item_id";
  615. $opts = unserialize(Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchOne($sql));
  616. $sql = "SELECT coupon_code FROM groupdeals_coupons WHERE order_item_id = $item_id";
  617. $coupon = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchOne($sql);
  618. unset ($variable);
  619. foreach ($opts['options'] as $option)
  620. {
  621. //echo "<pre>";print_r($option);die();
  622. $variable[] = "".$option['label'].": ".$option['value']."";
  623. }
  624. /*if ($ids == 73)
  625. {
  626. echo "<pre>";print_r($variable[1]);die();
  627. }*/
  628. $name = $item->getName();
  629. $commission = (100 - $commis_percent) / 100;
  630. if ($commission != 1)
  631. {
  632. $unitPrice = ($item->getPrice() - $additional_ammount) * $commission;
  633. }
  634. else
  635. {
  636. $unitPrice = ($item->getPrice() - $additional_ammount) - $commis_fixed;
  637. }
  638. $sku=$item->getSku();
  639. $qty=$item->getQtyOrdered();
  640. if ($res > 0 && $freq == '0||2')//daily report
  641. {
  642. $product_order_details[$res][$item_id]['order'] = $order->getIncrementId();
  643. $product_order_details[$res][$item_id]['coupon'] = $coupon;
  644. $product_order_details[$res][$item_id]['fname'] = $fname;
  645. $product_order_details[$res][$item_id]['lname'] = $lname;
  646. $product_order_details[$res][$item_id]['address'] = str_replace(",","-",$address);
  647. $product_order_details[$res][$item_id]['city'] = $city;
  648. $product_order_details[$res][$item_id]['state'] = $state;
  649. $product_order_details[$res][$item_id]['pcode'] = $postcode;
  650. $product_order_details[$res][$item_id]['phone'] = $phone;
  651. $product_order_details[$res][$item_id]['option1'] = $variable[0];
  652. $product_order_details[$res][$item_id]['option2'] = $variable[1];
  653. $product_order_details[$res][$item_id]['name'] = $name;
  654. $product_order_details[$res][$item_id]['ids'] = $ids;
  655. $product_order_details[$res][$item_id]['unitPrice'] = $item->getPrice();
  656. $product_order_details[$res][$item_id]['qty'] += $qty;
  657. $product_order_details[$res][$item_id]['total_merchant'] += ($qty * $unitPrice);
  658. $product_order_details[$res][$item_id]['total_rev'] += ($qty * $item->getPrice());
  659. $product_order_details[$res][$item_id]['aa'] = ($qty * $additional_ammount);
  660. $product_order_details[$res][$item_id]['com'] = ($commis_percent);
  661. $product_order_details[$res][$item_id]['com_fixed'] = ($qty * $commis_fixed);
  662. $report_summery[$res]['qty']+= $qty;
  663. $report_summery[$res]['revenue']+= ($qty * $item->getPrice());
  664. $report_summery[$res]['merchant']+= ($qty * $unitPrice);
  665. }
  666. }
  667. }
  668. $mage_csv = new Varien_File_Csv();
  669. $file_path = '/tmp/sample.csv';
  670. foreach($product_order_details as $merch_id=>$arr)
  671. {
  672. unset($products_row);
  673. $products_row[] = array('Order ID', 'Coupon Code','First Name','Last Name','Address','City','State','Postcode','Phone Number','Product Name', 'Product Id', 'Unit Price ($)', 'Qty Sold', 'Option 1','Option 2', 'Total Revenue($)', 'Additional Ammount ($)', 'Commission Rate(%)','Commission Rate(fixed)', 'Net Payable to Merchant($)', 'Net Revenue iKoala($)');
  674. foreach ($arr as $key=>$value)
  675. {
  676. $data = array();
  677. $data[] = $value['order'];
  678. $data[] = $value['coupon'];
  679. $data[] = $value['fname'];
  680. $data[] = $value['lname'];
  681. $data[] = $value['address'];
  682. $data[] = $value['city'];
  683. $data[] = $value['state'];
  684. $data[] = $value['pcode'];
  685. $data[] = $value['phone'];
  686. $data[] = $value['name'];
  687. $data[] = $value['ids'];
  688. $data[] = $value['unitPrice'];
  689. $data[] = $value['qty'];
  690. $data[] = $value['option1'];
  691. $data[] = $value['option2'];
  692. $data[] = $value['total_rev'];
  693. $data[] = $value['aa'];
  694. $data[] = $value['com'];
  695. $data[] = $value['com_fixed'];
  696. $data[] = $value['total_merchant'];
  697. $data[] = $value['total_rev'] - $value['total_merchant'];
  698. $products_row[] = $data;
  699. }
  700. $products_row[]=array("","");
  701. $products_row[]=array("","");
  702. $products_row[]=array("Total Items",$report_summery[$merch_id]['qty']);
  703. $products_row[]=array("Total Revenue($)",$report_summery[$merch_id]['revenue']);
  704. $products_row[]=array("Total Commission($)",($report_summery[$merch_id]['revenue'] - $report_summery[$merch_id]['merchant']));
  705. $products_row[]=array("Total payable to merchant($)",$report_summery[$merch_id]['merchant']);
  706. $sql = "SELECT email, name FROM groupdeals_merchants WHERE merchants_id = $merch_id";
  707. $email_add = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql);
  708. //echo "<pre>";print_r($email_add);die();
  709. $email_ser = explode('||', $email_add[0]['email']);
  710. $email = $email_ser[1];
  711. $name_ser = explode('||', $email_add[0]['name']);
  712. $name = $name_ser[1];
  713. //echo $name;echo $email;die();
  714. $mage_csv->saveData($file_path, $products_row);
  715. try
  716. {
  717. $today = date("d-m-Y", Mage::getModel('core/date')->timestamp(time()));
  718. $mail = new Zend_Mail();
  719. $mail->setFrom("deals@ikoala.com.au","iKoala Team");
  720. $mail->addTo($email,$name);//$email
  721. $mail->addCc('merchantsalesreports@ikoala.com.au','ikOala');//$email
  722. $mail->addCc('accounts@ikoala.com.au','ikOala');//$email
  723. $mail->setSubject("Daily Reports");
  724. $mail->setBodyHtml("Dear $name, <br/><br/> Please find attached the daily reports of your products. <br/><br/>Sincerly,<br/>iKoala Team"); // here u also use setBodyText options.
  725. $fileContents = file_get_contents($file_path);
  726. $file = $mail->createAttachment($fileContents);
  727. $file->filename = "daily_reports_$today.csv";
  728. $mail->send();
  729. }
  730. catch(Exception $e)
  731. {
  732. echo $e->getMassage();
  733. }
  734. }
  735. }
  736. public function send_daily_report(){
  737. $day = new DateTime();
  738. $day->modify( "-1 day" );
  739. $fromDate = $day->format("Y-m-d 00:00:00");
  740. $sql = "SELECT DISTINCT order_id FROM sales_flat_order_item where created_at>'$fromDate'";
  741. $orders = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql);
  742. foreach ($orders as $neworder)
  743. {
  744. $order_id = $neworder['order_id'];
  745. $order = Mage::getModel('sales/order')->load($order_id);
  746. $items = $order->getAllItems();
  747. $itemcount=count($items);
  748. $name=array();
  749. $unitPrice=array();
  750. $sku=array();
  751. $ids=array();
  752. $qty=array();
  753. foreach ($items as $itemId => $item)
  754. {
  755. $ids=$item->getProductId();
  756. $_prod = Mage::getModel('catalog/product')->load($ids);
  757. if ($_prod->getData('commission_percent') > 0)
  758. {
  759. $commis_percent = $_prod->getData('commission_percent');//commission_percent
  760. }
  761. else
  762. {
  763. $commis_percent = 0;
  764. }
  765. if ($_prod->getData('commission_fixed') > 0)
  766. {
  767. $commis_fixed = $_prod->getData('commission_fixed');//commission_fixed
  768. }
  769. else
  770. {
  771. $commis_fixed = 0;
  772. }
  773. if ($_prod->getData('aammount') > 0)
  774. {
  775. $additional_ammount = $_prod->getData('aammount');//aammount
  776. }
  777. else
  778. {
  779. $additional_ammount = 0;
  780. }
  781. $sql = "SELECT merchant_id FROM groupdeals WHERE product_id = $ids";
  782. $res = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchOne($sql);
  783. $sql = "SELECT twitter FROM groupdeals_merchants WHERE merchants_id = $res";
  784. $freq = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchOne($sql);
  785. $name = $item->getName();
  786. $commission = (100 - $commis_percent) / 100;
  787. if ($commission != 1)
  788. {
  789. $unitPrice = ($item->getPrice() - $additional_ammount) * $commission;
  790. }
  791. else
  792. {
  793. $unitPrice = ($item->getPrice() - $additional_ammount) - $commis_fixed;
  794. }
  795. $sku=$item->getSku();
  796. $qty=$item->getQtyOrdered();
  797. if ($res > 0 && $freq == '0||2')//daily report
  798. {
  799. $product_order_details[$res][$ids]['name'] = $name;
  800. $product_order_details[$res][$ids]['ids'] = $ids;
  801. $product_order_details[$res][$ids]['unitPrice'] = $item->getPrice();
  802. $product_order_details[$res][$ids]['qty'] += $qty;
  803. $product_order_details[$res][$ids]['total_merchant'] += ($qty * $unitPrice);
  804. $product_order_details[$res][$ids]['total_rev'] += ($qty * $item->getPrice());
  805. //$product_order_details[$res][$ids]['gp'] = ($item->getPrice());
  806. $product_order_details[$res][$ids]['aa'] = ($qty * $additional_ammount);
  807. $product_order_details[$res][$ids]['com'] = ($commis_percent);
  808. $product_order_details[$res][$ids]['com_fixed'] = ($qty * $commis_fixed);
  809. }
  810. }
  811. }
  812. $mage_csv = new Varien_File_Csv();
  813. $file_path = '/tmp/sample.csv';
  814. foreach($product_order_details as $merch_id=>$arr)
  815. {
  816. unset($products_row);
  817. $products_row[] = array('Product Name', 'Product Id', 'Unit Price ($)', 'Qty Sold', 'Total Revenue($)', 'Additional Ammount ($)', 'Commission Rate(%)','Commission Rate(fixed)', 'Net Payable to Merchant($)', 'Net Revenue iKoala($)');
  818. foreach ($arr as $key=>$value)
  819. {
  820. $data = array();
  821. $data[] = $value['name'];
  822. $data[] = $value['ids'];
  823. $data[] = $value['unitPrice'];
  824. $data[] = $value['qty'];
  825. $data[] = $value['total_rev'];
  826. $data[] = $value['aa'];
  827. $data[] = $value['com'];
  828. $data[] = $value['com_fixed'];
  829. $data[] = $value['total_merchant'];
  830. $data[] = $value['total_rev'] - $value['total_merchant'];
  831. $products_row[] = $data;
  832. }
  833. $sql = "SELECT email, name FROM groupdeals_merchants WHERE merchants_id = $merch_id";
  834. $email_add = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql);
  835. //echo "<pre>";print_r($email_add);die();
  836. $email_ser = explode('||', $email_add[0]['email']);
  837. $email = $email_ser[1];
  838. $name_ser = explode('||', $email_add[0]['name']);
  839. $name = $name_ser[1];
  840. //echo $name;echo $email;die();
  841. $mage_csv->saveData($file_path, $products_row);
  842. try
  843. {
  844. $today = date("d-m-Y", Mage::getModel('core/date')->timestamp(time()));
  845. $mail = new Zend_Mail();
  846. $mail->setFrom("deals@ikoala.com.au","iKoala Team");
  847. $mail->addTo($email,$name);
  848. $mail->addCc('merchantsalesreports@ikoala.com.au','ikOala');//$email
  849. $mail->addCc('accounts@ikoala.com.au','ikOala');//$email
  850. $mail->setSubject("Daily Reports");
  851. $mail->setBodyHtml("Dear $name, <br/><br/> Please find attached the daily reports of your products. <br/><br/>Sincerly,<br/>iKoala Team"); // here u also use setBodyText options.
  852. $fileContents = file_get_contents($file_path);
  853. $file = $mail->createAttachment($fileContents);
  854. $file->filename = "daily_reports_$today.csv";
  855. $mail->send();
  856. }
  857. catch(Exception $e)
  858. {
  859. echo $e->getMassage();
  860. }
  861. }
  862. }
  863. public function send_weekely_report(){
  864. $day = new DateTime();
  865. $day->modify( "-7 day" );
  866. $fromDate = $day->format("Y-m-d 00:00:00");
  867. $sql = "SELECT DISTINCT order_id FROM sales_flat_order_item where created_at>'$fromDate'";
  868. $orders = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql);
  869. foreach ($orders as $neworder)
  870. {
  871. $order_id = $neworder['order_id'];
  872. $order = Mage::getModel('sales/order')->load($order_id);
  873. $items = $order->getAllItems();
  874. $itemcount=count($items);
  875. $name=array();
  876. $unitPrice=array();
  877. $sku=array();
  878. $ids=array();
  879. $qty=array();
  880. foreach ($items as $itemId => $item)
  881. {
  882. $ids=$item->getProductId();
  883. $_prod = Mage::getModel('catalog/product')->load($ids);
  884. if ($_prod->getData('commission_percent') > 0)
  885. {
  886. $commis_percent = $_prod->getData('commission_percent');//commission_percent
  887. }
  888. else
  889. {
  890. $commis_percent = 0;
  891. }
  892. if ($_prod->getData('commission_fixed') > 0)
  893. {
  894. $commis_fixed = $_prod->getData('commission_fixed');//commission_fixed
  895. }
  896. else
  897. {
  898. $commis_fixed = 0;
  899. }
  900. if ($_prod->getData('aammount') > 0)
  901. {
  902. $additional_ammount = $_prod->getData('aammount');//aammount
  903. }
  904. else
  905. {
  906. $additional_ammount = 0;
  907. }
  908. $sql = "SELECT merchant_id FROM groupdeals WHERE product_id = $ids";
  909. $res = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchOne($sql);
  910. $sql = "SELECT twitter FROM groupdeals_merchants WHERE merchants_id = $res";
  911. $freq = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchOne($sql);
  912. $name = $item->getName();
  913. $commission = (100 - $commis_percent) / 100;
  914. if ($commission != 1)
  915. {
  916. $unitPrice = ($item->getPrice() - $additional_ammount) * $commission;
  917. }
  918. else
  919. {
  920. $unitPrice = ($item->getPrice() - $additional_ammount) - $commis_fixed;
  921. }
  922. $sku=$item->getSku();
  923. $qty=$item->getQtyOrdered();
  924. if ($res > 0 && 1 == 1)//weekely report, $freq == '0||1'
  925. {
  926. $product_order_details[$res][$ids]['name'] = $name;
  927. $product_order_details[$res][$ids]['ids'] = $ids;
  928. $product_order_details[$res][$ids]['unitPrice'] = $item->getPrice();
  929. $product_order_details[$res][$ids]['qty'] += $qty;
  930. $product_order_details[$res][$ids]['total_merchant'] += ($qty * $unitPrice);
  931. $product_order_details[$res][$ids]['total_rev'] += ($qty * $item->getPrice());
  932. //$product_order_details[$res][$ids]['gp'] = ($item->getPrice());
  933. $product_order_details[$res][$ids]['aa'] = ($qty * $additional_ammount);
  934. $product_order_details[$res][$ids]['com'] = ($commis_percent);
  935. $product_order_details[$res][$ids]['com_fixed'] = ($qty * $commis_fixed);
  936. }
  937. }
  938. }
  939. $mage_csv = new Varien_File_Csv();
  940. $file_path = '/tmp/sample.csv';
  941. foreach($product_order_details as $merch_id=>$arr)
  942. {
  943. unset($products_row);
  944. $products_row[] = array('Product Name', 'Product Id', 'Unit Price ($)', 'Qty Sold', 'Total Revenue($)', 'Additional Ammount ($)', 'Commission Rate(%)','Commission Rate(fixed)', 'Net Payable to Merchant($)', 'Net Revenue iKoala($)');
  945. foreach ($arr as $key=>$value)
  946. {
  947. $data = array();
  948. $data[] = $value['name'];
  949. $data[] = $value['ids'];
  950. $data[] = $value['unitPrice'];
  951. $data[] = $value['qty'];
  952. $data[] = $value['total_rev'];
  953. $data[] = $value['aa'];
  954. $data[] = $value['com'];
  955. $data[] = $value['com_fixed'];
  956. $data[] = $value['total_merchant'];
  957. $data[] = $value['total_rev'] - $value['total_merchant'];
  958. $products_row[] = $data;
  959. }
  960. $sql = "SELECT email, name FROM groupdeals_merchants WHERE merchants_id = $merch_id";
  961. $email_add = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql);
  962. //echo "<pre>";print_r($email_add);die();
  963. $email_ser = explode('||', $email_add[0]['email']);
  964. $email = $email_ser[1];
  965. $name_ser = explode('||', $email_add[0]['name']);
  966. $name = $name_ser[1];
  967. //echo $name;echo $email;die();
  968. $mage_csv->saveData($file_path, $products_row);
  969. try
  970. {
  971. $today = date("d-m-Y", Mage::getModel('core/date')->timestamp(time()));
  972. $mail = new Zend_Mail();
  973. $mail->setFrom("deals@ikoala.com.au","iKoala Team");
  974. $mail->addTo($email,$name);
  975. $mail->addCc('merchantsalesreports@ikoala.com.au','ikOala');//$email
  976. $mail->addCc('accounts@ikoala.com.au','ikOala');//$email
  977. $mail->setSubject("Weekely Reports");
  978. $mail->setBodyHtml("Dear $name, <br/><br/> Please find attached the weekely reports of your products. <br/><br/>Sincerly,<br/>iKoala Team"); // here u also use setBodyText options.
  979. $fileContents = file_get_contents($file_path);
  980. $file = $mail->createAttachment($fileContents);
  981. $file->filename = "weekely_reports_$today.csv";
  982. $mail->send();
  983. }
  984. catch(Exception $e)
  985. {
  986. echo $e->getMassage();
  987. }
  988. }
  989. }
  990. public function getLinks()
  991. {
  992. $store_array = array(5,3,6,4);
  993. foreach ($store_array as $store)
  994. {
  995. if ($store == 5)
  996. {
  997. $store_var = "?store=everyday";
  998. }
  999. elseif ($store == 3)
  1000. {
  1001. $store_var = "?store=kids";
  1002. }
  1003. elseif ($store == 6)
  1004. {
  1005. $store_var = "?store=business";
  1006. }
  1007. elseif ($store == 4)
  1008. {
  1009. $store_var = "?store=travel";
  1010. }
  1011. $collection = Mage::getModel('catalog/product')->getCollection()
  1012. ->addFieldToFilter('product_store', array('finset'=>$store))
  1013. ->setOrder('groupdeal_type','ASC')
  1014. ->addAttributeToSelect( '*' )
  1015. ->load();
  1016. //echo "<pre>";print_r($collection);die();
  1017. foreach ($collection as $collect)
  1018. {
  1019. //$store_url[$store] = $this->getUrl('');
  1020. $id = $collect->getId();
  1021. $_topprod = Mage::getModel('catalog/product')->load($id);
  1022. if (($_topprod->getGroupdealStatus()==1) && ($_topprod->isSaleable()))
  1023. {
  1024. $topcat = $_topprod->getCategoryIds();
  1025. unset ($topcatname);
  1026. foreach ($topcat as $value)
  1027. {
  1028. $_cate = Mage::getModel('catalog/category')->load($value);
  1029. $topcatname[] = $_cate->getName();
  1030. }
  1031. /*if (in_array($_REQUEST['cityfilter'], $topcatname))
  1032. {
  1033. $store_url[$store] = $_topprod->getProductUrl();
  1034. if ($_REQUEST['cityfilter'] && $_REQUEST['cityfilter']!="")
  1035. {
  1036. $store_url[$store].="?city=".urlencode($_REQUEST['cityfilter'])."";
  1037. }
  1038. break;
  1039. }
  1040. else*/
  1041. if(in_array('National', $topcatname))
  1042. {
  1043. $store_url[$store] = "".$_topprod->getProductUrl()."$store_var";
  1044. break;
  1045. }
  1046. }
  1047. }
  1048. }
  1049. return $store_url;
  1050. }
  1051. public function create_pdf($data)
  1052. {
  1053. //$this->_isExport = true;
  1054. // echo "aaa";
  1055. // die();
  1056. $pdf = new Zend_Pdf();
  1057. $page = $pdf->newPage('1684: 1190:');//Zend_Pdf_Page::SIZE_A4_LANDSCAPE //'1684: 1190:'
  1058. $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES);
  1059. $page->setFont($font, 9);
  1060. $width = $page->getWidth();
  1061. $pageHeight = $page->getHeight();
  1062. $i=0;
  1063. foreach ($data as $row) {
  1064. $b++;
  1065. if ($line_height > 30)
  1066. {
  1067. $j = $line_height ;
  1068. }
  1069. else
  1070. {
  1071. $j+=30;
  1072. }
  1073. $line_height=0;
  1074. $i = 10;
  1075. $f=0;
  1076. foreach ($row as $column)
  1077. {
  1078. $f++;
  1079. if ($f == 3 )
  1080. {
  1081. $str_arr= str_split(strtolower($column),11);
  1082. }
  1083. elseif ($f==1 && $column == "Total payable to merchant($)")
  1084. {
  1085. $str_arr= str_split(strtolower($column),17);
  1086. }
  1087. elseif ($f==1)
  1088. {
  1089. $str_arr= str_split(strtolower($column),20);
  1090. }
  1091. else
  1092. {
  1093. $str_arr= str_split(strtolower($column),28);
  1094. }
  1095. $height = $j;
  1096. foreach ($str_arr as $string)
  1097. {
  1098. $page->drawText($string, $i, $pageHeight-$height);
  1099. $height+=30;
  1100. }
  1101. if ($height > $line_height)
  1102. {
  1103. $line_height = $height;
  1104. }
  1105. if ($f == 1 ||$f == 2 ||$f == 9 || $f == 10 || $f == 11 || $f == 13 || $f == 14 ||$f == 15 )
  1106. {
  1107. $i+=80;
  1108. }
  1109. else
  1110. {
  1111. $i+=115;
  1112. }
  1113. }
  1114. if ($b == 19)
  1115. {
  1116. $pdf->pages[] = $page;
  1117. $b = 0;
  1118. $page = $pdf->newPage('1684: 1190:');//Zend_Pdf_Page::SIZE_A4_LANDSCAPE //'1684: 1190:'
  1119. $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES);
  1120. $page->setFont($font, 9);
  1121. $width = $page->getWidth();
  1122. $pageHeight = $page->getHeight();
  1123. $i=0;
  1124. $j= 0;
  1125. $height=0;
  1126. $line_height =0;
  1127. }
  1128. }
  1129. //echo "<pre>";print_r($page);die();
  1130. //echo "bbb";
  1131. $pdf->pages[] = $page;
  1132. $pdf->save('/tmp/test1.pdf');
  1133. return $pdf->render();
  1134. }
  1135. }
  1136. ?>