public function actionCreate()
 {
     $model = new fJournal('cashbank');
     //$this->performAjaxValidation($model);
     $_myDebit = 0;
     $_myCredit = 0;
     $_myBalance = 0;
     $model->balance = "NOT OK";
     if (isset($_POST['account_no_id'])) {
         $model->attributes = $_POST['fJournal'];
         $model->validate();
         if (isset($_POST['fJournal']['cb_receiver'])) {
             //Expense
             $model->account_no_id = $_POST['account_no_id'];
             $model->debit = $_POST['debit'];
             $model->credit = $_POST['credit'];
             $model->user_remark = $_POST['user_remark'];
             foreach ($model->debit as $_debit) {
                 $_myDebit = $_myDebit + $_debit;
             }
             foreach ($model->credit as $_credit) {
                 $_myCredit = $_myCredit + $_credit;
             }
             $_myBalance = $_myDebit - $_myCredit;
             $model->balance = "OK";
             if ($model->validate()) {
                 $modelHeader = new uJournal();
                 $modelHeader->input_date = $_POST['fJournal']['input_date'];
                 $modelHeader->yearmonth_periode = Yii::app()->settings->get("System", "cCurrentPeriod");
                 $modelHeader->remark = $_POST['fJournal']['remark'];
                 $modelHeader->user_ref = $_POST['fJournal']['cb_receiver'];
                 $modelHeader->entity_id = sUser::model()->getGroup();
                 //default Group
                 $modelHeader->module_id = 2;
                 //CB
                 $modelHeader->state_id = 1;
                 $modelHeader->journal_type_id = 2;
                 //CB-expense
                 $modelHeader->save();
                 $_tdebet = 0;
                 $_tcredit = 0;
                 for ($i = 0; $i < sizeof($model->account_no_id); ++$i) {
                     $modelDetail = new uJournalDetail();
                     $modelDetail->parent_id = $modelHeader->id;
                     $modelDetail->account_no_id = $model->account_no_id[$i];
                     if ($model->debit[$i] != null) {
                         $modelDetail->debit = $model->debit[$i];
                     } else {
                         $modelDetail->debit = 0;
                     }
                     if ($model->credit[$i] != null) {
                         $modelDetail->credit = $model->credit[$i];
                     } else {
                         $modelDetail->credit = 0;
                     }
                     $modelDetail->user_remark = $model->user_remark[$i];
                     $modelDetail->save();
                 }
                 $modelDetail = new uJournalDetail();
                 $modelDetail->parent_id = $modelHeader->id;
                 $modelDetail->account_no_id = $_POST['fJournal']['var_account'];
                 $modelDetail->debit = 0;
                 $modelDetail->credit = $_myBalance;
                 $modelDetail->system_remark = "Automated by System";
                 $modelDetail->user_remark = $model->user_remark[0];
                 $modelDetail->save();
                 //Create System_ref
                 $_ref = "CB-" . $modelDetail->account->cashbank->mtext . "-" . $modelHeader->yearmonth_periode . "-EXP-" . str_pad($modelHeader->id, 5, "0", STR_PAD_LEFT);
                 $modelHeader->updateByPk((int) $modelHeader->id, array('system_ref' => $_ref));
                 Yii::app()->user->setFlash("success", "Journal created succesfully...");
                 $this->redirect(array('/mCashbank'));
             }
             $this->render('create', array('model' => $model));
             Yii::app()->end();
         } else {
             //Income
             $model->account_no_id = $_POST['account_no_id'];
             $model->debit = $_POST['debit'];
             $model->credit = $_POST['credit'];
             $model->user_remark = $_POST['user_remark'];
             foreach ($model->debit as $_debit) {
                 $_myDebit = $_myDebit + $_debit;
             }
             foreach ($model->credit as $_credit) {
                 $_myCredit = $_myCredit + $_credit;
             }
             $_myBalance = $_myCredit - $_myDebit;
             $model->balance = "OK";
             if ($model->validate()) {
                 $modelHeader = new uJournal();
                 $modelHeader->input_date = $_POST['fJournal']['input_date'];
                 $modelHeader->yearmonth_periode = Yii::app()->settings->get("System", "cCurrentPeriod");
                 $modelHeader->remark = $_POST['fJournal']['remark'];
                 $modelHeader->user_ref = $_POST['fJournal']['cb_received_from'];
                 $modelHeader->entity_id = sUser::model()->getGroup();
                 //default Group
                 $modelHeader->module_id = 2;
                 //CB
                 $modelHeader->state_id = 1;
                 $modelHeader->journal_type_id = 1;
                 //CB-Income
                 $modelHeader->save();
                 $modelDetail = new uJournalDetail();
                 $modelDetail->parent_id = $modelHeader->id;
                 $modelDetail->account_no_id = $_POST['fJournal']['var_account'];
                 $modelDetail->debit = $_myBalance;
                 $modelDetail->credit = 0;
                 $modelDetail->system_remark = "Automated by System";
                 $modelDetail->user_remark = $model->user_remark[0];
                 $modelDetail->save();
                 //Create System_ref
                 $_ref = "CB-" . $modelDetail->account->cashbank->mtext . "-" . $modelHeader->yearmonth_periode . "-INC-" . str_pad($modelHeader->id, 5, "0", STR_PAD_LEFT);
                 $modelHeader->updateByPk((int) $modelHeader->id, array('system_ref' => $_ref));
                 $_tdebet = 0;
                 $_tcredit = 0;
                 for ($i = 0; $i < sizeof($model->account_no_id); ++$i) {
                     $modelDetail = new uJournalDetail();
                     $modelDetail->parent_id = $modelHeader->id;
                     $modelDetail->account_no_id = $model->account_no_id[$i];
                     if ($model->debit[$i] != null) {
                         $modelDetail->debit = $model->debit[$i];
                     } else {
                         $modelDetail->debit = 0;
                     }
                     if ($model->credit[$i] != null) {
                         $modelDetail->credit = $model->credit[$i];
                     } else {
                         $modelDetail->credit = 0;
                     }
                     $modelDetail->user_remark = $model->user_remark[$i];
                     $modelDetail->save();
                 }
                 Yii::app()->user->setFlash("success", "Journal created succesfully... View Jurnal: " . CHtml::link($modelHeader->system_ref, Yii::app()->createUrl("/mCashbank/view", array("id" => $modelHeader->id))));
                 $this->redirect(array('/mCashbank'));
             }
             $this->render('create', array('model' => $model));
             Yii::app()->end();
         }
     }
     $this->render('create', array('model' => $model));
 }
 public function actionCreate()
 {
     $model = new fJournal();
     //$this->performAjaxValidation($model);
     //default value
     $_myDebit = 0;
     $_myCredit = 0;
     $model->balance = "NOT OK";
     if (isset($_POST['account_no_id'])) {
         $model->attributes = $_POST['fJournal'];
         $model->account_no_id = $_POST['account_no_id'];
         $model->debit = $_POST['debit'];
         $model->credit = $_POST['credit'];
         $model->user_remark = $_POST['user_remark'];
         foreach ($model->debit as $_debit) {
             $_myDebit = $_myDebit + $_debit;
         }
         foreach ($model->credit as $_credit) {
             $_myCredit = $_myCredit + $_credit;
         }
         if ($_myDebit == $_myCredit && $_myDebit != 0 && $_myCredit != 0) {
             $model->balance = "OK";
         } else {
             $model->balance = "NOT OK";
         }
         if ($model->validate()) {
             $modelHeader = new uJournal();
             $modelHeader->input_date = $_POST['fJournal']['input_date'];
             $modelHeader->yearmonth_periode = Yii::app()->settings->get("System", "cCurrentPeriod");
             $modelHeader->remark = $_POST['fJournal']['remark'];
             $modelHeader->entity_id = 1;
             $modelHeader->module_id = 1;
             //GL
             $modelHeader->state_id = 1;
             $modelHeader->journal_type_id = 1;
             $modelHeader->save();
             for ($i = 0; $i < sizeof($model->account_no_id); ++$i) {
                 $modelDetail = new uJournalDetail();
                 $modelDetail->parent_id = $modelHeader->id;
                 $modelDetail->account_no_id = $model->account_no_id[$i];
                 if ($model->debit[$i] != null) {
                     $modelDetail->debit = $model->debit[$i];
                 } else {
                     $modelDetail->debit = 0;
                 }
                 if ($model->credit[$i] != null) {
                     $modelDetail->credit = $model->credit[$i];
                 } else {
                     $modelDetail->credit = 0;
                 }
                 $modelDetail->user_remark = $model->user_remark[$i];
                 $modelDetail->sub_account_id = 0;
                 $modelDetail->save();
             }
             //Create System_ref
             $_ref = "GL-" . $modelHeader->yearmonth_periode . "-" . str_pad($modelHeader->id, 5, "0", STR_PAD_LEFT);
             $modelHeader->updateByPk((int) $modelHeader->id, array('system_ref' => $_ref));
             Yii::app()->user->setFlash("success", "<strong>Great!</strong> Journal created succesfully...");
             $this->redirect(array('/m2/uJournal'));
         }
     }
     $this->render('create', array('model' => $model));
 }
 public function actionJournalPayment()
 {
     if (isset($_POST['journal_id'])) {
         $total = 0;
         $m_ref = array();
         foreach ($_POST['journal_id'] as $a => $val) {
             $model = vPorder::model()->findByPk((int) $val);
             $total = $total + $model->sum_po;
             $m_ref[] = $model->system_ref;
             if ($model->journal_state_id == 3) {
                 Yii::app()->user->setFlash("error", "<strong>Error!</strong> This PO already journalled...");
                 $this->redirect(array('/m2/mAccpayable', 'id' => 3));
             } else {
                 vPorder::model()->updateByPk((int) $val, array('journal_state_id' => 3));
             }
         }
         $modelHeader = new uJournal();
         $modelHeader->input_date = Yii::app()->dateFormatter->format("dd-MM-yyyy", time());
         $modelHeader->yearmonth_periode = Yii::app()->settings->get("System", "cCurrentPeriod");
         $modelHeader->remark = implode($m_ref, " ");
         $modelHeader->entity_id = sUser::model()->getGroup();
         //default group
         $modelHeader->module_id = 3;
         //AP
         $modelHeader->journal_type_id = 2;
         //Payment
         $modelHeader->state_id = 1;
         $modelHeader->created_by = Yii::app()->user->id;
         $modelHeader->created_date = time();
         $modelHeader->save();
         //Create System_ref
         $_ref = "AP-" . $modelHeader->yearmonth_periode . "-" . str_pad($modelHeader->id, 5, "0", STR_PAD_LEFT);
         $modelHeader->updateByPk((int) $modelHeader->id, array('system_ref' => $_ref));
         $modelDetail = new uJournalDetail();
         $modelDetail->parent_id = $modelHeader->id;
         $_inventory = tAccount::model()->with('hutang')->find('hutang.mvalue=1')->id;
         $modelDetail->account_no_id = $_inventory;
         $modelDetail->debit = $total;
         $modelDetail->credit = 0;
         $modelDetail->user_remark = implode($m_ref, " ");
         $modelDetail->save();
         $modelPayment = vPorderPayment::model()->findAll('parent_id = ' . $model->id);
         foreach ($modelPayment as $payment) {
             $modelDetail = new uJournalDetail();
             $modelDetail->parent_id = $modelHeader->id;
             $modelDetail->account_no_id = $payment->payment_source_id;
             $modelDetail->debit = 0;
             $modelDetail->credit = $payment->amount;
             $modelDetail->user_remark = implode($m_ref, " ");
             $modelDetail->save();
         }
         //in case ada selisih
         if ($payment->amount != $total) {
             $modelDetail = new uJournalDetail();
             $modelDetail->parent_id = $modelHeader->id;
             $_inventory = tAccount::model()->with('hutang')->find('hutang.mvalue=1')->id;
             $modelDetail->account_no_id = $_inventory;
             $modelDetail->debit = $payment->amount - $total;
             $modelDetail->credit = 0;
             $modelDetail->user_remark = 'Correction: ' . implode($m_ref, " ");
             $modelDetail->save();
         }
         Yii::app()->user->setFlash("success", "<strong>Great!</strong> Payment Journal created succesfully...");
         $this->render('viewJournal', array('model' => $modelHeader));
     } else {
         $this->redirect(array('/m2/mAccpayable'));
     }
 }