Anda di halaman 1dari 5

1.

Membuat database

2. Konfigurasi GII semua Table


3. Tambahkan script user
- Main.php => 'components'=>array(
'user'=>array(
// enable cookie-based authentication
'class' => 'WebUser',
'allowAutoLogin'=>true,
),
- userIdentity.php =>
private $_id;
public function authenticate()
{
$user = User::model()->find('username=:username AND password
= :password', array(
':username' => $this->username,
':password' => $this->password
));
if($user===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else {
$this->errorCode=self::ERROR_NONE;
$this->_id = $user->id;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}

tambahkan WebUser.php di components


<?php
class WebUser extends CWebUser
{
private function loadModel()
{
$model = User::model()->findByPk(Yii::app()->user->id);
if($model) {
return $model;
}
return false;
}
public function getLevel()
{
$model = $this->loadModel();
if($model) {
return $model->level;
}
return false;
}
}

4. ubah tampilan menu bar : views/layouts/main.php


5. Hak Akses
Dengan Ketentuan :

admin : all action


penjualan :
1. Semua action penjualan
2. Create barang, View barang, List barang
Staf
1. semua action barang
2. create supplier, view supplier, update supplier, list supplier

Menu akan tampil sesuai hak akses masing masing level


1. Admin -> Semua menu
2. Staf -> Barang & Supplier
3. Penjualan -> Penjualan & Barang

SCRIPT :
a. userController :
public function accessRules()
{
return array(
array('allow',
'expression' => 'Yii::app()->user->level=="admin"',
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
b. supplierController :
public function accessRules()
{
return array(
array('allow', // allow authenticated user to perform 'create' and
'update' actions
'actions'=>array('create','update', 'view', 'index'),
'expression' => 'Yii::app()->user->level == "staf"'
),
array('allow',
'expression' => 'Yii::app()->user->level == "admin"'
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
c. penjualanController :
public function accessRules()
{
return array(
array('allow',
'expression' => 'Yii::app()->user->level == "admin" ||
Yii::app()->user->level == "penjualan"'
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}

d. barangController :
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view'
actions
'actions'=>array('index','view', 'create'),
'expression' => 'Yii::app()->user->level=="penjualan"'
),
array('allow',
'expression' => 'Yii::app()->user->level == "admin" ||
Yii::app()->user->level == "staf"'
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
6. dropdown create user, penjualan, barang (sama):
views/user/_form.php
<?php echo $form->dropDownList($model,'level', CHtml::listData(User::model()>findAll(), 'level', 'username'), array(
'empty' => '=== Level ==='
)); ?>
7. (BARANG & PENJUALAN SAMA) menampilkan nama suplier di index, view,
admin :
index : barang/_view.php
<?php echo CHtml::encode($data->idSupplier->nama); ?>
view : barang/view.php
array(
'name' => 'id_supplier',
'value' => $model->idSupplier->nama
),
Admin : barang/admin.php
array(
'name' => 'id_supplier',
'value' => function($data) {
return $data->idSupplier->nama;
}),
8. Model Penjualan :
//tanggal Otomatis
protected function beforeValidate()
{
if($this->isNewRecord) {

$this->tanggal = date('Y-m-d');
}
return parent::beforeValidate();
}
//mengurangi stock barang otomatis
protected function afterSave()
{
if($this->isNewRecord) {
$this->idBarang->stok -= $this->quantity;
$this->idBarang->save();
}
parent::afterSave();
}
//delete otomatis
protected function afterDelete()
{
if($this->isNewRecord) {
$this->idBarang->stok += $this->quantity;
$this->idBarang->save();
}
parent::afterDelete();
}
9. Menghilangkan penjualan ketika barang di hapus, model barang
protected function afterDelete()
{
Penjualan::model()->deleteAll('id_barang = ' . $this->id);
parent::afterDelete();
}
10.
Menghilangkan barang ketika supplier di hapus, model supplier
protected function afterDelete()
{
$list_barang = Barang::model()->findAll('id_supplier = ' . $this->id);
foreach ($list_barang as $barang) {
$barang->delete();
Penjualan::model()->deleteAll('id_barang = ' . $barang->id);
}
parent::afterDelete();
}