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

class MUser_privileges extends CI_Model {

	public $tablename;

	/**
	 * Class constructor and validate authenticate user
	 *
	 * @return  void
	 */
	public function __construct() {
		parent::__construct();
		$this->tablename = 'user_privileges';
	}

	public function get_by_id( $id ) {
		$data = [];
		$this->db->where( 'id', $id );
		$this->db->limit( 1 );
		$q = $this->db->get( $this->tablename );
		if ( $q->num_rows() > 0 ) {
			foreach ( $q->result_array() as $row ) {
				$data = $row;
			}
		}

		$q->free_result();

		return $data;
	}

	public function get_by_ref_user( $ref_user ) {
		$data = [];
		$this->db->where( 'ref_user', $ref_user );
		$this->db->limit( 1 );
		$q = $this->db->get( $this->tablename );
		if ( $q->num_rows() > 0 ) {
			foreach ( $q->result_array() as $row ) {
				$data = $row;
			}
		}

		$q->free_result();

		return $data;
	}

	public function get_all() {
		$data = [];
		if ( $this->session->user_type != 'Admin' ) {
			$this->db->where( 'company_id', $this->session->user_company );
		}
		$q = $this->db->get( $this->tablename );
		if ( $q->num_rows() > 0 ) {
			foreach ( $q->result_array() as $row ) {
				$data[] = $row;
			}
		}

		$q->free_result();

		return $data;
	}

	public function create() {
		$data = [
			'company_id'               => $this->session->user_company,
			'ref_user'                 => $this->input->post( 'ref_user' ),
			'inventory_menu'           => $this->input->post( 'inventory_menu' ) ?? '0',
			'sales'                    => $this->input->post( 'sales' ) ?? '0',
			'sales_return'             => $this->input->post( 'sales_return' ) ?? '0',
			'purchase'                 => $this->input->post( 'purchase' ) ?? '0',
			'purchase_return'          => $this->input->post( 'purchase_return' ) ?? '0',
			'supplier'                 => $this->input->post( 'supplier' ) ?? '0',
			'item'                     => $this->input->post( 'item' ) ?? '0',
			'customer'                 => $this->input->post( 'customer' ) ?? '0',
			'hr_menu'                  => $this->input->post( 'hr_menu' ) ?? '0',
			'employee'                 => $this->input->post( 'employee' ) ?? '0',
			'accounts_menu'            => $this->input->post( 'accounts_menu' ) ?? '0',
			'journal'                  => $this->input->post( 'journal' ) ?? '0',
			'ac_head'                  => $this->input->post( 'ac_head' ) ?? '0',
			'money_receipt'            => $this->input->post( 'money_receipt' ) ?? '0',
			'payment_receipt'          => $this->input->post( 'payment_receipt' ) ?? '0',
			'report_menu'              => $this->input->post( 'report_menu' ) ?? '0',
			'purchase_report'          => $this->input->post( 'purchase_report' ) ?? '0',
			'purchase_return_report'   => $this->input->post( 'purchase_return_report' ) ?? '0',
			'sales_report'             => $this->input->post( 'sales_report' ) ?? '0',
			'sales_return_report'      => $this->input->post( 'sales_return_report' ) ?? '0',
			'inventory_report'         => $this->input->post( 'inventory_report' ) ?? '0',
			'ledger_report'            => $this->input->post( 'ledger_report' ) ?? '0',
			'trial_balance_report'     => $this->input->post( 'trial_balance_report' ) ?? '0',
			'balance_sheet_report'     => $this->input->post( 'balance_sheet_report' ) ?? '0',
			'income_statement_report'  => $this->input->post( 'income_statement_report' ) ?? '0',
			'bills_receivable_report'  => $this->input->post( 'bills_receivable_report' ) ?? '0',
			'bills_payable_report'     => $this->input->post( 'bills_payable_report' ) ?? '0',
			'cash_book_report'         => $this->input->post( 'cash_book_report' ) ?? '0',
			'bank_book_report'         => $this->input->post( 'bank_book_report' ) ?? '0',
			'settings_menu'            => $this->input->post( 'settings_menu' ) ?? '0',
			'basic_settings'           => $this->input->post( 'basic_settings' ) ?? '0',
			'currency_settings'        => $this->input->post( 'currency_settings' ) ?? '0',
			'company_settings'         => $this->input->post( 'company_settings' ) ?? '0',
			'default_ac_head_settings' => $this->input->post( 'default_ac_head_settings' ) ?? '0',
			'user_menu'                => $this->input->post( 'user_menu' ) ?? '0',
			'user_section'             => $this->input->post( 'user_section' ) ?? '0',
			'created'                  => date( 'Y-m-d H:i:s', time() ),
			'created_by'               => $this->session->user_id,
		];
		$this->db->insert( $this->tablename, $data );
	}

	public function update() {
		$data = [
			'company_id'               => $this->session->user_company,
			'ref_user'                 => $this->input->post( 'ref_user' ),
			'inventory_menu'           => $this->input->post( 'inventory_menu' ) ?? '0',
			'sales'                    => $this->input->post( 'sales' ) ?? '0',
			'sales_return'             => $this->input->post( 'sales_return' ) ?? '0',
			'purchase'                 => $this->input->post( 'purchase' ) ?? '0',
			'purchase_return'          => $this->input->post( 'purchase_return' ) ?? '0',
			'supplier'                 => $this->input->post( 'supplier' ) ?? '0',
			'item'                     => $this->input->post( 'item' ) ?? '0',
			'customer'                 => $this->input->post( 'customer' ) ?? '0',
			'hr_menu'                  => $this->input->post( 'hr_menu' ) ?? '0',
			'employee'                 => $this->input->post( 'employee' ) ?? '0',
			'accounts_menu'            => $this->input->post( 'accounts_menu' ) ?? '0',
			'journal'                  => $this->input->post( 'journal' ) ?? '0',
			'ac_head'                  => $this->input->post( 'ac_head' ) ?? '0',
			'money_receipt'            => $this->input->post( 'money_receipt' ) ?? '0',
			'payment_receipt'          => $this->input->post( 'payment_receipt' ) ?? '0',
			'report_menu'              => $this->input->post( 'report_menu' ) ?? '0',
			'purchase_report'          => $this->input->post( 'purchase_report' ) ?? '0',
			'purchase_return_report'   => $this->input->post( 'purchase_return_report' ) ?? '0',
			'sales_report'             => $this->input->post( 'sales_report' ) ?? '0',
			'sales_return_report'      => $this->input->post( 'sales_return_report' ) ?? '0',
			'inventory_report'         => $this->input->post( 'inventory_report' ) ?? '0',
			'ledger_report'            => $this->input->post( 'ledger_report' ) ?? '0',
			'trial_balance_report'     => $this->input->post( 'trial_balance_report' ) ?? '0',
			'balance_sheet_report'     => $this->input->post( 'balance_sheet_report' ) ?? '0',
			'income_statement_report'  => $this->input->post( 'income_statement_report' ) ?? '0',
			'bills_receivable_report'  => $this->input->post( 'bills_receivable_report' ) ?? '0',
			'bills_payable_report'     => $this->input->post( 'bills_payable_report' ) ?? '0',
			'cash_book_report'         => $this->input->post( 'cash_book_report' ) ?? '0',
			'bank_book_report'         => $this->input->post( 'bank_book_report' ) ?? '0',
			'settings_menu'            => $this->input->post( 'settings_menu' ) ?? '0',
			'basic_settings'           => $this->input->post( 'basic_settings' ) ?? '0',
			'currency_settings'        => $this->input->post( 'currency_settings' ) ?? '0',
			'company_settings'         => $this->input->post( 'company_settings' ) ?? '0',
			'default_ac_head_settings' => $this->input->post( 'default_ac_head_settings' ) ?? '0',
			'user_menu'                => $this->input->post( 'user_menu' ) ?? '0',
			'user_section'             => $this->input->post( 'user_section' ) ?? '0',
			'modified'                 => date( 'Y-m-d H:i:s', time() ),
			'modified_by'              => $this->session->userdata( 'user_id' ),
		];

		$this->db->where( 'ref_user', $this->input->post( 'ref_user' ) );
		$this->db->update( $this->tablename, $data );
	}

	public function delete_by_ref_user( $ref_user ) {
		$this->db->where( 'ref_user', $ref_user );
		$this->db->delete( $this->tablename );
	}

	public function delete( $id ) {
		$this->db->where( 'id', $id );
		$this->db->delete( $this->tablename );
	}

	public function power_user( $company_id, $ref_user ) {
		$data = [
			'company_id'               => $company_id,
			'ref_user'                 => $ref_user,
			'inventory_menu'           => 1,
			'sales'                    => 1,
			'sales_return'             => 1,
			'purchase'                 => 1,
			'purchase_return'          => 1,
			'supplier'                 => 1,
			'item'                     => 1,
			'customer'                 => 1,
			'hr_menu'                  => 1,
			'employee'                 => 1,
			'accounts_menu'            => 1,
			'journal'                  => 1,
			'ac_head'                  => 1,
			'money_receipt'            => 1,
			'payment_receipt'          => 1,
			'report_menu'              => 1,
			'purchase_report'          => 1,
			'purchase_return_report'   => 1,
			'sales_report'             => 1,
			'sales_return_report'      => 1,
			'inventory_report'         => 1,
			'ledger_report'            => 1,
			'trial_balance_report'     => 1,
			'balance_sheet_report'     => 1,
			'income_statement_report'  => 1,
			'bills_receivable_report'  => 1,
			'bills_payable_report'     => 1,
			'cash_book_report'         => 1,
			'bank_book_report'         => 1,
			'settings_menu'            => 1,
			'basic_settings'           => 1,
			'currency_settings'        => 1,
			'company_settings'         => 1,
			'default_ac_head_settings' => 1,
			'user_menu'                => 1,
			'user_section'             => 1,
			'created'                  => date( 'Y-m-d H:i:s', time() ),
			'created_by'               => $this->session->user_id,
		];
		$this->db->insert( $this->tablename, $data );
	}

	public function delete_by_cmp( $cmp_id ) {
		$this->db->where( 'company_id', $cmp_id );
		$this->db->delete( $this->tablename );
	}

}