load->model('Admin_model'); $this->load->model('Inventory_model'); $this->load->helper('url'); $this->load->library('email'); $this->load->helper('file'); $this->load->helper('common_helper'); $this->load->library('Ciqrcode'); date_default_timezone_set('Asia/Kathmandu'); $this->nepaliDateObject = new NepaliDate(); $this->nepali_current_date = cuurentNepaliDate($this->nepaliDateObject); } public function dashboard() { if (isset($_SESSION['admin_id'])) { $data['currentURL'] = current_url(); //get logo $data['logo'] = $this->Admin_model->get_logo(); $data['school_info'] = $this->Admin_model->get_logo_from_setting(); //get menu list $data['subject_count'] = $this->db->count_all('subject'); $data['course_count'] = $this->db->count_all('course'); $data['classroom_count'] = $this->db->count_all('classroom'); $data['teacher_count'] = $this->db->count_all('teacher'); $data['student_count'] = $this->db->count_all('students'); $data['exam_count'] = $this->db->count_all('exam'); $data['session_count'] = $this->db->count_all('classroom_session'); $data['application_count'] = $this->db->count_all('applications'); $data['notification_count'] = $this->db->count_all('notifications'); // $data['current_report_data'] = $fee_type_data; $course_query = "SELECT * FROM course WHERE is_active='yes'"; $data['courses'] = $this->Admin_model->get_query_result($course_query); // report end by shivakumar $data['menulist'] = $this->Admin_model->get_adminMenu(); // dd($data['menulist']); $this->load->view('inventory/header', $data); $this->load->view('inventory/dashboard'); $this->load->view('inventory/footer'); } } //subject start public function item_unit() { if (!isset($_SESSION['admin_id'])) redirect(base_url() . 'admin'); $data['currentURL'] = current_url(); $data['title'] = 'Item Measurement Units'; $data['logo'] = $this->Admin_model->get_logo(); $data['school_info'] = $this->Admin_model->get_logo_from_setting(); $admin_info = $this->db->query("SELECT * FROM admin WHERE id = " . $_SESSION['admin_id'])->row_array(); // dd($admin_info); // $hr_db = $this->load->database('hr', TRUE); if (isset($_POST['addItemUnit']) && $_POST['addItemUnit'] == 'Add') { $title = $_POST['title']; $short_label = $_POST['title']; $isUnique = $this->Inventory_model->get_unique_name('title', $title, 'inventory_measurements'); if ($isUnique) { $td['title'] = $title; $td['short_label'] = $short_label; $td['is_active'] = 'yes'; $td['created_on'] = $this->nepali_current_date; $td['modified_on'] = $this->nepali_current_date; $td['created_by'] = $admin_info['admin_name']; $td['modified_by'] = $admin_info['admin_name']; $this->db->insert('inventory_measurements', $td); $this->session->set_flashdata('success', str_replace('%s', 'Item Unit', 'New item unit has been added.')); } else { $this->session->set_flashdata('danger', str_replace('%s', 'Item Unit', 'Item unit title already exist.')); } redirect(base_url() . 'admin/inventory/item-unit'); } else if (isset($_POST['updateItemUnit']) && $_POST['updateItemUnit'] == 'Update') { $title = $_POST['title']; $short_label = $_POST['short_label']; $recId = $_POST['unitId']; $td = array( 'title' => $title, 'short_label' => $short_label, 'is_active' => $_POST['is_active'], 'modified_on' => $this->nepali_current_date, 'modified_by' => $admin_info['admin_name'], ); $this->db->where('id', $recId); $update = $this->db->update("inventory_measurements", $td); if ($update) { $this->session->set_flashdata('success', str_replace('%s', 'Item Unit', 'Record has been updated successfully.')); } else { $this->session->set_flashdata('danger', str_replace('%s', 'Item Unit', 'THE_ADD_ERROR_MSG')); } redirect(base_url() . 'admin/inventory/item-unit'); } $query = $this->db->query('select * from inventory_measurements'); $data['inventory_measurements'] = $query->result_array(); $this->load->view('inventory/header', $data); $this->load->view('inventory/item_units', $data); $this->load->view('inventory/footer'); } public function item_category() { if (!isset($_SESSION['admin_id'])) redirect(base_url() . 'admin'); $data['currentURL'] = current_url(); $data['title'] = 'Item Category'; $data['logo'] = $this->Admin_model->get_logo(); $data['school_info'] = $this->Admin_model->get_logo_from_setting(); $admin_info = $this->db->query("SELECT * FROM admin WHERE id = " . $_SESSION['admin_id'])->row_array(); if (isset($_POST['addCategory']) && $_POST['addCategory'] == 'Add') { $title = $_POST['title']; $classBased = $_POST['is_class_based']; $isUnique = $this->Inventory_model->get_unique_name('title', $title, 'inventory_item_category'); if ($isUnique) { $td['title'] = $title; $td['class_based'] = $classBased; $td['is_active'] = 'yes'; $td['created_on'] = $this->nepali_current_date; $td['modified_on'] = $this->nepali_current_date; $td['created_by'] = $admin_info['admin_name']; $td['modified_by'] = $admin_info['admin_name']; $this->db->insert('inventory_item_category', $td); $this->session->set_flashdata('success', str_replace('%s', 'Category', 'Item category has been added.')); } else { $this->session->set_flashdata('danger', str_replace('%s', 'Category', 'Item category already exist.')); } redirect(base_url() . 'admin/inventory/item-categories'); } else if (isset($_POST['updateCategory']) && $_POST['updateCategory'] == 'Update') { $title = $_POST['title']; $recId = $_POST['recId']; $td = array( 'title' => $title, 'class_based' => $_POST['is_class_based'], 'is_active' => $_POST['is_active'], 'modified_on' => $this->nepali_current_date, 'modified_by' => $admin_info['admin_name'], ); $this->db->where('id', $recId); $update = $this->db->update("inventory_item_category", $td); if ($update) { $this->session->set_flashdata('success', str_replace('%s', 'Category', 'Record has been updated successfully.')); } else { $this->session->set_flashdata('danger', str_replace('%s', 'Category', 'THE_ADD_ERROR_MSG')); } redirect(base_url() . 'admin/inventory/item-categories'); } $query = $this->db->query('select * from inventory_item_category'); $data['inventory_categories'] = $query->result_array(); $this->load->view('inventory/header', $data); $this->load->view('inventory/item-category', $data); $this->load->view('inventory/footer'); } public function item_list() { if (!isset($_SESSION['admin_id'])) redirect(base_url() . 'admin'); $data['currentURL'] = current_url(); $data['title'] = 'Item Lists'; $data['logo'] = $this->Admin_model->get_logo(); $data['school_info'] = $this->Admin_model->get_logo_from_setting(); $admin_info = $this->db->query("SELECT * FROM admin WHERE id = " . $_SESSION['admin_id'])->row_array(); if (isset($_POST['addItem']) && $_POST['addItem'] == 'Add') { $config['upload_path'] = 'assets_inventory/images/items/'; $config['allowed_types'] = 'gif|jpg|png|jpeg'; $this->load->library('upload', $config); $this->upload->initialize($config); $title = $_POST['title']; $photo = array('upload_data' => $this->upload->data()); $photoSize = $_FILES['photo']['size']; $update_array = array(); if ($photoSize > 0) { if (!$this->upload->do_upload('photo')) { $item_photo = "No-Image.jpg"; } else { $photo = array('upload_data' => $this->upload->data()); $item_photo = $photo['upload_data']['file_name']; } } else { $item_photo = "No-Image.jpg"; } $courses = isset($_POST['course_id']) && !empty($_POST['course_id']) ? $_POST['course_id'] : []; // dd($_POST['course_id']); $insert_array = array(); foreach ($courses as $c_key => $c_value) { $td['title'] = $title; $td['course_id'] = $c_value; $td['category_id'] = $_POST['item_category']; $td['measurement_id'] = $_POST['item_measurement_unit']; $td['description'] = $_POST['description']; $td['photo'] = $item_photo; $td['created_on'] = $this->nepali_current_date; $td['modified_on'] = $this->nepali_current_date; $td['created_by'] = $admin_info['admin_name']; $td['modified_by'] = $admin_info['admin_name']; // If the item category is book. if ($_POST['item_category'] == 1) { $td['book_publication'] = $_POST['publication']; $td['book_edition'] = $_POST['edition']; $td['book_isbn'] = $_POST['isbn']; } } if ($_POST['item_category'] == 3) { $td['title'] = $title; $td['category_id'] = $_POST['item_category']; $td['measurement_id'] = $_POST['item_measurement_unit']; $td['description'] = $_POST['description']; $td['photo'] = $item_photo; $td['created_on'] = $this->nepali_current_date; $td['modified_on'] = $this->nepali_current_date; $td['created_by'] = $admin_info['admin_name']; $td['modified_by'] = $admin_info['admin_name']; if ($_POST['item_category'] == 3) { // dd($_POST); $td['item_variant'] = implode(',', $_POST['variant']); } echo '
';[]
				print_r($td);
			}

			$insert = $this->db->insert('inventory_items', $td);

			if ($insert) {
				array_push($insert_array, true);
			} else {
				array_push($insert_array, false);
			}

			// dd($td);

			if (in_array(true, $insert_array)) {
				$this->session->set_flashdata('success', str_replace('%s', 'Item', 'Item has been added.'));
				redirect(base_url() . 'admin/inventory/items');
			} else {
				$this->session->set_flashdata('danger', str_replace('%s', 'Item', 'Something went wrong. Please try again later.'));
				redirect(base_url() . 'admin/inventory/items');
			}
		} else if (isset($_POST['updateItem']) && $_POST['updateItem'] == 'Update') {
			$title = $_POST['title'];
			$recId = $_POST['recId'];
			// var_dump($_FILES);

			$photoSize = $_FILES['photo']['size'];
			// echo $photoSize;
			$update_array = array();

			if ($photoSize > 0) {
				unlink('assets_inventory/images/items/' . $_POST['photo_name']);
				$config['upload_path'] = 'assets_inventory/images/items/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg';
				$this->load->library('upload', $config);
				$this->upload->initialize($config);
				$photo = array('upload_data' => $this->upload->data());

				if (!$this->upload->do_upload('photo')) {
					$item_photo = "No-Image.jpg";
				} else {
					$photo = array('upload_data' => $this->upload->data());
					$item_photo = $photo['upload_data']['file_name'];
				}
			} else {
				$item_photo = $_POST['photo_name'];
			}

			$courses = isset($_POST['course_id']) && !empty($_POST['course_id']) ? $_POST['course_id'] : [];

			foreach ($courses as $c_key => $c_value) {
				$td['title'] = $title;
				$td['course_id'] = $c_value;
				$td['category_id'] = $_POST['item_category'];
				$td['measurement_id'] = $_POST['item_measurement_unit'];
				$td['description'] = $_POST['description'];
				$td['photo'] = $item_photo;
				$td['modified_on'] = $this->nepali_current_date;
				$td['modified_by'] = $admin_info['admin_name'];

				$this->db->where('id', $recId);
				$update = $this->db->update('inventory_items', $td);
				if ($update) {
					array_push($update_array, true);
				} else {
					array_push($update_array, false);
				}
			}

			if (in_array(true, $update_array)) {
				$this->session->set_flashdata('success', str_replace('%s', 'Item', 'Item has been updated successfully.'));
				redirect(base_url() . 'admin/inventory/items');
			} else {
				$this->session->set_flashdata('danger', str_replace('%s', 'Item', 'Something went wrong. Please try again later.'));
				redirect(base_url() . 'admin/inventory/items');
			}
		}

		$queryCategory = $this->db->query('select * from inventory_item_category WHERE is_active="yes"');
		$data['inventory_categories'] = $queryCategory->result_array();

		$queryItemUnit = $this->db->query('select * from inventory_measurements WHERE is_active="yes"');
		$data['inventory_measurements'] = $queryItemUnit->result_array();

		$queryCourse = $this->db->query('select * from course WHERE is_active="yes"');
		$data['courses'] = $queryCourse->result_array();

		$queryPublications = $this->db->query('select * from inventory_publications WHERE is_active="yes"');
		$data['book_publications'] = $queryPublications->result_array();

		$query = $this->db->query("SELECT i.*, IF(i.course_id is null, '-', c.course_name) as course_name from inventory_items i LEFT JOIN course c ON i.course_id = c.id");
		$data['inventory_items'] = $query->result_array();

		$queryVariants = $this->db->query('select * from inventory_variants WHERE is_active="yes"');
		$data['inventory_variants'] = $queryVariants->result_array();

		$this->load->view('inventory/header', $data);
		$this->load->view('inventory/item-list', $data);
		$this->load->view('inventory/footer');
	}

	public function item_purchase()
	{
		if (!isset($_SESSION['admin_id']))
			redirect(base_url() . 'admin');
		$data['currentURL'] = current_url();
		$data['title'] = 'Purchase List';
		$data['logo'] = $this->Admin_model->get_logo();
		$data['school_info'] = $this->Admin_model->get_logo_from_setting();

		$admin_info = $this->db->query("SELECT * FROM admin WHERE id = " . $_SESSION['admin_id'])->row_array();

		if (isset($_POST['deletePurchaseRecord']) && $_POST['deletePurchaseRecord'] == 'Confirm') {

			// dd($_POST);

			$purchase_record = $this->db->query("SELECT * FROM inventory_item_purchase WHERE id = " . $_POST['recId'])->row_array();
			$item_stock = $this->db->query("SELECT * FROM inventory_stocks WHERE item_id = " . $purchase_record['item_id'])->row_array();

			$ud['total_stock'] = $item_stock['total_stock'] - $purchase_record['qty'];
			$ud['current_stock'] = $item_stock['current_stock'] - $purchase_record['qty'];
			$ud['modified_on'] = $this->nepali_current_date;
			$ud['modified_by'] = $admin_info['admin_name'];
			$this->db->where('id', $item_stock['id']);
			$this->db->update('inventory_stocks', $ud);


			$this->db->where('id', $_POST['recId']);
			$delete = $this->db->delete('inventory_item_purchase');

			if ($delete) {
				$this->session->set_flashdata('success', str_replace('%s', 'Purchase Record', 'The purchase record has been deleted successfully.'));
				redirect(base_url() . 'admin/inventory/item-purchase');
			} else {
				$this->session->set_flashdata('danger', str_replace('%s', 'Purchase Record', 'Something went wrong. Please try again later.'));
				redirect(base_url() . 'admin/inventory/item-purchase');
			}
		}

		$query = $this->db->query('select * from inventory_items');
		$data['inventory_items'] = $query->result_array();

		$purchaseQuery = $this->db->query('select ip.*, i.title from inventory_item_purchase ip LEFT JOIN inventory_items i ON i.id=ip.item_id');
		$data['purchase_list'] = $purchaseQuery->result_array();

		$this->load->view('inventory/header', $data);
		$this->load->view('inventory/item-purchase', $data);
		$this->load->view('inventory/footer');
	}

	public function add_edit_new_entry($id)
	{
		$data['currentURL'] = current_url();

		$data['logo'] = $this->Admin_model->get_logo();
		$data['school_info'] = $this->Admin_model->get_logo_from_setting();

		$admin_info = $this->db->query("SELECT * FROM admin WHERE id = " . $_SESSION['admin_id'])->row_array();

		$query = $this->db->query('select * from inventory_items');
		$data['inventory_items'] = $query->result_array();
		$query = $this->db->query('select * from inventory_suppliers');
		$data['inventory_suppliers'] = $query->result_array();

		$today_date_time = explode(' ', $this->nepali_current_date);

		$data['today_date'] = $today_date_time[0];

		if ($id == 0) {
			$data['title'] = 'Add New Entry';
		} else {
			$purchaseQuery = $this->db->query('select ip.*, i.title from inventory_item_purchase ip LEFT JOIN inventory_items i ON i.id=ip.item_id WHERE ip.id =' . $id);


			$data['purchase_item'] = $purchaseQuery->row_array();
			$data['title'] = $data['purchase_item']['title'];
		}

		if ($_POST > 0) {

			if (isset($_POST['addPurchase']) && $_POST['addPurchase'] == 'Add') {

				$config['upload_path'] = 'assets_inventory/images/purchase/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg';
				$this->load->library('upload', $config);
				$this->upload->initialize($config);

				$photo = array('upload_data' => $this->upload->data());
				$photoSize = $_FILES['photo']['size'];
				echo $photoSize;

				if ($photoSize > 0) {
					if (!$this->upload->do_upload('photo')) {
						$item_photo = "No-Image.jpg";
					} else {
						$photo = array('upload_data' => $this->upload->data());

						$item_photo = $photo['upload_data']['file_name'];
					}
				} else {
					$item_photo = "No-Image.jpg";
				}

				// Start of Inserting in stocks table for tracking stock of items.

				$item_stock = $this->db->query("SELECT * FROM inventory_stocks WHERE item_id = " . $_POST['item'])->row_array();

				if (empty($item_stock)) {
					$sid['item_id'] = $_POST['item'];
					$sid['total_stock'] = $_POST['qty'];
					$sid['current_stock'] = $_POST['qty'];
					$sid['sold_stock'] = 0;
					$sid['created_on'] = $this->nepali_current_date;
					$sid['modified_on'] = $this->nepali_current_date;
					$sid['created_by'] = $admin_info['admin_name'];
					$sid['modified_by'] = $admin_info['admin_name'];
					$this->db->insert('inventory_stocks', $sid);
				} else {
					$sid['total_stock'] = $item_stock['total_stock'] + $_POST['qty'];
					$sid['current_stock'] = $item_stock['current_stock'] + $_POST['qty'];
					$sid['modified_on'] = $this->nepali_current_date;
					$sid['modified_by'] = $admin_info['admin_name'];
					$this->db->where('id', $item_stock['id']);
					$this->db->update('inventory_stocks', $sid);
				}
				// End of Inserting in stocks table for tracking stock of items.


				$td['item_id'] = $_POST['item'];
				$td['purchased_from'] = $_POST['supplier'];
				$td['purchased_on'] = $_POST['purchased_on'];
				$td['qty'] = $_POST['qty'];
				$td['remaining_qty'] = $_POST['qty'];
				$td['unit_price'] = $_POST['unit_price'];
				$td['total_price'] = $_POST['total_price'];
				$td['selling_price'] = $_POST['selling_price'];
				$td['paid_amount'] = $_POST['paid_amount'];
				$td['remaining_amount'] = $_POST['remaining_amount'];
				$td['remarks'] = $_POST['remarks'];
				$td['photo'] = $item_photo;
				$td['created_on'] = $this->nepali_current_date;
				$td['modified_on'] = $this->nepali_current_date;
				$td['created_by'] = $admin_info['admin_name'];
				$td['modified_by'] = $admin_info['admin_name'];
				// dd($_POST);
				$insert = $this->db->insert('inventory_item_purchase', $td);

				if ($insert) {
					$this->session->set_flashdata('success', str_replace('%s', 'New Purchase Record', 'New purchase record has been added successfully.'));
					redirect(base_url() . 'admin/inventory/item-purchase');
				} else {
					$this->session->set_flashdata('danger', str_replace('%s', 'New Purchase Record', 'Something went wrong. Please try again later.'));
					redirect(base_url() . 'admin/inventory/item-purchase');
				}
			} else if (isset($_POST['updatePurchase']) && $_POST['updatePurchase'] == 'Update') {
				print_r($_FILES);
				$photoSize = $_FILES['photo']['size'];
				$recId = $_POST['recId'];

				if ($photoSize > 0) {
					unlink('assets_inventory/images/purchase/' . $_POST['photo_name']);
					$config['upload_path'] = 'assets_inventory/images/purchase/';
					$config['allowed_types'] = 'gif|jpg|png|jpeg';
					$this->load->library('upload', $config);
					$this->upload->initialize($config);
					$photo = array('upload_data' => $this->upload->data());

					if (!$this->upload->do_upload('photo')) {
						$item_photo = "No-Image.jpg";
					} else {
						$photo = array('upload_data' => $this->upload->data());
						$item_photo = $photo['upload_data']['file_name'];
					}
				} else {
					$item_photo = $_POST['photo_name'];
				}


				$purchase_record = $this->db->query("SELECT * FROM inventory_item_purchase WHERE id = $recId")->row_array();
				$item_stock = $this->db->query("SELECT * FROM inventory_stocks WHERE item_id = " . $_POST['item'])->row_array();
				// Start of clearing old records in stocks
				$sid['total_stock'] = $item_stock['total_stock'] - $purchase_record['remaining_qty'];
				$sid['current_stock'] = $item_stock['current_stock'] - $purchase_record['remaining_qty'];
				$sid['modified_on'] = $this->nepali_current_date;
				$sid['modified_by'] = $admin_info['admin_name'];
				$this->db->where('id', $item_stock['id']);
				$this->db->update('inventory_stocks', $sid);
				// End of clearing old records in stocks 


				$td['item_id'] = $_POST['item'];
				$td['purchased_from'] = $_POST['supplier'];
				$td['purchased_on'] = $_POST['purchased_on'];
				$td['qty'] = $_POST['qty'];
				$td['remaining_qty'] = $_POST['qty'];
				$td['unit_price'] = $_POST['unit_price'];
				$td['total_price'] = $_POST['total_price'];
				$td['selling_price'] = $_POST['selling_price'];
				$td['paid_amount'] = $_POST['paid_amount'];
				$td['remaining_amount'] = $_POST['remaining_amount'];
				$td['remarks'] = $_POST['remarks'];
				$td['photo'] = $item_photo;
				$td['modified_on'] = $this->nepali_current_date;
				$td['modified_by'] = $admin_info['admin_name'];

				$this->db->where('id', $recId);
				$update = $this->db->update('inventory_item_purchase', $td);
				// dd($_POST);
				if ($update) {
					// Start of Updating new records in stocks
					$updated_item_stock = $this->db->query("SELECT * FROM inventory_stocks WHERE item_id = " . $_POST['item'])->row_array();
					$uid['total_stock'] = $updated_item_stock['total_stock'] + $_POST['qty'];
					$uid['current_stock'] = $updated_item_stock['current_stock'] + $_POST['qty'];
					$uid['modified_on'] = $this->nepali_current_date;
					$uid['modified_by'] = $admin_info['admin_name'];

					$this->db->where('id', $item_stock['id']);
					$this->db->update('inventory_stocks', $uid);
					// End of Updating new records in stocks

					$this->session->set_flashdata('success', str_replace('%s', 'Purchase Record', 'Purchase record has been updated successfully.'));
					redirect(base_url() . 'admin/inventory/item-purchase');
				} else {
					$this->session->set_flashdata('danger', str_replace('%s', 'Purchase Record', 'Something went wrong. Please try again later.'));
					redirect(base_url() . 'admin/inventory/item-purchase');
				}
			}
		}

		$data['recId'] = $id;
		$this->load->view('inventory/header', $data);
		$this->load->view('inventory/add-edit-new-entry', $data);
		$this->load->view('inventory/footer');
	}

	public function suppliers()
	{
		if (!isset($_SESSION['admin_id']))
			redirect(base_url() . 'admin');
		$data['currentURL'] = current_url();
		$data['title'] = 'Suppliers';
		$data['logo'] = $this->Admin_model->get_logo();
		$data['school_info'] = $this->Admin_model->get_logo_from_setting();

		$admin_info = $this->db->query("SELECT * FROM admin WHERE id = " . $_SESSION['admin_id'])->row_array();
		// dd($admin_info);
		// $hr_db = $this->load->database('hr', TRUE);

		if (isset($_POST['addSupplier']) && $_POST['addSupplier'] == 'Add') {
			$name = $_POST['name'];
			$address = $_POST['address'];
			$contact_number = $_POST['contact_number'];
			$isUnique = $this->Inventory_model->get_unique_name('name', $name, 'inventory_suppliers');

			if ($isUnique) {
				$td['name'] = $name;
				$td['address'] = $address;
				$td['contact_number'] = $contact_number;
				$td['is_active'] = 'yes';
				$td['created_on'] = $this->nepali_current_date;
				$td['modified_on'] = $this->nepali_current_date;
				$td['created_by'] = $admin_info['admin_name'];
				$td['modified_by'] = $admin_info['admin_name'];

				$this->db->insert('inventory_suppliers', $td);

				$this->session->set_flashdata('success', str_replace('%s', 'Supplier', 'New supplier has been added.'));
			} else {
				$this->session->set_flashdata('danger', str_replace('%s', 'Supplier', 'Supplier name already exist.'));
			}

			if (isset($_POST['purchaseId'])) {
				redirect(base_url() . 'admin/inventory/add-edit-new-entry/' . $_POST['purchaseId']);
			} else {
				redirect(base_url() . 'admin/inventory/suppliers');
			}
		} else if (isset($_POST['updateItemUnit']) && $_POST['updateItemUnit'] == 'Update') {
			$name = $_POST['name'];
			$address = $_POST['address'];
			$contact_number = $_POST['contact_number'];
			$isUnique = $this->Inventory_model->get_unique_name('name', $name, 'inventory_suppliers');
			if ($isUnique) {
				$td = array(
					'title' => $title,
					'short_label' => $short_label,
					'is_active' => $_POST['is_active'],
					'modified_on' => $this->nepali_current_date,
					'modified_by' => $admin_info['admin_name'],
				);
				$this->db->where('id', $recId);
				$update = $this->db->update("inventory_measurements", $td);

				if ($update) {

					$this->session->set_flashdata('success', str_replace('%s', 'Supplier', 'Record has been updated successfully.'));
				} else {
					$this->session->set_flashdata('danger', str_replace('%s', 'Supplier', 'THE_ADD_ERROR_MSG'));
				}
			} else {
				$this->session->set_flashdata('danger', str_replace('%s', 'Supplier', 'Supplier name already exist.'));
			}
			redirect(base_url() . 'admin/inventory/suppliers');
		}

		$query = $this->db->query('select * from inventory_suppliers');
		$data['inventory_measurements'] = $query->result_array();

		$this->load->view('inventory/header', $data);
		$this->load->view('inventory/suppliers', $data);
		$this->load->view('inventory/footer');
	}

	public function sales()
	{
		if (!isset($_SESSION['admin_id']))
			redirect(base_url() . 'admin');
		$data['currentURL'] = current_url();
		$data['title'] = 'Sales';
		$data['logo'] = $this->Admin_model->get_logo();
		$data['school_info'] = $this->Admin_model->get_logo_from_setting();

		$admin_info = $this->db->query("SELECT * FROM admin WHERE id = " . $_SESSION['admin_id'])->row_array();

		$query = $this->db->query('select * from inventory_items');
		$data['inventory_items'] = $query->result_array();

		$studentQuery = $this->db->query("SELECT * FROM students WHERE is_active = 'yes'");
		$data['students'] = $studentQuery->result_array();

		$teacherQuery = $this->db->query("SELECT * FROM teacher WHERE is_active = 'yes'");
		$data['teachers'] = $teacherQuery->result_array();

		$salesQuery = $this->db->query('select iis.*, i.title, s.name, s.studentId from inventory_item_sales iis LEFT JOIN inventory_items i ON i.id=iis.item_id LEFT JOIN  students s ON s.id = iis.userId');
		$data['sales_list'] = $salesQuery->result_array();

		$data['theExportHeader'] = 'Sales List' . '-' . date('YmdHis', strtotime($this->nepali_current_date));

		$this->load->view('inventory/header', $data);
		$this->load->view('inventory/sales', $data);
		$this->load->view('inventory/footer');
	}

	public function add_new_sales($id)
	{
		$data['currentURL'] = current_url();

		$data['logo'] = $this->Admin_model->get_logo();
		$data['school_info'] = $this->Admin_model->get_logo_from_setting();
		$batchId = $data['school_info']['batch_id'];
		$admin_info = $this->db->query("SELECT * FROM admin WHERE id = " . $_SESSION['admin_id'])->row_array();


		$studentQuery = $this->db->query("SELECT s.*, cl.section_id,sb.roll_no,c.course_name,se.section_name FROM students s JOIN student_batch sb ON sb.student_id=s.id AND sb.batch_id=$batchId JOIN classroom cl ON cl.id = sb.classroom_id AND s.course=cl.course_id JOIN section se ON cl.section_id=se.id JOIN course c ON c.id=cl.course_id  WHERE s.is_active = 'yes'");
		$data['students'] = $studentQuery->result_array();
		// dd($data['students']);
		$teacherQuery = $this->db->query("SELECT * FROM teacher WHERE is_active = 'yes'");
		$data['teachers'] = $teacherQuery->result_array();

		$query = $this->db->query('SELECT i.*, iis.current_stock as available_stock, m.title as unit_label, m.short_label from inventory_items i JOIN inventory_stocks iis ON i.id = iis.item_id LEFT JOIN inventory_measurements m ON m.id = i.measurement_id');
		$data['inventory_items'] = $query->result_array();

		foreach ($data['inventory_items'] as $i_key => $item) {
			$item_purchases = $this->db->query('SELECT * FROM inventory_item_purchase WHERE item_id = ' . $item['id'] . ' AND remaining_qty > 0  ORDER BY selling_price ASC')->result_array();

			$data['inventory_items'][$i_key]['item_purchases'] = $item_purchases;
		}
		// dd($data['inventory_items']);

		$today_date_time = explode(' ', $this->nepali_current_date);

		$data['today_date'] = $today_date_time[0];

		if ($id == 0) {
			$data['title'] = 'Add New Entry';
		}

		if ($_POST > 0) {
			if (isset($_POST['addSalesEntry']) && $_POST['addSalesEntry'] == 'Submit') {
				// dd($_POST);

				$items = $_POST['itemId'];
				$itemQty = $_POST['itemQty'];
				$purchaseId = $_POST['purchaseId'];
				$studentId = $_POST['studentId'];
				$sellingPrice = $_POST['sellingPrice'];
				$insert_array = array();

				// $dateTime = str_replace(':', '', str_replace(' ', '', str_replace('-', '', $this->nepali_current_date)));


				// str_replace(':', '', str_replace(' ', '', str_replace('-', '', strtotime($this->nepali_current_date))));
				$salesId = str_replace(':', '', str_replace(' ', '', str_replace('-', '', $this->nepali_current_date)));

				// print_r($dateTime);
				// dd($insert_array);

				// for(int a = 0; a < count($items)
				foreach ($items as $i_key => $item) {

					$td['item_id'] = $item;
					$td['userId'] = $studentId[$i_key];
					$td['qty'] = $itemQty[$i_key];
					$td['salesId'] = $salesId;
					$td['unit_price'] = $sellingPrice[$i_key];
					$td['sales_on'] = $this->nepali_current_date;
					$td['total_amount'] = $itemQty[$i_key] * $sellingPrice[$i_key];
					$td['paid_amount'] = $itemQty[$i_key] * $sellingPrice[$i_key];
					$td['remaining_amount'] = $td['paid_amount'] - $td['total_amount'];

					// Start of updating stock record in inventory_stocks table
					$item_stocks = $this->db->query('SELECT * FROM inventory_stocks WHERE item_id = ' . $item)->row_array();
					$is_td['current_stock'] = $item_stocks['current_stock'] - $td['qty'];
					$is_td['sold_stock'] = $item_stocks['sold_stock'] + $td['qty'];
					$is_td['modified_on'] = $this->nepali_current_date;
					$is_td['modified_by'] = $admin_info['admin_name'];
					$this->db->where('id', $item_stocks['id']);
					$this->db->update('inventory_stocks', $is_td);
					// End of updating stock record in inventory_stocks table


					// Updating stock record in purchase table
					$item_purchase = $this->db->query('SELECT * FROM inventory_item_purchase WHERE id = ' . $purchaseId[$i_key])->row_array();
					$ip_td['remaining_qty'] = $item_purchase['remaining_qty'] - $td['qty'];
					$ip_td['modified_on'] = $this->nepali_current_date;
					$ip_td['modified_by'] = $admin_info['admin_name'];
					$this->db->where('id', $item_purchase['id']);
					$this->db->update('inventory_item_purchase', $ip_td);
					// End of Updating stock record in purchase table

					$td['created_on'] = $this->nepali_current_date;
					$td['created_by'] = $admin_info['admin_name'];
					$td['modified_on'] = $this->nepali_current_date;
					$td['modified_by'] = $admin_info['admin_name'];

					$insert = $this->db->insert('inventory_item_sales', $td);

					if ($insert) {
						array_push($insert_array, true);
					} else {
						array_push($insert_array, false);
					}
				}

				if (!in_array(false, $insert_array)) {
					$this->session->set_flashdata('success', str_replace('%s', 'Sales Record', 'New sales record has been added successfully.'));
					redirect(base_url() . 'admin/inventory/item-sales');
				} else {
					$this->session->set_flashdata('danger', str_replace('%s', 'Sales Record', 'Something went wrong. Please try again later.'));
					redirect(base_url() . 'admin/inventory/item-sales');
				}
			}
		}

		$data['recId'] = $id;
		$this->load->view('inventory/header', $data);
		$this->load->view('inventory/add-sales-record', $data);
		$this->load->view('inventory/footer');
	}

	public function item_stocks()
	{
		if (!isset($_SESSION['admin_id']))
			redirect(base_url() . 'admin');
		$data['currentURL'] = current_url();
		$data['title'] = 'Item Stocks';
		$data['logo'] = $this->Admin_model->get_logo();
		$data['school_info'] = $this->Admin_model->get_logo_from_setting();

		$admin_info = $this->db->query("SELECT * FROM admin WHERE id = " . $_SESSION['admin_id'])->row_array();
		// dd($admin_info);
		// $hr_db = $this->load->database('hr', TRUE);

		$query = $this->db->query('select iis.*, i.title from inventory_stocks iis JOIN inventory_items i ON i.id = iis.item_id');
		$data['inventory_stocks'] = $query->result_array();
		$data['theExportHeader'] = 'Stock List' . '-' . date('YmdHis', strtotime($this->nepali_current_date));
		$this->load->view('inventory/header', $data);
		$this->load->view('inventory/item-stocks', $data);
		$this->load->view('inventory/footer');
	}

	public function view_sales_details($id)
	{
		$data['currentURL'] = current_url();

		$data['logo'] = $this->Admin_model->get_logo();
		$data['school_info'] = $this->Admin_model->get_logo_from_setting();

		$admin_info = $this->db->query("SELECT * FROM admin WHERE id = " . $_SESSION['admin_id'])->row_array();

		$query = $this->db->query('select * from inventory_items');
		$data['inventory_items'] = $query->result_array();
		$query = $this->db->query('select * from inventory_suppliers');
		$data['inventory_suppliers'] = $query->result_array();

		$today_date_time = explode(' ', $this->nepali_current_date);
		$batchId = $data['school_info']['batch_id'];

		$data['today_date'] = $today_date_time[0];

		if ($id == 0) {
			$data['title'] = 'Add New Entry';
		} else {
			// $purchaseQuery = $this->db->query('select ip.*, i.title from inventory_item_purchase ip LEFT JOIN inventory_items i ON i.id=ip.item_id WHERE ip.id =' . $id);


			// $data['purchase_item'] = $purchaseQuery->row_array();
			// $data['title'] = $data['purchase_item']['title'];

			$saleDetail = $this->db->query("SELECT iis.*, i.title, s.name, c.course_name, se.section_name 
			FROM inventory_item_sales iis 
			JOIN students s ON s.id = iis.userId 
			JOIN student_batch sb ON sb.student_id=s.id AND sb.batch_id=$batchId
			JOIN inventory_items i ON i.id = iis.item_id 
			JOIN classroom cl ON cl.id = sb.classroom_id AND s.course=cl.course_id 
			JOIN section se ON cl.section_id=se.id JOIN course c ON c.id=cl.course_id
			WHERE iis.id = $id")->row_array();
			// dd($saleDetail);
			$data['title'] = $saleDetail['name'];
			$data['sales_detail'] = $saleDetail;
		}
		$data['recId'] = $id;
		$this->load->view('inventory/header', $data);
		$this->load->view('inventory/view-sales-detail', $data);
		$this->load->view('inventory/footer');
	}

	public function view_item_details($id)
	{
		$data['currentURL'] = current_url();

		$data['logo'] = $this->Admin_model->get_logo();
		$data['school_info'] = $this->Admin_model->get_logo_from_setting();

		$today_date_time = explode(' ', $this->nepali_current_date);
		$batchId = $data['school_info']['batch_id'];

		$data['today_date'] = $today_date_time[0];

		if ($id == 0) {
			$data['title'] = 'Add New Entry';
		} else {
			$item = $this->db->query('SELECT i.* FROM inventory_items i WHERE i.id = ' . $id)->row_array();

			$item_stocks =	$this->db->query('SELECT * FROM inventory_stocks WHERE item_id = ' . $id)->row_array();
			$item['stock'] = $item_stocks;

			$item_purchase_history = $this->db->query("SELECT ip.*, s.name as supplier_name, s.contact_number as supplier_contact_number, s.address as supplier_address FROM inventory_item_purchase ip JOIN inventory_suppliers s ON s.id = ip.purchased_from WHERE item_id = " . $id)->result_array();
			$item['purchases'] = $item_purchase_history;
			// dd($item_purchase_history);
			// $item_sales_history = $this->db->query("select iis.*, i.title, s.name, s.studentId from inventory_item_sales iis LEFT JOIN inventory_items i ON i.id=iis.item_id LEFT JOIN  students s ON s.id = iis.userId WHERE item_id = " . $id)->result_array();

			$item_sales_history = $this->db->query("SELECT iis.*, i.title, s.name, c.course_name, se.section_name 
			FROM inventory_item_sales iis 
			JOIN students s ON s.id = iis.userId 
			JOIN student_batch sb ON sb.student_id=s.id AND sb.batch_id=$batchId
			JOIN inventory_items i ON i.id = iis.item_id 
			JOIN classroom cl ON cl.id = sb.classroom_id AND s.course=cl.course_id 
			JOIN section se ON cl.section_id=se.id JOIN course c ON c.id=cl.course_id WHERE iis.item_id = " . $id)->result_array();

			$item['sales'] = $item_sales_history;

			$item_measurement_units = $this->db->query("SELECT * FROM inventory_measurements")->result_array();
			$data['measurement_units'] = $item_measurement_units;

			$courses = $this->db->query("SELECT * FROM course")->result_array();
			$data['courses'] = $courses;

			$item_categories = $this->db->query("SELECT * FROM inventory_item_category ")->result_array();
			$data['categories'] = $item_categories;



			$data['item_info'] = $item;;
			$data['title'] = $item['title'];
		}
		$data['recId'] = $id;
		$this->load->view('inventory/header', $data);
		$this->load->view('inventory/view-item-detail', $data);
		$this->load->view('inventory/footer');
	}


	public function view_invoice($id)
	{
		$data['currentURL'] = current_url();

		$data['logo'] = $this->Admin_model->get_logo();
		$data['school_info'] = $this->Admin_model->get_logo_from_setting();

		$today_date_time = explode(' ', $this->nepali_current_date);
		$batchId = $data['school_info']['batch_id'];
		$data['today_date'] = $today_date_time[0];
		$data['recId'] = $id;


		$salesId = urlsafe_b64decode($id);

		$item_sales = $this->db->query('SELECT * FROM inventory_item_sales WHERE salesId = ' . $salesId)->result_array();

		$item_id = $item_sales[0]['item_id'];
		$userId = $item_sales[0]['userId'];

		$item = $this->db->query("SELECT * FROM inventory_items WHERE id = $item_id")->row_array();
		$student = $this->db->query("SELECT * FROM students WHERE id = $userId")->row_array();

		dd($item_sales);

		$this->load->view('inventory/header', $data);
		$this->load->view('inventory/view-item-detail', $data);
		$this->load->view('inventory/footer');
	}

	public function book_publications()
	{
		if (!isset($_SESSION['admin_id']))
			redirect(base_url() . 'admin');
		$data['currentURL'] = current_url();
		$data['title'] = 'Book Publications';
		$data['logo'] = $this->Admin_model->get_logo();
		$data['school_info'] = $this->Admin_model->get_logo_from_setting();

		$admin_info = $this->db->query("SELECT * FROM admin WHERE id = " . $_SESSION['admin_id'])->row_array();
		// dd($admin_info);
		// $hr_db = $this->load->database('hr', TRUE);

		if (isset($_POST['addPublication']) && $_POST['addPublication'] == 'Add') {
			$title = $_POST['title'];
			$isUnique = $this->Inventory_model->get_unique_name('title', $title, 'inventory_publications');

			if ($isUnique) {
				$td['title'] = $title;
				$td['is_active'] = 'yes';
				$td['created_on'] = $this->nepali_current_date;
				$td['modified_on'] = $this->nepali_current_date;
				$td['created_by'] = $admin_info['admin_name'];
				$td['modified_by'] = $admin_info['admin_name'];

				$this->db->insert('inventory_publications', $td);

				$this->session->set_flashdata('success', str_replace('%s', 'Book Publication', 'New publication has been added.'));
			} else {
				$this->session->set_flashdata('danger', str_replace('%s', 'Book Publication', 'Publication title already exist.'));
			}
			redirect(base_url() . 'admin/inventory/book-publication');
		} else if (isset($_POST['updatePublication']) && $_POST['updatePublication'] == 'Update') {
			$title = $_POST['title'];
			$recId = $_POST['unitId'];

			$td = array(
				'title' => $title,
				'is_active' => $_POST['is_active'],
				'modified_on' =>  $this->nepali_current_date,
				'modified_by' =>  $admin_info['admin_name'],
			);
			$this->db->where('id', $recId);
			$update = $this->db->update("inventory_publications", $td);

			if ($update) {

				$this->session->set_flashdata('success', str_replace('%s', 'Book Publication', 'Record has been updated successfully.'));
			} else {
				$this->session->set_flashdata('danger', str_replace('%s', 'Book Publication', 'THE_ADD_ERROR_MSG'));
			}
			redirect(base_url() . 'admin/inventory/book-publication');
		}

		$query = $this->db->query('select * from inventory_publications');
		$data['book_publications'] = $query->result_array();

		$this->load->view('inventory/header', $data);
		$this->load->view('inventory/book_publications', $data);
		$this->load->view('inventory/footer');
	}

	public function item_variants()
	{
		if (!isset($_SESSION['admin_id']))
			redirect(base_url() . 'admin');
		$data['currentURL'] = current_url();
		$data['title'] = 'Item Category';
		$data['logo'] = $this->Admin_model->get_logo();
		$data['school_info'] = $this->Admin_model->get_logo_from_setting();

		$admin_info = $this->db->query("SELECT * FROM admin WHERE id = " . $_SESSION['admin_id'])->row_array();

		if (isset($_POST['addVariant']) && $_POST['addVariant'] == 'Add') {
			$title = $_POST['title'];
			$category = $_POST['item_category'];
			$isUnique = $this->Inventory_model->get_unique_name('title', $title, 'inventory_variants');

			if ($isUnique) {
				$td['title'] = $title;
				$td['category'] = $category;
				$td['is_active'] = 'yes';
				$td['created_on'] = $this->nepali_current_date;
				$td['modified_on'] = $this->nepali_current_date;
				$td['created_by'] = $admin_info['admin_name'];
				$td['modified_by'] = $admin_info['admin_name'];

				$this->db->insert('inventory_variants', $td);

				$this->session->set_flashdata('success', str_replace('%s', 'Variants', 'Item variant has been added.'));
			} else {
				$this->session->set_flashdata('danger', str_replace('%s', 'Variants', 'Item variant already exist.'));
			}
			redirect(base_url() . 'admin/inventory/item-variants');
		} else if (isset($_POST['updateVariant']) && $_POST['updateVariant'] == 'Update') {
			$title = $_POST['title'];
			$recId = $_POST['recId'];

			$td = array(
				'title' => $title,
				'category' => $_POST['item_category'],
				'is_active' => $_POST['is_active'],
				'modified_on' =>  $this->nepali_current_date,
				'modified_by' =>  $admin_info['admin_name'],
			);
			$this->db->where('id', $recId);
			$update = $this->db->update("inventory_variants", $td);

			if ($update) {

				$this->session->set_flashdata('success', str_replace('%s', 'Variants', 'Record has been updated successfully.'));
			} else {
				$this->session->set_flashdata('danger', str_replace('%s', 'Variants', 'THE_ADD_ERROR_MSG'));
			}
			redirect(base_url() . 'admin/inventory/item-variants');
		}

		$data['inventory_categories'] = $this->db->query("SELECT * FROM inventory_item_category WHERE is_active='yes'")->result_array();
		$query = $this->db->query("SELECT iv.*, ic.title AS category_title FROM inventory_variants iv LEFT JOIN inventory_item_category ic ON iv.category=ic.id");
		$data['inventory_variants'] = $query->result_array();

		$this->load->view('inventory/header', $data);
		$this->load->view('inventory/item-variants', $data);
		$this->load->view('inventory/footer');
	}
}