Sptpd.php 2.59 KB
<?php

/**
 * @Author: irul
 * @Date:   2019-09-23 15:28:04
 * @Last Modified by:   irul
 * @Last Modified time: 2019-10-01 23:43:21
 */

namespace Integrasi\Reklame\Base\Models;

use \Illuminate\Database\Capsule\Manager as DB;

class Sptpd extends AbstractModel
{
	protected $table = 'pad_sptpd';
	// protected $visible = array(
	// 	'id',
	// 	'sptno',
	// 	'customer_id',
	// 	'customer_usaha_id',
	// 	'pajak_id',
	// 	'tahun',
	// 	'terimatgl',
	// 	'type_id',
	// 	'so',
	// 	'jatuhtempotgl',
	// 	'masadari',
	// 	'masasd',
	// 	'minomset',
	// 	'dasar',
	// 	'tarif',
	// 	'denda',
	// 	'bunga',
	// 	'setoran',
	// 	'kenaikan',
	// 	'kompensasi',
	// 	'lain2',
	// 	'pajak_terhutang',
	// 	'r_bayarid',
	// 	'rekening_id',
	// 	'create_date',
	// 	'create_uid',
	// 	'enabled',

	// 	'spt',
	// 	'customer',
	// 	'kohir',
	// 	'rincianReklame',
	// );
	protected $guarded = array();

	protected static function boot()
	{
		parent::boot();
		static::creating(function ($model) {
			return $model->_onCreating();
		});
	}

	public function _onCreating()
	{
		$maxSptno = call_user_func($this->getParentNamespace() . '\Sptpd::select',
			DB::raw('max(sptno) as nomor'))
			->where(DB::raw('tahun'), date('Y', strtotime($this->attributes['terimatgl'])))
			->first();
		$sptno = ($maxSptno->nomor ?: 0) + 1;

		$this->attributes['sptno']      = $sptno;
		$this->attributes['create_uid'] = \Api\AuthBasic::getUid();
		return true;
	}

	// == ACCESSORS == //

	public function getSptpdnoAttribute($value)
	{
		$qry = "select get_sptpdno(?) as sptno";
		$row = DB::select(DB::raw($qry), array($this->attributes['id']));
		return $row[0]->sptno;
	}

	// == QUERY SCOPES == //

	public static function scopeFindBySptpdno($query, $sptno)
	{
		return $query->where(DB::raw('get_sptpdno(id)'), array($sptno));
	}

	public static function scopeFindByKohirno($query, $kohirno)
	{
		return $query->where(DB::raw('get_kohirno(id)'), array($kohirno));
	}

	// == RELATIONSHIPS == //

	/**
	 * Get the customer that owns the sptpd.
	 */
	public function customer()
	{
		return $this->belongsTo($this->getParentNamespace() . '\Customer');
	}

	/**
	 * Get the spt record associated with the sptpd.
	 */
	public function spt()
	{
		return $this->hasOne($this->getParentNamespace() . '\Spt');
	}

	/**
	 * Get the kohir record associated with the sptpd.
	 */
	public function kohir()
	{
		return $this->hasOne($this->getParentNamespace() . '\Kohir');
	}

	/**
	 * Get the rincian reklame record associated with the sptpd.
	 */
	public function rincianReklame()
	{
		return $this->hasMany($this->getParentNamespace() . '\SptpdRincianReklame', 'sptpd_id', 'id');
	}
}