|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @package API |
| 4 | + * @version 1.5 |
| 5 | + * @author Brian Edgerton |
| 6 | + * @link http://www.edgewebworks.com |
| 7 | + * @copyright Copyright (C) 2011 Edge Web Works, LLC. All rights reserved. |
| 8 | + * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php |
| 9 | +*/ |
| 10 | + |
| 11 | +defined('_JEXEC') or die( 'Restricted access' ); |
| 12 | + |
| 13 | +jimport('joomla.plugin.plugin'); |
| 14 | + |
| 15 | +class RedshopApiResourceDetails extends ApiResource |
| 16 | +{ |
| 17 | + public function get() |
| 18 | + { |
| 19 | + $startdate = JRequest::getVar('startdate'); |
| 20 | + $startdate .= '00:00:00'; |
| 21 | + $enddate = JRequest::getVar('enddate'); |
| 22 | + $enddate .= ' 23:59:59'; |
| 23 | + $config =& JFactory::getConfig(); |
| 24 | + $offset = $config->getValue('config.offset'); |
| 25 | + $startdate= & JFactory::getDate($startdate,$offset); |
| 26 | + $enddate= & JFactory::getDate($enddate,$offset); |
| 27 | + $startdate = $startdate->toFormat('%F %T'); |
| 28 | + $enddate = $enddate->toFormat('%F %T'); |
| 29 | + |
| 30 | + $db = JFactory::getDBO(); |
| 31 | + $query = "SELECT a.order_item_sku AS product_sku,b.product_name,SUM(a.product_quantity)AS quantity, |
| 32 | + SUM(a.product_final_price) AS product_sales |
| 33 | + FROM #__redshop_order_item AS a,#__redshop_product AS b WHERE a.product_id=b.product_id |
| 34 | + AND a.order_id IN (SELECT order_id FROM #__redshop_orders |
| 35 | + WHERE order_status='S' AND order_payment_status='Paid') |
| 36 | + AND FROM_UNIXTIME(a.mdate) |
| 37 | + BETWEEN '$startdate' AND '$enddate' |
| 38 | + GROUP BY a.order_item_sku ORDER BY product_sales DESC"; |
| 39 | + |
| 40 | + $db->setQuery( $query ); |
| 41 | + $details['data'] =array($db->loadObjectList()); |
| 42 | + |
| 43 | + $query ="SELECT SUM(order_total) |
| 44 | + FROM #__redshop_orders |
| 45 | + WHERE DATE(FROM_UNIXTIME(mdate))BETWEEN '".$startdate."'AND '".$enddate."' |
| 46 | + AND order_status='s' AND order_payment_status='Paid'"; |
| 47 | + $db->setQuery( $query ); |
| 48 | + $total =(int)$db->loadResult(); |
| 49 | + |
| 50 | + if($total>9999) |
| 51 | + { |
| 52 | + $total = $total/1000; |
| 53 | + $total .='K'; |
| 54 | + } |
| 55 | + elseif(!$total) |
| 56 | + { $total=0;} |
| 57 | + |
| 58 | + $details['total'] =array("total"=>$total); |
| 59 | + |
| 60 | + $this->plugin->setResponse($details); |
| 61 | + |
| 62 | + //$this->plugin->setResponse( 'Guru Katre' ); |
| 63 | + } |
| 64 | + |
| 65 | + public function post() |
| 66 | + { |
| 67 | + $db = JFactory::getDBO(); |
| 68 | + |
| 69 | + require_once(dirname(__FILE__).DS.'helper.php'); |
| 70 | + |
| 71 | + //get date from app |
| 72 | + $startdate = JRequest::getVar('startdate'); |
| 73 | + $enddate = JRequest::getVar('enddate'); |
| 74 | + |
| 75 | + $startdate .= ' 00:00:00'; |
| 76 | + $enddate .= ' 23:59:59'; |
| 77 | + |
| 78 | + $sdate = $startdate; |
| 79 | + $edate = $enddate; |
| 80 | + |
| 81 | + //get offset value |
| 82 | + $config =& JFactory::getConfig(); |
| 83 | + $offset = $config->getValue('config.offset'); |
| 84 | + |
| 85 | + $startdate= & JFactory::getDate($startdate,$offset); |
| 86 | + $enddate= & JFactory::getDate($enddate,$offset); |
| 87 | + $startdate = $startdate->toFormat('%F %T'); |
| 88 | + $enddate = $enddate->toFormat('%F %T'); |
| 89 | + //query for product details |
| 90 | + $query = "SELECT a.order_item_sku AS product_sku,b.product_name,SUM(a.product_quantity)AS quantity, |
| 91 | + SUM(a.product_final_price) AS product_sales |
| 92 | + FROM #__redshop_order_item AS a,#__redshop_product AS b WHERE a.product_id=b.product_id |
| 93 | + AND a.order_id IN (SELECT order_id FROM #__redshop_orders |
| 94 | + WHERE order_status='S' AND order_payment_status='Paid') |
| 95 | + AND FROM_UNIXTIME(a.mdate) |
| 96 | + BETWEEN '$startdate' AND '$enddate' |
| 97 | + GROUP BY a.order_item_sku ORDER BY product_sales DESC"; |
| 98 | + |
| 99 | + $db->setQuery( $query ); |
| 100 | + $details['data'] =array($db->loadObjectList()); |
| 101 | + $i = 0; |
| 102 | + while(count($details['data'][0])>=$i) |
| 103 | + { |
| 104 | + if($details['data'][0][$i]->product_sales > 9999) |
| 105 | + { |
| 106 | + $value = $details['data'][0][$i]->product_sales/1000; |
| 107 | + $value = number_format($value, 2, '.', ''); |
| 108 | + $value .='K'; |
| 109 | + $details['data'][0][$i]->product_sales = $value; |
| 110 | + |
| 111 | + } |
| 112 | + $i++; |
| 113 | + } |
| 114 | + $total = Sale_Data::total($startdate,$enddate); |
| 115 | + $projected_sale = Sale_Data::projected_sale($sdate,$edate,$total); |
| 116 | + $total = Sale_Data::compress($total); |
| 117 | + $projected_sale = Sale_Data::compress($projected_sale); |
| 118 | + |
| 119 | + $details['total'] =array("total"=>$total); |
| 120 | + $details['projected_sale'] =array("projected_sale"=>$projected_sale); |
| 121 | + $this->plugin->setResponse($details); |
| 122 | + } |
| 123 | + |
| 124 | + public function put() |
| 125 | + { |
| 126 | + $app = JFactory::getApplication(); |
| 127 | + $data = JRequest::getVar('jform', array(), 'post', 'array'); |
| 128 | + $context = 'com_content.edit.article'; |
| 129 | + |
| 130 | + // Fake parameters |
| 131 | + $values = (array) $app->getUserState($context.'.id'); |
| 132 | + array_push($values, (int) $data['id']); |
| 133 | + $values = array_unique($values); |
| 134 | + $app->setUserState($context.'.id', $values); |
| 135 | + if ( !JRequest::getInt( 'id' ) ) { |
| 136 | + $_POST['id'] = $data['id']; |
| 137 | + $_REQUEST['id'] = $data['id']; |
| 138 | + } |
| 139 | + |
| 140 | + // Simply call post as Joomla will just save an article with an id |
| 141 | + $this->post(); |
| 142 | + |
| 143 | + $response = $this->plugin->get( 'response' ); |
| 144 | + if ( isset( $response->success ) && $response->success ) { |
| 145 | + JResponse::setHeader( 'status', 200, true ); |
| 146 | + $response->code = 200; |
| 147 | + $this->plugin->setResponse( $response ); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments