<?php

require_once ("secure_area.php");

class Sales extends Secure_area {

	function __construct() {
		parent::__construct('sales');
		$this->load->library('sale_lib');
		//$this->output->enable_profiler(TRUE);
	}

	function index() {
		$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
		if(!$pageWasRefreshed){
			$this->sale_lib->clear_all();
		}
		$this->_reload();
	}

	function item_search() {
		$suggestions = array();


		if ($this->sale_lib->get_mode() == 'return' || $this->sale_lib->get_mode() == 'global_delivery') {

			$this->sale_lib->is_valid_receipt_shawpon(str_replace('#', '', $this->input->post('q'))) && $suggestions[] = $this->input->post('q');
		}

		if ($this->sale_lib->get_mode() == 'global') {
			$suggestions = array_merge($suggestions, $this->Item->get_item_search_suggestions_global($this->input->post('q'), 500));
		}else{
			$suggestions = array_merge($suggestions, $this->Item->get_item_search_suggestions($this->input->post('q'), $this->input->post('limit')));
		}
		echo implode("\n", $suggestions);
	}

	function customer_search() {
		$suggestions = $this->Customer->get_customer_search_suggestions_shawpon($this->input->post('q'), $this->input->post('limit'));
		echo implode("\n", $suggestions);
	}

	function select_customer() {
		if(strlen($this->input->post("customer"))>=9){
			$query = $this->db->query("
                    SELECT id
                    FROM pos_ospos_customers 
                    WHERE gs_card_no ='".mysql_real_escape_string(trim($this->input->post("customer")))."'");
			$customer_id = $query->row()->id;

		}else{
			$customer_id = $this->input->post("customer");
		}
		$this->sale_lib->set_customer($customer_id);
		$this->_reload();
	}

	function change_mode() {

		$mode = $this->input->post("mode");
		$previous_mode = $this->session->userdata('sale_mode');

		if ($mode == "global") {
			$this->sale_lib->clear_all();
		} elseif ($mode == "sale" && $previous_mode == "global") {
			$this->sale_lib->clear_all();
		} elseif ($mode == "return" && $previous_mode == "global") {
			$this->sale_lib->clear_all();
		}

		$this->sale_lib->set_mode($mode);
		$this->_reload();
	}

	function set_comment() {
		$this->sale_lib->set_comment($this->input->post('comment'));
	}

	function set_receive_location() {
		$this->sale_lib->set_receive_location($this->input->post('receive_location'));
	}

	function set_email_receipt() {
		$this->sale_lib->set_email_receipt($this->input->post('email_receipt'));
	}

	//Alain Multiple Payments
	function add_payment() {
		$data = array();

		$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';

		if(!$pageWasRefreshed){

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

			$amount_tendered = $this->input->post('amount_tendered');
			$ac_no_check = $this->input->post('ac_no_check');
			$check_name = $this->input->post('check_name');
			$holder_name_credit = $this->input->post('holder_name_credit');
			$holder_name_debit = $this->input->post('holder_name_debit');
			$holder_name_check = $this->input->post('holder_name_check');
			$bank_name_check = $this->input->post('bank_name_check');
			$bank_name_credit = $this->input->post('bank_name_credit');
			$bank_name_debit = $this->input->post('bank_name_debit');
			$branch_name_check = $this->input->post('branch_name_check');
			$check_date = $this->input->post('check_date');
			$card_expire_year = $this->input->post('card_expire_year');
			$card_expire_month = $this->input->post('card_expire_month');
			$card_no_credit = $this->input->post('card_no_credit');
			$card_no_debit = $this->input->post('card_no_debit');

			$check_date = date('Y-m-d', strtotime($check_date));

			$customer_id = $this->sale_lib->get_customer();
			if ($customer_id != -1) {
				$user_point = $this->input->post('user_point');
				$total = $this->sale_lib->get_total();
				$perPointCost = $this->Sale->getPointCost();
				$maxUse = floor($total / $perPointCost->value) + 1;

				$user_points = $this->Customer->get_user_points($customer_id);
				if (!empty($this->sale_lib->get_user_points())) {
					$usable_points = $user_points->point - $this->sale_lib->get_user_points();
				} else {
					$usable_points = $user_points->point;
				}
				//echo $user_point;exit;

				if ($maxUse >= $usable_points)
				$maxUse = $usable_points + 1;

				//echo $maxUse;exit;

				$this->form_validation->set_rules('user_point', 'User Point', "is_natural|less_than[$maxUse]");
			}


			$this->form_validation->set_rules('amount_tendered', 'lang:sales_amount_tendered', 'numeric');
			$this->form_validation->set_rules('ac_no', 'lang:ac_no', 'numeric');


			if ($this->form_validation->run() == FALSE) {
				if ($this->input->post('payment_type') == $this->lang->line('sales_gift_card'))
				$data['error'] = $this->lang->line('sales_must_enter_numeric_giftcard');
				else
				$data['error'] = $this->lang->line('sales_must_enter_numeric');

				if ($this->input->post('ac_no') == '')
				$data['error'] = $this->lang->line('sales_must_enter_ac_no');

				if (!empty($this->input->post('user_point')))
				$maxUse = $maxUse - 1;
				$data['error'] = 'Maximum usable point:' . $maxUse;

				$this->_reload($data);
				return;
			}

			if (!empty($user_point)) {
				$this->sale_lib->set_user_points($user_point);
			}

			$payment_type = $this->input->post('payment_type');
			if ($payment_type == $this->lang->line('sales_giftcard')) {
				$payments = $this->sale_lib->get_payments();
				$payment_type = $this->input->post('payment_type') . ':' . $payment_amount = $this->input->post('amount_tendered');
				$current_payments_with_giftcard = isset($payments[$payment_type]) ? $payments[$payment_type]['payment_amount'] : 0;
				$cur_giftcard_value = $this->Giftcard->get_giftcard_value($this->input->post('amount_tendered')) - $current_payments_with_giftcard;


				if ($cur_giftcard_value <= 0) {
					$data['error'] = 'Giftcard balance is ' . to_currency($this->Giftcard->get_giftcard_value($this->input->post('amount_tendered'))) . ' !';
					$this->_reload($data);
					return;
				}

				$new_giftcard_value = $this->Giftcard->get_giftcard_value($this->input->post('amount_tendered')) - $this->sale_lib->get_amount_due();
				$new_giftcard_value = ( $new_giftcard_value >= 0 ) ? $new_giftcard_value : 0;
				$data['warning'] = 'Giftcard ' . $this->input->post('amount_tendered') . ' balance is ' . to_currency($new_giftcard_value) . ' !';
				$payment_amount = min($this->sale_lib->get_amount_due(), $this->Giftcard->get_giftcard_value($this->input->post('amount_tendered')));
			} else {
				$payment_amount = $this->input->post('amount_tendered');
			}

			if (!$this->sale_lib->add_payment($payment_type, $amount_tendered, $ac_no_check, $check_name, $holder_name_credit, $holder_name_debit, $holder_name_check, $bank_name_check, $bank_name_credit, $bank_name_debit, $branch_name_check, $check_date, $card_expire_year, $card_expire_month, $card_no_credit, $card_no_debit)) {
				$data['error'] = 'Unable to Add Payment! Please try again!';
			}
		}
		$this->_reload($data);
	}

	//function global search

	function globalsearch($product_code) {
		//		$product_code ="20";
		//              $payment_amount = $this->input->post( 'item' );
		////////////////CALL TO API//////////////

		$client = new SoapClient(LIVE_SERVER_PATH . '/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));

		$api_data = array();
		$api_data['api_user'] = API_USER;
		$api_data['api_password'] = API_PASSWORD;
		$api_data['location_code'] = LOCATION_CODE;

		$checkMode = strpos($product_code, '@');
		if (!empty($checkMode)) {
			// manual add
			$selection = explode('@', $product_code);
			$product_code = $selection[0];
			$itemCode = $this->Sale->get_value('stock_id', 'stock_master', array('stock_id' => $product_code));
			$api_data['product_code'] = $itemCode->stock_id;
		} else {
			// add from bad code
			$lot = substr($this->input->post("item"), 8);
			$itemId = str_replace($lot, '', $product_code);
			$api_data['product_code'] = $itemId;
		}

		$api_data_final = json_encode($api_data);

		$result = $client->__soapCall('productSearchList', array('data' => $api_data_final));
		//        echo '<pre>';
		//        print_r(json_decode($result));exit;
		$data = array();

		$data['golbal_products'] = json_decode($result);

		$this->load->view('sales/globalsearch', $data);
	}

	function global_delivery($recept_id){
		$data['sale_items'] = $this->Sale->get_global_items_shawpon($recept_id);

		$this->db->set_dbprefix('ospos_');
		$data['receive_location'] = LOCATION_NAME;
		$this->load->view('sales/global_delivery', $data);
	}

	function confirm_global_delivery($recept_id){

		/*
		if(!empty($this->input->post('delivered'))){
		$this->Sale->update_data('sales', ['is_delivered' => 1], ['sale_id' => $recept_id]);
		}*/
		$this->_reload();
	}

	function confirm_delivery($recept_id){
		if(!empty($this->input->post('delivered'))){
			$this->Sale->update_data('sales', ['is_delivered' => 1], ['sale_id' => $recept_id]);
		}
		$this->_reload();
	}

	//Alain Multiple Payments
	function delete_payment($payment_id) {
		$this->sale_lib->delete_payment($payment_id);
		//$this->sale_lib->clear_user_points();
		$this->_reload();
	}

	function add() {

		$data = array();
		$mode = $this->sale_lib->get_mode();

		$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';

		if(!$pageWasRefreshed){

			$checkMode = strpos($this->input->post("item"), '@');
			$invoice = strpos($this->input->post("item"), '#');
			if (!empty($checkMode)) {
				// manual add
				// item_id - lot_no
				$selection = explode('@', $this->input->post("item"));
				$item_id_or_number_or_item_kit_or_receipt = $selection[0];
				$lot = $selection[1];
			} else if ($invoice != 0) {
				// sale id for reurn
				$item_id_or_number_or_item_kit_or_receipt = str_replace('#', '', $this->input->post("item"));
				$this->db->set_dbprefix('');
				$stockMove = $this->Sale->get_value('lot_no', 'stock_moves', ['trans_no' => $item_id_or_number_or_item_kit_or_receipt]);
				$this->db->set_dbprefix('ospos_');
				$lot = $stockMove->lot_no;
			} else {
				// add from bad code

				if($mode=='return'){

					if(is_numeric($this->input->post("item"))){
						$lot = substr($this->input->post("item"), 8);
						$item_id_or_number_or_item_kit_or_receipt = str_replace($lot, '', $this->input->post("item"));
					}else{
						$item_id_or_number_or_item_kit_or_receipt=$this->input->post("item");
					}
				}else{
					$lot = substr($this->input->post("item"), 8);
					$item_id_or_number_or_item_kit_or_receipt = str_replace($lot, '', $this->input->post("item"));
				}
			}

			$quantity = $mode == "sale" ? 1 : -1;

			if ($this->sale_lib->is_valid_receipt_shawpon($item_id_or_number_or_item_kit_or_receipt) && $mode == 'return') {


				$this->sale_lib->return_entire_sale_shawpon($item_id_or_number_or_item_kit_or_receipt);
				//print_r($this->sale_lib->return_entire_sale($item_id_or_number_or_item_kit_or_receipt));
				//exit;
			} elseif ($this->sale_lib->is_valid_item_kit($item_id_or_number_or_item_kit_or_receipt)) {

				$this->sale_lib->add_item_kit($item_id_or_number_or_item_kit_or_receipt);
			}
			else if (!$this->sale_lib->add_item($item_id_or_number_or_item_kit_or_receipt, $quantity, '', $lot)) {

				$data['error'] = $this->lang->line('sales_unable_to_add_item');
			}

			if ($this->sale_lib->out_of_stock($item_id_or_number_or_item_kit_or_receipt,$lot)) {
				$data['warning'] = $this->lang->line('sales_quantity_less_than_zero');
			}





			//        if ($mode == 'return' && $this->sale_lib->is_valid_receipt($item_id_or_number_or_item_kit_or_receipt)) {
			//            echo 1;
			//            $this->sale_lib->return_entire_sale($item_id_or_number_or_item_kit_or_receipt);
			//        } else if ($this->sale_lib->is_valid_item_kit($item_id_or_number_or_item_kit_or_receipt)) {
			//            echo 2;
			//            $this->sale_lib->add_item_kit($item_id_or_number_or_item_kit_or_receipt, $item_location);
			//        } else if (!$this->sale_lib->add_item($item_id_or_number_or_item_kit_or_receipt, $quantity, '', $lot)) {
			//            echo 3;
			//            $data['error'] = $this->lang->line('sales_unable_to_add_item');
			//        }
			//
			//        if ($this->sale_lib->out_of_stock($item_id_or_number_or_item_kit_or_receipt)) {
			//            echo 4;
			//            $data['warning'] = $this->lang->line('sales_quantity_less_than_zero');
			//        }
			//        exit;

			////////////////Shawpon Fix////////////////

			$this->Sale_suspended->delete_all_shawpon();
			$data['cart'] = $this->sale_lib->get_cart();

			$data['subtotal'] = $this->sale_lib->get_subtotal();
			$data['taxes'] = $this->sale_lib->get_taxes();
			$data['total'] = $this->sale_lib->get_total();
			$data['receipt_title'] = $this->lang->line('sales_receipt');
			$data['transaction_time'] = date('m/d/Y h:i:s a');
			$customer_id = $this->sale_lib->get_customer();
			$employee_id = $this->Employee->get_logged_in_employee_info()->person_id;
			$comment = $this->input->post('comment');
			$emp_info = $this->Employee->get_info($employee_id);
			$payment_type = $this->input->post('payment_type');
			$data['payment_type'] = $this->input->post('payment_type');
			//Alain Multiple payments
			$data['payments'] = $this->sale_lib->get_payments();
			$data['amount_change'] = to_currency($this->sale_lib->get_amount_due() * -1);
			$data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;

			if ($customer_id != -1) {
				$cust_info = $this->Customer->get_info($customer_id);
				$data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
			}

			$total_payments = 0;

			foreach ($data['payments'] as $payment) {
				$total_payments += $payment['payment_amount'];
			}
			$this->Sale_suspended->save($data['cart'], $customer_id, $employee_id, $comment, $data['payments']);

			////////////////Shawpon Fix////////////////
		}
		$this->_reload($data);
	}

	function edit_item($line) {
		$data = array();
		$this->form_validation->set_rules('price', 'lang:items_price', 'required|numeric');
		$this->form_validation->set_rules('quantity', 'lang:items_quantity', 'required|numeric');

		$description = $this->input->post("description");
		$serialnumber = $this->input->post("serialnumber");
		$price = $this->input->post("price");
		$quantity = $this->input->post("quantity");
		$discount = $this->input->post("discount");


		if ($this->form_validation->run() != FALSE) {
			$this->sale_lib->edit_item($line, $description, $serialnumber, $quantity, $discount, $price);
		} else {
			$data['error'] = $this->lang->line('sales_error_editing_item');
		}

		if ($this->sale_lib->out_of_stock($this->sale_lib->get_item_id($line),$this->sale_lib->get_lot_no($line))) {
			$data['warning'] = $this->lang->line('sales_quantity_less_than_zero');
		}

		     ////////////////Shawpon Fix////////////////

			$this->Sale_suspended->delete_all_shawpon();
			$data['cart'] = $this->sale_lib->get_cart();

			$data['subtotal'] = $this->sale_lib->get_subtotal();
			$data['taxes'] = $this->sale_lib->get_taxes();
			$data['total'] = $this->sale_lib->get_total();
			$data['receipt_title'] = $this->lang->line('sales_receipt');
			$data['transaction_time'] = date('m/d/Y h:i:s a');
			$customer_id = $this->sale_lib->get_customer();
			$employee_id = $this->Employee->get_logged_in_employee_info()->person_id;
			$comment = $this->input->post('comment');
			$emp_info = $this->Employee->get_info($employee_id);
			$payment_type = $this->input->post('payment_type');
			$data['payment_type'] = $this->input->post('payment_type');
			//Alain Multiple payments
			$data['payments'] = $this->sale_lib->get_payments();
			$data['amount_change'] = to_currency($this->sale_lib->get_amount_due() * -1);
			$data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;

			if ($customer_id != -1) {
				$cust_info = $this->Customer->get_info($customer_id);
				$data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
			}

			$total_payments = 0;

			foreach ($data['payments'] as $payment) {
				$total_payments += $payment['payment_amount'];
			}
			$this->Sale_suspended->save($data['cart'], $customer_id, $employee_id, $comment, $data['payments']);

			////////////////Shawpon Fix////////////////

		$this->_reload($data);
	}

	function delete_item($item_number) {
		$this->sale_lib->delete_item($item_number);
		
		////////////////Shawpon Fix////////////////

			$this->Sale_suspended->delete_all_shawpon();
			$data['cart'] = $this->sale_lib->get_cart();

			$data['subtotal'] = $this->sale_lib->get_subtotal();
			$data['taxes'] = $this->sale_lib->get_taxes();
			$data['total'] = $this->sale_lib->get_total();
			$data['receipt_title'] = $this->lang->line('sales_receipt');
			$data['transaction_time'] = date('m/d/Y h:i:s a');
			$customer_id = $this->sale_lib->get_customer();
			$employee_id = $this->Employee->get_logged_in_employee_info()->person_id;
			$comment = $this->input->post('comment');
			$emp_info = $this->Employee->get_info($employee_id);
			$payment_type = $this->input->post('payment_type');
			$data['payment_type'] = $this->input->post('payment_type');
			//Alain Multiple payments
			$data['payments'] = $this->sale_lib->get_payments();
			$data['amount_change'] = to_currency($this->sale_lib->get_amount_due() * -1);
			$data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;

			if ($customer_id != -1) {
				$cust_info = $this->Customer->get_info($customer_id);
				$data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
			}

			$total_payments = 0;

			foreach ($data['payments'] as $payment) {
				$total_payments += $payment['payment_amount'];
			}
			$this->Sale_suspended->save($data['cart'], $customer_id, $employee_id, $comment, $data['payments']);

			////////////////Shawpon Fix////////////////
		$this->_reload();
	}

	function remove_customer() {
		$this->sale_lib->remove_customer();
		$this->_reload();
	}

	function new_card(){
		$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
		if(!$pageWasRefreshed){
			$this->Sale->update_card();
		}
		$this->_reload($data);
	}

	function complete() {

		$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';

		if(!$pageWasRefreshed){

			if(intval($this->input->post('global_delivery_shawpon'))==1 AND trim($this->input->post('global_delivery_receipt_no'))){

				$success = $this->Sale->update_delivery_global($this->input->post('global_delivery_receipt_no'),$this->input->post('bag_big'),$this->input->post('bag_medium'),$this->input->post('bag_small'));

				if($success==true){

					$this->_reload(array('success' => $this->lang->line('sales_successfully_delivered_global')));
				}else{
					$this->_reload(array('error_message' => $this->lang->line('sales_unsuccessfully_delivered_global')));

				}

			}else{

				$data['cart'] = $this->sale_lib->get_cart();


				$data["mode"] = $this->sale_lib->get_mode();


				if($data['mode']=="global"){
					$selected_location = $data['cart'][1]['location'];

				}else{
					if($data['cart'][1]['location'] == LOCATION_CODE){
						$selected_location = LOCATION_CODE;
					}else{
						$this->db->set_dbprefix('');
						$select_location = $this->Sale->get_value('loc_code', 'locations', ['location_name' => $data['cart'][1]['location']]);
						$selected_location = $select_location->loc_code;
						$this->db->set_dbprefix('ospos_');
					}
				}

				$location = $data['cart'][1]['location'];

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


				if ($data['cart']) {
					foreach ($data['cart'] as $c) {
						$tax = $this->Item->get_item_tax($c['item_id']);
						$data['item_tax'][$c['item_id']] = $tax->percent;
					}
					unset($c);
				}

				//$data['customer_info'] = $this->sale_lib->get_customer();


				$data['taxes'] = $this->sale_lib->get_taxes();

				$data['total'] = $this->sale_lib->get_total()-$purchase_discount_amount;
				$data['receipt_title'] = $this->lang->line('sales_receipt');
				$data['transaction_time'] = date('m/d/Y h:i:s a');
				$customer_id = $this->sale_lib->get_customer();
				$employee_id = $this->Employee->get_logged_in_employee_info()->person_id;
				$comment = $this->sale_lib->get_comment();
				$receive_location = $this->sale_lib->get_receive_location();

				if(trim($receive_location)==""){
					$data['receive_location'] = LOCATION_CODE;
				}else{
					$data['receive_location'] = $this->sale_lib->get_receive_location();
				}

				$emp_info = $this->Employee->get_info($employee_id);
				$data['payments'] = $this->sale_lib->get_payments();
				$data['amount_change'] = to_currency(($this->sale_lib->get_amount_due()-$purchase_discount_amount) * -1);
				$data['purchase_discount_amount'] = $purchase_discount_amount;
				$data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;

				$data['cart_point'] = $this->sale_lib->get_user_points();
				$pointCost = $this->Sale->getPointCost();
				$data['cart_point_value'] = $pointCost->value;
				$data['cart_point_cost'] = $pointCost->value * $data['cart_point'];


				/////////////////////////////////////////////////////////////////////
				////previous due///

				if(($this->sale_lib->get_amount_due()-$purchase_discount_amount)==0){
					$data['previous_due'] = ($this->sale_lib->get_amount_due()-$purchase_discount_amount);
				}else{
					$data['previous_due'] = ($this->sale_lib->get_amount_due()-$purchase_discount_amount) * -1;
				}


				$data['previous_due'] = ($this->sale_lib->get_amount_due()-$purchase_discount_amount) * -1;
				/////Grand Total///
				$data['grand_total'] = $this->sale_lib->get_total() + $data['previous_due']-$purchase_discount_amount;


				$total_item_amount = $this->sale_lib->get_total();
				$previous_due = $this->Customer->customer_previous_due($customer_id);
				$previous_due_shawpon = $previous_due;
				$previous_due_advance = $previous_due;

				$remain_item_amount = $total_item_amount;

				$data['final_due'] = $previous_due;

				if ($customer_id != -1) {
					$cust_info = $this->Customer->get_info($customer_id);
					$data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
					$data['customerID'] = $customer_id;

					$data['customer_card_no'] = $cust_info->gs_card_no;

					if(trim($data['customer_card_no'])==""){

						if( trim($this->input->post('new_card_number'))){
							$data['customer_card_no']=trim($this->input->post('new_card_number'));
						}
					}
				}

				//SAVE sale to database
				$data_return = $this->Sale->save($data['cart'], $customer_id, $employee_id, $comment, $data['payments'], NULL, $receive_location, $selected_location,$data);

				$data['sale_id']=$data_return['sale_id'];
				$data['receipt_no']=$data_return['receipt_no'];

				if ($data['sale_id'] == '-1') {
					$data['error_message'] = $this->lang->line('sales_transaction_failed');
				}
				$data['complete'] = "complete";



				$this->load->view("sales/receipt", $data);
				// $this->load->view("sales/register",$data);
				$this->sale_lib->clear_all();

				$this->_reload($data);

			}
		}
	}


	function receipt_reprint($receipt_no){
		$sale_info = $this->Sale->get_info($this->Sale->get_sale_by_invoice_number_shawpon_local($receipt_no)->row()->sale_id)->row_array();

		$this->sale_lib->copy_entire_sale_shawpon($sale_info['sale_id'],1);
		$data['cart'] = $this->sale_lib->get_cart();

		if($data['cart'][1]['location'] == LOCATION_CODE){
			$selected_location = LOCATION_CODE;
		}else{
			$this->db->set_dbprefix('');
			$select_location = $this->Sale->get_value('loc_code', 'locations', ['location_name' => $data['cart'][1]['location']]);
			$selected_location = $select_location->loc_code;
			$this->db->set_dbprefix('ospos_');
		}

		if ($data['cart']) {
			foreach ($data['cart'] as $c) {
				$tax = $this->Item->get_item_tax($c['item_id']);
				$data['item_tax'][$c['item_id']] = $tax->percent;
			}
			unset($c);
		}



		//$data['customer_info'] = $this->sale_lib->get_customer();
		$location = $data['cart'][1]['location'];
		$data["mode"] = $this->sale_lib->get_mode();

		$data['taxes'] = $this->sale_lib->get_taxes();

		$data['total'] = $this->sale_lib->get_total();
		$data['receipt_title'] = $this->lang->line('sales_receipt');
		$data['transaction_time'] = date('m/d/Y h:i:s a');
		//$customer_id = $this->sale_lib->get_customer();
		$customer_id = $sale_info['customer_id'];

		$employee_id = $this->Employee->get_logged_in_employee_info()->person_id;
		$comment = $this->sale_lib->get_comment();
		$receive_location = $this->sale_lib->get_receive_location();

		if(trim($receive_location)==""){
			$data['receive_location']=$sale_info['receive_location'];
		}else{
			$data['receive_location'] = $receive_location;
		}

		if(intval($sale_info['is_global'])>0){
			$data["mode"]="global";
		}


		if ($customer_id != -1) {
			$cust_info = $this->Customer->get_info($customer_id);
			$data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
			$data['customer_card_no'] = $cust_info->gs_card_no;
			$data['customerID'] = $customer_id;
		}


		$emp_info = $this->Employee->get_info($employee_id);
		//$data['payments'] = $this->sale_lib->get_payments();

		$data['purchase_discount_amount']=0;

		foreach ($this->sale_lib->get_payments() as $data_key => $data_value) {
			if($data_key=="Purchase Discount"){
				$data['purchase_discount_amount']=$data_value['payment_amount'];
			}else{
				$data['payments'][$data_key]=$data_value;
			}
		}


		//$data['payments'] = $this->sale_lib->get_sale_payments_shawpon($sale_info['sale_id'])->row_array();

		$previous_due_payment_amount=0;
		$payment_type_str=str_replace(" ","",$sale_info['payment_type']);
		$payment_type_str=str_replace("&nbsp;","",$payment_type_str);
		$find_previous_due_str="PreviousDuePayment:";
		$previous_due_position=strpos($payment_type_str, $find_previous_due_str);

		if($previous_due_position){

			$previous_due_next=substr($payment_type_str,$previous_due_position+19);
			$find_bdt_due_str="BDT";
			$bdt_position=strpos($previous_due_next, $find_bdt_due_str);
			$previous_due_payment_amount=substr($previous_due_next,0,$bdt_position);
		}

		$data['final_due'] = $previous_due_payment_amount;

		$data['amount_change'] = to_currency($this->sale_lib->get_amount_due() * -1);
		$data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;

		$data['cart_point'] = $this->sale_lib->get_user_points();
		$pointCost = $this->Sale->getPointCost();
		$data['cart_point_value'] = $pointCost->value;
		$data['cart_point_cost'] = $pointCost->value * $data['cart_point'];

		$data['sale_id']=$sale_info['sale_id'];
		$data['receipt_no']=$receipt_no;

		if ($data['sale_id'] == '-1') {
			$data['error_message'] = $this->lang->line('sales_transaction_failed');
		}

		$this->load->view("sales/receipt_reprint", $data);
		$this->sale_lib->clear_all();

		$this->load->view("reports/invoices");
		///$this->sale_lib->clear_all();
		//$this->_reload($data);
		//redirect('/reports/invoices');
	}

	function receipt_challan($receipt_no){

		$sale_info = $this->Sale->get_info($this->Sale->get_sale_by_invoice_number_shawpon_local($receipt_no)->row()->sale_id)->row_array();
		$this->sale_lib->copy_entire_sale_shawpon($sale_info['sale_id']);
		$summary_data=array();
		foreach ($this->sale_lib->get_cart() as $data_key => $data_value) {

			$summary_data[$data_value['gender']][$data_value['product_description']]['total_qty']=$summary_data[$data_value['gender']][$data_value['product_description']]['total_qty']+$data_value['quantity'];
			$summary_data[$data_value['gender']][$data_value['product_description']]['total_sale_amount']=$summary_data[$data_value['gender']][$data_value['product_description']]['total_sale_amount']+($data_value['total_sale_amount']+$data_value['total_discount']);
			$summary_data[$data_value['gender']][$data_value['product_description']]['total_discount']=$summary_data[$data_value['gender']][$data_value['product_description']]['total_discount']+$data_value['total_discount'];
			$summary_data[$data_value['gender']][$data_value['product_description']]['vat_amount']=$summary_data[$data_value['gender']][$data_value['product_description']]['vat_amount']+$data_value['vat_amount'];
			$summary_data[$data_value['gender']][$data_value['product_description']]['total_invoice_amount']=$summary_data[$data_value['gender']][$data_value['product_description']]['total_invoice_amount']+($data_value['total_sale_amount']+$data_value['vat_amount']);
		}

		$data['cart'] = $this->sale_lib->get_cart();
		$data['summary_cart'] = $summary_data;
		$data['payment_type'] = $sale_info['payment_type'];
		$data['invoice_date'] = date("d/m/Y",strtotime($sale_info['sale_time']));


		if($data['cart'][1]['location'] == LOCATION_CODE){
			$selected_location = LOCATION_CODE;
		}else{
			$this->db->set_dbprefix('');
			$select_location = $this->Sale->get_value('loc_code', 'locations', ['location_name' => $data['cart'][1]['location']]);
			$selected_location = $select_location->loc_code;
			$this->db->set_dbprefix('ospos_');
		}

		if ($data['cart']) {
			foreach ($data['cart'] as $c) {
				$tax = $this->Item->get_item_tax($c['item_id']);
				$data['item_tax'][$c['item_id']] = $tax->percent;
			}
			unset($c);
		}

		//$data['customer_info'] = $this->sale_lib->get_customer();
		$location = $data['cart'][1]['location'];
		$data["mode"] = $this->sale_lib->get_mode();

		$data['taxes'] = $this->sale_lib->get_taxes();

		$data['total'] = $this->sale_lib->get_total();
		$data['receipt_title'] = $this->lang->line('sales_receipt');
		$data['transaction_time'] = date('m/d/Y h:i:s a');
		//$customer_id = $this->sale_lib->get_customer();
		$customer_id = $sale_info['customer_id'];

		$employee_id = $this->Employee->get_logged_in_employee_info()->person_id;
		$comment = $this->sale_lib->get_comment();
		$receive_location = $this->sale_lib->get_receive_location();

		if(trim($receive_location)==""){
			$data['receive_location']=$sale_info['receive_location'];
		}else{
			$data['receive_location'] = $receive_location;
		}

		if(intval($sale_info['is_global'])>0){
			$data["mode"]="global";
		}


		if ($customer_id != -1) {
			$cust_info = $this->Customer->get_info($customer_id);
			$data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
			$data['customerID'] = $customer_id;
		}

		$emp_info = $this->Employee->get_info($employee_id);
		$data['payments'] = $this->sale_lib->get_payments();
		//$data['payments'] = $this->sale_lib->get_sale_payments_shawpon($sale_info['sale_id'])->row_array();

		$previous_due_payment_amount=0;
		$payment_type_str=str_replace(" ","",$sale_info['payment_type']);
		$payment_type_str=str_replace("&nbsp;","",$payment_type_str);
		$find_previous_due_str="PreviousDuePayment:";
		$previous_due_position=strpos($payment_type_str, $find_previous_due_str);

		if($previous_due_position){

			$previous_due_next=substr($payment_type_str,$previous_due_position+19);
			$find_bdt_due_str="BDT";
			$bdt_position=strpos($previous_due_next, $find_bdt_due_str);
			$previous_due_payment_amount=substr($previous_due_next,0,$bdt_position);
		}

		$data['final_due'] = $previous_due_payment_amount;

		$data['amount_change'] = to_currency($this->sale_lib->get_amount_due() * -1);
		$data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;

		$data['cart_point'] = $this->sale_lib->get_user_points();
		$pointCost = $this->Sale->getPointCost();
		$data['cart_point_value'] = $pointCost->value;
		$data['cart_point_cost'] = $pointCost->value * $data['cart_point'];

		$data['sale_id']=$sale_info['sale_id'];
		$data['receipt_no']=$receipt_no;

		if ($data['sale_id'] == '-1') {
			$data['error_message'] = $this->lang->line('sales_transaction_failed');
		}

		$this->load->view("sales/sales_challan",$data);

		/*$this->load->view("sales/receipt_challan", $data);
		$this->sale_lib->clear_all();
		$this->load->view("reports/invoices");*/
	}



	function receipt($sale_id) {

		$sale_info = $this->Sale->get_info($sale_id)->row_array();


		$this->sale_lib->copy_entire_sale($sale_id);
		$data['cart'] = $this->sale_lib->get_cart();
		$data['payments'] = $this->sale_lib->get_payments();
		$data['subtotal'] = $this->sale_lib->get_subtotal();
		$data['taxes'] = $this->sale_lib->get_taxes();
		$data['total'] = $this->sale_lib->get_total();
		$data['receipt_title'] = $this->lang->line('sales_receipt');
		$data['transaction_time'] = date('m/d/Y h:i:s a', strtotime($sale_info['sale_time']));
		$customer_id = $this->sale_lib->get_customer();
		$emp_info = $this->Employee->get_info($sale_info['employee_id']);
		$data['payment_type'] = $sale_info['payment_type'];
		$data['amount_change'] = to_currency($this->sale_lib->get_amount_due() * -1);
		$data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;

		if ($customer_id != -1) {
			$cust_info = $this->Customer->get_info($customer_id);
			$data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
		}
		$data['sale_id'] = 'POS ' . $sale_id;
		$this->load->view("sales/receipt", $data);
		//$this->sale_lib->clear_all();
	}

	function edit($sale_id) {
		$data = array();

		$data['customers'] = array('' => 'No Customer');
		foreach ($this->Customer->get_all()->result() as $customer) {
			$data['customers'][$customer->person_id] = $customer->first_name . ' ' . $customer->last_name;
		}

		$data['employees'] = array();
		foreach ($this->Employee->get_all()->result() as $employee) {
			$data['employees'][$employee->person_id] = $employee->first_name . ' ' . $employee->last_name;
		}

		$data['sale_info'] = $this->Sale->get_info($sale_id)->row_array();


		$this->load->view('sales/edit', $data);
	}

	function delete($sale_id) {
		$data = array();

		if ($this->Sale->delete($sale_id)) {
			$data['success'] = true;
		} else {
			$data['success'] = false;
		}

		$this->load->view('sales/delete', $data);
	}

	function save($sale_id) {
		$sale_data = array(
		'sale_time' => date('Y-m-d', strtotime($this->input->post('date'))),
		'customer_id' => $this->input->post('customer_id') ? $this->input->post('customer_id') : null,
		'employee_id' => $this->input->post('employee_id'),
		'comment' => $this->input->post('comment')
		);

		if ($this->Sale->update($sale_data, $sale_id)) {
			echo json_encode(array('success' => true, 'message' => $this->lang->line('sales_successfully_updated')));
		} else {
			echo json_encode(array('success' => false, 'message' => $this->lang->line('sales_unsuccessfully_updated')));
		}
	}

	function _payments_cover_total($data) {
		$total_payments = 0;

		$previous_due = 0;

		if ($this->session->userdata['customer'] != -1) {
			$previous_due = $this->Customer->customer_previous_due($this->session->userdata['customer']);
		}
		foreach ($this->sale_lib->get_payments() as $payment) {
			$total_payments += $payment['payment_amount'];
		}

		/* Changed the conditional to account for floating point rounding */
		if (( $this->sale_lib->get_mode() == 'sale' ) && ( ( to_currency_no_money($this->sale_lib->get_total() + $previous_due-$data['purchase_discount_amount']) - $total_payments ) > 1e-6 )) {
			return false;
		}

		return true;
	}

	function _reload($data = array()) {

		//Add Month in Dropdown

		for ($m = 1; $m <= 12; $m++) {
			$month = date('F', mktime(0, 0, 0, $m, 1, date('Y')));

			$data['month'][$m] = $month;
		}


		//Add Year in Dropdown

		$n = 0;
		while ($n < 16) {
			$date = date("Y") + $n;

			$data['year'][$date] = $date;
			$n++;
		}

		//add bank name in Dropdown
		$bank_id = $this->Item->get_bank_id();
		foreach ($bank_id as $key => $id) {
			$data['bank_id'][$id['id']] = $id['name'];
		}

		$person_info = $this->Employee->get_logged_in_employee_info();
		$data['cart'] = $this->sale_lib->get_cart();




		$data['lot_details'] = $this->sale_lib->get_lot_detail();
		$data['modes'] = array('sale' => $this->lang->line('sales_sale'), 'return' => $this->lang->line('sales_return'), 'global' => $this->lang->line('sales_global'), 'global_delivery' => 'Global Delivery');
		$data['mode'] = $this->sale_lib->get_mode();
		$data['subtotal'] = $this->sale_lib->get_subtotal();
		$data['taxes'] = $this->sale_lib->get_taxes();
		$data['total'] = $this->sale_lib->get_total();
		$data['items_module_allowed'] = $this->Employee->has_permission('items', $person_info->person_id);
		$data['comment'] = $this->sale_lib->get_comment();
		$data['bag_big'] = $this->sale_lib->get_bag_big();
		$data['bag_medium'] = $this->sale_lib->get_bag_medium();
		$data['bag_small'] = $this->sale_lib->get_bag_small();
		$data['email_receipt'] = $this->sale_lib->get_email_receipt();
		$data['payments_total'] = $this->sale_lib->get_payments_total();
		$data['amount_due'] = round($this->sale_lib->get_amount_due());
		$data['payments'] = $this->sale_lib->get_payments();
		$data['cart_point'] = $this->sale_lib->get_user_points();
		$pointCost = $this->Sale->getPointCost();
		$data['cart_point_value'] = $pointCost->value;
		$data['cart_point_cost'] = $pointCost->value * $data['cart_point'];

		if ($this->session->userdata['customer'] != -1) {

			if($data['mode']=="global"){
				$data['payment_options'] = array(
				$this->lang->line('sales_cash') => $this->lang->line('sales_cash'),
				$this->lang->line('sales_check') => $this->lang->line('sales_check'),
				//$this->lang->line('sales_giftcard') => $this->lang->line('sales_giftcard'),
				$this->lang->line('sales_debit') => $this->lang->line('sales_debit'),
				$this->lang->line('sales_credit') => $this->lang->line('sales_credit'),
				'User Point' => 'User Point',
				'Purchase Bonus' => 'Special Sales Discount'
				);
			}else{

				$data['payment_options'] = array(
				$this->lang->line('sales_cash') => $this->lang->line('sales_cash'),
				$this->lang->line('sales_check') => $this->lang->line('sales_check'),
				//$this->lang->line('sales_giftcard') => $this->lang->line('sales_giftcard'),
				$this->lang->line('sales_debit') => $this->lang->line('sales_debit'),
				$this->lang->line('sales_credit') => $this->lang->line('sales_credit'),
				'User Point' => 'User Point',
				'Due' => 'Due',
				'Purchase Bonus' => 'Special Sales Discount'
				//$this->lang->line('sales_due') => $this->lang->line('sales_due')
				);

			}
		} else {
			$data['payment_options'] = array(
			$this->lang->line('sales_cash') => $this->lang->line('sales_cash'),
			$this->lang->line('sales_check') => $this->lang->line('sales_check'),
			//$this->lang->line('sales_giftcard') => $this->lang->line('sales_giftcard'),
			$this->lang->line('sales_debit') => $this->lang->line('sales_debit'),
			$this->lang->line('sales_credit') => $this->lang->line('sales_credit'),
			'Purchase Bonus' => 'Special Sales Discount'
			//'User Point' => 'User Point'
			);
		}

		$customer_id = $this->sale_lib->get_customer();

		if(isset($data['complete'])){
			$data['customer']="";
		}


		if ($customer_id != -1) {
			$info = $this->Customer->get_info($customer_id);
			$data['customer'] = $info->first_name . ' ' . $info->last_name;
			$data['customer_gs_card'] = $info->gs_card_no;
			$data['customer_gs_id'] = $info->id;
			$data['customer_email'] = $info->email;
			$data['customer_state'] = $info->state;
			$data['previous_due'] = $this->Customer->customer_previous_due($customer_id);
			$user_points = $this->Customer->get_user_points($customer_id);
			$data['user_points'] = $user_points->point;

			$purchase_discount_elegible_percentage_amount_temp=($user_points->point*100)+$data['subtotal'];

			$purchase_discount_elegible_percentage_amount_cal=0;

			if(POINT_CARD_ACTIVE==1){
				if(trim($data['customer_gs_card']) AND trim($data['customer_state'])==""){
					if($purchase_discount_elegible_percentage_amount_temp>POINT_FOR_FIRST_DISCOUNT_START_GTHAN){

						if($purchase_discount_elegible_percentage_amount_temp>POINT_FOR_SECOND_DISCOUNT_START_GTHAN){
							$data['purchase_discount_percentage']= SECOND_DISCOUNT_PERCENTAGE;
							$data['purchase_discount_amount']= round(number_format((($data['subtotal']*SECOND_DISCOUNT_PERCENTAGE)/100),2,'.',''));

						}else if($purchase_discount_elegible_percentage_amount_temp<=POINT_FOR_SECOND_DISCOUNT_START_GTHAN){
							$data['purchase_discount_percentage']= FIRST_DISCOUNT_PERCENTAGE;
							$data['purchase_discount_amount']= round(number_format((($data['subtotal']*FIRST_DISCOUNT_PERCENTAGE)/100),2,'.',''));
						}
					}else{
						$data['purchase_discount_percentage']= FIRST_DISCOUNT_PERCENTAGE;
						$data['purchase_discount_amount']= round(number_format((($data['subtotal']*FIRST_DISCOUNT_PERCENTAGE)/100),2,'.',''));
					}
					$purchase_discount_elegible_percentage_amount_cal=$data['subtotal'];

				}elseif(trim($data['customer_gs_card']) AND trim($data['customer_state'])!=""){


					if($user_points->point==0){
						$purchase_discount_elegible_percentage_amount_temp=($user_points->point*100)+$data['subtotal'];

						if($data['subtotal']>POINT_FOR_FIRST_DISCOUNT_START_GTHAN){
							if(($data['subtotal']-POINT_FOR_CARD)>POINT_FOR_SECOND_DISCOUNT_START_GTHAN){
								$data['purchase_discount_percentage']= SECOND_DISCOUNT_PERCENTAGE;
								$data['purchase_discount_amount']= round(number_format(((($data['subtotal']-POINT_FOR_CARD)*SECOND_DISCOUNT_PERCENTAGE)/100),2,'.',''));

							}else if(($data['subtotal']-POINT_FOR_CARD)<=POINT_FOR_SECOND_DISCOUNT_START_GTHAN){
								$data['purchase_discount_percentage']= FIRST_DISCOUNT_PERCENTAGE;
								$data['purchase_discount_amount']= round(number_format(((($data['subtotal']-POINT_FOR_CARD)*FIRST_DISCOUNT_PERCENTAGE)/100),2,'.',''));
							}
						}else{
							$data['purchase_discount_percentage']= 0;
							$data['purchase_discount_amount']= number_format(0,2,'.','');
						}
						$purchase_discount_elegible_percentage_amount_cal=$data['subtotal']-POINT_FOR_CARD;
					}else{
						if($purchase_discount_elegible_percentage_amount_temp>POINT_FOR_FIRST_DISCOUNT_START_GTHAN){

							if($purchase_discount_elegible_percentage_amount_temp>POINT_FOR_SECOND_DISCOUNT_START_GTHAN){
								$data['purchase_discount_percentage']= SECOND_DISCOUNT_PERCENTAGE;
								$data['purchase_discount_amount']= round(number_format((($data['subtotal']*SECOND_DISCOUNT_PERCENTAGE)/100),2,'.',''));

							}else if($purchase_discount_elegible_percentage_amount_temp<=POINT_FOR_SECOND_DISCOUNT_START_GTHAN){
								$data['purchase_discount_percentage']= FIRST_DISCOUNT_PERCENTAGE;
								$data['purchase_discount_amount']= round(number_format((($data['subtotal']*FIRST_DISCOUNT_PERCENTAGE)/100),2,'.',''));
							}
						}else{
							$data['purchase_discount_percentage']= FIRST_DISCOUNT_PERCENTAGE;
							$data['purchase_discount_amount']= round(number_format((($data['subtotal']*FIRST_DISCOUNT_PERCENTAGE)/100),2,'.',''));
						}
						$purchase_discount_elegible_percentage_amount_cal=$data['subtotal'];
					}

				}else{
					$data['purchase_discount_percentage']= 0;
					$data['purchase_discount_amount']= number_format(0,2,'.','');
					$purchase_discount_elegible_percentage_amount_cal=$data['subtotal']-POINT_FOR_CARD;
				}

			}else{
				$data['purchase_discount_percentage']= 0;
				$data['purchase_discount_amount']= number_format(0,2,'.','');
				$purchase_discount_elegible_percentage_amount_cal=-1;
			}

			$data['purchase_discount_elegible_percentage_amount']=$purchase_discount_elegible_percentage_amount_cal ;


		}

		$data['amount_due'] = round($data['amount_due']-$data['purchase_discount_amount']);

		$data['payments_cover_total'] = $this->_payments_cover_total($data);

		//load locations
		//        $this->load->model('cron_model');
		//        $this->db->set_dbprefix('');
		//        $locations = $this->cron_model->get_data('locations');
		//        $this->db->set_dbprefix('ospos_');
		//        if($locations){
		//            foreach($locations as $l){
		//                $posLocations[$l->loc_code] = $l->loc_code;
		//            }
		//        }
		//
		//        $data['posLocations'] = $posLocations;


		$this->load->view("sales/register", $data);
	}

	function cancel_sale() {
		$this->sale_lib->clear_all();
		$this->_reload();
	}

	function suspend() {
		$this->Sale_suspended->delete_all_shawpon();
		$data['cart'] = $this->sale_lib->get_cart();

		$data['subtotal'] = $this->sale_lib->get_subtotal();
		$data['taxes'] = $this->sale_lib->get_taxes();
		$data['total'] = $this->sale_lib->get_total();
		$data['receipt_title'] = $this->lang->line('sales_receipt');
		$data['transaction_time'] = date('m/d/Y h:i:s a');
		$customer_id = $this->sale_lib->get_customer();
		$employee_id = $this->Employee->get_logged_in_employee_info()->person_id;
		$comment = $this->input->post('comment');
		$emp_info = $this->Employee->get_info($employee_id);
		$payment_type = $this->input->post('payment_type');
		$data['payment_type'] = $this->input->post('payment_type');
		//Alain Multiple payments
		$data['payments'] = $this->sale_lib->get_payments();
		$data['amount_change'] = to_currency($this->sale_lib->get_amount_due() * -1);
		$data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;

		if ($customer_id != -1) {
			$cust_info = $this->Customer->get_info($customer_id);
			$data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
		}

		$total_payments = 0;

		foreach ($data['payments'] as $payment) {
			$total_payments += $payment['payment_amount'];
		}

		//SAVE sale to database
		$data['sale_id'] = 'POS ' . $this->Sale_suspended->save($data['cart'], $customer_id, $employee_id, $comment, $data['payments']);
		if ($data['sale_id'] == 'POS -1') {
			$data['error_message'] = $this->lang->line('sales_transaction_failed');
		}
		$this->sale_lib->clear_all();
		$this->_reload(array('success' => $this->lang->line('sales_successfully_suspended_sale')));
	}

	function suspended() {
		$data = array();
		$data['suspended_sales'] = $this->Sale_suspended->get_all()->result_array();
		$this->load->view('sales/suspended', $data);
	}

	function unsuspend() {
		$sale_id = $this->input->post('suspended_sale_id');
		$this->sale_lib->clear_all();
		$this->sale_lib->copy_entire_suspended_sale($sale_id);
		//$this->Sale_suspended->delete($sale_id);
		$this->_reload();
	}

	function globalsale() {

		$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';

		if(!$pageWasRefreshed){

			$item_id = $this->input->post('item_id');
			$location_name = $this->input->post('location_name');
			$description = $this->input->post('description');
			$loc_code = $this->input->post('loc_code');
			$lot_no = $this->input->post('lot_no');
			$price = $this->input->post('price');
			$this->session->set_userdata(['global_receive_location_code' => $loc_code]);
			$this->session->set_userdata(['global_receive_location' => $location_name]);
			$data = array();

			$mode = $this->sale_lib->get_mode();

			$item_id_or_number_or_item_kit_or_receipt = $sale_id;

			$quantity = $mode == "global" ? 1 : -1;

			//		if($this->sale_lib->is_valid_receipt($item_id_or_number_or_item_kit_or_receipt) && $mode=='return')
			//		{
			//
			//			$this->sale_lib->return_entire_sale($item_id_or_number_or_item_kit_or_receipt);
			//
			//		}
			//
			//		elseif($this->sale_lib->is_valid_item_kit($item_id_or_number_or_item_kit_or_receipt))
			//		{
			//
			//                	$this->sale_lib->add_item_kit($item_id_or_number_or_item_kit_or_receipt);
			//		}
			if (!$this->sale_lib->add_item_global($item_id, $quantity, $loc_code, $lot_no, $mode,0,$price,$description,null)) {

				$data['error'] = $this->lang->line('sales_unable_to_add_item');
			}
			//
			//		if($this->sale_lib->out_of_stock($item_id_or_number_or_item_kit_or_receipt))
			//		{
			//			$data['warning'] = $this->lang->line('sales_quantity_less_than_zero');
			//		}
		}


		$this->_reload($data);
	}

	function completeSele() {

		$this->sale_lib->clear_all();
		$this->_reload();
	}

	function delete_point() {
		$this->sale_lib->clear_user_points();
		$this->_reload();
	}

}

?>