Exemple #1
0
 function cronjob($runHours = 'month')
 {
     $updatedRE = false;
     //Get Time
     $now = date('Y-m-d');
     //data de azi
     //Convert Time
     $model = new Default_Model_RecurrentExpenses();
     $select = $model->getMapper()->getDbTable()->select()->where('NOT `deleted`');
     $result = $model->fetchAll($select);
     if ($result) {
         foreach ($result as $values) {
             $date = $values->getDatePaid() ? $values->getDatePaid() : $values->getDate();
             //1 mai 2014
             $day = date('d', strtotime($date));
             $feb = date('L', strtotime($date)) ? 29 : 28;
             //an bisect
             $days = array(0, 31, $feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31);
             //nr zile pe luna
             //$month=array(1,2,3,4,5,6,7,8,9,10,11,12);
             $newdate = strtotime('+' . $days[date('n', strtotime($date))] . ' days', strtotime($date));
             if ($day < 29 && $day > 1) {
                 $newdate = date('Y-m', $newdate) . "-{$day}";
             } else {
                 if (date('n', strtotime($date)) == 1) {
                     //january
                     if ($feb == 29) {
                         $newdate = date('Y-02') . "-" . ($day == 1 ? '01' : '29');
                     } else {
                         $newdate = date('Y-02') . "-" . ($day == 1 ? '01' : '28');
                     }
                 } else {
                     if ($days[date('n', strtotime($date))] > $days[date('n', strtotime($date)) + 1] && $day > 1) {
                         $newdate = date('Y-m-d', strtotime('+' . $days[date('n', strtotime($date)) + 1] . ' days', strtotime($date)));
                     } else {
                         $newdate = date('Y-m-d', $newdate);
                     }
                 }
                 //$newdate=date ( 'Y-m-d' , $newdate );
             }
             if ($newdate <= date('Y-m-d')) {
                 $model->find($values->getId());
                 $model->setDatePaid($newdate);
                 if ($model->save()) {
                     $modelExpenses = new Default_Model_Expenses();
                     $modelExpenses->setIdMember($model->getIdMember());
                     $modelExpenses->setName($model->getName());
                     $modelExpenses->setPrice($model->getPrice());
                     $modelExpenses->setType($model->getType());
                     $modelExpenses->setDate($model->getDatePaid());
                     if ($expenseId = $modelExpenses->save()) {
                         $productGroups = new Default_Model_ProductGroups();
                         $select = $productGroups->getMapper()->getDbTable()->select()->where('idProduct=?', $model->getId())->where('repeated=?', 1);
                         $result = $productGroups->fetchRow($select);
                         if (NULL != $result) {
                             $idGroup = $result->getIdGroup();
                             $productGroups->setIdProduct($expenseId);
                             $productGroups->setIdGroup($idGroup);
                             $productGroups->setId('');
                             $productGroups->setRepeated(0);
                             $productGroups->save();
                         }
                     }
                     $updatedRE = true;
                 }
             }
         }
     }
     return $updatedRE;
 }