<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Brinvoiceend extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('Brinvoicemodel', 'invoice_model');
		$this->load->model('BrRefundModel', 'refund_model');
		$this->load->model('Brcustomermodel', 'customer_model');
		$this->load->model('Brcustomerpointmodel', 'customerpoint_model');
		$this->load->model('Brcustomerusedpointmodel', 'customerusedpointmodel');
	}
	//added invoice end
	public function addinvoice() {

		// $request= new Request();
		// $admin=$request->getBody();

		// 'inputadminname':$('#inputadminname').val(),
		//     'inputloginname':$('#inputloginname').val(),
		//     'inputPassword'
		// $name=$this->input->post('name');

		//invoice_date at 2020-11-13
		$invoice_date = $this->input->post('invoice_date');
		//end invoice date

		//add security trim input-todo
		//hash password for security
		$userId = $this->input->post('userId');
		$display_name = $this->input->post('display_name');
		$mobile = $this->input->post('mobile');
		$pic_1 = $this->input->post('pic_1');
		$pic_2 = $this->input->post('pic_2');
		$consultant_1 = $this->input->post('consultant_1');

		$product_add_ticker_array = $this->input->post('product_add_ticker_array');
		$product_id_array = $this->input->post('product_id_array');
		$product_price_array = $this->input->post('product_price_array');
		$product_qty_array = $this->input->post('product_qty_array');
		$product_discount_array = $this->input->post('product_discount_array');

		$product_remark_array = [];
		if (!empty($this->input->post('product_remark_array'))) {
			$product_remark_array = $this->input->post('product_remark_array');
		}

		$payment_type_1 = $this->input->post('payment_type_1');
		$payment_amount_1 = $this->input->post('payment_amount_1');
		$payment_type_2 = $this->input->post('payment_type_2');
		$payment_amount_2 = $this->input->post('payment_amount_2');
		$payment_type_3 = $this->input->post('payment_type_3');
		$payment_amount_3 = $this->input->post('payment_amount_3');
		$shopId = $this->input->post('shopId');

		$data = array(
			'invoice_date' => $invoice_date,
			'userId' => $userId,
			'display_name' => $display_name,
			'mobile' => $mobile,
			'pic_1' => $pic_1,
			'pic_2' => $pic_2,
			'consultant_1' => $consultant_1,
			'product_add_ticker_array' => $product_add_ticker_array,
			'product_id_array' => $product_id_array,
			'product_price_array' => $product_price_array,
			'product_qty_array' => $product_qty_array,
			'product_discount_array' => $product_discount_array,
			'product_remark_array' => $product_remark_array,
			'payment_type_1' => $payment_type_1,
			'payment_amount_1' => $payment_amount_1,
			'payment_type_2' => $payment_type_2,
			'payment_amount_2' => $payment_amount_2,
			'payment_type_3' => $payment_type_3,
			'payment_amount_3' => $payment_amount_3,
			'shopId' => $shopId,

		);

		$code = "";
		// if($this->frontenduser_model->admin_exists($inputadminname)==false){
		//     $code="FAILURE";
		//     $msg="已有管理員姓名".$inputadminname;
		// }
		// else
		$invoicelist = "";
		if ($this->customer_model->customermobile_exists($mobile) == false && empty($userId)) {
			// $entercode=$code;
			$code = "FAILURE";
			$msg = "新增失敗。已有客戶電話號碼" . $mobile;
		} elseif ($this->invoice_model->invoice_exists() == false) {
			$code = "FAILURE";
			$msg = "已有發票";
		} else {
			$result = $this->invoice_model->insertInvoice($data);

			if (empty($result)) {
				$code = "FAILURE";
				$msg = "記錄不能加入";

			} else {
				$code = "SUCCESS";
				$msg = "記錄成功加入";
				$insert_id = $result;
				$invoicelist = $result;
			}
			// if($result==false){
			//     $code="FAILURE";
			//     $msg="記錄不能加入";
			// }
			// else{

			//     $code="SUCCESS";
			//     $msg="記錄成功加入";
			//     //for add user it will add a secret key to database

			//     //end

			// }
		}

		header('Content-Type: application/json');
		// $code="SUCCESS";
		// $code="FAILURE";
		// $msg="已有相關記錄";【
		// $myArray = ['id'=>12, 'name'=>'Topi'];
		// $myArray = ['recordstatus'=>$code, 'msg'=>$msg,'insert_id'=>$insert_id];
		$myArray = ['recordstatus' => $code, 'msg' => $msg, 'invoicelist' => $invoicelist];
		echo json_encode($myArray);
		// echo "posted";

	}

	public function updateinvoicepayment() {

		//added paymentdate field at 2020-11-13
		$paymentdate = $this->input->post('paymentdate');
		//end added paymentdate field

		$invoiceid = $this->input->post('invoiceid');

		$payment_id_1 = $this->input->post('payment_id_1');
		$payment_type_1 = $this->input->post('payment_type_1');
		$payment_amount_1 = $this->input->post('payment_amount_1');
		$payment_id_2 = $this->input->post('payment_id_2');
		$payment_type_2 = $this->input->post('payment_type_2');
		$payment_amount_2 = $this->input->post('payment_amount_2');
		$payment_id_3 = $this->input->post('payment_id_3');
		$payment_type_3 = $this->input->post('payment_type_3');
		$payment_amount_3 = $this->input->post('payment_amount_3');
		$shopId = $this->input->post('shopId');

		$data = array(
			'paymentdate' => $paymentdate,
			'invoiceid' => $invoiceid,
			// 'display_name'=>$display_name,
			// 'mobile'=>$mobile,
			// 'pic_1'=>$pic_1,
			// 'consultant_1'=>$consultant_1,
			// 'product_add_ticker_array'=>$product_add_ticker_array,
			// 'product_id_array'=>$product_id_array,
			// 'product_price_array'=>$product_price_array,
			// 'product_qty_array'=>$product_qty_array,
			// 'product_discount_array'=>$product_discount_array,
			'payment_id_1' => $payment_id_1,
			'payment_type_1' => $payment_type_1,
			'payment_amount_1' => $payment_amount_1,
			'payment_id_2' => $payment_id_2,
			'payment_type_2' => $payment_type_2,
			'payment_amount_2' => $payment_amount_2,
			'payment_id_3' => $payment_id_3,
			'payment_type_3' => $payment_type_3,
			'payment_amount_3' => $payment_amount_3,
			'shopId' => $shopId,

		);

		if ($this->invoice_model->check_invoice_exists($invoiceid) == false) {
			$code = "FAILURE";
			$msg = "已有發票";
		} else {
			$result = $this->invoice_model->updateInvoicePayment($data);

			if (empty($result)) {
				$code = "FAILURE";
				$msg = "記錄不能加入";
			} else {
				$code = "SUCCESS";
				$msg = "記錄成功加入";
				$insert_id = $result;
				$invoicelist = $result;
			}
			// if($result==false){
			//     $code="FAILURE";
			//     $msg="記錄不能加入";
			// }
			// else{

			//     $code="SUCCESS";
			//     $msg="記錄成功加入";
			//     //for add user it will add a secret key to database

			//     //end

			// }
		}

		header('Content-Type: application/json');
		// $code="SUCCESS";
		// $code="FAILURE";
		// $msg="已有相關記錄";【
		// $myArray = ['id'=>12, 'name'=>'Topi'];
		// $myArray = ['recordstatus'=>$code, 'msg'=>$msg,'insert_id'=>$insert_id];
		$myArray = ['recordstatus' => $code, 'msg' => $msg, 'invoicelist' => $invoicelist];
		echo json_encode($myArray);
		// echo "posted";
	}
	public function getTotalPayment() {

		$invoiceId = $this->input->post('id');
		$paymentlist = $this->invoice_model->getInvoicePayment($invoiceId);

		$totalPayment = 0;
		foreach ($paymentlist as $payment) {
			$totalPayment = $totalPayment + $payment->payment_amount;
		}

		header('Content-Type: application/json');
		echo json_encode($totalPayment);
	} //public function getTotalPayment(){
	public function getRefundData() {
		$invoiceId = $this->input->post('id');
		$refundlist = $this->invoice_model->getRefundData($invoiceId);

		$totalRefund = 0;
		foreach ($refundlist as $refund) {
			$totalRefund = $totalRefund + $refund->refund_amount;
		}

		$myArray = ['refundDate' => date('Y-m-d', $refund->refund_date), 'refundAmount' => $totalRefund];
		header('Content-Type: application/json');
		echo json_encode($myArray);
	}

	public function setRefundDate() {
		$refund_invoice_id = $this->input->post('refund_invoice_id');
		$refund_date = $this->input->post('refund_date');
		$refund_amount = $this->input->post('refund_amount');
		if ($this->refund_model->refund_exists($refund_invoice_id) == false) {
			//update refund
			$data = array(
				'invoice_id' => $refund_invoice_id,
				'refund_date' => $refund_date,
				'refund_amount' => $refund_amount,

			);
			$result = $this->refund_model->updateRefund($data);
			if ($result == false) {
				$code = "FAILURE";
				$msg = "記錄不能更改";
			} else {
				$code = "SUCCESS";
				$msg = "記錄成功更改";
			}

		} else {
			//insert refund
			$data = array(
				'invoice_id' => $refund_invoice_id,
				'refund_date' => $refund_date,
				'refund_amount' => $refund_amount,

			);

			$result = $this->refund_model->insertRefund($data);
			if ($result == false) {
				$code = "FAILURE";
				$msg = "記錄不能加入";
			} else {
				$code = "SUCCESS";
				$msg = "記錄成功加入";
				//for add user it will add a secret key to database

				//end

			}
		}
		$myArray = ['recordstatus' => $code, 'msg' => $msg];
		echo json_encode($myArray);

	}

	public function deleteInvoice() {
		$invoice_id = $this->input->post('invoice_id');

		if ($this->invoice_model->check_invoice_exists($invoice_id) == true) {
			//update refund
			$data = array(
				'invoice_id' => $invoice_id,

			);
			$result = $this->invoice_model->deleteInvoice($data);
			if ($result == false) {
				$code = "FAILURE";
				$msg = "發票不能删除";
			} else {
				//clear customer point and referer point of invoice added at 2020-12-09
				$result2 = $this->customerpoint_model->deletePointByInvoiceid($invoice_id);

				//clear customer used point
				//added at 2021-01-04
				$result3 = $this->customerusedpointmodel->deleteUsedPointByInvoiceid($invoice_id);

				$code = "SUCCESS";
				$msg = "發票成功删除";
			}

		} else {
			//insert refund

			$code = "FAILURE";
			$msg = "沒有發票記錄";

		}
		$myArray = ['recordstatus' => $code, 'msg' => $msg];
		echo json_encode($myArray);

	}

	//end added invoice end

	public function get($id = Null) {
		$partnerlist = $this->frontenduser_model->getBannerAll();
		echo '1';
		print_r($partnerlist);
	}

	public function add() {

		// $request= new Request();
		// $admin=$request->getBody();

		// 'inputadminname':$('#inputadminname').val(),
		//     'inputloginname':$('#inputloginname').val(),
		//     'inputPassword'
		// $name=$this->input->post('name');

		//add security trim input-todo
		//hash password for security
		$shortcode = $this->input->post('shortcode');
		$code = $this->input->post('code');
		$name = $this->input->post('name');

		$data = array(
			'shortcode' => $shortcode,
			'code' => $code,
			'name' => $name,

		);

		$code = "";
		// if($this->frontenduser_model->admin_exists($inputadminname)==false){
		//     $code="FAILURE";
		//     $msg="已有管理員姓名".$inputadminname;
		// }
		// else
		if ($this->shop_model->shopcode_exists(strtolower($shortcode)) == false) {
			$code = "FAILURE";
			$msg = "已有門店簡稱" . $shortcode;
		} else {
			$result = $this->shop_model->insertShop($data);
			if ($result == false) {
				$code = "FAILURE";
				$msg = "記錄不能加入";
			} else {
				$code = "SUCCESS";
				$msg = "記錄成功加入";
				//for add user it will add a secret key to database

				//end

			}
		}

		header('Content-Type: application/json');
		// $code="SUCCESS";
		// $code="FAILURE";
		// $msg="已有相關記錄";
		// $myArray = ['id'=>12, 'name'=>'Topi'];
		$myArray = ['recordstatus' => $code, 'msg' => $msg];
		echo json_encode($myArray);
		// echo "posted";

	}
	public function edit() {

		$shortcode = $this->input->post('shortcode');
		$code = $this->input->post('code');
		$name = $this->input->post('name');
		$id = $this->input->post('id');

		$data = array(

			'shortcode' => $shortcode,
			'code' => $code,
			'name' => $name,
			'id' => $id,
		);

		$code = "";
		// if($this->frontenduser_model->admin_existsbyid($inputadminname,$id)==false){
		//     $code="FAILURE";
		//     $msg="已有管理員姓名".$inputadminname;
		// }
		// else
		if ($this->shop_model->shopCodeExistByID(strtolower($shortcode), $id) == false) {
			$code = "FAILURE";
			$msg = "已有門店簡稱" . $shortcode;
		} else {
			$result = $this->shop_model->updateShop($data);
			if ($result == false) {
				$code = "FAILURE";
				$msg = "記錄不能更改";
			} else {
				$code = "SUCCESS";
				$msg = "記錄成功更改";
			}

		}

		header('Content-Type: application/json');
		// $code="SUCCESS";
		// $code="FAILURE";
		// $msg="已有相關記錄";
		// $myArray = ['id'=>12, 'name'=>'Topi'];
		$myArray = ['recordstatus' => $code, 'msg' => $msg];
		echo json_encode($myArray);
		// echo "posted";

	}
	public function delete() {
		$inputdeletecheckboxvalue = $this->input->post('inputdeletecheckboxvalue');
		$result = true;
		foreach ($inputdeletecheckboxvalue as $key => $values) {
			if ($this->shop_model->deleteShop($values) == false) {
				$result = false;
			}

		} //foreach ($inputdeletecheckboxvalue as $key => $values){

		if ($result == true) {
			$code = "SUCCESS";
			$msg = "相關記錄成功删徐";
		} else {
			$code = "FAILURE";
			$msg = "相關記錄不能删徐";
		}

		// $msg=$inputdeletecheckboxvalue;
		header('Content-Type: application/json');
		$myArray = ['recordstatus' => $code, 'msg' => $msg];
		echo json_encode($myArray);
	}
	public function getadmindata() {
		$id = $this->input->post('user_id');
		$data = $this->frontenduser_model->getAdmindateByid($id);
		header('Content-Type: application/json');
		echo json_encode($data);
	}

} //class Admins {
