Customer.php 3.02 KB
<?php

/**
 * @Author: irul
 * @Date:   2019-09-23 20:33:14
 * @Last Modified by:   irul
 * @Last Modified time: 2019-10-01 23:43:20
 */

namespace Integrasi\Reklame\Base\Models;

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

class Customer extends AbstractModel
{
	protected $table   = 'pad_customer';
	protected $visible = array(
		'id',
		'npwpd',
		'rp',
		'pb',
		'reg_date',
		'customernm',
		// 'kecamatan_id',
		'kecamatan',
		// 'kelurahan_id',
		'kelurahan',
		'kabupaten',
		'alamat',
		'kodepos',
		'telphone',
		'wpnama',
		'wpalamat',
		'wpkelurahan',
		'wpkecamatan',
		'wpkabupaten',
		'wptelp',
		'wpkodepos',
	);
	protected $appends = array(
		'kecamatan',
		'kelurahan',
	);
	protected $guarded = array();

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

	public function _onCreating($attr)
	{
		/**
		 * Increment formno.
		 */
		// $maxFormno = Customer::select(DB::raw('max(formno) as nomor'))
		$maxFormno = call_user_func($this->getParentNamespace() . '\Customer::select',
			DB::raw('max(formno) as nomor'))
			->where(DB::raw('rp'), trim($this->attributes['rp']))
			->first();
		$formno = ($maxFormno->nomor ?: 0) + 1;

		$this->attributes['formno']     = $formno;
		$this->attributes['create_uid'] = \Api\AuthBasic::getUid();

		/**
		 * Get kecamatannm
		 */
		// $attr->wpkecamatan = trim(Kecamatan::find($attr->wpkecamatan_id)->kecamatannm);
		$attr->wpkecamatan = trim(call_user_func($this->getParentNamespace() . '\Kecamatan::find',
			$attr->wpkecamatan_id)->kecamatannm);

		/**
		 * Get kelurahannm
		 */
		// $attr->wpkelurahan = trim(Kelurahan::find($attr->wpkelurahan_id)->kelurahannm);
		$attr->wpkelurahan = trim(call_user_func($this->getParentNamespace() . '\Kelurahan::find',
			$attr->wpkelurahan_id)->kelurahannm);

		/**
		 * Unset wpkecamatan_id dan wpkelurahan_id
		 * karena tidak ada fieldnya pada table pad_customer
		 */
		unset($attr->wpkecamatan_id);
		unset($attr->wpkelurahan_id);

		return true;
	}

	// == ACCESSORS == //

	public function getNpwpdAttribute($value)
	{
		// return strtolower($value);
		$qry = "select get_npwpd(?, false) as npwpd";
		$row = DB::select(DB::raw($qry), array($this->attributes['id']));
		return $row[0]->npwpd;
	}

	public function getKecamatanAttribute($value)
	{
		// $row = Kecamatan::find($this->attributes['kecamatan_id']);
		$row = call_user_func($this->getParentNamespace() . '\Kecamatan::find',
			$this->attributes['kecamatan_id']);
		return $row->kecamatannm;
	}

	public function getKelurahanAttribute($value)
	{
		// $row = Kelurahan::find($this->attributes['kelurahan_id']);
		$row = call_user_func($this->getParentNamespace() . '\Kelurahan::find',
			$this->attributes['kelurahan_id']);
		return $row->kelurahannm;
	}

	// == QUERY SCOPES == //

	public function scopeFindByNpwpd($query, $npwpd)
	{
		// $data = Customer::where(DB::raw('get_npwpd(id, false)'), array($npwpd))->toSql();
		// print_r($data); die();
		return $query->where(DB::raw('get_npwpd(id, false)'), array($npwpd));
	}
}