<?php

class Sale_lib {

	var $CI;

	function __construct() {
		$this->CI = & get_instance();
	}

	function get_cart() {
		if (!$this->CI->session->userdata('cart'))
		$this->set_cart(array());

		return $this->CI->session->userdata('cart');
	}

	function get_lot_detail() {
		if (!$this->CI->session->userdata('lot_detail'))
		$this->set_lot_detail(array());

		return $this->CI->session->userdata('lot_detail');
	}

	function set_cart($cart_data) {
		$this->CI->session->set_userdata('cart', $cart_data);
	}

	function set_lot_detail($lot_detail) {
		$this->CI->session->set_userdata('lot_detail', $lot_detail);
	}

	function empty_lot_detail() {
		$this->CI->session->unset_userdata('lot_detail');
	}

	//Alain Multiple Payments
	function get_payments() {
		if (!$this->CI->session->userdata('payments'))
		$this->set_payments(array());

		return $this->CI->session->userdata('payments');
	}

	//Alain Multiple Payments
	function set_payments($payments_data) {
		$this->CI->session->set_userdata('payments', $payments_data);
	}

	function get_comment() {
		return $this->CI->session->userdata('comment');
	}

	function set_comment($comment) {
		$this->CI->session->set_userdata('comment', $comment);
	}

	function clear_comment() {
		$this->CI->session->unset_userdata('comment');
	}


	function get_bag_big() {
		return $this->CI->session->userdata('bag_big');
	}
	function set_bag_big($bag_big) {
		$this->CI->session->set_userdata('bag_big', $bag_big);
	}

	function clear_bag_big() {
		$this->CI->session->unset_userdata('bag_big');
	}


	function get_bag_medium() {
		return $this->CI->session->userdata('bag_medium');
	}
	function set_bag_medium($bag_medium) {
		$this->CI->session->set_userdata('bag_medium', $bag_medium);
	}

	function clear_bag_medium() {
		$this->CI->session->unset_userdata('bag_medium');
	}

	function get_bag_small() {
		return $this->CI->session->userdata('bag_small');
	}
	function set_bag_small($bag_small) {
		$this->CI->session->set_userdata('bag_small', $bag_small);
	}

	function clear_bag_small() {
		$this->CI->session->unset_userdata('bag_small');
	}





	function get_receive_location() {
		return $this->CI->session->userdata('receive_location');
	}

	function set_receive_location($receive_location) {
		$this->CI->session->set_userdata('receive_location', '');
		$this->CI->session->set_userdata('receive_location', $receive_location);
	}

	function clear_receive_location() {
		$this->CI->session->unset_userdata('receive_location');
	}

	function get_user_points() {
		return $this->CI->session->userdata('user_points');
	}

	function set_user_points($points) {
		$this->CI->session->set_userdata('user_points', $this->get_user_points() + $points);
	}

	function clear_user_points() {
		$this->CI->session->unset_userdata('user_points');
	}

	function get_email_receipt() {
		return $this->CI->session->userdata('email_receipt');
	}

	function set_email_receipt($email_receipt) {
		$this->CI->session->set_userdata('email_receipt', $email_receipt);
	}

	function clear_email_receipt() {
		$this->CI->session->unset_userdata('email_receipt');
	}

	function add_payment($payment_id, $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, $point = '') {


		$payments = $this->get_payments();
		$payment = array($payment_id =>
		array(
		'payment_type' => $payment_id,
		'payment_amount' => $amount_tendered,
		'check_name' => $check_name,
		'ac_no_check' => $ac_no_check,
		'holder_name_check' => $holder_name_check,
		'bank_name_check' => $bank_name_check,
		'branch_name_check' => $branch_name_check,
		'bank_name_debit' => $bank_name_debit,
		'card_no_debit' => $card_no_debit,
		'holder_name_debit' => $holder_name_debit,
		'holder_name_credit' => $holder_name_credit,
		'bank_name_credit' => $bank_name_credit,
		'check_date' => $check_date,
		'card_expire_year' => $card_expire_year,
		'card_expire_month' => $card_expire_month,
		'card_no_credit' => $card_no_credit,
		'user_point' => $point
		)
		);

		//payment_method already exists, add to payment_amount
		if (isset($payments[$payment_id])) {

			$payments[$payment_id]['payment_amount'] += $amount_tendered;
		} else {

			//add to existing array
			$payments += $payment;
		}

		$this->set_payments($payments);
		return true;
	}

	//Alain Multiple Payments
	function edit_payment($payment_id, $payment_amount) {
		$payments = $this->get_payments();
		if (isset($payments[$payment_id])) {
			$payments[$payment_id]['payment_type'] = $payment_id;
			$payments[$payment_id]['payment_amount'] = $payment_amount;
			$this->set_payments($payment_id);
		}

		return false;
	}

	//Alain Multiple Payments
	function delete_payment($payment_id) {
		$payments = $this->get_payments();
		unset($payments[urldecode($payment_id)]);
		$this->set_payments($payments);
	}

	//Alain Multiple Payments
	function empty_payments() {
		$this->CI->session->unset_userdata('payments');
	}

	//Alain Multiple Payments
	function get_payments_total() {
		$subtotal = 0;
		foreach ($this->get_payments() as $payments) {
			$subtotal+=$payments['payment_amount'];
		}
		return to_currency_no_money($subtotal);
	}

	//Alain Multiple Payments
	function get_amount_due() {
		$amount_due = 0;
		$payment_total = $this->get_payments_total();
		$sales_total = $this->get_total();
		$amount_due = to_currency_no_money($sales_total - $payment_total);
		return $amount_due;
	}

	function get_customer() {
		if (!$this->CI->session->userdata('customer'))
		$this->set_customer(-1);

		return $this->CI->session->userdata('customer');
	}

	function set_customer($customer_id) {
		$this->CI->session->set_userdata('customer', $customer_id);
	}

	function get_mode() {
		if (!$this->CI->session->userdata('sale_mode'))
		$this->set_mode('sale');

		return $this->CI->session->userdata('sale_mode');
	}

	function set_mode($mode) {
		$this->CI->session->set_userdata('sale_mode', $mode);
	}

	function add_item($item_id, $quantity = 1, $location = null, $lot_no = null, $mode = null, $discount = 0, $price = null, $description = null, $serialnumber = null,$reprint=0,$item_cost_price,$item_unit_price,$total_discount,$total_sale_amount,$vat_amount) {

		$location = !empty($location) ? $location : LOCATION_NAME;
		//make sure item exists
		if (!$this->CI->Item->exists($item_id)) {
			//try to get item id given an item_number
			$item_id = $this->CI->Item->get_item_id($item_id);

			if (!$item_id)
			return false;
		}


		//Alain Serialization and Description
		//Get all items in the cart so far...
		$items = $this->get_cart();

		//We need to loop through all items in the cart.
		//If the item is already there, get it's key($updatekey).
		//We also need to get the next key that we are going to use in case we need to add the
		//item to the cart. Since items can be deleted, we can't use a count. we use the highest key + 1.

		$maxkey = 0;                       //Highest key so far
		$itemalreadyinsale = FALSE;        //We did not find the item yet.
		$insertkey = 0;                    //Key to use for new entry.
		$updatekey = 0;                    //Key to use to update(quantity)

		foreach ($items as $item) {
			//We primed the loop so maxkey is 0 the first time.
			//Also, we have stored the key in the element itself so we can compare.

			if ($maxkey <= $item['line']) {
				$maxkey = $item['line'];
			}
			//check location. if item id same butlocation are different another item added

			if ($mode == "global") {
				if ($item['item_id'] == $item_id && $item['location'] == $location && $item['lot_no'] == $lot_no) {
					$itemalreadyinsale = TRUE;
					$updatekey = $item['line'];
				}
			} else {

				if ($item['item_id'] == $item_id && $item['lot_no'] == $lot_no) {
					$itemalreadyinsale = TRUE;
					$updatekey = $item['line'];
				}
			}
		}

		$insertkey = $maxkey + 1;

		if($reprint==1){
			$sale_data_info=$this->CI->Item->get_info_shawpon_reprint($item_id,$lot_no,$price);
		}else{
			$sale_data_info=$this->CI->Item->get_info_shawpon($item_id,$lot_no);
		}




		$price_shawpon=0;
		$vat_amount_shawpon=0;
		/*
		//15012017 :: START shawpon@gmail.com
		if(intval($sale_data_info->item_size_id)>0){
		$vat_amount_temp=(($sale_data_info->price-10)/100)*(0.99999*$sale_data_info->rate);
		$price_shawpon=round(($sale_data_info->price)-$vat_amount_temp);
		$vat_amount_shawpon=$sale_data_info->price-$price_shawpon;
		}
		*/

		if(intval($sale_data_info->item_size_id)>0){
			$vat_amount_temp=($sale_data_info->price*$sale_data_info->rate)/100;
			$price_shawpon=$sale_data_info->price;
			$vat_amount_shawpon=$vat_amount_temp;
		}else{
			$vat_amount_temp=($sale_data_info->price*$sale_data_info->rate)/100;
			$price_shawpon=$sale_data_info->price;
			$vat_amount_shawpon=$vat_amount_temp;
		}
		//15012017 :: END shawpon@gmail.com

		//array/cart records are identified by $insertkey and item_id is just another field.
	
		$item = array(($insertkey) =>
		array(
		'item_id' => $item_id,
		'location' => $location,
		'lot_no' => $lot_no,
		'line' => $insertkey,
		'name' => $sale_data_info->name,
		'item_number' => $sale_data_info->stock_id,
		'description' => $sale_data_info->name,
		'serialnumber' => $serialnumber != null ? $serialnumber : '',
		'allow_alt_description' => $sale_data_info->name,
		'is_serialized' => $this->CI->Item->get_info($item_id)->is_serialized,
		'quantity' => $quantity,
		'discount' => $discount,
		'price' => $price_shawpon,
		'price_with_vat' => $sale_data_info->price,
		'vat_amount' => $vat_amount_shawpon,
		'vat_rate' => $sale_data_info->rate,
		'stock_qty' => $sale_data_info->qty,
		'item_size_id' => $sale_data_info->item_size_id,
		'product_description' => $sale_data_info->product_description,
		'gender' => $sale_data_info->gender,
		'item_cost_price' => $item_cost_price,
		'item_unit_price' => $item_unit_price,
		'total_discount' => $total_discount,
		'total_sale_amount' => $total_sale_amount,
		'vat_amount' => $vat_amount
		)
		);


		$alllots = $this->get_lot_detail();

		if ($itemalreadyinsale && $this->check_item_lot($item_id, $lot_no) && ($this->CI->Item->get_info($item_id)->is_serialized == 0)) {
			$alllots[$item_id][$updatekey]['quantity'] = $items[$updatekey]['quantity'] + $quantity;
		} else {
			$newIndex = count($alllots) + 1;
			$alllots[$item_id][$newIndex]['lot_no'] = $lot_no;
			$alllots[$item_id][$newIndex]['location'] = $location;
			$alllots[$item_id][$newIndex]['quantity'] = $quantity;
			$alllots[$item_id][$newIndex]['price'] = $price != null ? $price : $this->CI->Item->get_info($item_id)->unit_price;
			$alllots[$item_id][$newIndex]['discount'] = $discount;
		}
		$this->set_lot_detail($alllots);

		
		$price_list=$this->CI->Item->get_info_price_list_shawpon($item_id,$lot_no,LOCATION_CODE);
		
		if(count($price_list)==1){
			//Item already exists and is not serialized, add to quantity
			if ($itemalreadyinsale && ($this->CI->Item->get_info($item_id)->is_serialized == 0)) {
				$items[$updatekey]['quantity']+=$quantity;
			} else {
				//add to existing array
				$items+=$item;
			}
		}else{
			$items+=$item;
		}

		$this->set_cart($items);



		return true;
	}

	function add_item_global($item_id, $quantity = 1, $location = null, $lot_no = null, $mode = null, $discount = 0, $price = null, $description = null, $serialnumber = null) {
		$items = $this->get_cart();

		$maxkey = 0;                       //Highest key so far
		$itemalreadyinsale = FALSE;        //We did not find the item yet.
		$insertkey = 0;                    //Key to use for new entry.
		$updatekey = 0;                    //Key to use to update(quantity)

		foreach ($items as $item) {
			//We primed the loop so maxkey is 0 the first time.
			//Also, we have stored the key in the element itself so we can compare.

			if ($maxkey <= $item['line']) {
				$maxkey = $item['line'];
			}
			//check location. if item id same butlocation are different another item added

			if ($item['item_id'] == $_POST['item_id'] && $item['location'] == $_POST['loc_code'] && $item['lot_no'] == $_POST['lot_no']) {
				$itemalreadyinsale = TRUE;
				$updatekey = $item['line'];
			}
		}

		$insertkey = $maxkey + 1;

		$price_shawpon=0;
		$vat_amount_shawpon=0;
		/*
		//15012017 :: START shawpon@gmail.com
		if(intval($_POST['item_size_id'])>0){
		$vat_amount_temp=(($_POST['price']-10)/100)*(0.99999*$_POST['vat_rate']);
		$price_shawpon=round(($_POST['price'])-$vat_amount_temp);
		$vat_amount_shawpon=$_POST['price']-$price_shawpon;
		}
		*/

		if(intval($_POST['item_size_id'])>0){
			$vat_amount_temp=($_POST['price']*$_POST['vat_rate'])/100;
			$price_shawpon=$_POST['price'];
			$vat_amount_shawpon=$vat_amount_temp;
		}

		//15012017 :: END shawpon@gmail.com

		//array/cart records are identified by $insertkey and item_id is just another field.
		$item = array(($insertkey) =>
		array(
		'item_id' => $_POST['item_id'],
		'location' => $_POST['loc_code'],
		'lot_no' => $_POST['lot_no'],
		'line' => $insertkey,
		'name' => $_POST['description'],
		'item_number' => $_POST['item_id'],
		'description' => $_POST['description'],
		'serialnumber' => $serialnumber != null ? $serialnumber : '',
		'allow_alt_description' => $_POST['description'],
		'is_serialized' => $this->CI->Item->get_info($item_id)->is_serialized,
		'quantity' => $quantity,
		'discount' => $_POST['discount'],
		'price' => $price_shawpon,
		'price_with_vat' => $_POST['price'],
		'vat_amount' => $vat_amount_shawpon,
		'vat_rate' => $_POST['vat_rate'],
		'stock_qty' => $_POST['stock_qty'],
		'item_size_id' => $_POST['item_size_id'],
		)
		);

		$alllots = $this->get_lot_detail();

		if ($itemalreadyinsale && $this->check_item_lot($_POST['item_id'],  $_POST['lot_no'])) {
			$alllots[$_POST['item_id']][$updatekey]['quantity'] = $items[$updatekey]['quantity'] + $quantity;
		} else {
			$newIndex = count($alllots) + 1;
			$alllots[$item_id][$newIndex]['lot_no'] = $_POST['lot_no'];
			$alllots[$item_id][$newIndex]['location'] = $_POST['loc_code'];
			$alllots[$item_id][$newIndex]['quantity'] = $quantity;
			$alllots[$item_id][$newIndex]['price'] = $_POST['price'];
			$alllots[$item_id][$newIndex]['discount'] = $_POST['discount'];
		}
		$this->set_lot_detail($alllots);

		//Item already exists and is not serialized, add to quantity
		if ($itemalreadyinsale) {
			$items[$updatekey]['quantity']+=$quantity;
		} else {
			//add to existing array
			$items+=$item;
		}

		$this->set_cart($items);

		return true;
	}

	function out_of_stock($item_id,$lot_no) {
		//make sure item exists

		if (!$this->CI->Item->exists($item_id)) {
			//try to get item id given an item_number
			$item_id = $this->CI->Item->get_item_id($item_id);

			if (!$item_id)
			return false;
		}

		//$item = $this->CI->Item->get_info($item_id);
		$item_qty = $this->CI->Item->get_stock_qty_info($item_id,$lot_no);

		$quanity_added = $this->get_quantity_already_added($item_id);

		if ($item_qty - $quanity_added < 0) {
			return true;
		}

		return false;
	}

	function get_quantity_already_added($item_id) {
		$items = $this->get_cart();
		$quanity_already_added = 0;
		foreach ($items as $item) {
			if ($item['item_id'] == $item_id) {
				$quanity_already_added+=$item['quantity'];
			}
		}

		return $quanity_already_added;
	}

	function get_item_id($line_to_get) {
		$items = $this->get_cart();

		foreach ($items as $line => $item) {
			if ($line == $line_to_get) {
				return $item['item_id'];
			}
		}

		return -1;
	}
	function get_lot_no($line_to_get) {
		$items = $this->get_cart();

		foreach ($items as $line => $item) {
			if ($line == $line_to_get) {
				return $item['lot_no'];
			}
		}

		return -1;
	}

	function edit_item($line, $description, $serialnumber, $quantity, $discount, $price) {
		$items = $this->get_cart();
		if (isset($items[$line])) {
			$items[$line]['description'] = $description;
			$items[$line]['serialnumber'] = $serialnumber;
			$items[$line]['quantity'] = $quantity;
			$items[$line]['discount'] = $discount;
			$items[$line]['price'] = $price;
			$this->set_cart($items);
			$alllots = $this->get_lot_detail();
			$alllots[$items[$line]['item_id']][$line]['quantity'] = $quantity;
			$alllots[$items[$line]['item_id']][$line]['discount'] = $discount;

			$this->set_lot_detail($alllots);
		}


		return false;
	}

	//    function is_valid_receipt($receipt_sale_id) {
	//        //POS #
	//        $pieces = explode(' ', $receipt_sale_id);
	//
	//        if (count($pieces) == 2) {
	//            return $this->CI->Sale->exists($pieces[1]);
	//        }
	//
	//        return false;
	//    }

	function is_valid_receipt(&$receipt_sale_id) {
		//POS #
		$pieces = explode(' ', $receipt_sale_id);

		if (count($pieces) == 2 && strtolower($pieces[0]) == 'pos') {
			return $this->CI->Sale->exists($pieces[1]);
		} else {
			$sale_info = $this->CI->Sale->get_sale_by_invoice_number($receipt_sale_id);
			if ($sale_info->num_rows() > 0) {
				$receipt_sale_id = 'POS ' . $sale_info->row()->sale_id;
				return true;
			}
		}
		return false;
	}


	function is_valid_receipt_shawpon(&$receipt_sale_id) {
		//POS #
		$pieces = explode(' ', $receipt_sale_id);

		if (count($pieces) == 2 && strtolower($pieces[0]) == 'pos') {
			return $this->CI->Sale->exists($pieces[1]);
		} else {
			$sale_info = $this->CI->Sale->get_sale_by_invoice_number_shawpon_local($receipt_sale_id);
			if ($sale_info->num_rows() > 0) {
				$receipt_sale_id = $receipt_sale_id;
				return true;
			}else{
				$sale_info = $this->CI->Sale->get_sale_by_invoice_number_shawpon_server($receipt_sale_id);
				if ($sale_info->num_rows() > 0) {
					$receipt_sale_id = $receipt_sale_id;
					return true;
				}
			}
		}
		return false;
	}

	function is_valid_item_kit($item_kit_id) {
		//KIT #

		$pieces = explode(' ', $item_kit_id);

		if (count($pieces) == 2) {

			return $this->CI->Item_kit->exists($pieces[1]);
		}

		return false;
	}

	function return_entire_sale($receipt_sale_id) {

		//POS #
		$pieces = explode(' ', $receipt_sale_id);
		$sale_id = $pieces[1];

		$this->empty_cart();
		$this->remove_customer();

		foreach ($this->CI->Sale->get_sale_items($sale_id)->result() as $row) {
			if($row->quantity_purchased > 0){
				$this->CI->db->set_dbprefix('');
				$stockMove = $this->CI->Sale->get_value('loc_code, lot_no', 'stock_moves', ['trans_no' => $sale_id]);

				$this->CI->db->set_dbprefix('ospos_');
				$this->add_item($row->item_id, -$row->quantity_purchased, $stockMove->loc_code, $stockMove->lot_no, 'return', $row->discount_percent, $row->item_unit_price, $row->description, $row->serialnumber);
			}
		}
		$this->set_customer($this->CI->Sale->get_customer($sale_id)->person_id);
	}
	function return_entire_sale_shawpon($receipt_sale_id) {

		/*$pieces = explode(' ', $receipt_sale_id);

		if (count($pieces) == 2 && strtolower($pieces[0]) == 'pos') {
		return $this->CI->Sale->exists($pieces[1]);
		} else {
		$sale_info = $this->CI->Sale->get_sale_by_invoice_number_shawpon_local($receipt_sale_id);
		if ($sale_info->num_rows() > 0) {
		$receipt_sale_id = $receipt_sale_id;
		return true;
		}else{
		$sale_info = $this->CI->Sale->get_sale_by_invoice_number_shawpon_server($receipt_sale_id);
		if ($sale_info->num_rows() > 0) {
		$receipt_sale_id = $receipt_sale_id;
		return true;
		}
		}
		}*/

		$sale_id = $receipt_sale_id;

		$this->empty_cart();
		$this->remove_customer();


		$sale_info = $this->CI->Sale->get_sale_by_invoice_number_shawpon_local($receipt_sale_id);

		if ($sale_info->num_rows() > 0) {
			foreach ($this->CI->Sale->get_sale_items_by_receive_id_local($sale_info->row()->sale_id)->result() as $row) {
				if($row->quantity_purchased > 0){
					$this->add_item($row->stock_id, -$row->quantity_purchased, $sale_info->row()->location, $row->lot_no, 'return', $row->discount_percent, $row->item_cost_price, $row->description,null);
				}
			}
			$this->set_customer($this->CI->Sale->get_customer_local($sale_info->row()->sale_id)->id);
		}else{
			$sale_info = $this->CI->Sale->get_sale_by_invoice_number_shawpon_server($receipt_sale_id);

			if ($sale_info->num_rows() > 0) {
				foreach ($this->CI->Sale->get_sale_items_by_receive_id_live($sale_info->row()->sale_id)->result() as $row) {
					if($row->quantity_purchased > 0){
						$this->add_item($row->stock_id, -$row->quantity_purchased, $sale_info->row()->location, $row->lot_no, 'return', $row->discount_percent, $row->item_cost_price, $row->description,null);
					}
				}

				$this->set_customer($this->CI->Sale->get_customer_server($sale_info->row()->sale_id)->id);
			}
		}
	}

	function add_item_kit($external_item_kit_id) {

		//KIT #
		$pieces = explode(' ', $external_item_kit_id);
		$item_kit_id = $pieces[1];

		foreach ($this->CI->Item_kit_items->get_info($item_kit_id) as $item_kit_item) {
			$this->add_item($item_kit_item['item_id'], $item_kit_item['quantity']);
		}
	}

	function copy_entire_sale($sale_id) {
		$this->empty_cart();
		$this->remove_customer();

		foreach ($this->CI->Sale->get_sale_items($sale_id)->result() as $row) {
			$this->add_item($row->item_id, $row->quantity_purchased, $row->discount_percent, $row->item_unit_price, $row->description, $row->serialnumber);
		}
		foreach ($this->CI->Sale->get_sale_payments($sale_id)->result() as $row) {
			$this->add_payment($row->payment_type, $row->payment_amount);
		}
		$this->set_customer($this->CI->Sale->get_customer($sale_id)->person_id);
	}
	function copy_entire_sale_shawpon($sale_id,$reprint=0) {
		$this->empty_cart();
		$this->remove_customer();


		foreach ($this->CI->Sale->get_sale_items($sale_id)->result() as $row) {

			$this->add_item($row->stock_id, $row->quantity_purchased,LOCATION_CODE,$row->lot_no, null,$row->discount_percent, $row->item_cost_price, $row->description, null,$reprint,$row->item_cost_price,$row->item_unit_price,$row->total_discount,$row->total_sale_amount,$row->vat_amount);
		}
		
		foreach ($this->CI->Sale->get_sale_payments($sale_id)->result() as $row) {
			$this->add_payment($row->payment_type, $row->payment_amount);
		}
		$this->set_customer($this->CI->Sale->get_customer($sale_id)->person_id);
	}

	
	function copy_entire_suspended_sale($sale_id) {
		$this->empty_cart();
		$this->remove_customer();

		foreach ($this->CI->Sale_suspended->get_sale_items($sale_id)->result() as $row) {

			$this->add_item($row->stock_id, $row->quantity_purchased, LOCATION_CODE,$row->lot_no,null, $row->discount_percent,$row->item_cost_price, $row->description, null);
		}

		foreach ($this->CI->Sale_suspended->get_sale_payments($sale_id)->result() as $row) {
			$this->add_payment($row->payment_type, $row->payment_amount);
		}
		$this->set_customer($this->CI->Sale_suspended->get_customer($sale_id)->id);

		$this->set_comment($this->CI->Sale_suspended->get_comment($sale_id));

		$this->set_bag_big($this->CI->Sale_suspended->get_bag_big($sale_id));

		$this->set_bag_medium($this->CI->Sale_suspended->get_bag_medium($sale_id));
		$this->set_bag_small($this->CI->Sale_suspended->get_bag_small($sale_id));

	}

	function delete_item($line) {
		$items = $this->get_cart();
		$item_lots = $this->get_lot_detail();

		unset($item_lots[$items[$line]['item_id']]);
		unset($items[$line]);

		$this->set_cart($items);
		$this->set_lot_detail($item_lots);
	}

	function empty_cart() {
		$this->CI->session->unset_userdata('cart');
	}

	function remove_customer() {
		$this->CI->session->unset_userdata('customer');
	}

	function clear_mode() {
		$this->CI->session->unset_userdata('sale_mode');
	}

	function clear_all() {
		//$this->clear_mode();
		$this->empty_cart();
		$this->clear_comment();
		$this->clear_bag_big();
		$this->clear_bag_medium();
		$this->clear_bag_small();
		$this->clear_receive_location();
		$this->clear_user_points();
		$this->clear_email_receipt();
		$this->empty_payments();
		$this->remove_customer();

		$this->empty_lot_detail();
	}

	function get_taxes() {
		$taxes = array();
		foreach ($this->get_cart() as $line => $item) {
			if($item['discount']>0){
				$price_per_unit=($item['price']*$item['quantity'])-(($item['price']*$item['quantity'])*$item['discount']/100);
				$name ="VAT (".$item['vat_rate']."%)";
				$tax_amount = ($price_per_unit * $item['vat_rate'])/100;

			}else{
				$name ="VAT (".$item['vat_rate']."%)";
				//$tax_amount = ($item['vat_amount'] * $item['quantity']);
				$tax_amount = number_format(((($item['price']*$item['vat_rate'])/100) * $item['quantity']), 2, '.', '');
			}
			$taxes[$name] += $tax_amount;
		}

		return $taxes;
	}

	function get_subtotal() {
		$subtotal = 0;
		foreach ($this->get_cart() as $item) {
			$subtotal+=($item['price'] * $item['quantity'] - $item['price'] * $item['quantity'] * $item['discount'] / 100);
		}
		return to_currency_no_money($subtotal);
	}

	function get_total() {
		$total = 0;
		foreach ($this->get_cart() as $item) {
			$total += ( $item['price'] * $item['quantity'] - $item['price'] * $item['quantity'] * $item['discount'] / 100);
		}
		foreach ($this->get_taxes() as $tax) {
			$total+=$tax;
		}

		$cart_point = $this->get_user_points();
		if (!empty($cart_point)) {
			$pointCost = $this->CI->Sale->getPointCost();
			$cart_point_cost = $pointCost->value * $cart_point;

			$total = $total - $cart_point_cost;
		}

		return to_currency_no_money(round($total));
	}

	function check_item_lot($item_id, $lot_no) {
		$cart = $this->get_lot_detail();
		if ($cart) {
			foreach (array_reverse($cart, true) as $item => $iteminfo) {
				if ($item == $item_id) {
					foreach ($iteminfo as $info) {
						if ($info['lot_no'] == $lot_no) {
							return TRUE;
						} else {
							return FALSE;
						}
					}
				} else {
					return FALSE;
				}
			}
		}
	}


	function get_sale_payments_shawpon($sale_id)
	{
		return  $this->CI->Sale->get_sale_payments_data_shawpon($sale_id);
	}


}

?>