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

class Bills_to_pay extends Model {

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

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

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

			$ref = Bills_to_pay::insert($bills_to_pay)
			->execute();
		}

		return $ref;
	}

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

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

	public static function getAll($offset, $limit, $filter) {
		$data = Bills_to_pay::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_billstype'], $filter['fb_simbol'], $filter['fb_value'])
			->where($filter['f_formpay'], $filter['f_fpsimbol'], $filter['f_fpvalue'])
			->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 getBillsToPay($id) {
		$data = Bills_to_pay::select()
			->where('id', $id)
		->one();

		return $data;
	}

	public static function getBtp($ref, $value) {
		$response = '';
		$data = Bills_to_pay::select()
			->where($ref, $value)
		->one();

		if($data) {
			$response = self::getPaymentForm($data['form_to_pay']);
		}		

		return $response;
	}

	public static function getAllPaymentForm() {
		$data = Bills_to_pay::select()
			->table('payment_form')
			->orderBy(['name' => 'asc'])
		->get();

		return $data;
	}

	public static function getAllBillsToPaytype() {
		$data = Bills_to_pay::select()
			->table('billstopay_type')
			->orderBy(['name' => 'asc'])
		->get();

		return $data;
	}

	public static function getPaymentForm($form_to_pay) {
		$response = '';
		$data = Bills_to_pay::select('name')
			->table('payment_form')
			->where('id', $form_to_pay)
		->one();

		if($data) {
			$response = $data['name'];
		}

		return $response;
	}

	public static function getAllBillsToPaySituation() {
		$data = Bills_to_pay::select()
			->table('billstopay_situation')
			->orderBy(['name' => 'asc'])
		->get();

		return $data;
	}

	public static function getBillsToPaySituation($ref) {
		$data = array();

		$data = Bills_to_pay::select()
			->table('billstopay_situation')
			->where('id', $ref)
		->one();

		return $data;
	}

	public static function getBillsToPayType($type) {
		$ref = Bills_to_pay::select('name')
			->table('billstopay_type')
			->where('id', $type)
		->one();

		return $ref['name'];
	}

	public static function getCount($filter) {
		$c = Bills_to_pay::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_billstype'], $filter['fb_simbol'], $filter['fb_value'])
			->where($filter['f_formpay'], $filter['f_fpsimbol'], $filter['f_fpvalue'])
			->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 saveBillsToPayType($billstopay_new) {
		$ref = Bills_to_pay::insert($billstopay_new)
			->table('billstopay_type')
		->execute();

		return $ref;
	}

	public static function saveFormToPay($formtopay_new) {
		$ref = Bills_to_pay::insert($formtopay_new)
			->table('payment_form')
		->execute();

		return $ref;
	}

	// Relatórios
    public static function getContentReports($offset, $limit, $filter) {
    	$data = Bills_to_pay::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_docnumber'], $filter['d_docnumber_simbol'], $filter['d_docnumber_value'])
    		->where($filter['d_billstype'], $filter['d_billstype_simbol'], $filter['d_billstype_value'])
    		->where($filter['d_provider'], $filter['d_provider_simbol'], $filter['d_provider_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'])
    		->offset($offset)
    		->limit($limit)
    		->orderBy(['expirate_date' => 'asc'])
    	->get();

    	return $data;
    }


    public static function updateBtpType($ref, $data) {
		Bills_to_pay::update($data)
			->table('billstopay_type')
			->where('id', $ref)
		->execute();
	}
}