setDBO(VoxelCareDB::getDatabase()); $view =JRequest::getVar('view'); // Orden $this->filter_order_Dir = $mainframe->getUserStateFromRequest( $option.$view.'.filter_order_Dir', 'filter_order_Dir', '', 'word' ); $this->filter_order = $mainframe->getUserStateFromRequest( $option.$view.'.filter_order', 'filter_order', 'userid', 'cmd' ); $this->searchstartdate = $mainframe->getUserStateFromRequest( "$option.$view.searchstartdate", 'searchstartdate', $this->searchstartdate, 'string' ); $this->searchstartdate = JString::strtolower( $this->searchstartdate ); $this->searchenddate = $mainframe->getUserStateFromRequest( "$option.$view.searchenddate", 'searchenddate', $this->searchenddate, 'string' ); $this->searchenddate = JString::strtolower( $this->searchenddate ); $this->searchreporttype = $mainframe->getUserStateFromRequest( "$option.$view.searchreporttype", 'searchreporttype', $this->searchreporttype, 'string' ); $this->searchreporttype = JString::strtolower( $this->searchreporttype ); $this->searchperiodicity = $mainframe->getUserStateFromRequest( "$option.$view.searchperiodicity", 'searchperiodicity', $this->searchperiodicity, 'int' ); $this->searchproducts = $mainframe->getUserStateFromRequest( "$option.$view.searchproducts", 'searchproducts', $this->searchproducts, 'array' ); $this->searchminproducts = $mainframe->getUserStateFromRequest( "$option.$view.searchminproducts", 'searchminproducts', $this->searchminproducts, 'int' ); $this->searchpaymentmethods = $mainframe->getUserStateFromRequest( "$option.$view.searchpaymentmethods", 'searchpaymentmethods', $this->searchpaymentmethods, 'array' ); $this->searchgroupbygroups = $mainframe->getUserStateFromRequest( "$option.$view.searchgroupbygroups", 'searchgroupbygroups', $this->searchgroupbygroups, 'int' ); $this->searchfranchise = $mainframe->getUserStateFromRequest( "$option.$view.searchfranchise", 'searchfranchise', '', 'string' ); if (!$this->searchfranchise) { $userId =LegacyHelper::getUserId(); $this->_db->setQuery("SELECT uf.franchise id FROM jos_vxc_userfranchise uf WHERE uf.userid = ".$userId); $this->searchfranchise = $this->_db->loadResult(); } //$limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', 10, 'int' ); $limit = 0; // No hay limite $limitstart = JRequest::getVar('limitstart',0); $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0); $this->setState('limit', $limit); $this->setState('limitstart', $limitstart); } function _buildQuery() { switch($this->searchreporttype) { case 'salesevolution': case 'invoiceevolution': return $this->_buildQueryEvolution(); case 'usercredit': return $this->_buildQueryUserCredit(); case 'usercreditinc': return $this->_buildQueryUserCreditInc(); case 'manufacturing': return $this->_buildQueryManufacturing(); case 'userincoming': return $this->_buildQueryUserIncoming(); case 'predesign': return $this->_buildPredesign(); case 'creditcard': case 'samples': return $this->_buildQueryBatches(); case 'scanners': return $this->_buildQueryScanners(); default: return ''; } } function _buildPredesign() { $where = array(); $hasStartDate = false; if ($this->searchstartdate) { $startDateArray = explode('/', $this->searchstartdate); if (count($startDateArray)==3) { $mysqlStartDate =sprintf("%04d",(int)$startDateArray[2]).'-'.sprintf("%02d",(int)$startDateArray[1]).'-'.sprintf("%02d",(int)$startDateArray[0]); $hasStartDate = true; } } if ($this->searchenddate) { $endDateArray = explode('/', $this->searchenddate); if (count($endDateArray)==3) { $hasEndDate = true; $mysqlEndDate = sprintf("%04d",(int)$endDateArray[2]).'-'.sprintf("%02d",(int)$endDateArray[1]).'-'.sprintf("%02d",(int)$endDateArray[0]); } } if (!$hasStartDate) { $mysqlStartDate = '2009-10-01'; $hasStartDate = true; } if (!$hasEndDate) { $mysqlEndDate = date( 'Y-m-d H:i:s' ); $hasEndDate = true; } if (!$hasStartDate || !$hasEndDate) $where[] = " 1=0 "; $startDateTime = strtotime($mysqlStartDate); $endDateTime = strtotime($mysqlEndDate); if ($startDateTime < $endDateTime) { $where[] = "o.predesigned_date >= '".$mysqlStartDate."'"; $where[] = "o.predesigned_date <= '".$mysqlEndDate."'"; } $where[] = "o.predesigned_date IS NOT NULL"; $where[] = " uf.franchise = ". $this->searchfranchise; $where =( count($where) ) ? ' WHERE ' . implode( ' AND ', $where ) : ''; $this->_query = "SELECT u.id userid, u.name username, o.predesigned_date `date` , o.id `order` FROM jos_vxc_order o LEFT JOIN jos_vxc_user u ON u.id = o.predesigning_user LEFT JOIN jos_vxc_userfranchise uf ON uf.userid = u.id $where ORDER BY u.name ASC, o.predesigned_date ASC "; return $this->_query; } function _buildQueryScanners() { $where = array(); $where[] = " uf.franchise = ". $this->searchfranchise; $where =( count($where) ) ? ' WHERE ' . implode( ' AND ', $where ) : ''; $this->_query = "SELECT u.name podologo, sl.scanner_serial scanner_serial, sl.scanner_type scanner_type, COUNT(*) numscans, MAX(sl.scan_date) last_scan FROM #__vxc_scanlog sl LEFT JOIN #__vxc_user u ON u.id = sl.userid LEFT JOIN #__vxc_userfranchise uf ON uf.userid = u.id $where GROUP BY u.id,sl.scanner_serial ORDER BY u.name,sl.scanner_serial "; return $this->_query; } function _buildQueryBatches() { $where = array(); $hasStartDate = false; if ($this->searchstartdate) { $startDateArray = explode('/', $this->searchstartdate); if (count($startDateArray)==3) { $mysqlStartDate =sprintf("%04d",(int)$startDateArray[2]).'-'.sprintf("%02d",(int)$startDateArray[1]).'-'.sprintf("%02d",(int)$startDateArray[0]); $hasStartDate = true; } } if ($this->searchenddate) { $endDateArray = explode('/', $this->searchenddate); if (count($endDateArray)==3) { $hasEndDate = true; $mysqlEndDate = sprintf("%04d",(int)$endDateArray[2]).'-'.sprintf("%02d",(int)$endDateArray[1]).'-'.sprintf("%02d",(int)$endDateArray[0]); } } if (!$hasStartDate) { $mysqlStartDate = '2009-10-01'; $hasStartDate = true; } if (!$hasEndDate) { $mysqlEndDate = date( 'Y-m-d H:i:s' ); $hasEndDate = true; } if (!$hasStartDate || !$hasEndDate) $where[] = " 1=0 "; $startDateTime = strtotime($mysqlStartDate); $endDateTime = strtotime($mysqlEndDate); if ($startDateTime < $endDateTime) { $where[] = "b.payment_date >= '".$mysqlStartDate."'"; $where[] = "b.payment_date <= '".$mysqlEndDate."'"; } if ($this->searchreporttype == 'creditcard') $where[] = " pm.nameid = 'CREDITCARD' "; else if ($this->searchreporttype == 'samples') $where[] = " pm.nameid = 'SAMPLE' "; /* $where[] = " u.amount_ordered>0 "; $where[] = " (u.amount_paid - u.amount_ordered) != 0 ";*/ $where[] = " uf.franchise = ". $this->searchfranchise; $where =( count($where) ) ? ' WHERE ' . implode( ' AND ', $where ) : ''; $this->_query = "SELECT u.name username, b.id batch, GROUP_CONCAT(o.id SEPARATOR ',') orders, b.payment_date payment_date, i.total invoiced FROM #__vxc_batch b LEFT JOIN #__vxc_order o ON o.batch = b.id LEFT JOIN #__vxc_user u ON b.payment_user = u.id LEFT JOIN #__vxc_userfranchise uf ON uf.userid = u.id LEFT JOIN #__vxc_paymentmethod pm ON pm.id = b.payment_method LEFT JOIN #__vxc_invoice i ON i.batch = b.id $where GROUP BY b.id ORDER BY b.payment_date ASC "; return $this->_query; } function _buildQueryUserCreditInc() { $where = array(); $groupby =array(); $hasStartDate = false; if ($this->searchstartdate) { $startDateArray = explode('/', $this->searchstartdate); if (count($startDateArray)==3) { $mysqlStartDate =sprintf("%04d",(int)$startDateArray[2]).'-'.sprintf("%02d",(int)$startDateArray[1]).'-'.sprintf("%02d",(int)$startDateArray[0]); $hasStartDate = true; } } if ($this->searchenddate) { $endDateArray = explode('/', $this->searchenddate); if (count($endDateArray)==3) { $hasEndDate = true; $mysqlEndDate = sprintf("%04d",(int)$endDateArray[2]).'-'.sprintf("%02d",(int)$endDateArray[1]).'-'.sprintf("%02d",(int)$endDateArray[0]); } } if (!$hasStartDate) { $mysqlStartDate = '2009-10-01'; $hasStartDate = true; } if (!$hasEndDate) { $mysqlEndDate = date( 'Y-m-d H:i:s' ); $hasEndDate = true; } if (!$hasStartDate || !$hasEndDate) $where[] = " 1=0 "; $startDateTime = strtotime($mysqlStartDate); $endDateTime = strtotime($mysqlEndDate); if ($startDateTime < $endDateTime) { //$startDateArray = explode('-',$mysqlStartDate); //$endDateArray = explode('-',$mysqlEndDate); $where[] = "pm.paymentdate >= '".$mysqlStartDate."'"; $where[] = "pm.paymentdate <= '".$mysqlEndDate."'"; } // $where[] = " u.amount_ordered>0 "; $where[] = " (u.amount_paid - u.amount_ordered) != 0 "; $where[] = " uf.franchise = ". $this->searchfranchise; $where =( count($where) ) ? ' WHERE ' . implode( ' AND ', $where ) : ''; $groupby = ( count($groupby) ) ? ' GROUP BY ' . implode( ' , ', $groupby ) : ''; $this->_query = "SELECT u.name username, pm.amount amount, pm.paymentdate paymentdate, IFNULL( u2.name,'TPV') adminuser, pm.remarks remarks FROM #__vxc_user u LEFT JOIN #__vxc_userfranchise uf ON uf.userid = u.id LEFT JOIN #__vxc_paidmoneylog pm ON pm.receiveruser = u.id LEFT JOIN #__vxc_user u2 ON u2.id = pm.payeruser $where $groupby ORDER BY paymentdate ASC "; return $this->_query; } function _buildQueryUserCredit() { $where = array(); $groupby =array(); $where[] = " u.amount_ordered>0 "; $where[] = " (u.amount_paid - u.amount_ordered) != 0 "; $where[] = " uf.franchise = ". $this->searchfranchise; $where =( count($where) ) ? ' WHERE ' . implode( ' AND ', $where ) : ''; $groupby = ( count($groupby) ) ? ' GROUP BY ' . implode( ' , ', $groupby ) : ''; $orderby = ''; $orders = array(''); if (!in_array($this->filter_order,$orders)) $this->filter_order = 'id'; if (($this->filter_order) && ($this->filter_order_Dir)) { $orderby = ' ORDER BY '. $this->filter_order .' '. $this->filter_order_Dir; } $this->_query = "SELECT u.name username, u.amount_paid amount_paid, u.amount_ordered amount_ordered, ( u.amount_paid - u.amount_ordered ) credit FROM #__vxc_user u LEFT JOIN #__vxc_userfranchise uf ON uf.userid = u.id $where $groupby $orderby "; return $this->_query; } function _buildQueryEvolution() { $whereAts = array(); $whereOrders = array(); if ($this->searchproducts && count($this->searchproducts)>0) { $whereMaterials = array(); $prods =&$this->getProducts(); foreach($this->searchproducts as $prod) { $values = $prods[$prod]['values']; if (count($values)>0) { $whereMaterials[] = "(ty.name = 'InsoleOptions' AND atvl.name IN ('".implode("','",$values)."'))"; $whereMaterials[] = "(ty.name = 'InsoleOptions' AND atvr.name IN ('".implode("','",$values)."'))"; } $product = $prods[$prod]['product']; if (count($product)>0) { $whereMaterials[] = "(at.name = 'general.product' AND at.value IN ('".implode("','",$product)."'))"; } } $whereMaterials =( count($whereMaterials) ) ? implode( ' OR ', $whereMaterials ) : ''; if ($whereMaterials) $whereAts[] = "($whereMaterials)"; } if ($this->searchpaymentmethods && count($this->searchpaymentmethods)>0) { $whereOrders[] = " pm.nameid IN ('".implode("','",$this->searchpaymentmethods)."')"; } $havingStr = ''; if ($this->searchreporttype=='salesevolution') { if ($this->searchminproducts > 0) { $havingStr = 'HAVING total >= '. $this->searchminproducts; } $whereOrders[] = "o.batch IS NOT NULL"; $whereOrders[] = "o.deleted IS NOT TRUE"; } $whereAts =( count($whereAts) ) ? implode( ' AND ', $whereAts ) : ''; $hasStartDate = false; if ($this->searchstartdate) { $startDateArray = explode('/', $this->searchstartdate); if (count($startDateArray)==3) { $mysqlStartDate =sprintf("%04d",(int)$startDateArray[2]).'-'.sprintf("%02d",(int)$startDateArray[1]).'-'.sprintf("%02d",(int)$startDateArray[0]); $hasStartDate = true; } } if ($this->searchenddate) { $endDateArray = explode('/', $this->searchenddate); if (count($endDateArray)==3) { $hasEndDate = true; $mysqlEndDate = sprintf("%04d",(int)$endDateArray[2]).'-'.sprintf("%02d",(int)$endDateArray[1]).'-'.sprintf("%02d",(int)$endDateArray[0]); } } if (!$hasStartDate) { $mysqlStartDate = '2009-10-01'; $hasStartDate = true; } if (!$hasEndDate) { $mysqlEndDate = date( 'Y-m-d H:i:s' ); $hasEndDate = true; } if (!$hasStartDate || !$hasEndDate) $whereOrders[] = " 1=0 "; $startDateTime = strtotime($mysqlStartDate); $endDateTime = strtotime($mysqlEndDate); $weekMode = ($this->searchperiodicity == 0); $casos = array(); $mesosemanaCond = ''; if ($startDateTime < $endDateTime) { $startDateArray = explode('-',$mysqlStartDate); $endDateArray = explode('-',$mysqlEndDate); $mesosemanaCond = ','; if ($weekMode) { $startWeek = (int)date("W",$startDateTime); $endWeek =(int)date("W",$endDateTime); if ($startWeek > 40 && $startDateArray[1]==1 ) $startWeek =1; if ($endWeek > 40 && $endDateArray[1]==1 ) $endWeek =1; $week = $startWeek; $year = (int) $startDateArray[0]; do { $txt = $year.'-'.$week; $casos[$txt] = $txt; if ($week==53) { $year = $year +1; $week = 1; } else $week ++; } while (($year != $endDateArray[0] || $week != $endWeek ) ); $txt = $year.'-'.$week; $casos[$txt] = $txt; $mesosemanaCond .= "CONCAT(YEAR(b.payment_date),'-',WEEK(b.payment_date,3)) AS mesosemana"; } else { $month = (int)$startDateArray[1]; $year = (int) $startDateArray[0]; $mesosemanaCond .= "CONCAT(YEAR(b.payment_date),'-',MONTH(b.payment_date)) AS mesosemana"; if ($year != $endDateArray[0] || $month!= $endDateArray[1]) { do { $txt = $year.'-'.$month; $casos[$txt] = $txt; if ($month==12) { $year = $year +1; $month = 1; } else $month ++; } while (($year != $endDateArray[0] || $month != $endDateArray[1] )); } $txt = $year.'-'.$month; $casos[$txt] = $txt; } $whereOrders[] = "b.payment_date >= '".$mysqlStartDate."'"; $whereOrders[] = "b.payment_date <= '".$mysqlEndDate."'"; } $whereOrders[] = " uf.franchise = ". $this->searchfranchise; $whereOrders =( count($whereOrders) ) ? implode( ' AND ', $whereOrders ) : ''; $pivotColumns = array(); foreach($casos as $key=>$value) $pivotColumns[] = "SUM(CASE mesosemana WHEN '$key' THEN cuenta ELSE NULL END) AS '$value'"; $columns = array(); $columns[] = "podologo"; $columns[] = "listagrupos"; $columns[] = "SUM(cuenta) total"; foreach($pivotColumns as $col) $columns[] = $col; $columnsStr = implode(',',$columns); if ($this->searchreporttype == 'salesevolution') { $this->_query = " SELECT $columnsStr FROM ( SELECT COUNT(*) cuenta, (SELECT GROUP_CONCAT(g.name SEPARATOR ', ') FROM #__vxc_group g, #__vxc_usergroup ug WHERE ug.group = g.id AND ug.user = u.id ) listagrupos , u.name podologo $mesosemanaCond FROM #__vxc_order o LEFT JOIN #__vxc_batch b ON o.batch = b.id LEFT JOIN #__vxc_paymentmethod pm ON pm.id = b.payment_method LEFT JOIN #__vxc_user u ON b.payment_user = u.id LEFT JOIN #__vxc_userfranchise uf ON uf.userid = u.id WHERE $whereOrders AND o.id IN ( SELECT at.orderid FROM #__vxc_attribute at LEFT JOIN #__vxc_attributetype ty ON ty.id = at.type LEFT JOIN #__vxc_attributetypevalue atvl ON atvl.id = at.value_left LEFT JOIN #__vxc_attributetypevalue atvr ON atvr.id = at.value_right WHERE at.orderid = o.id AND $whereAts ) GROUP BY u.id,o.id ) AS datos GROUP BY podologo, listagrupos $havingStr "; } else { $this->_query = " SELECT $columnsStr FROM ( SELECT SUM(i.total) cuenta, (SELECT GROUP_CONCAT(g.name SEPARATOR ', ') FROM #__vxc_group g, #__vxc_usergroup ug WHERE ug.group = g.id AND ug.user = u.id ) listagrupos , u.name podologo $mesosemanaCond FROM #__vxc_invoice i LEFT JOIN #__vxc_batch b ON b.id = i.batch LEFT JOIN #__vxc_paymentmethod pm ON pm.id = b.payment_method LEFT JOIN #__vxc_user u ON b.payment_user = u.id LEFT JOIN #__vxc_userfranchise uf ON uf.userid = u.id WHERE $whereOrders AND b.id IN ( SELECT o.batch FROM #__vxc_order o LEFT JOIN #__vxc_attribute at ON at.orderid = o.id LEFT JOIN #__vxc_attributetype ty ON ty.id = at.type LEFT JOIN #__vxc_attributetypevalue atvl ON atvl.id = at.value_left LEFT JOIN #__vxc_attributetypevalue atvr ON atvr.id = at.value_right WHERE o.batch = b.id AND $whereAts ) GROUP BY u.id,i.id ) AS datos GROUP BY podologo, listagrupos $havingStr "; } //echo $this->_query; return $this->_query; } function getData() { if (empty($this->_data)) { $query = $this->_buildQuery(); //echo nl2br($query); $this->_db->setQuery( $query,$this->getState('limitstart'), $this->getState('limit') ); $this->_data = $this->_db->loadObjectList(); } return $this->_data; } function getList() { // table ordering $lists['order_Dir'] = $this->filter_order_Dir; $lists['order'] = $this->filter_order; // search filter $lists['searchstartdate']= $this->searchstartdate; $lists['searchenddate']= $this->searchenddate; $lists['searchperiodicity']= $this->searchperiodicity; $lists['searchproducts']= $this->searchproducts; $lists['searchminproducts']= $this->searchminproducts; $lists['searchpaymentmethods']= $this->searchpaymentmethods; $lists['searchgroupbygroups']= $this->searchgroupbygroups; $lists['searchreporttype']= $this->searchreporttype; $lists['paymentmethods'] = $this->getPaymentMethods(); $lists['products'] = $this->getProducts(); return $lists; } function getTotal() { // Load the content if it doesn't already exist if (empty($this->_total)) { $query = $this->_buildQuery(); $this->_total = $this->_getListCount($query); } return $this->_total; } function getPagination() { // Load the content if it doesn't already exist if (empty($this->_pagination)) { jimport('joomla.html.pagination'); $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') ); } return $this->_pagination; } function getPaymentMethods() { $db =& $this->_db; $sql = "SELECT pm.nameid value , pm.name text FROM #__vxc_paymentmethod pm "; $db->setQuery($sql); $objs =& $db->loadObjectList(); $freeMethods = array(); $nonFreeMethods = array(); foreach($objs as $obj) if ($obj->value == 'SAMPLE' || $obj->value == 'TEST') $freeMethods[] =$obj; else $nonFreeMethods[] = $obj; return array('free' => $freeMethods, 'nonFree' => $nonFreeMethods); } function getProducts() { return array('POS' => array( 'name' => 'Positivo', 'values'=>array('POSITIVO'), 'product'=>array('positivo') ), 'POLI' => array( 'name' => 'Polipropileno termoconformado' , 'values'=>array('TERMOPOLIET3','TERMOPOLIET4','TERMOPOLIPRO2','TERMOPOLIPRO3','TERMOPOLIPRO4','TERMOPOLIPRO5') ), 'MECA' => array( 'name' => 'Polipropileno mecanizado' , 'values'=>array('MECAABS','MECAPOLIEHD','MECAPOLIPRO'), 'product'=>array('mecapoli') ), 'MECA3SIDED' => array( 'name' => 'Polipropileno mecanizado a 3 caras' , 'values'=>array('MECAPOLIPRO3SIDED') ), 'RES' => array( 'name' => 'Resina' , 'values'=>array('TERMORESINAFLEX','TERMORESINAFLUX','TERMORESINAFLUXFLEX','TERMORESINAHERFLEX') ), 'FIB' => array( 'name' => 'Fibra' , 'values'=>array('TERMOCARBONO15') ), 'EVA' => array( 'name' => 'EVA' , 'values'=>array('EVABI3055','EVAMONO30','EVAMONO40','EVAMONO55'), 'product'=>array('evastandard','palmilla') ), '3DCONFORT' => array( 'name' => '3D Confort' , 'values'=>array('3DCONFORTMONO30','3DCONFORTMONO40','3DCONFORTBI3055','3DCONFORTBI4055SPORT'), 'product'=>array('eva3dcomfort') ), 'PODOFLAT' => array( 'name' => 'Podo Flat' , 'values'=>array(), 'product'=>array('flat') ) ); } function _buildQueryManufacturing() { /* * * SELECT o.manufacturing_date ,COUNT(o.id) FROM jos_vxc_order o WHERE EXISTS( SELECT at.id FROM jos_vxc_attribute AT WHERE at.orderid = o.id AND ( (at.name='general.product' AND at.value='evastandard') )) GROUP BY o.manufacturing_date ORDER BY o.manufacturing_date DESC * */ return "SELECT id id FROM jos_vxc_order LIMIT 0,100"; } function _buildQueryUserIncoming() { } }// class