<?php
namespace src\models;
use \core\Model;

class Bills_to_receive extends Model {

	public static function saveAcount($billstoreceiver, $billstopay_repeat = '', $qtd_repeat = '') {
		$qtd_repeat_init = 1;
		$expirate_date = $billstoreceiver['expirate_date'];

		if(isset($billstopay_repeat) && !empty($billstopay_repeat)) {
			$qtd_repeat_init = $qtd_repeat;
		}

		for($i=0;$i<$qtd_repeat_init;$i++) {
			$billstoreceiver['expirate_date'] = date('Y-m-d', strtotime('+'.$i.'Months', strtotime($expirate_date)));

			$ref = Bills_to_receive::insert($billstoreceiver)
			->execute();
		}

		return $ref;
	}

	public static function updateAccount($refRow, $ref, $data) {
		Bills_to_receive::update($data)
			->where($refRow, $ref)
		->execute();
	}

	public static function deleteAccounts($collun, $ref) {
		Bills_to_receive::delete()
			->where($collun, $ref)
			->where('id_situation', '!=', 3)
			->where('id_situation', '!=', 4)
		->execute();
	}

	public static function getAll($offset, $limit, $filter) {
		$data = Bills_to_receive::select()
			->where($filter['f_text'], $filter['f_text_simbol'], $filter['f_text_value'])
			->where($filter['f_billsstatus'], $filter['fs_simbol'], $filter['fs_value'])
			->where($filter['f_billsorigin'], $filter['fo_simbol'], $filter['fo_value'])
			->where($filter['f_bsituation'], $filter['f_bsimbol'], $filter['f_bvalue'])
			->where($filter['f_date_init'], $filter['f_date_init_simbol'], $filter['f_date_init_value'])
			->where($filter['f_date_end'], $filter['f_date_end_simbol'], $filter['f_date_end_value'])
			->offset($offset)
			->limit($limit)
			->orderBy(['status' => '!= 1', 'expirate_date' => 'asc'])
		->get();

		return $data;
	}

	public static function getBillsToReceiver($id) {
		$data = Bills_to_receive::select()
			->where('id', $id)
		->one();

		return $data;
	}

	public static function getAllReceiverForm() {
		$data = Bills_to_receive::select()
			->table('receiver_form')
			->orderBy(['name' => 'asc'])
		->get();

		return $data;
	}

	public static function getReceiverForm($ref) {
		$data = Bills_to_receive::select('name')
			->table('receiver_form')
			->where('id', $ref)
		->one();

		return $data['name'];
	}

	public static function getAllReceiverOrigin() {
		$data = Bills_to_receive::select()
			->table('receiver_origin')
			->orderBy(['name' => 'asc'])
		->get();

		return $data;
    }

    public static function getReceiverOrigin($id) {
		$data = Bills_to_receive::select()
			->table('receiver_origin')
			->where('id', $id)
		->one();

		return $data;
    }

    public static function getCount($filter) {
		$c = Bills_to_receive::select()
			->where($filter['f_text'], $filter['f_text_simbol'], $filter['f_text_value'])
			->where($filter['f_billsstatus'], $filter['fs_simbol'], $filter['fs_value'])		
			->where($filter['f_billsorigin'], $filter['fo_simbol'], $filter['fo_value'])
			->where($filter['f_bsituation'], $filter['f_bsimbol'], $filter['f_bvalue'])
			->where($filter['f_date_init'], $filter['f_date_init_simbol'], $filter['f_date_init_value'])
			->where($filter['f_date_end'], $filter['f_date_end_simbol'], $filter['f_date_end_value'])
		->count();

		return $c;
	}

	public static function saveReceiverOrigin($data) {
		$ref = Bills_to_receive::insert($data)
			->table('receiver_origin')
		->execute();

		return $ref;
	}

	public static function updateReceiverOrigin($data, $ref) {
		Bills_to_receive::update($data)
			->table('receiver_origin')
			->where('id', $ref)
		->execute();
	}

	public static function saveFormToReceiver($data) {
		$ref = Bills_to_pay::insert($data)
			->table('receiver_form')
		->execute();

		return $ref;
	}

	// Relatórios
    public static function getContentReports($offset, $limit, $filter) {
    	$data = Bills_to_receive::select()
    		->where($filter['d_init'], $filter['d_init_simbol'], $filter['d_init_value'])
    		->where($filter['d_end'], $filter['d_end_simbol'], $filter['d_end_value'])    		
    		->where($filter['d_status'], $filter['d_status_simbol'], $filter['d_status_value'])
    		->where($filter['d_provider'], $filter['d_provider_simbol'], $filter['d_provider_value'])
    		->where($filter['d_billstype'], $filter['d_billstype_simbol'], $filter['d_billstype_value'])
    		->where($filter['d_formpay'], $filter['d_formpay_simbol'], $filter['d_formpay_value'])
    		->where($filter['d_situation'], $filter['d_situation_simbol'], $filter['d_situation_value'])
    		->where($filter['d_docnumber'], $filter['d_docnumber_simbol'], $filter['d_docnumber_value'])
    		->where($filter['d_reqnumber'], $filter['d_reqnumber_simbol'], $filter['d_reqnumber_value'])  		
    		->offset($offset)
    		->limit($limit)
    		->orderBy(['expirate_date' => 'asc'])
    	->get();

    	return $data;
    }
}