<?php
 namespace VnChocola\Module\Api\Reserves;
 use TCMS\APIController;
 use Model;
 use VNChocola\Model\ReservesModel;
 use VNChocola\Model\CustomersModel;
 use VNChocola\Model\CarsInfoModel;
 class IndexController extends APIController {
 public function getList() {
 $model = Model::load('ReservesModel');
 $auth = load_auth();
 if ($auth->hasRole(\RoleGlobal::$role->viewScheduler)) {
 $user = $auth->getUser();
 if (!$auth->hasRole(\RoleGlobal::$role->viewAllBranch)) {
 $model->where('branchID', $user->branchID);
 }
 else {
 $model->whereIn('branchID', $user->getBranchsId());
 }
 if (!$auth->hasRole(\RoleGlobal::$role->viewReserveAll)) {
 $model->where('staffID', $user->staffId);
 }
 $limit = \Request::get('limit', 0);
 if ($limit > 0) {
 $model->limit($limit);
 }
 $data = $model->toList();
 return $this->ok($data);
 }
 return showUnauthorized();
 }
 public function find($id) {
 $model = Model::load('ReservesModel');
 $data = $model->find($id);
 if (!$data) {
 return $this->notFound();
 }
 return $this->ok($data);
 }
 private function formatPlateNumber($plate) {
 if ($plate) {
 $plate = trim(strtolower($plate));
 $plate = str_replace('-', "", $plate);
 $plate = str_replace('.', "", $plate);
 $plate = str_replace(' ', "", $plate);
 return $plate;
 }
 return '';
 }
 public function create(ReservesModel $model) {
 $model->id = null;
 $auth = load_auth();
 if ($auth->hasRole(\RoleGlobal::$role->createReserve)) {
 $model->branchID = $auth->getUser()->branchID;
 if ($model->plate) $model->search_plate = $this->formatPlateNumber($model->plate);
 if ($model->isValid()) {
 $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(ReservesModel $model) {
 $auth = load_auth();
 if ($auth->hasRole(\RoleGlobal::$role->updateReserve)) {
 if ($model->plate) $model->search_plate = $this->formatPlateNumber($model->plate);
 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->deleteReserve)) {
 $model = Model::load('ReservesModel');
 if ($model->delete($id)) {
 return $this->ok(['success' => true]);
 }
 return $this->error('Xóa thất bại');
 }
 return showUnauthorized();
 }
 public function find_customers() {
 $phone_4number = \Request::get('phone_4number', "");
 $plate_4number = \Request::get('plate_4number', "");
 $customer_name = \Request::get('customer_name', "");
 $customer_code = \Request::get('customer_code', "");
 $carframes = \Request::get('carframes', "");
 $modelCarInfo = Model::load('CarsInfoModel');
 $modelCustomers = Model::load('CustomersModel');
 $auth = load_auth();
 if ($auth->hasRole(\RoleGlobal::$role->viewScheduler)) {
 $user = $auth->getUser();
 if (!$auth->hasRole(\RoleGlobal::$role->viewAllBranch)) {
 $modelCustomers->where('branchID', $user->branchID);
 }
 else {
 $modelCustomers->whereIn('branchID', $user->getBranchsId());
 }
 if (!$auth->hasRole(\RoleGlobal::$role->viewCustomerAll)) {
 $modelCustomers->where('staffID', $user->staffId);
 }
 if ($plate_4number || $carframes) {
 if ($plate_4number) {
 $modelCarInfo->like("plate", $plate_4number);
 }
 if ($carframes) {
 $modelCarInfo->like("frames", $carframes);
 }
 $cars = $modelCarInfo->toList();
 if (!empty($cars)) {
 foreach ($cars as $car) {
 $modelCustomers->where('id', $car->customerID);
 }
 if ($phone_4number) {
 $modelCustomers->like("phone", $phone_4number);
 }
 if ($customer_name) {
 $modelCustomers->like("name", $customer_name);
 }
 if ($customer_code) {
 $modelCustomers->like("code", $customer_code);
 }
 }
 else {
 return $this->ok(json_encode([]));
 }
 }
 else {
 if ($phone_4number) {
 $modelCustomers->like("phone", $phone_4number);
 }
 if ($customer_name) {
 $modelCustomers->like("name", $customer_name);
 }
 if ($customer_code) {
 $modelCustomers->like("code", $customer_code);
 }
 }
 return $this->ok(json_encode($modelCustomers->toList()));
 }
 return showUnauthorized();
 }
 public function check_code() {
 $code = \Request::get('code', "");
 $model = Model::load('ReservesModel');
 $model->where("code", $code);
 $data = $model->count();
 return $this->ok($data);
 }
 public function today_visit() {
 $model = Model::load('ReservesModel');
 $auth = load_auth();
 if ($auth->hasRole(\RoleGlobal::$role->viewScheduler)) {
 $user = $auth->getUser();
 if (!$auth->hasRole(\RoleGlobal::$role->viewAllBranch)) {
 $model->where('branchID', $user->branchID);
 }
 else {
 $model->whereIn('branchID', $user->getBranchsId());
 }
 if (!$auth->hasRole(\RoleGlobal::$role->viewReserveAll)) {
 $model->where('staffID', $user->staffId);
 }
 $model->where("fromDate", date("Y-m-d 00:00:00"), '>=');
 $model->where("fromDate", date("Y-m-d 23:59:59"), '<=');
 $data = $model->toList();
 return $this->ok(json_encode($data));
 }
 return showUnauthorized();
 }
 public function scheduler_data() {
 $datatime = \Request::get('date', date("Y-m-d"));
 $datatime = date("Y-m-d H:i:s", strtotime($datatime . " 00:00:00"));
 $model = Model::load('ReservesModel');
 $auth = load_auth();
 if ($auth->hasRole(\RoleGlobal::$role->viewScheduler)) {
 $user = $auth->getUser();
 if (!$auth->hasRole(\RoleGlobal::$role->viewAllBranch)) {
 $model->where('branchID', $user->branchID);
 }
 else {
 $model->whereIn('branchID', $user->getBranchsId());
 }
 if (!$auth->hasRole(\RoleGlobal::$role->viewReserveAll)) {
 $model->where('staffID', $user->staffId);
 }
 $model->where("fromDate", $datatime);
 return $this->ok(json_encode($model->toList()));
 }
 return showUnauthorized();
 }
 public function scheduler_update() {
 $auth = load_auth();
 if ($auth->hasRole(\RoleGlobal::$role->updateReserve)) {
 $id = \Request::get('id');
 $nameCustomer = \Request::get('nameCustomer');
 $plate = \Request::get('plate');
 $staffID = \Request::get('staffID');
 $fromDate = \Request::get('fromDate');
 $fromTime = \Request::get('fromTime');
 $toTime = \Request::get('toTime');
 $purposeID = \Request::get('purposeID');
 $note = \Request::get('note');
 $model = Model::load('ReservesModel');
 $model->id = $id;
 $model->nameCustomer = $nameCustomer;
 $model->plate = $plate;
 $model->staffID = $staffID;
 $model->fromDate = $fromDate;
 $model->fromTime = $fromTime;
 $model->toTime = $toTime;
 $model->purposeID = $purposeID;
 $model->note = $note;
 $model->update();
 return $this->ok(json_encode([]));
 }
 return showUnauthorized();
 }
 public function scheduler_create() {
 $auth = load_auth();
 if ($auth->hasRole(\RoleGlobal::$role->createReserve)) {
 $user = $auth->getUser();
 $id = \Request::get('id');
 $code = \Request::get('code');
 $nameCustomer = \Request::get('nameCustomer');
 $plate = \Request::get('plate');
 $staffID = \Request::get('staffID');
 $fromDate = \Request::get('fromDate');
 $fromTime = \Request::get('fromTime');
 $toTime = \Request::get('toTime');
 $purposeID = \Request::get('purposeID');
 $note = \Request::get('note');
 $model = Model::load('ReservesModel');
 $model->nameCustomer = $nameCustomer;
 $model->code = $code;
 $model->plate = $plate;
 $model->search_plate = $plate;
 $model->staffID = $staffID;
 $model->branchID = $user->branchID;
 $model->fromDate = $fromDate;
 $model->fromTime = $fromTime;
 $model->toTime = $toTime;
 $model->purposeID = $purposeID;
 $model->note = $note;
 $model->save();
 if ($model->save()) return $this->ok(['success' => true]);
 return $this->badRequest('Lưu dữ liệu thất bại');
 }
 return showUnauthorized();
 }
 public function reserves_data() {
 $fromDate = \Request::get('fromDate');
 $toDate = \Request::get('toDate');
 if (!$fromDate) {
 $fromDate = date("Y-m-d");
 }
 else {
 $seconds = $fromDate / 1000;
 $fromDate = date("Y-m-d", $seconds);
 }
 if (!$toDate) {
 $toDate = date("Y-m-d");
 }
 else {
 $seconds = $toDate / 1000;
 $toDate = date("Y-m-d", $seconds);
 }
 $model = Model::load('ReservesModel');
 $auth = load_auth();
 if ($auth->hasRole(\RoleGlobal::$role->viewScheduler)) {
 $user = $auth->getUser();
 if (!$auth->hasRole(\RoleGlobal::$role->viewAllBranch)) {
 $model->where('branchID', $user->branchID);
 }
 else {
 $model->whereIn('branchID', $user->getBranchsId());
 }
 if (!$auth->hasRole(\RoleGlobal::$role->viewReserveAll)) {
 $model->where('staffID', $user->staffId);
 }
 $model->where("fromDate", date($fromDate . " 00:00:00"), '>=');
 $model->where("fromDate", date($toDate . " 23:59:59"), '<=');
 $data = $model->toList();
 return $this->ok(json_encode($data));
 }
 return showUnauthorized();
 }
 public function getReserveToDay() {
 $branchID = \Request::get('branchID');
 if (!$branchID) return $this->badRequest('Vui lòng truyền chi nhánh');
 $auth = load_auth();
 if (!$auth->hasRole(\RoleGlobal::$role->viewAllBranch)) {
 $user = $auth->getUser();
 if ($user->branchID != $branchID) return $this->unauthorized(lang('unauthorized.branch'));
 }
 $cardInfoModel = \Model::load('CarsInfoModel');
 $reservesModel = Model::load('ReservesModel');
 $reservesModel->like('fromDate', date('Y-m-d') . '%');
 $reservesModel->where('branchID', $branchID)->where('status', 4, '<>');
 $reservesModel->orderByDesc('id');
 $reservesModel->join('Staffs', 'staffID', 'id');
 $reservesModel->join('Purposes', 'purposeID', 'id');
 $reserveCus = $reservesModel->toList();
 $reserves = [];
 foreach ($reserveCus as $reserve) {
 if (empty($reserve->jbt_Staffs)) continue;
 $staff = $reserve->jbt_Staffs[0]->name;
 if (!empty($reserve->jbt_Purposes)) {
 $purpose = $reserve->jbt_Purposes[0]->title;
 }
 else {
 $purpose = '';
 }
 $fromTime = $reserve->fromTime;
 $toTime = $reserve->toTime;
 if ($reserve->fromDate) {
 $date = explode(' ', $reserve->fromDate)[0];
 $fromTime = $date . ' ' . $fromTime;
 $toTime = $date . ' ' . $toTime;
 }
 $carType = '';
 if (!empty($reserve->plate)) {
 $p = $cardInfoModel->where('search_plate', $this->formatPlateNumber($reserve->plate))->first();
 if ($p) $carType = isset($p->type) ? $p->type : '';
 }
 $reserves[] = [ 'id' => $reserve->id, 'customerName' => $reserve->nameCustomer, 'plate' => isset($reserve->plate) ? $reserve->plate : '', 'search_plate' => !empty($reserve->search_plate) ? $reserve->search_plate : $this->formatPlateNumber(isset($reserve->plate) ? $reserve->plate : ''), 'carType' => $carType, 'image' => '', 'fromTime' => $fromTime, 'toTime' => $toTime, 'staff' => $staff, 'purpose' => $purpose ];
 }
 if ($reserves) {
 $visitModel = Model::load('VisitCustomersModel');
 $visitModel->where('branchID', $branchID);
 $visitModel->like('created_at', date('Y-m-d') . '%');
 $visitCustomers = $visitModel->select('search_plate','status')->toList();
 if ($visitCustomers) {
 foreach ($visitCustomers as $v) {
 if (empty($v->search_plate)) continue;
 $key = false;
 foreach ($reserves as $k => $r) {
 if ($v->status != 10 && $r['search_plate'] == $v->search_plate) {
 $key = $k;
 break;
 }
 }
 if ($key !== false) {
 unset($reserves[$key]);
 }
 }
 }
 }
 return $this->ok($reserves);
 }
 public function getAllReserveToDay() {
 $branchID = \Request::get('branchID');
 $fromCompartment = \Request::get('fromCompartment');
 if (!$branchID) return $this->badRequest('Vui lòng truyền chi nhánh');
 $auth = load_auth();
 if (!$auth->hasRole(\RoleGlobal::$role->viewAllBranch)) {
 $user = $auth->getUser();
 if ($user->branchID != $branchID) return $this->unauthorized(lang('unauthorized.branch'));
 }
 $cardInfoModel = \Model::load('CarsInfoModel');
 $reservesModel = Model::load('ReservesModel');
 $reservesModel->like('fromDate', date('Y-m-d') . '%');
 $reservesModel->where('branchID', $branchID)->where('status', 4, '<>');
 $reservesModel->orderByDesc('id');
 if ($fromCompartment) $reservesModel->join('Staffs', 'CVDV', 'id');
 else $reservesModel->join('Staffs', 'staffID', 'id');
 $reservesModel->join('Purposes', 'purposeID', 'id');
 $reserveCus = $reservesModel->toList();
 $allReserveCus = [];
 foreach ($reserveCus as $reserve) {
 if (empty($reserve->jbt_Staffs)) continue;
 $staff = $reserve->jbt_Staffs[0]->name;
 if (!empty($reserve->jbt_Purposes)) {
 $purpose = $reserve->jbt_Purposes[0]->title;
 }
 else {
 $purpose = '';
 }
 $fromTime = $reserve->fromTime;
 $toTime = $reserve->toTime;
 if ($reserve->fromDate) {
 $date = explode(' ', $reserve->fromDate)[0];
 $fromTime = $date . ' ' . $fromTime;
 $toTime = $date . ' ' . $toTime;
 }
 $allReserveCus[] = [ 'id' => $reserve->id, 'customerName' => $reserve->nameCustomer, 'plate' => isset($reserve->plate) ? $reserve->plate : '', 'carType' => '', 'image' => '', 'fromTime' => $fromTime, 'toTime' => $toTime, $fromCompartment ? 'sa' : 'staff' => $staff, 'purpose' => $purpose, 'compartmentID' => isset($reserve->carcompartmentID) ? $reserve->carcompartmentID : '', 'status' => isset($reserve->status) ? $reserve->status : '', ];
 }
 $reserves = [];
 foreach ($reserveCus as $reserve) {
 if (empty($reserve->jbt_Staffs)) continue;
 $staff = $reserve->jbt_Staffs[0]->name;
 if (!empty($reserve->jbt_Purposes)) {
 $purpose = $reserve->jbt_Purposes[0]->title;
 }
 else {
 $purpose = '';
 }
 $fromTime = $reserve->fromTime;
 $toTime = $reserve->toTime;
 if ($reserve->fromDate) {
 $date = explode(' ', $reserve->fromDate)[0];
 $fromTime = $date . ' ' . $fromTime;
 $toTime = $date . ' ' . $toTime;
 }
 $carType = '';
 if (!empty($reserve->plate)) {
 $p = $cardInfoModel->where('search_plate', $this->formatPlateNumber($reserve->plate))->first();
 if ($p) $carType = isset($p->type) ? $p->type : '';
 }
 $reserves[] = [ 'id' => $reserve->id, 'customerName' => $reserve->nameCustomer, 'plate' => isset($reserve->plate) ? $reserve->plate : '', 'search_plate' => !empty($reserve->search_plate) ? $reserve->search_plate : $this->formatPlateNumber(isset($reserve->plate) ? $reserve->plate : ''), 'carType' => $carType, 'image' => '', 'fromTime' => $fromTime, 'toTime' => $toTime, $fromCompartment ? 'sa' : 'staff' => $staff, 'purpose' => $purpose, 'compartmentID' => isset($reserve->carcompartmentID) ? $reserve->carcompartmentID : '', 'status' => isset($reserve->status) ? $reserve->status : '', ];
 }
 if ($reserves) {
 $visitModel = Model::load('VisitCustomersModel');
 $visitModel->where('branchID', $branchID);
 $visitModel->like('created_at', date('Y-m-d') . '%');
 $visitCustomers = $visitModel->select('search_plate','compartmentStatus')->toList();
 if ($visitCustomers) {
 foreach ($visitCustomers as $v) {
 if (empty($v->search_plate)) continue;
 $key = false;
 foreach ($reserves as $k => $r) {
 if (((isset($v->compartmentStatus) && $v->compartmentStatus != 10) || !isset($v->compartmentStatus)) && $r['search_plate'] == $v->search_plate) {
 $key = $k;
 break;
 }
 }
 if ($key !== false) {
 unset($reserves[$key]);
 }
 }
 }
 }
 return $this->ok([ 'reserves' => $reserves, 'allReserves' => $allReserveCus ]);
 }
 }
 