<?php
 namespace VnChocola\Module\Api\Hotcustomers;
 use TCMS\APIController;
 use Model;
 use VNChocola\Model\HotCustomersModel;
 class IndexController extends APIController {
 public function getList() {
 $auth = load_auth();
 $model = Model::load('HotCustomersModel');
 if($auth->hasRole(\RoleGlobal::$role->viewHotCustomer)){
 $user = $auth->getUser();
 if(!$auth->hasRole(\RoleGlobal::$role->viewHotCustomerAll)){
 $model->where('staffID',$user->staffId);
 }
 if(!$auth->hasRole(\RoleGlobal::$role->viewAllBranch)){
 $model->where('branchID',$user->branchID);
 }
 else {
 $model->whereIn('branchID', $user->getBranchsId());
 }
 $limit = \Request::get('limit', 0);
 if($limit > 0){
 $model->limit($limit);
 }
 $data = $model->toList();
 return $this->ok($data);
 }
 return showUnauthorized();
 }
 public function find($id) {
 $auth = load_auth();
 $model = Model::load('HotCustomersModel');
 if($auth->hasRole(\RoleGlobal::$role->viewHotCustomer)){
 $data = $model->find($id);
 if(!$data) return $this->notFound();
 return $this->ok($data);
 }
 return showUnauthorized();
 }
 public function create(HotCustomersModel $model) {
 $auth = load_auth();
 if($auth->hasRole(\RoleGlobal::$role->createHotCustomer)){
 if($model->isValid()) {
 if(asset($model->cPhone) && count($model->where("cPhone",$model->cPhone)->toList())>0){
 return $this->error('Khách hàng đã tồn tại');
 }
 $saveRes = $model->save();
 if($saveRes) return $this->ok(['success' => true, 'message' => $saveRes]);
 return $this->badRequest('Lưu dữ liệu thất bại');
 }
 return $this->error($model->getErrors());
 }
 return showUnauthorized();
 }
 public function update(HotCustomersModel $model) {
 $auth = load_auth();
 if($auth->hasRole(\RoleGlobal::$role->updateHotCustomer)){
 if($model->isValid()) {
 if($model->save()) return $this->ok(['success' => true]);
 return $this->badRequest('Lưu dữ liệu thất bại');
 }
 return $this->error($model->getErrors());
 }
 return showUnauthorized();
 }
 public function delete() {
 $id = \Request::get('data');
 $auth = load_auth();
 if($auth->hasRole(\RoleGlobal::$role->deleteHotCustomer)){
 $model = Model::load('HotCustomersModel');
 if($model->delete($id)) return $this->ok(['success' => true]);
 return $this->error('Xóa thất bại');
 }
 return showUnauthorized();
 }
 private function convertStringToDateTime($value) {
 if(strpos($value, '(') !== false) {
 $startIndex = strpos($value, ' (');
 $value = trim(substr($value, 0, $startIndex));
 $value = date('Y-m-d H:i:s', strtotime($value));
 return $value;
 }
 return date('Y-m-d H:i:s', strtotime($value));
 }
 private function convertStringToDate($value) {
 if(strpos($value, '(') !== false) {
 $startIndex = strpos($value, ' (');
 $value = trim(substr($value, 0, $startIndex));
 $value = date('Y-m-d', strtotime($value));
 return $value;
 }
 return date('Y-m-d', strtotime($value));
 }
 public function searchFrm(){
 $cPhone = \Request::get('cPhone', "");
 $cardDate = \Request::get('cardDate', "");
 $cCode = \Request::get('cCode', "");
 $cName = \Request::get('cName', "");
 $cType = \Request::get('cType', "");
 $staffID = \Request::get('staffID', "");
 $cStatus = \Request::get('cStatus', "");
 $cSource = \Request::get('cSource', "");
 $deliveryDate = \Request::get('deliveryDate', "");
 $contractDate = \Request::get('contractDate', "");
 $lostDate = \Request::get('lostDate', "");
 $model = Model::load('HotCustomersModel');
 $auth = load_auth();
 if($auth->hasRole(\RoleGlobal::$role->viewHotCustomer)){
 $user = $auth->getUser();
 if(!$auth->hasRole(\RoleGlobal::$role->viewHotCustomerAll)){
 $model->where('staffID',$user->staffId);
 }
 if(!$auth->hasRole(\RoleGlobal::$role->viewAllBranch)){
 $model->where('branchID',$user->branchID);
 }
 else {
 $model->whereIn('branchID', $user->getBranchsId());
 }
 if(strlen($contractDate)>0 && $contractDate == "00"){
 $model->whereNotNull("contractDate");
 $model->where("contractDate","1111-11-11 00:00:00","<>");
 return $this->ok(json_encode($model->toList()));
 }
 if(strlen($lostDate)>0 && $lostDate == "00"){
 $model->whereNotNull("lostDate");
 $model->where("lostDate","1111-11-11 00:00:00","<>");
 return $this->ok(json_encode($model->toList()));
 }
 if(strlen($cPhone)>0){
 $model->like("cPhone",$cPhone);
 }
 if(strlen($cCode)>0){
 $model->like("cCode",$cCode);
 }
 if(strlen($cName)>0){
 $model->like("cName",$cName);
 }
 if(strlen($cStatus)>0){
 $model->like("cStatus",$cStatus);
 }
 if(strlen($cSource)>0){
 $model->like("cSource",$cSource);
 }
 if(strlen($cType)>0){
 $model->where("cType",$cType);
 }
 if(strlen($staffID)>0){
 $model->where("staffID",$staffID);
 }
 if(strlen($cardDate) > 0){
 $cardDate = $this->convertStringToDate($cardDate);
 $model->where("cardDate", $cardDate . ' 00:00:00', '>=');
 $model->where("cardDate", $cardDate . ' 23:59:59', '<=');
 }
 if(strlen($deliveryDate) > 0){
 $deliveryDate = $this->convertStringToDate($deliveryDate);
 $dataMode = Model::load('NewCarsModel');
 $dataMode->where("deliveryDate", $deliveryDate . ' 00:00:00', '>=');
 $dataMode->where("deliveryDate", $deliveryDate . ' 23:59:59', '<=');
 return $this->searchHotCustomersWithNewCar($model,$dataMode->toList());
 }
 return $this->ok(json_encode($model->toList()));
 }
 return showUnauthorized();
 }
 private function searchHotCustomersWithNewCar($model,$newCar){
 $hotCustomersData = [];
 foreach($newCar as $item){
 $hotCustomerData = $model->find($item->HotCustomerID);
 if($hotCustomerData != ""){
 if(!in_array($hotCustomerData,$hotCustomersData)) array_push($hotCustomersData,$hotCustomerData);
 }
 }
 return $this->ok(json_encode($hotCustomersData));
 }
 public function check_code(){
 $code = \Request::get('code', "");
 $model = Model::load('HotCustomersModel');
 $model->where("cCode", $code);
 $data = $model->count();
 return $this->ok($data);
 }
 public function reminding(){
 $date = \Request::get('date',"");
 if(strlen($date)>0){
 $WcModel = Model::load('WorkContentsModel');
 $date = $this->convertStringToDate($date);
 $WcModel->where("nextContactDate", $date . ' 00:00:00', '>=');
 $WcModel->where("nextContactDate", $date . ' 23:59:59', '<=');
 $model = Model::load('HotCustomersModel');
 return $this->searchHotCustomersWithWorkContent($model,$WcModel->toList());
 }
 }
 private function searchHotCustomersWithWorkContent($model,$wContent){
 $hotCustomersData = [];
 $auth = load_auth();
 if($auth->hasRole(\RoleGlobal::$role->viewHotCustomer)){
 $user = $auth->getUser();
 foreach($wContent as $item){
 if(!$auth->hasRole(\RoleGlobal::$role->viewHotCustomerAll)){
 $model->where('staffID',$user->staffId);
 }
 if(!$auth->hasRole(\RoleGlobal::$role->viewAllBranch)){
 $model->where('branchID',$user->branchID);
 }
 else {
 $model->whereIn('branchID', $user->getBranchsId());
 }
 $hotCustomerData = $model->find($item->HotCustomerID);
 if($hotCustomerData != ""){
 if(!in_array($hotCustomerData,$hotCustomersData)) array_push($hotCustomersData,$hotCustomerData);
 }
 }
 }
 return $this->ok($hotCustomersData);
 }
 }
