protected function saveEnrol($iAccountId, $iUserId, $aMeals, $sDayEnd) { $aMealIdsAll = array(); // get submited meals id for ($i = 0; $i < 7; $i++) { if (isset($_POST['day' . $i])) { $aMealIdsAll = array_merge($aMealIdsAll, $_POST['day' . $i]); } } // wyszukujemy zamowienia $aOrderedMeals = array(); $aOrderIndex = array(); $oOrder = new Model_Order(); $oCourse = new Model_Course(); $iIndex = 0; $sDate = ''; // rozpoczynamy transakcje $oOrder->transaction(true); // iterujemy po posilkach w danym tyg. foreach ($aMeals as $iMealId => $aMeal) { // sprawdzamy czy data nie jest starsza niz obecna if (strtotime($aMeal['date'] . ' ' . $sDayEnd) < time()) { continue; } // sprawdzamy czy zmieniono danie na obecny dzien if ($aMeal['date'] == date('Y-m-d')) { // czyscimy tekst paska informacyjnego $this->oCurrentUser->propertie('aMealName', ''); } // zmieniamy na kolejny dzien index tablicy $aMealIds if ($sDate != $aMeal['date']) { // czyscimy zapisy na wszystkie dania tego dnia $oOrder->removeEnrols($iAccountId, $iUserId, $aMeal['date']); $sDate = $aMeal['date']; $iIndex++; } // sprawdzamy czy posilek z danego tyg zostal wybrany przez uzytkownika if (in_array($iMealId, $aMealIdsAll)) { // zapisujemy nowy $oOrder->reset(); $oOrder->date = $aMeal['date']; $oOrder->user_id = (int) $this->oCurrentUser->user_id; $oOrder->meal_id = (int) $iMealId; $oOrder->price = (double) $aMeal['price']; $oOrder->account_id = (int) $this->oCurrentUser->account_id; if (!$oOrder->save()) { throw new Lithium_Exception('catering.saving_failed'); } // sprawdzamy ilosc zamowien na ten dzien $iCount = $oOrder->countOrders((int) $this->oCurrentUser->user_id, $aMeal['date']); if ($iCount > 1) { $aOrders = $oOrder->getOrdersForDiscount((int) $this->oCurrentUser->user_id, $aMeal['date']); foreach ($aOrders as $aOrder) { $aOrder['discount'] = (int) $aOrder['discount']; // gdy znizka rowna 0 nie ma co przeliczac if ($aOrder['discount']) { $oOrder->reset(); $oOrder->order_id = $aOrder['order_id']; $oOrder->price = round($aOrder['price'] * $aOrder['discount'] / 100, 2); if (!$oOrder->save()) { // throw new Lithium_Exception( 'catering.saving_failed' ); } } } } } } if (!$oOrder->transaction()) { throw new Lithium_Exception('save_meals_failed'); } $aMeta = $this->mTemplate->aMeta; $aMeta[] = '<meta http-equiv="refresh" content="1;url=' . $this->mTemplate->anchor() . '" />'; $this->mTemplate->aMeta = $aMeta; $this->mTemplate->content = $this->getLang('save_meals_successfully'); }
protected function saveOrders($iAccountId, $iUserId, $aMeals) { $aMealIdsAll = array(); // get submited meals id for ($i = 0; $i < 7; $i++) { if (isset($_POST['day' . $i])) { $aMealIdsAll = array_merge($aMealIdsAll, $_POST['day' . $i]); } } // wyszukujemy zamowienia $aOrderedMeals = array(); $aOrderIndex = array(); $oOrder = new Model_Order(); $oCourse = new Model_Course(); $iIndex = 0; $sDate = ''; // rozpoczynamy transakcje $oOrder->transaction(true); // iterujemy po posilkach w danym tyg. foreach ($aMeals as $iMealId => $aMeal) { // zmieniamy na kolejny dzien index tablicy $aMealIds if ($sDate != $aMeal['date']) { // czyscimy zapisy na wszystkie dania tego dnia $oOrder->removeEnrols($iAccountId, $iUserId, $aMeal['date']); $sDate = $aMeal['date']; $iIndex++; } // sprawdzamy czy posilek z danego tyg zostal wybrany przez uzytkownika if (in_array($iMealId, $aMealIdsAll)) { // zapisujemy nowy $oOrder->reset(); $oOrder->date = $aMeal['date']; $oOrder->user_id = (int) $iUserId; $oOrder->meal_id = (int) $iMealId; $oOrder->price = (double) $aMeal['price']; $oOrder->account_id = (int) $iAccountId; if (!$oOrder->save()) { throw new Lithium_Exception('Catering.saving_failed'); } // sprawdzamy ilosc zamowien na ten dzien $iCount = $oOrder->countOrders((int) $iUserId, $aMeal['date']); if ($iCount > 1) { $aOrders = $oOrder->getOrdersForDiscount((int) $iUserId, $aMeal['date']); foreach ($aOrders as $aOrder) { $aOrder['discount'] = (int) $aOrder['discount']; // gdy znizka rowna 0 nie ma co przeliczac if ($aOrder['discount']) { $oOrder->reset(); $oOrder->order_id = $aOrder['order_id']; $oOrder->price = round($aOrder['price'] * $aOrder['discount'] / 100, 2); if (!$oOrder->save()) { // throw new Lithium_Exception( 'catering.saving_failed' ); } } } } } } if (!$oOrder->transaction()) { if (IN_PRODUCTION) { return false; } else { throw new Lithium_Exception('catering.saving_failed'); } } return true; }