public function actionCreate()
 {
     if (!Yii::app()->request->isAjaxRequest) {
         return;
     }
     if (isset($_POST) && !empty($_POST)) {
         $status = false;
         $msg = 'Anggaran berhasil disimpan.';
         $user = Yii::app()->user->getId();
         $id = -1;
         //require_once(Yii::app()->basePath . '/vendors/frontaccounting/ui.inc');
         app()->db->autoCommit = false;
         $transaction = app()->db->beginTransaction();
         try {
             $ref = new PeReferenceCom();
             $docref = $ref->get_next_reference(AKTIVITAS);
             $aktivitas = new PeAktivitas();
             foreach ($_POST as $k => $v) {
                 if ($k == 'amount') {
                     $v = get_number($v);
                 }
                 $_POST['PeAktivitas'][$k] = $v;
             }
             $date = $_POST['PeAktivitas']['trans_date'];
             $_POST['PeAktivitas']['entry_time'] = Now();
             $_POST['PeAktivitas']['users_id'] = $user;
             $_POST['PeAktivitas']['doc_ref'] = $docref;
             $aktivitas->attributes = $_POST['PeAktivitas'];
             if (!$aktivitas->save()) {
                 throw new Exception("Gagal menyimpan aktivitas.");
             }
             $id = $docref;
             $ref->save(AKTIVITAS, $aktivitas->aktivitas_id, $docref);
             $bank_account = Pe::get_act_code_from_bank_act($aktivitas->pe_bank_accounts_id);
             $act_sub = $aktivitas->peSubAktivitas->account_code;
             //debet kode beban - kredit kas bank
             Pe::add_gl(AKTIVITAS, $aktivitas->aktivitas_id, $date, $docref, $act_sub, $aktivitas->note, $aktivitas->amount, $user);
             Pe::add_gl(AKTIVITAS, $aktivitas->aktivitas_id, $date, $docref, $bank_account, '-', -$aktivitas->amount, $user);
             $transaction->commit();
             $status = true;
         } catch (Exception $ex) {
             $transaction->rollback();
             $status = false;
             $msg = $ex;
         }
         echo CJSON::encode(array('success' => $status, 'id' => $id, 'msg' => $msg));
         Yii::app()->end();
     }
 }
 public function actionCreateTransfer()
 {
     if (!Yii::app()->request->isAjaxRequest) {
         return;
     }
     if (isset($_POST) && !empty($_POST)) {
         //require_once(Yii::app()->basePath . '/vendors/frontaccounting/ui.inc');
         $status = false;
         $msg = 'Transfer antar kas berhasil diproses.';
         $id = -1;
         $bank_asal = $_POST['bank_act_asal'];
         $bank_tujuan = $_POST['bank_act_tujuan'];
         $trans_date = $_POST['trans_date'];
         $memo = $_POST['memo'];
         $amount = get_number($_POST['amount']);
         if ($bank_asal == $bank_tujuan) {
             echo CJSON::encode(array('success' => false, 'id' => -1, 'msg' => 'Bank asal dan bank tujuan tidak boleh sama.'));
             Yii::app()->end();
         }
         if ($amount <= 0) {
             echo CJSON::encode(array('success' => false, 'id' => -1, 'msg' => 'Jumlah yang akan ditransfer harus lebih dari nol.'));
             Yii::app()->end();
         }
         app()->db->autoCommit = false;
         $transaction = app()->db->beginTransaction();
         try {
             $ref = new PeReferenceCom();
             $docref = $ref->get_next_reference(BANKTRANSFER);
             $bank_account_asal = Pe::get_act_code_from_bank_act($bank_asal);
             $bank_account_tujuan = Pe::get_act_code_from_bank_act($bank_tujuan);
             $trans_no = Pe::get_next_trans_no_bank_trans();
             $user = Yii::app()->user->getId();
             //debet kode bank tujuan - kredit kode bank asal
             Pe::add_gl(BANKTRANSFER, $trans_no, $trans_date, $docref, $bank_account_tujuan, $memo, $amount, $user);
             Pe::add_gl(BANKTRANSFER, $trans_no, $trans_date, $docref, $bank_account_asal, $memo, -$amount, $user);
             $ref->save(BANKTRANSFER, $trans_no, $docref);
             $id = $docref;
             $transaction->commit();
             $status = true;
         } catch (Exception $ex) {
             $transaction->rollback();
             $status = false;
             $msg = $ex;
         }
         echo CJSON::encode(array('success' => $status, 'id' => $id, 'msg' => $msg));
         Yii::app()->end();
     }
 }
 public function actionCreate()
 {
     if (!Yii::app()->request->isAjaxRequest) {
         return;
     }
     if (isset($_POST) && !empty($_POST)) {
         $status = false;
         $msg = 'Anggaran berhasil disimpan.';
         $bulanStr = $_POST['bulanStr'];
         $bulan = $_POST['periode_bulan'];
         $tahun = $_POST['periode_tahun'];
         $detils = CJSON::decode($_POST['detil']);
         //require_once(Yii::app()->basePath . '/vendors/frontaccounting/ui.inc');
         app()->db->autoCommit = false;
         $transaction = app()->db->beginTransaction();
         try {
             $anggaran = new PeAnggaran();
             $ref = new PeReferenceCom();
             $docref = $ref->get_next_reference(ANGGARAN);
             $_POST['PeAnggaran']['doc_ref'] = $docref;
             $_POST['PeAnggaran']['periode_bulan'] = $bulan;
             $_POST['PeAnggaran']['periode_tahun'] = $tahun;
             $_POST['PeAnggaran']['lock'] = 0;
             $_POST['PeAnggaran']['trans_date'] = Yii::app()->dateFormatter->format('yyyy-MM-dd', time());
             $_POST['PeAnggaran']['users_id'] = Yii::app()->user->getId();
             $anggaran->attributes = $_POST['PeAnggaran'];
             if (!$anggaran->save()) {
                 throw new Exception("Gagal menyimpan anggaran.");
             }
             foreach ($detils as $detil) {
                 $anggaran_detil = new PeAnggaranDetil();
                 $_POST['PeAnggaranDetil']['account_code'] = $detil['account_code'];
                 $_POST['PeAnggaranDetil']['amount'] = $detil['amount'];
                 $_POST['PeAnggaranDetil']['anggaran_id'] = $anggaran->id;
                 $anggaran_detil->attributes = $_POST['PeAnggaranDetil'];
                 //  $ag_detil[] = $anggaran_detil;
                 if (!$anggaran_detil->save()) {
                     throw new Exception("Gagal menyimpan anggaran_detil.");
                 }
             }
             //$anggaran->peAnggaranDetils = $ag_detil;
             $ref->save(ANGGARAN, $anggaran->id, $docref);
             $transaction->commit();
             $status = true;
         } catch (Exception $ex) {
             $transaction->rollback();
             $status = false;
             $msg = $ex;
         }
         //            $anggaran->withRelated->save(true,array('peAnggaranDetils'));
         echo CJSON::encode(array('success' => $status, 'bulan' => $bulanStr, 'tahun' => $tahun, 'id' => $docref, 'msg' => $msg));
         Yii::app()->end();
     }
 }