Example #1
0
File: Tools.php Project: valizr/MMA
 public static function showIncomeDashboardbyDateCat($from = '', $to = '', $categoryIncome = array(), $timeframe)
 {
     $resultInfo = array();
     $idUser = Zend_Registry::get('user')->getId();
     $expenses = new Default_Model_Expenses();
     $diff = abs(strtotime($to) - strtotime($from));
     $nr_days = $diff / 86400;
     if ($nr_days < 7) {
         $timeframe = 'd';
     }
     //if ($nr_days>40) $timeframe='w';
     //if ()
     switch ($timeframe) {
         case "m":
             $y1 = date('Y', strtotime($from));
             $y2 = date('Y', strtotime($to));
             $m1 = date('m', strtotime($from));
             //4
             $m2 = date('m', strtotime($to));
             $nrLuni = ($y2 - $y1) * 12 + ($m2 - $m1) + 1;
             //13
             $feb = date('L', strtotime($from)) ? 29 : 28;
             //an bisect
             $daysOfMonth = array(0, 31, $feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31);
             //nr zile pe luna
             $months = array();
             $firstMonth = 1;
             for ($i = 1; $i <= $nrLuni; $i++) {
                 if ($firstMonth == 1) {
                     $startDay = date('d', strtotime($from));
                 } else {
                     $startDay = '01';
                 }
                 if ($firstMonth == $nrLuni) {
                     $endDay = date('d', strtotime($to));
                 } else {
                     $endDay = $daysOfMonth[$m1 + $i == 13 ? 12 : ($m1 + $i - 1) % 12];
                 }
                 $firstMonth++;
                 $yearDate = $y1 + floor(($m1 + $i - 2) / 12);
                 $monthDate = $m1 + $i == 13 ? 12 : (strlen(($m1 + $i - 1) % 12) == 1 ? '0' . ($m1 + $i - 1) % 12 : ($m1 + $i - 1) % 12);
                 $select = $expenses->getMapper()->getDbTable()->select()->from(array('e' => 'expenses'), array('price' => 'SUM(e.price)'))->where('NOT e.deleted')->where('e.idMember = ? ', $idUser)->where('e.type=?', 1)->where('e.date>=?', $yearDate . "-" . $monthDate . "-" . $startDay)->where('e.date<=?', $yearDate . "-" . $monthDate . "-" . $endDay);
                 if ($categoryIncome[0] != 2) {
                     //if we want to see specific categories, not all the expenses
                     $select->joinLeft(array('pg' => 'product_groups'), 'e.`id` = pg.`idProduct`', array(''))->joinLeft(array('g' => 'groups'), 'g.`id` = pg.`idGroup`', array('g.id', 'g.name', 'ufiles' => 'g.color'))->where('g.id IN (?)', $categoryIncome)->group('g.id')->order('g.name')->setIntegrityCheck(false);
                 }
                 $result = $expenses->fetchAll($select);
                 //get results for all categories of expenses that exist for this week
                 foreach ($result as $values) {
                     $valueName = $values->getName();
                     $valuePrice = $values->getPrice();
                     $valueUfiles = $values->getUfiles();
                     $returnValues[date('MY', strtotime($yearDate . "-" . $monthDate . "-" . $startDay))][$values->getId()][0] = !empty($valueName) ? $values->getName() : Zend_Registry::get('translate')->_('admin_income');
                     $returnValues[date('MY', strtotime($yearDate . "-" . $monthDate . "-" . $startDay))][$values->getId()][1] = date('M', strtotime($yearDate . "-" . $monthDate . "-" . $startDay)) . " " . $yearDate;
                     $returnValues[date('MY', strtotime($yearDate . "-" . $monthDate . "-" . $startDay))][$values->getId()][2] = !empty($valuePrice) ? $values->getPrice() : 0;
                     $returnValues[date('MY', strtotime($yearDate . "-" . $monthDate . "-" . $startDay))][$values->getId()][3] = !empty($valueUfiles) ? $values->getUfiles() : '#58a87d';
                     //color of the category
                     //print_r($returnValues);
                 }
             }
             //print_r($returnValues);
             return $returnValues;
             break;
         case "w":
             $weeks = array();
             $weeks = Self::weeks($from, $to);
             $temp_week = '';
             $contor_saptamani = 0;
             $year = date('Y', strtotime($from));
             foreach ($weeks as $week) {
                 if ($temp_week) {
                     if ($week < $temp_week) {
                         $year++;
                     }
                     //it means that this week is from the next year: why 1 is less than last week (52)
                 }
                 $temp_week = $week;
                 $timestamp = mktime(0, 0, 0, 1, 1, $year) + ($week - 1) * 7 * 24 * 60 * 60;
                 $timestamp_for_monday = $timestamp - 86400 * (date('N', $timestamp) - 1);
                 $first_dow = $contor_saptamani == 0 ? $from : date('Y-m-d', $timestamp_for_monday);
                 $monday = date('Y-m-d', $timestamp_for_monday);
                 $last_dow = $contor_saptamani == count($weeks) - 1 ? $to : date('Y-m-d', strtotime($monday) + 24 * 3600 * 6);
                 $contor_saptamani++;
                 $select = $expenses->getMapper()->getDbTable()->select()->from(array('e' => 'expenses'), array('price' => 'SUM(e.price)'))->where('NOT e.deleted')->where('e.idMember = ? ', $idUser)->where('e.type=?', 1)->where('e.date>=?', $first_dow)->where('e.date<=?', $last_dow);
                 if ($categoryIncome[0] != 2) {
                     //if we want to see specific categories, not all the expenses
                     $select->joinLeft(array('pg' => 'product_groups'), 'e.`id` = pg.`idProduct`', array(''))->joinLeft(array('g' => 'groups'), 'g.`id` = pg.`idGroup`', array('g.id', 'g.name', 'ufiles' => 'g.color'))->where('g.id IN (?)', $categoryIncome)->group('g.id')->order('g.name')->setIntegrityCheck(false);
                 }
                 $result = $expenses->fetchAll($select);
                 //get results for all categories of expenses that exist for this week
                 foreach ($result as $values) {
                     $valueName = $values->getName();
                     $valuePrice = $values->getPrice();
                     $valueUfiles = $values->getUfiles();
                     $returnValues[$week][$values->getId()][0] = !empty($valueName) ? $values->getName() : Zend_Registry::get('translate')->_('admin_income');
                     $returnValues[$week][$values->getId()][1] = $first_dow . "/" . $last_dow;
                     $returnValues[$week][$values->getId()][2] = !empty($valuePrice) ? $values->getPrice() : 0;
                     $returnValues[$week][$values->getId()][3] = !empty($valueUfiles) ? $values->getUfiles() : '#58a87d';
                     //color of the category
                 }
             }
             return $returnValues;
             break;
         case "d":
             for ($i = 0; $i < $nr_days; $i++) {
                 $date = date('Y-m-d', strtotime($from . ' +' . $i . ' days'));
                 $select = $expenses->getMapper()->getDbTable()->select()->from(array('e' => 'expenses'), array('price' => 'SUM(e.price)'))->where('NOT e.deleted')->where('e.idMember = ? ', $idUser)->where('e.type=?', 1)->where('e.date=?', $date);
                 if ($categoryIncome[0] != 2) {
                     //if we want to see specific categories, not all the expenses
                     $select->joinLeft(array('pg' => 'product_groups'), 'e.`id` = pg.`idProduct`', array(''))->joinLeft(array('g' => 'groups'), 'g.`id` = pg.`idGroup`', array('g.id', 'g.name', 'ufiles' => 'g.color'))->where('g.id IN (?)', $categoryIncome)->group('g.id')->order('g.name')->setIntegrityCheck(false);
                 }
                 $result = $expenses->fetchAll($select);
                 //get results for all categories of expenses that exist for this week
                 foreach ($result as $values) {
                     $valueName = $values->getName();
                     $valuePrice = $values->getPrice();
                     $valueUfiles = $values->getUfiles();
                     $returnValues[$date][$values->getId()][0] = !empty($valueName) ? $values->getName() : Zend_Registry::get('translate')->_('admin_income');
                     $returnValues[$date][$values->getId()][1] = $date;
                     $returnValues[$date][$values->getId()][2] = !empty($valuePrice) ? $values->getPrice() : 0;
                     $returnValues[$date][$values->getId()][3] = !empty($valueUfiles) ? $values->getUfiles() : '#58a87d';
                     //color of the category
                 }
             }
             return $returnValues;
             break;
     }
 }