Exemplo n.º 1
0
 public function getCustomer()
 {
     if (!$this->customer) {
         if ($this->getUserLevel() != UserGroups::$GUEST) {
             $userId = $this->getUserId();
             if ($this->getUserLevel() == UserGroups::$USER) {
                 $userManager = new UserManager($this->config, $this->args);
                 $this->customer = $userManager->selectByPK($userId);
             } else {
                 if ($this->getUserLevel() == UserGroups::$COMPANY) {
                     $customerManager = new CompanyManager($this->config, $this->args);
                     $this->customer = $customerManager->selectByPK($userId);
                 } else {
                     if ($this->getUserLevel() == UserGroups::$SERVICE_COMPANY) {
                         $customerManager = new ServiceCompanyManager($this->config, $this->args);
                         $this->customer = $customerManager->selectByPK($userId);
                     } else {
                         if ($this->getUserLevel() == UserGroups::$ADMIN) {
                             $adminManager = new AdminManager($this->config, $this->args);
                             $this->customer = $adminManager->selectByPK($userId);
                         }
                     }
                 }
             }
         }
     }
     return $this->customer;
 }
 public function service()
 {
     //getting parameters
     ini_set('upload_max_filesize', '7M');
     $name = $_FILES['company_price']['name'];
     $type = $_FILES['company_price']['type'];
     $tmp_name = $_FILES['company_price']['tmp_name'];
     $size = $_FILES['company_price']['size'];
     $response = $this->checkInputFile('company_price');
     if ($response !== 'ok') {
         $jsonArr = array('status' => "err", "errText" => $response);
         echo "<script>var l= new parent.ngs.UploadCompanyPriceAction(); l.afterAction('" . json_encode($jsonArr) . "'); </script>";
         return false;
     }
     if (!$size || $size > 7 * 1024 * 1024) {
         $jsonArr = array('status' => "err", "errText" => "Maximum file size can be 7MB");
         echo "<script>var l= new parent.ngs.UploadCompanyPriceAction(); l.afterAction('" . json_encode($jsonArr) . "'); </script>";
         return false;
     }
     //start to save new price file
     $fname = explode('.', $name);
     end($fname);
     $newFileExt = current($fname);
     if (!in_array($newFileExt, $this->supported_file_formats)) {
         $jsonArr = array('status' => "err", "errText" => "Not supported file format!");
         echo "<script>var l= new parent.ngs.UploadCompanyPriceAction(); l.afterAction('" . json_encode($jsonArr) . "'); </script>";
         return false;
     }
     $userLevel = $this->getUserLevel();
     if ($userLevel === UserGroups::$ADMIN) {
         $companyId = $this->secure($_REQUEST["up_selected_company"]);
     } else {
         if ($userLevel === UserGroups::$COMPANY) {
             $companyId = $this->getUserId();
             assert($companyId == $this->secure($_REQUEST["up_selected_company"]));
         } else {
             $jsonArr = array('status' => "err", "errText" => "Not Access!");
             echo "<script>var l= new parent.ngs.UploadCompanyPriceAction(); l.afterAction('" . json_encode($jsonArr) . "'); </script>";
             return false;
         }
     }
     $dir = DATA_DIR . "/companies_prices/";
     if (!is_dir($dir)) {
         mkdir($dir, 0777);
     }
     $dir = DATA_DIR . "/companies_prices/" . $companyId . '/';
     if (!is_dir($dir)) {
         mkdir($dir, 0777);
     }
     $company_duplicated_price_upload_hours = $this->getCmsVar('company_duplicated_price_upload_hours');
     $companiesPriceListManager = CompaniesPriceListManager::getInstance($this->config, $this->args);
     if (isset($_REQUEST['merge_into_last_price']) && $_REQUEST['merge_into_last_price'] == 1) {
         $duplicatedUpload = $this->checkIfSamePriceAlreadyExists($companyId, $tmp_name);
         $companyLastPriceMinutes = $companiesPriceListManager->getCompanyLastPriceMinutes($companyId);
         if ($companyLastPriceMinutes / 60 < $company_duplicated_price_upload_hours && $duplicatedUpload) {
             $jsonArr = array('status' => "err", "errText" => "Same Price already exists! please try in " . $company_duplicated_price_upload_hours . " hours.");
             echo "<script>var l= new parent.ngs.UploadCompanyPriceAction(); l.afterAction('" . json_encode($jsonArr) . "'); </script>";
             return false;
         }
         $companyLastPriceDtos = $companiesPriceListManager->getCompanyLastPrices($companyId);
         $companyLastPriceInfoDto = end($companyLastPriceDtos);
         $lastPriceName = $companyLastPriceInfoDto->getFileName();
         $newFileName = $lastPriceName . '_' . (count($companyLastPriceDtos) + 1);
         $newFileFullName = $dir . $newFileName . '.' . $newFileExt;
         move_uploaded_file($tmp_name, $newFileFullName);
         $companiesPriceListManager->addCompanyPrice($companyId, $newFileName, $newFileExt, $userLevel == UserGroups::$ADMIN ? "admin" : "company", $this->getUserId());
         $this->updateCompanyPriceText($companyId, count($companyLastPriceDtos));
         $jsonArr = array('status' => "ok");
         echo "<script>var l= new parent.ngs.UploadCompanyPriceAction(); l.afterAction('" . json_encode($jsonArr) . "'); </script>";
         return true;
     }
     $companyTodayPriceUploadedTimes = $companiesPriceListManager->getCompanyTodayPriceUploadedTimes($companyId);
     $company_price_upload_a_day_max_count = $this->getCmsVar('company_price_upload_a_day_max_count');
     if ($companyTodayPriceUploadedTimes >= $company_price_upload_a_day_max_count) {
         $jsonArr = array('status' => "err", "errText" => "You exeeded your daily maximum upload count! (max:" . $company_price_upload_a_day_max_count . " times a day)");
         echo "<script>var l= new parent.ngs.UploadCompanyPriceAction(); l.afterAction('" . json_encode($jsonArr) . "'); </script>";
         return false;
     }
     $companyLastPriceMinutes = $companiesPriceListManager->getCompanyLastPriceMinutes($companyId);
     $duplicatedUpload = $this->checkIfSamePriceAlreadyExists($companyId, $tmp_name);
     if ($companyLastPriceMinutes / 60 < $company_duplicated_price_upload_hours && $duplicatedUpload) {
         $jsonArr = array('status' => "err", "errText" => "Sorry You can not upload same price in " . $company_duplicated_price_upload_hours . " hours. Your company last uploaded price seams to be same as this one!");
         echo "<script>var l= new parent.ngs.UploadCompanyPriceAction(); l.afterAction('" . json_encode($jsonArr) . "'); </script>";
         return false;
     } else {
         if ($companyLastPriceMinutes < 10 && !isset($_REQUEST['new_price_confirmed'])) {
             $jsonArr = array('status' => "war");
             echo "<script>var l= new parent.ngs.UploadCompanyPriceAction(); l.afterAction('" . json_encode($jsonArr) . "'); </script>";
             return false;
         }
     }
     $companyLastPriceDtos = $companiesPriceListManager->getCompanyLastPrices($companyId);
     if (!empty($companyLastPriceDtos)) {
         $companyLastPriceFirstUploadedDto = end($companyLastPriceDtos);
         $lastPriceFiles = array();
         $lastPriceFileName = $companyLastPriceFirstUploadedDto->getFileName();
         foreach ($companyLastPriceDtos as $key => $companyLastPriceDto) {
             $lastPriceName = $companyLastPriceDto->getFileName();
             $lastPriceExt = $companyLastPriceDto->getFileExt();
             $lastPriceFiles[] = array($dir . $lastPriceName . '.' . $lastPriceExt, $lastPriceName . '.' . $lastPriceExt);
         }
         $this->createZip($lastPriceFiles, $dir . $lastPriceFileName . '.zip');
         $lastPriceUploadedDateTime = $companyLastPriceFirstUploadedDto->getUploadDateTime();
         $lastPriceUploaderType = $companyLastPriceFirstUploadedDto->getUploaderType();
         $lastPriceUploaderId = $companyLastPriceFirstUploadedDto->getUploaderId();
         foreach ($companyLastPriceDtos as $key => $companyLastPriceDto) {
             $lastPriceName = $companyLastPriceDto->getFileName();
             $lastPriceExt = $companyLastPriceDto->getFileExt();
             if (is_file($dir . $lastPriceName . '.' . $lastPriceExt)) {
                 unlink($dir . $lastPriceName . '.' . $lastPriceExt);
             }
             $companiesPriceListManager->deleteByPK($companyLastPriceDto->getId());
         }
         $companiesPriceListManager->addCompanyPrice($companyId, $lastPriceFileName, 'zip', $lastPriceUploaderType, $lastPriceUploaderId, $lastPriceUploadedDateTime);
     }
     $now = date("Y-m-d-H-i-s");
     $newFileName = 'price_' . $now;
     $newFileFullName = $dir . $newFileName . '.' . $newFileExt;
     move_uploaded_file($tmp_name, $newFileFullName);
     $companiesPriceListManager->addCompanyPrice($companyId, $newFileName, $newFileExt, $userLevel == UserGroups::$ADMIN ? "admin" : "company", $this->getUserId());
     $jsonArr = array('status' => "ok");
     echo "<script>var l= new parent.ngs.UploadCompanyPriceAction(); l.afterAction('" . json_encode($jsonArr) . "'); </script>";
     $companyManager = new CompanyManager($this->config, $this->args);
     $company = $companyManager->selectByPK($companyId);
     $companyManager->updateCompanyRating($company);
     if (!isset($_REQUEST['silent_mode']) || $_REQUEST['silent_mode'] != 1) {
         //$this->sendNewEmailUploadedToAllCompanyAccessedCustomers($company);
         if ($this->getCmsVar('enable_upload_price_alert') == 1) {
             if ($company->getHidden() == 0) {
                 $this->addEventIntoEventsTableForOnlineCustomers($company);
             }
         }
         $this->sendSmsToAdminIfUploaderIsNotItself($company->getName());
         $this->sendSmsToSmsInterestedCompanies($company->getId(), $company->getName());
     }
     $this->updateCompanyPriceText($companyId, 0);
     return true;
 }