CustomerUsaha.php 1.49 KB
<?php

/**
 * @Author: irul
 * @Date:   2019-09-20 21:39:58
 * @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 CustomerUsaha extends AbstractModel
{
	protected $table   = 'pad_customer_usaha';
	protected $visible = array(
		'id',
		'customer_id',
		'usaha_id',
		'konterid',
		'reg_date',
		'kecamatan_id',
		'kelurahan_id',
		'opnm',
		'opalamat',
		'pnama',
		'palamat',
		'pkelurahan',
		'pkecamatan',
		'pkabupaten',
		'ptelp',
		'pkodepos',
		'nopd',
	);
	protected $appends = array('nopd');
	protected $guarded = array();

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

	public function _onCreating()
	{
		// $maxKonterid = CustomerUsaha::select(DB::raw('max(konterid) as nomor'))
		$maxKonterid = call_user_func($this->getParentNamespace() . '\CustomerUsaha::select',
			DB::raw('max(konterid) as nomor'))
			->where(DB::raw('customer_id'), trim($this->attributes['customer_id']))
			->first();
		//  ->toSql();
		$konterid = ($maxKonterid->nomor ?: 0) + 1;

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

		return true;
	}

	// == ACCESSORS == //

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