示例#1
0
文件: Budget.php 项目: arbi/MyCode
 /**
  * @param $data
  * @param $budgetId
  * @return int
  */
 public function saveBudget($data, $budgetId)
 {
     /** @var \DDD\Dao\Finance\Budget\Budget $budgetDao */
     $budgetDao = $this->getServiceLocator()->get('dao_finance_budget_budget');
     $dateRange = Helper::refactorDateRange($data['period']);
     if ($budgetId) {
         // Apply amount changes to balance
         $oldData = $budgetDao->getBudgetData($budgetId);
         $balance = $oldData['balance'] + $data['amount'] - $oldData['amount'];
     } else {
         // Starting balance is the same as amount
         $balance = $data['amount'];
     }
     $params = ['name' => $data['name'], 'status' => $data['status'], 'from' => $dateRange['date_from'], 'to' => $dateRange['date_to'], 'amount' => $data['amount'], 'balance' => $balance, 'description' => $data['description'], 'department_id' => $data['is_global'] ? null : $data['department_id'], 'is_global' => $data['is_global']];
     if ($data['country_id'] > 0) {
         $params['country_id'] = $data['country_id'];
     } else {
         $params['country_id'] = null;
     }
     if ($budgetId) {
         $budgetDao->save($params, ['id' => $budgetId]);
     } else {
         $auth = $this->getServiceLocator()->get('library_backoffice_auth');
         $userId = $auth->getIdentity()->id;
         $params['user_id'] = $userId;
         $budgetId = $budgetDao->save($params);
     }
     return $budgetId;
 }
示例#2
0
文件: Budget.php 项目: arbi/MyCode
 /**
  * @param $params
  * @param $userId
  * @return array
  */
 public function getAllBudgets($params, $userId)
 {
     $this->resultSetPrototype->setArrayObjectPrototype(new \ArrayObject());
     $where = new Where();
     if (isset($params['name']) && $params['name']) {
         $where->like($this->getTable() . '.name', $params['name'] . '%');
     }
     if (isset($params['status']) && $params['status']) {
         $where->equalTo($this->getTable() . '.status', $params['status']);
     }
     if (isset($params['user']) && $params['user']) {
         $where->equalTo($this->getTable() . '.user_id', $params['user']);
     }
     if (isset($params['period']) && $params['period']) {
         $dateRange = Helper::refactorDateRange($params['period']);
         $where->greaterThanOrEqualTo($this->getTable() . '.to', $dateRange['date_from']);
         $where->lessThanOrEqualTo($this->getTable() . '.from', $dateRange['date_to']);
     }
     if (isset($params['frozen']) && $params['frozen'] >= 0) {
         $where->equalTo($this->getTable() . '.frozen', $params['frozen']);
     }
     if (isset($params['archived']) && $params['archived'] >= 0) {
         $where->equalTo($this->getTable() . '.archived', $params['archived']);
     }
     if ($userId) {
         $where->equalTo($this->getTable() . '.user_id', $userId);
     }
     if (isset($params['department']) && $params['department'] >= 0) {
         $where->equalTo($this->getTable() . '.department_id', $params['department']);
     }
     if (isset($params['country']) && $params['country'] >= 0) {
         $where->equalTo($this->getTable() . '.country_id', $params['country']);
     }
     if (isset($params['global']) && $params['global'] >= 0) {
         $where->equalTo($this->getTable() . '.is_global', $params['global']);
     }
     $offset = $params['iDisplayStart'];
     $limit = $params['iDisplayLength'];
     $sortCol = $params['iSortCol_0'];
     $sortDir = $params['sSortDir_0'];
     $result = $this->fetchAll(function (Select $select) use($offset, $limit, $sortCol, $sortDir, $where) {
         $sortColumns = ['status', 'name', 'department_name', 'from', 'amount', 'balance', 'user_name'];
         $select->columns(['id', 'name', 'from', 'to', 'amount', 'description', 'status', 'user_id', 'department_id', 'country_id', 'is_global', 'balance']);
         $select->join(['users' => DbTables::TBL_BACKOFFICE_USERS], $this->getTable() . '.user_id = users.id', ['user_name' => new Expression('CONCAT(firstname, " ", lastname)')], Select::JOIN_LEFT);
         $select->join(['teams' => DbTables::TBL_TEAMS], $this->getTable() . '.department_id = teams.id', ['department_name' => 'name'], Select::JOIN_LEFT);
         $select->where($where);
         $select->group($this->getTable() . '.id')->order($sortColumns[$sortCol] . ' ' . $sortDir)->offset((int) $offset)->limit((int) $limit);
         $select->quantifier(new Expression('SQL_CALC_FOUND_ROWS'));
     });
     $statement = $this->adapter->query('SELECT FOUND_ROWS() as total');
     $resultCount = $statement->execute();
     $row = $resultCount->current();
     $total = $row['total'];
     return ['result' => $result, 'total' => $total];
 }
示例#3
0
文件: Inventory.php 项目: arbi/MyCode
 /**
  * @param $apartmentId
  * @param $dateRange
  * @param $weekDays
  * @param $price
  * @param $priceType
  * @param int $setLockPrice
  * @param int $forceLockPrice
  * @param int $forceUpdatePrice
  * @return $this|array|bool|Inventory
  */
 public function updateInventoryRangeByPrice($apartmentId, $dateRange, $weekDays, $price, $priceType, $setLockPrice = 0, $forceLockPrice = 0, $forceUpdatePrice = 0)
 {
     /**
      * @var \DDD\Dao\Apartment\Inventory $inventoryDao
      */
     $inventoryDao = $this->getServiceLocator()->get('dao_apartment_inventory');
     // Define Variables
     $dateRange = Helper::refactorDateRange($dateRange);
     $weekDays = Helper::reformatWeekdays($weekDays);
     // check price changes
     if (!$forceUpdatePrice) {
         $priceAVGOld = $inventoryDao->getPriceAVGRange($apartmentId, $dateRange['date_from'], $dateRange['date_to'], $weekDays);
         $priceAVGNew = $inventoryDao->getPriceAVGRangeByPriceType($apartmentId, $dateRange['date_from'], $dateRange['date_to'], $weekDays, $price, $priceType);
         if ($priceAVGNew < $priceAVGOld - $priceAVGOld * self::PRICE_CHANGE_LIMIT / 100) {
             return ['status' => 'limit_exceed', 'msg' => TextConstants::PRICE_EXCEED_LIMIT];
         }
     }
     return $this->updatePriceByRange($apartmentId, $price, $dateRange['date_from'], $dateRange['date_to'], $weekDays, $priceType, $setLockPrice, $forceLockPrice);
 }
示例#4
0
 public function updateAction()
 {
     $output = ['bo' => ['status' => 'error']];
     try {
         /**
          * @var Request $request
          * @var \DDD\Service\Apartment\Inventory $inventoryService
          * @var Rate $apartmentRateService
          * @var \DDD\Service\Apartment\General $apartmentService
          */
         $request = $this->getRequest();
         /**
          * Definitions
          */
         if ($request->isPost() && $request->isXmlHttpRequest()) {
             $inventoryService = $this->getServiceLocator()->get('service_apartment_inventory');
             // Required as Route params
             $apartmentId = $this->params()->fromRoute('apartment_id');
             // Required
             $type = $this->params()->fromPost('type', null);
             // availability|price|price-extra
             $dateRange = $this->params()->fromPost('date_range', null);
             $weekDays = $this->params()->fromPost('days', null);
             // Optional (depends on type of choice)
             $availability = $this->params()->fromPost('avail', null);
             $amount = $this->params()->fromPost('amount', null);
             $priceType = $this->params()->fromPost('price_type', null);
             $lockPrice = $this->params()->fromPost('lock_price', null);
             $comment = $this->params()->fromPost('comment', null);
             $forceUpdatePrice = $this->params()->fromPost('force_update_price', null);
             // Check Imposable Situation
             if (is_null($apartmentId)) {
                 throw new \Exception('Some parameters from route are missing.');
             }
             // Check Required Parameters
             if (is_null($type) || is_null($dateRange) || is_null($weekDays) || empty($dateRange) || empty($weekDays)) {
                 throw new \Exception('Some required parameters are missing.');
             }
             if (!empty($dateRange)) {
                 $dates = explode(' - ', $dateRange);
                 $start = strtotime($dates[0]);
                 $end = strtotime($dates[1]);
                 if (count($dates) != 2 || $start < strtotime("-2 day") || $end > strtotime("+1 year")) {
                     throw new \Exception('Incorrect date range.');
                 }
             }
             // Check Action Type
             if (!in_array($type, ['availability', 'price', 'price-extra'])) {
                 throw new \Exception('Type is wrong.');
             }
             if ($type == 'availability' && !is_null($availability)) {
                 if (!$availability && !$comment) {
                     throw new \Exception(TextConstants::AVAILABILITY_CLOSE_MSG);
                 }
                 $responseUpdate = $inventoryService->updateInventoryRangeByAvailability($apartmentId, $dateRange, $weekDays, $availability);
                 if ($responseUpdate['status'] == 'success') {
                     $output['bo']['status'] = 'success';
                     $output['bo']['msg'] = $responseUpdate['msg'];
                     $accommodationsDao = $this->getServiceLocator()->get('dao_accommodation_accommodations');
                     $accInfo = $accommodationsDao->fetchOne(["id" => $apartmentId]);
                     $sellingStatus = [ApartmentService::APARTMENT_STATUS_LIVE_AND_SELLING, ApartmentService::APARTMENT_STATUS_SELLING_NOT_SEARCHABLE];
                     if (in_array($accInfo->getStatus(), $sellingStatus) && !is_null($availability) && $availability == 0) {
                         $auth = $this->getServiceLocator()->get('library_backoffice_auth');
                         $userId = $auth->getIdentity()->id;
                         $accName = $accInfo->getName();
                         $notifService = $this->getServiceLocator()->get('service_notifications');
                         $userManagerDao = $this->getServiceLocator()->get('dao_user_user_manager');
                         $userGroupDao = $this->getServiceLocator()->get('dao_user_user_groups');
                         $userInfo = $userManagerDao->getUserById($userId);
                         $roledUsers = $userGroupDao->getUsersByGroupId(Roles::ROLE_APARTMENT_AVAILABILITY_MONITOR);
                         $recipient = [];
                         foreach ($roledUsers as $roledUser) {
                             $recipient[] = $roledUser->getUserId();
                         }
                         $dateRangeArray = Helper::refactorDateRange($dateRange);
                         $now = date('Y-m-d');
                         $sender = NotificationService::$availabilityMonitoring;
                         $calendarDate = date('Y/m', strtotime($dateRangeArray['date_from']));
                         $days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
                         $exceptDays = [];
                         foreach ($weekDays as $key => $value) {
                             if ($value == 0) {
                                 $exceptDays[] = $days[$key];
                             }
                         }
                         if ($exceptDays) {
                             $exceptDays = implode(', ', $exceptDays);
                             $exceptDays = 'except: <b>' . $exceptDays . '</b>';
                         } else {
                             $exceptDays = '';
                         }
                         if ($availability) {
                             $notifMsg = TextConstants::OPEN_APARTMENT_INVENTORY;
                         } else {
                             $notifMsg = TextConstants::CLOSE_APARTMENT_INVENTORY;
                         }
                         if ($comment) {
                             $reasonMessage = !empty($comment) ? '<br><i>Reason:</i> "' . $comment . '"' : '';
                             /* @var $actionLogger \Library\ActionLogger\Logger */
                             $actionLogger = $this->getServiceLocator()->get('ActionLogger');
                             $actionLogger->save(ActionLogger::MODULE_APARTMENT_INVENTORY, $apartmentId, ActionLogger::ACTION_APARTMENT_INVENTORY_AVAILABILITY, 'Availability <b>closed</b> for ' . $dateRange . ' <b>' . $exceptDays . '</b> ' . $reasonMessage);
                         }
                         // notification
                         if (!$auth->hasRole(Roles::ROLE_NO_TRACK_AVAILABILITY_CHANGES)) {
                             $message = sprintf($notifMsg, $userId, $userInfo->getFirstname() . ' ' . $userInfo->getLastname(), $apartmentId, $calendarDate, $accName, $dateRangeArray['date_from'], $dateRangeArray['date_to'], $exceptDays);
                             $url = '/apartment/' . $apartmentId . '/calendar/' . $calendarDate;
                             $notificationData = ['recipient' => $recipient, 'sender' => $sender, 'sender_id' => $userId, 'message' => $message, 'url' => $url, 'show_date' => $now];
                             $notifService->createNotification($notificationData);
                         }
                     }
                 } else {
                     throw new \Exception($responseUpdate['msg']);
                 }
             } elseif ($type == 'price' && !is_null($amount) && !is_null($priceType)) {
                 // && !in_array($priceType, InventoryService::$changePriceActionList)
                 $responseUpdate = $inventoryService->updateInventoryRangeByPrice($apartmentId, $dateRange, $weekDays, $amount, $priceType, $lockPrice, 0, $forceUpdatePrice);
                 $output['bo']['status'] = $responseUpdate['status'];
                 $output['bo']['msg'] = $responseUpdate['msg'];
             } else {
                 throw new \Exception('Some optional parameters are missing.');
             }
         } else {
             throw new \Exception('Bad request.');
         }
     } catch (\RuntimeException $ex) {
         $output['bo']['status'] = 'success';
         $output['bo']['msg'] = 'availability successfully updated';
     } catch (\Exception $ex) {
         $output['bo']['msg'] = $ex->getMessage();
     }
     return new JsonModel($output);
 }