public function actionUnposting($id)
    {
        $_curPeriod = Yii::app()->settings->get("System", "cCurrentPeriod");
        $_lastPeriod = sParameter::cBeginDateBefore(Yii::app()->settings->get("System", "cCurrentPeriod"));
        $locked = uJournal::model()->updateByPk((int) $id, array('state_id' => 2, 'updated_date' => time(), 'updated_id' => Yii::app()->user->id));
        $models = uJournalDetail::model()->with('journal')->findAll(array('condition' => 'parent_id =' . $id));
        foreach ($models as $model) {
            $modelBalanceCurrent = tBalanceSheet::model()->find(array('condition' => 'parent_id =' . $model->account_no_id . ' AND yearmonth_periode = ' . $_curPeriod));
            $_debit = $model->debit;
            $_credit = $model->credit;
            $_curdebit = $modelBalanceCurrent->debit - $_debit;
            $_curcredit = $modelBalanceCurrent->credit - $_credit;
            $_curbalance = $modelBalanceCurrent->end_balance;
            if ($model->account->getSideValue() == 1 || isset($model->account->reverse)) {
                //Asset, Expense
                $_endbalance = $_curbalance - $_debit + $_credit;
            } else {
                //Pasiva, Income
                $_endbalance = $_curbalance - $_credit + $_debit;
            }
            $command = Yii::app()->db->createCommand('
					UPDATE  t_balance_sheet SET
					debit = ' . $_curdebit . ',
					credit = ' . $_curcredit . ',
					end_balance = ' . $_endbalance . '
					WHERE yearmonth_periode = ' . $_curPeriod . ' AND parent_id = ' . $model->account_no_id);
            $command->execute();
        }
    }
 function report($periode_date, $report_id)
 {
     $this->myheader($periode_date, $report_id);
     $w = array(130, 20);
     $model1 = tAccountMain::model()->with('account_list')->findAll('type_id= 1');
     $_labarugi = tAccount::labarugiDitahan($periode_date);
     $_s = 3;
     //Reset
     $_total = 0;
     $_subtotal = 0;
     $_grandtotal = 0;
     $_grandtotalA = 0;
     $_grandtotalP = 0;
     $_totalLast = 0;
     $_subtotalLast = 0;
     $_grandtotalLast = 0;
     $_grandtotalALast = 0;
     $_grandtotalPLast = 0;
     //Print/Preview Report
     foreach ($model1 as $mmm) {
         if ($mmm->id == 2) {
             //pasiva
             $_s = $_s + 3;
             $this->SetFont('Arial', 'B', 8);
             $this->Cell($w[0], 4, $mmm->name);
             $this->Cell($w[1], 4, '');
             $this->Ln();
         }
         foreach ($mmm->account_list as $mm) {
             $model2 = tAccount::model()->findByPk((int) $mm->parent_id);
             $this->SetFont('Arial', 'B', 8);
             if ($mmm->id == 2) {
                 $this->Cell(5, 4, '');
             }
             $this->Cell($w[0], 4, $model2->account_concat());
             $this->SetX($this->x0);
             $this->Cell($w[1], 4, '');
             $this->Ln();
             foreach ($model2->childs as $model) {
                 $this->SetFont('Arial', 'B', 8);
                 $this->Cell($_s, 4, '');
                 $this->Cell($w[0], 4, $model->account_concat());
                 $this->SetX($this->x0);
                 $this->Cell($w[1], 4, '');
                 $this->Ln();
                 if ($model->childs) {
                     foreach ($model->childs as $mod) {
                         if ($mod->childs) {
                             $this->SetFont('Arial', 'B', 8);
                         } else {
                             $this->SetFont('Arial', '', 8);
                         }
                         $this->Cell($_s + 3, 4, '');
                         $this->Cell($w[0], 4, $mod->account_concat());
                         $this->SetX($this->x0);
                         $_mod = $mod->balancesheet(array('condition' => 'yearmonth_periode =' . $periode_date));
                         $_periode_date_last = sParameter::cBeginDateBefore($periode_date);
                         $_modLast = $mod->balancesheet(array('condition' => 'yearmonth_periode =' . $_periode_date_last));
                         if (isset($_mod->end_balance)) {
                             $_balance = number_format($_mod->end_balance, 0, ',', '.');
                             $_mmodLast = isset($_modLast->end_balance) ? $_modLast->end_balance : 0;
                             $_balanceLast = number_format($_mmodLast, 0, ',', '.');
                             $_subtotal = $_subtotal + $_mod->end_balance;
                             $_grandtotal = $_grandtotal + $_mod->end_balance;
                             $_subtotalLast = $_subtotalLast + $_mmodLast;
                             $_grandtotalLast = $_grandtotalLast + $_mmodLast;
                         } else {
                             $_balance = 0;
                             $_balanceLast = 0;
                         }
                         if ($mod->childs) {
                             $this->Cell($w[1], 4, '', 0, 0, 'R');
                         } else {
                             $this->Cell($w[1], 4, $_balance, 0, 0, 'R');
                             $this->Cell($w[1] + 20, 4, $_balanceLast, 0, 0, 'R');
                         }
                         $this->Ln();
                         if ($mod->childs) {
                             foreach ($mod->childs as $m) {
                                 if ($m->childs) {
                                     $this->SetFont('Arial', 'B', 8);
                                 } else {
                                     $this->SetFont('Arial', '', 8);
                                 }
                                 $this->Cell($_s + 10, 4, '');
                                 $this->Cell($w[0], 4, $m->account_concat());
                                 $this->SetX($this->x0);
                                 $_m = $m->balancesheet(array('condition' => 'yearmonth_periode =' . $periode_date));
                                 $_mLast = $m->balancesheet(array('condition' => 'yearmonth_periode =' . $_periode_date_last));
                                 if (isset($_m->end_balance)) {
                                     $_balance = number_format($_m->end_balance, 0, ',', '.');
                                     $_mmLast = isset($_mLast->end_balance) ? $_mLast->end_balance : 0;
                                     $_balanceLast = number_format($_mmLast, 0, ',', '.');
                                     $_total = $_total + $_m->end_balance;
                                     $_subtotal = $_subtotal + $_m->end_balance;
                                     $_grandtotal = $_grandtotal + $_m->end_balance;
                                     $_totalLast = $_totalLast + $_mmLast;
                                     $_subtotalLast = $_subtotalLast + $_mmLast;
                                     $_grandtotalLast = $_grandtotalLast + $_mmLast;
                                 } else {
                                     $_balance = 0;
                                     $_balanceLast = 0;
                                 }
                                 if ($m->childs) {
                                     $this->Cell($w[1], 4, '', 0, 0, 'R');
                                 } else {
                                     $this->Cell($w[1], 4, $_balance, 0, 0, 'R');
                                     $this->Cell($w[1] + 20, 4, $_balanceLast, 0, 0, 'R');
                                 }
                                 $this->Ln();
                                 if ($m->childs) {
                                     ////////////////LAST
                                     foreach ($m->childs as $n) {
                                         if ($n->childs) {
                                             $this->SetFont('Arial', 'B', 8);
                                         } else {
                                             $this->SetFont('Arial', '', 8);
                                         }
                                         $this->Cell($_s + 15, 4, '');
                                         $this->Cell($w[0], 4, $n->account_concat());
                                         $this->SetX($this->x0);
                                         $_n = $n->balancesheet(array('condition' => 'yearmonth_periode =' . $periode_date));
                                         $_nLast = $n->balancesheet(array('condition' => 'yearmonth_periode =' . $_periode_date_last));
                                         if (isset($_n->end_balance)) {
                                             $_balance = number_format($_n->end_balance, 0, ',', '.');
                                             $_nnLast = isset($_nLast->end_balance) ? $_nLast->end_balance : 0;
                                             $_balanceLast = number_format($_nnLast, 0, ',', '.');
                                             $_total = $_total + $_n->end_balance;
                                             $_subtotal = $_subtotal + $_n->end_balance;
                                             $_grandtotal = $_grandtotal + $_n->end_balance;
                                             $_totalLast = $_totalLast + $_nnLast;
                                             $_subtotalLast = $_subtotalLast + $_nnLast;
                                             $_grandtotalLast = $_grandtotalLast + $_nnLast;
                                         } else {
                                             $_balance = 0;
                                             $_balanceLast = 0;
                                         }
                                         if ($n->childs) {
                                             $this->Cell($w[1], 4, '', 0, 0, 'R');
                                         } else {
                                             $this->Cell($w[1], 4, $_balance, 0, 0, 'R');
                                             $this->Cell($w[1] + 20, 4, $_balanceLast, 0, 0, 'R');
                                         }
                                         $this->Ln();
                                     }
                                     ///LAST
                                 }
                             }
                         }
                         if ($mod->childs && $mod->childsCount >= 2) {
                             $this->SetFont('Arial', 'B', 8);
                             $this->Cell($_s + 3, 4, '');
                             $this->Cell($w[0], 4, 'TOTAL ' . $mod->account_name);
                             $this->SetX($this->x0);
                             $this->Cell($w[1], 4, number_format($_total, 0, ',', '.'), 'T', 0, 'R');
                             $this->Cell($w[1], 4, '', 0);
                             $this->Cell($w[1], 4, number_format($_totalLast, 0, ',', '.'), 'T', 0, 'R');
                             $this->Ln(5);
                         }
                         if ($mod->childs) {
                             $_total = 0;
                             $_totalLast = 0;
                         }
                         if ($this->GetY() >= 250) {
                             $this->AddPage();
                             $this->myheader($periode_date, $report_id);
                         }
                     }
                 }
                 $this->SetFont('Arial', 'B', 8);
                 $this->Cell($_s, 4, '');
                 $this->Cell($w[0], 4, 'TOTAL ' . $model->account_name);
                 $this->SetX($this->x0 + 20);
                 $this->Cell($w[1], 4, number_format($_subtotal, 0, ',', '.'), 'T', 0, 'R');
                 $this->Cell($w[1], 4, '', 0);
                 $this->Cell($w[1], 4, number_format($_subtotalLast, 0, ',', '.'), 'T', 0, 'R');
                 $this->Ln(5);
                 $_subtotal = 0;
                 $_subtotalLast = 0;
                 if ($this->GetY() >= 250) {
                     $this->AddPage();
                     $this->myheader($periode_date, $report_id);
                 }
             }
             $this->SetFillColor(224, 224, 224);
             $this->SetFont('Arial', 'B', 8);
             if ($mmm->id == 2) {
                 $this->Cell(5, 4, '');
             }
             $this->Cell($w[0], 4, 'TOTAL ' . $model2->account_name, 0, 0, 'L', true);
             $this->SetX($this->x0 + 20);
             $this->Cell($w[1], 4, number_format($_grandtotal, 0, ',', '.'), 0, 0, 'R', true);
             $this->Cell($w[1] + 20, 4, number_format($_grandtotalLast, 0, ',', '.'), 0, 0, 'R', true);
             $this->Cell($w[1], 4, '');
             $this->Ln(8);
             if ($mmm->id == 1) {
                 //Aktiva
                 $_grandtotalA = $_grandtotal;
                 $_grandtotalALast = $_grandtotalLast;
             } else {
                 $_grandtotalP = $_grandtotalP + $_grandtotal;
                 $_grandtotalPLast = $_grandtotalPLast + $_grandtotalLast;
             }
             $_grandtotal = 0;
             $_grandtotalLast = 0;
         }
         if ($mmm->id == 2) {
             //Pasiva
             $_s = $_s + 3;
             $this->SetFont('Arial', 'B', 8);
             $this->Cell($w[0], 4, $mmm->name, 0, 0, 'L', true);
             $this->SetX($this->x0 + 20);
             $this->Cell($w[1], 4, number_format($_grandtotalP, 0, ',', '.'), 0, 0, 'R', true);
             $this->Cell($w[1] + 20, 4, number_format($_grandtotalPLast, 0, ',', '.'), 0, 0, 'R', true);
             $this->Ln();
         }
     }
 }
 function report($periode_date, $report_id)
 {
     $this->myheader($periode_date, $report_id);
     $w = array(100, 20);
     $model1 = tAccountMain::model()->with('account_list')->findAll('type_id= 2');
     $_s = 3;
     //Reset
     $_total = 0;
     $_subtotal = 0;
     $_grandtotal = 0;
     $_grandtotalI = 0;
     $_grandtotalH = 0;
     $_grandtotalE = 0;
     $_totalLast = 0;
     $_subtotalLast = 0;
     $_grandtotalLast = 0;
     $_grandtotalILast = 0;
     $_grandtotalHLast = 0;
     $_grandtotalELast = 0;
     //Print/Preview Report
     foreach ($model1 as $mmm) {
         foreach ($mmm->account_list as $mm) {
             $model2 = tAccount::model()->findByPk((int) $mm->parent_id);
             $this->SetFont('Arial', 'B', 8);
             if ($mmm->id == 2) {
                 $this->Cell(5, 4, '');
             }
             $this->Cell($w[0], 4, $model2->account_concat());
             $this->SetX($this->x0);
             $this->Cell($w[1], 4, '');
             $this->Ln();
             foreach ($model2->childs as $model) {
                 if ($model->childs) {
                     $this->SetFont('Arial', 'B', 8);
                 } else {
                     $this->SetFont('Arial', '', 8);
                 }
                 $this->Cell($_s, 4, '');
                 $this->Cell($w[0], 4, $model->account_concat());
                 $this->SetX($this->x0);
                 $_model = $model->balancesheet(array('condition' => 'yearmonth_periode =' . $periode_date));
                 $_periode_date_last = sParameter::cBeginDateBefore($periode_date);
                 $_modelLast = $model->balancesheet(array('condition' => 'yearmonth_periode =' . $_periode_date_last));
                 if (isset($_model->end_balance)) {
                     $_balance = number_format($_model->end_balance, 0, ',', '.');
                     $_mmodelLast = isset($_modelLast->end_balance) ? $_modelLast->end_balance : 0;
                     $_balanceLast = number_format($_mmodelLast, 0, ',', '.');
                     $_subtotal = $_subtotal + $_model->end_balance;
                     $_grandtotal = $_grandtotal + $_model->end_balance;
                     $_subtotalLast = $_subtotalLast + $_mmodelLast;
                     $_grandtotalLast = $_grandtotalLast + $_mmodelLast;
                 } else {
                     $_balance = 0;
                     $_balanceLast = 0;
                 }
                 if ($model->childs) {
                     $this->Cell($w[1], 4, '', 0, 0, 'R');
                 } else {
                     $this->Cell($w[1], 4, $_balance, 0, 0, 'R');
                     $this->Cell($w[1] + 20, 4, $_balanceLast, 0, 0, 'R');
                 }
                 $this->Ln();
                 if ($model->childs) {
                     foreach ($model->childs as $mod) {
                         if ($mod->childs) {
                             $this->SetFont('Arial', 'B', 8);
                         } else {
                             $this->SetFont('Arial', '', 8);
                         }
                         $this->Cell($_s + 3, 4, '');
                         $this->Cell($w[0], 4, $mod->account_concat());
                         $this->SetX($this->x0);
                         $_mod = $mod->balancesheet(array('condition' => 'yearmonth_periode =' . $periode_date));
                         $_modLast = $mod->balancesheet(array('condition' => 'yearmonth_periode =' . $_periode_date_last));
                         if (isset($_mod->end_balance)) {
                             $_balance = number_format($_mod->end_balance, 0, ',', '.');
                             $_mmodLast = isset($_modLast->end_balance) ? $_modLast->end_balance : 0;
                             $_balanceLast = number_format($_mmodLast, 0, ',', '.');
                             $_subtotal = $_subtotal + $_mod->end_balance;
                             $_grandtotal = $_grandtotal + $_mod->end_balance;
                             $_subtotalLast = $_subtotalLast + $_mmodLast;
                             $_grandtotalLast = $_grandtotalLast + $_mmodLast;
                         } else {
                             $_balance = 0;
                             $_balanceLast = 0;
                         }
                         if ($mod->childs) {
                             $this->Cell($w[1], 4, '', 0, 0, 'R');
                         } else {
                             $this->Cell($w[1], 4, $_balance, 0, 0, 'R');
                             $this->Cell($w[1] + 20, 4, $_balanceLast, 0, 0, 'R');
                         }
                         $this->Ln();
                         if ($mod->childs) {
                             foreach ($mod->childs as $m) {
                                 $this->SetFont('Arial', '', 8);
                                 $this->Cell($_s + 10, 4, '');
                                 $this->Cell($w[0], 4, $m->account_concat());
                                 $this->SetX($this->x0);
                                 $_m = $m->balancesheet(array('condition' => 'yearmonth_periode =' . $periode_date));
                                 $_mLast = $m->balancesheet(array('condition' => 'yearmonth_periode =' . $_periode_date_last));
                                 $_mmLast = isset($_mLast->end_balance) ? $_mLast->end_balance : 0;
                                 if (isset($_m->end_balance)) {
                                     $_balance = number_format($_m->end_balance, 0, ',', '.');
                                     $_balanceLast = number_format($_mmLast, 0, ',', '.');
                                     $_total = $_total + $_m->end_balance;
                                     $_subtotal = $_subtotal + $_m->end_balance;
                                     $_grandtotal = $_grandtotal + $_m->end_balance;
                                     $_totalLast = $_totalLast + $_mmLast;
                                     $_subtotalLast = $_subtotalLast + $_mmLast;
                                     $_grandtotalLast = $_grandtotalLast + $_mmLast;
                                 } else {
                                     $_balance = 0;
                                     $_balanceLast = 0;
                                 }
                                 if ($m->childs) {
                                     $this->Cell($w[1], 4, '', 0, 0, 'R');
                                 } else {
                                     $this->Cell($w[1], 4, $_balance, 0, 0, 'R');
                                     $this->Cell($w[1] + 20, 4, $_balanceLast, 0, 0, 'R');
                                 }
                                 $this->Ln();
                             }
                         }
                         if ($mod->childs) {
                             $this->SetFont('Arial', 'B', 8);
                             $this->Cell($_s + 3, 4, '');
                             $this->Cell($w[0], 4, 'TOTAL ' . $mod->account_name);
                             $this->SetX($this->x0);
                             $this->Cell($w[1], 4, number_format($_total, 0, ',', '.'), 'T', 0, 'R');
                             $this->Cell($w[1], 4, '', '');
                             $this->Cell($w[1], 4, number_format($_totalLast, 0, ',', '.'), 'T', 0, 'R');
                             $this->Ln(5);
                             $_total = 0;
                             $_totalLast = 0;
                             if ($this->GetY() >= 250) {
                                 $this->AddPage();
                                 $this->myheader($periode_date, $report_id);
                             }
                         }
                     }
                 }
                 if ($model->childs && $model->childsCount >= 2) {
                     $this->SetFont('Arial', 'B', 8);
                     $this->Cell($_s, 4, '');
                     $this->Cell($w[0], 4, 'TOTAL ' . $model->account_name);
                     $this->SetX($this->x0);
                     $this->Cell($w[1], 4, number_format($_subtotal, 0, ',', '.'), 'T', 0, 'R');
                     $this->Cell($w[1], 4, '', 0);
                     $this->Cell($w[1], 4, number_format($_subtotalLast, 0, ',', '.'), 'T', 0, 'R');
                     $this->Ln(5);
                 }
                 $_subtotal = 0;
                 $_subtotalLast = 0;
                 if ($this->GetY() >= 250) {
                     $this->AddPage();
                     $this->myheader($periode_date, $report_id);
                 }
             }
             $this->SetFont('Arial', 'B', 8);
             if ($mmm->id == 2) {
                 $this->Cell(5, 4, '');
             }
             $this->Cell($w[0], 4, 'TOTAL ' . $model2->account_name);
             $this->SetX($this->x0 + 20);
             $this->Cell($w[1], 4, number_format($_grandtotal, 0, ',', '.'), 0, 0, 'R');
             $this->Cell($w[1] + 20, 4, number_format($_grandtotalLast, 0, ',', '.'), 0, 0, 'R');
             $this->Cell($w[1], 4, '');
             $this->Ln(8);
             if ($mmm->id == 3) {
                 //income
                 $_grandtotalI = $_grandtotal;
                 $_grandtotalILast = $_grandtotalLast;
             } elseif ($mmm->id == 4) {
                 //HPP
                 $_grandtotalH = $_grandtotal;
                 $_grossprofit = $_grandtotalI - $_grandtotalH;
                 $_grandtotalHLast = $_grandtotalLast;
                 $_grossprofitLast = $_grandtotalILast - $_grandtotalHLast;
             } else {
                 //Expenses
                 $_grandtotalE = $_grandtotal;
                 $_netprofit = $_grossprofit - $_grandtotalE;
                 $_grandtotalELast = $_grandtotalLast;
                 $_netprofitLast = $_grossprofitLast - $_grandtotalELast;
             }
             $_grandtotal = 0;
             $_grandtotalLast = 0;
         }
         if ($mmm->id == 4) {
             //GROSS PROFIT
             //$_s=$_s+3;
             $this->SetFont('Arial', 'B', 8);
             $this->Cell(array_sum($w), 4, 'GROSS PROFIT', 'T');
             $this->SetX($this->x0 + 20);
             $this->Cell($w[1], 4, number_format($_grossprofit, 0, ',', '.'), 'T', 0, 'R');
             $this->Cell($w[1], 4, '');
             $this->Cell($w[1], 4, number_format($_grossprofitLast, 0, ',', '.'), 'T', 0, 'R');
             $this->Ln(8);
         }
     }
     $_s = $_s + 3;
     $this->SetFont('Arial', 'B', 8);
     $this->Cell(array_sum($w), 4, 'INCOME OPERATION', 'T');
     $this->SetX($this->x0 + 20);
     $this->Cell($w[1], 4, number_format($_netprofit, 0, ',', '.'), 'T', 0, 'R');
     $this->Cell($w[1] + 20, 4, number_format($_netprofitLast, 0, ',', '.'), 'T', 0, 'R');
     $this->Ln(8);
     //Other Income and Other Expenses
     $model1 = tAccountMain::model()->with('account_list')->findAll('type_id= 3');
     $_grandtotalOI = 0;
     $_grandtotalOE = 0;
     $_netprofitFinal = 0;
     $_grandtotalOILast = 0;
     $_grandtotalOELast = 0;
     $_netprofitFinalLast = 0;
     foreach ($model1 as $mmm) {
         foreach ($mmm->account_list as $mm) {
             $model2 = tAccount::model()->findByPk((int) $mm->parent_id);
             $this->SetFont('Arial', 'B', 8);
             if ($mmm->id == 2) {
                 $this->Cell(5, 4, '');
             }
             $this->Cell($w[0], 4, $model2->account_concat());
             $this->SetX($this->x0);
             $this->Cell($w[1], 4, '');
             $this->Ln();
             foreach ($model2->childs as $model) {
                 if ($model->childs) {
                     $this->SetFont('Arial', 'B', 8);
                 } else {
                     $this->SetFont('Arial', '', 8);
                 }
                 $this->Cell($_s, 4, '');
                 $this->Cell($w[0], 4, $model->account_concat());
                 $this->SetX($this->x0);
                 $_model = $model->balancesheet(array('condition' => 'yearmonth_periode =' . $periode_date));
                 $_periode_date_last = sParameter::cBeginDateBefore($periode_date);
                 $_modelLast = $model->balancesheet(array('condition' => 'yearmonth_periode =' . $_periode_date_last));
                 if (isset($_model->end_balance)) {
                     $_balance = number_format($_model->end_balance, 0, ',', '.');
                     $_mmodelLast = isset($_modelLast->end_balance) ? $_modelLast->end_balance : 0;
                     $_balanceLast = number_format($_mmodelLast, 0, ',', '.');
                     $_subtotal = $_subtotal + $_model->end_balance;
                     $_grandtotal = $_grandtotal + $_model->end_balance;
                     $_subtotalLast = $_subtotalLast + $_mmodelLast;
                     $_grandtotalLast = $_grandtotalLast + $_mmodelLast;
                 } else {
                     $_balance = 0;
                     $_balanceLast = 0;
                 }
                 if ($model->childs) {
                     $this->Cell($w[1], 4, '', 0, 0, 'R');
                 } else {
                     $this->Cell($w[1], 4, $_balance, 0, 0, 'R');
                     $this->Cell($w[1] + 20, 4, $_balanceLast, 0, 0, 'R');
                 }
                 $this->Ln();
                 if ($model->childs) {
                     foreach ($model->childs as $mod) {
                         if ($mod->childs) {
                             $this->SetFont('Arial', 'B', 8);
                         } else {
                             $this->SetFont('Arial', '', 8);
                         }
                         $this->Cell($_s + 3, 4, '');
                         $this->Cell($w[0], 4, $mod->account_concat());
                         $this->SetX($this->x0);
                         $_mod = $mod->balancesheet(array('condition' => 'yearmonth_periode =' . $periode_date));
                         $_modLast = $mod->balancesheet(array('condition' => 'yearmonth_periode =' . $_periode_date_last));
                         $_mmodLast = isset($_modLast->end_balance) ? $_modLast->end_balance : 0;
                         if (isset($_mod->end_balance)) {
                             if ($mod->reverse) {
                                 $_balanceR = -$_mod->end_balance;
                                 $_balanceRLast = -$_mmodLast;
                             } else {
                                 $_balanceR = $_mod->end_balance;
                                 $_balanceRLast = $_mmodLast;
                             }
                             $_balance = number_format($_balanceR, 0, ',', '.');
                             $_subtotal = $_subtotal + $_balanceR;
                             $_grandtotal = $_grandtotal + $_balanceR;
                             $_balanceLast = number_format($_balanceRLast, 0, ',', '.');
                             $_subtotalLast = $_subtotalLast + $_balanceRLast;
                             $_grandtotalLast = $_grandtotalLast + $_balanceRLast;
                         } else {
                             $_balance = 0;
                             $_balanceLast = 0;
                         }
                         if ($mod->childs) {
                             $this->Cell($w[1], 4, '', 0, 0, 'R');
                         } else {
                             $this->Cell($w[1], 4, $_balance, 0, 0, 'R');
                             $this->Cell($w[1] + 20, 4, $_balanceLast, 0, 0, 'R');
                         }
                         $this->Ln();
                         if ($mod->childs) {
                             foreach ($mod->childs as $m) {
                                 $this->SetFont('Arial', '', 8);
                                 $this->Cell($_s + 10, 4, '');
                                 $this->Cell($w[0], 4, $m->account_concat());
                                 $this->SetX($this->x0);
                                 $_m = $m->balancesheet(array('condition' => 'yearmonth_periode =' . $periode_date));
                                 $_mLast = $m->balancesheet(array('condition' => 'yearmonth_periode =' . $_periode_date_last));
                                 $_mmLast = isset($_mLast->end_balance) ? $_mLast->end_balance : 0;
                                 if (isset($_m->end_balance)) {
                                     if ($mod->reverse) {
                                         $_balanceR = -$_m->end_balance;
                                         $_balanceRLast = -$_mmLast;
                                     } else {
                                         $_balanceR = $_m->end_balance;
                                         $_balanceRLast = $_mmLast;
                                     }
                                     $_balance = number_format($_balanceR, 0, ',', '.');
                                     $_total = $_total + $_balanceR;
                                     $_subtotal = $_subtotal + $_balanceR;
                                     $_grandtotal = $_grandtotal + $_balanceR;
                                     $_balanceLast = number_format($_balanceRLast, 0, ',', '.');
                                     $_totalLast = $_totalLast + $_balanceRLast;
                                     $_subtotalLast = $_subtotalLast + $_balanceRLast;
                                     $_grandtotalLast = $_grandtotalLast + $_balanceRLast;
                                 } else {
                                     $_balance = 0;
                                     $_balanceLast = 0;
                                 }
                                 if ($m->childs) {
                                     $this->Cell($w[1], 4, '', 0, 0, 'R');
                                 } else {
                                     $this->Cell($w[1], 4, $_balance, 0, 0, 'R');
                                     $this->Cell($w[1], 4, $_balanceLast, 0, 0, 'R');
                                 }
                                 $this->Ln();
                             }
                         }
                         if ($mod->childs) {
                             $this->SetFont('Arial', 'B', 8);
                             $this->Cell($_s + 3, 4, '');
                             $this->Cell($w[0], 4, 'TOTAL ' . $mod->account_name);
                             $this->SetX($this->x0);
                             $this->Cell($w[1], 4, number_format($_total, 0, ',', '.'), 'T', 0, 'R');
                             $this->Cell($w[1], 4, '', 0);
                             $this->Cell($w[1], 4, number_format($_totalLast, 0, ',', '.'), 'T', 0, 'R');
                             $this->Ln(5);
                             $_total = 0;
                         }
                     }
                 }
                 if ($model->childs && $model->childsCount >= 2) {
                     $this->SetFont('Arial', 'B', 8);
                     $this->Cell($_s, 4, '');
                     $this->Cell($w[0], 4, 'TOTAL ' . $model->account_name);
                     $this->SetX($this->x0);
                     $this->Cell($w[1], 4, number_format($_subtotal, 0, ',', '.'), 'T', 0, 'R');
                     $this->Cell($w[1], 4, '', 0);
                     $this->Cell($w[1], 4, number_format($_subtotalLast, 0, ',', '.'), 'T', 0, 'R');
                     $this->Ln(5);
                 }
                 if ($_grandtotalOI == 0) {
                     //other Income
                     $_grandtotalOI = $_subtotal;
                     $_grandtotalOILast = $_subtotalLast;
                 } else {
                     //Expenses
                     $_grandtotalOE = $_subtotal;
                     $_grandtotalOELast = $_subtotalLast;
                 }
                 $_subtotal = 0;
                 $_subtotalLast = 0;
             }
             $this->SetFont('Arial', 'B', 8);
             if ($mmm->id == 2) {
                 $this->Cell(5, 4, '');
             }
             $this->Cell($w[0], 4, 'TOTAL ' . $model2->account_name);
             $this->SetX($this->x0 + 20);
             $this->Cell($w[1], 4, number_format($_grandtotal, 0, ',', '.'), 0, 0, 'R');
             $this->Cell($w[1], 4, '');
             $this->Cell($w[1], 4, number_format($_grandtotalLast, 0, ',', '.'), 0, 0, 'R');
             $this->Ln(8);
             $_grandtotalOIE = $_grandtotal;
             $_grandtotal = 0;
             $_grandtotalOIELast = $_grandtotalLast;
             $_grandtotalLast = 0;
         }
         $_netprofitFinal = $_netprofit + $_grandtotalOIE;
         $_netprofitFinalLast = $_netprofitLast + $_grandtotalOIELast;
         //$_s=$_s+3;
         $this->SetFont('Arial', 'B', 8);
         $this->Cell(array_sum($w), 4, 'NET INCOME', 'T');
         $this->SetX($this->x0 + 20);
         $this->Cell($w[1], 4, number_format($_netprofitFinal, 0, ',', '.'), 'T', 0, 'R');
         $this->Cell($w[1], 4, '');
         $this->Cell($w[1], 4, number_format($_netprofitFinalLast, 0, ',', '.'), 'T', 0, 'R');
         $this->Ln(8);
     }
 }
    public function actionUnposting($id)
    {
        $_curPeriod = Yii::app()->settings->get("System", "cCurrentPeriod");
        $_lastPeriod = sParameter::cBeginDateBefore(Yii::app()->settings->get("System", "cCurrentPeriod"));
        $locked = uJournal::model()->updateByPk((int) $id, array('state_id' => 2, 'updated_date' => time(), 'updated_by' => Yii::app()->user->id));
        $models = uJournalDetail::model()->with('journal')->findAll(array('condition' => 'parent_id = :id', 'params' => array(':id' => $id)));
        foreach ($models as $model) {
            $modelBalanceCurrent = tBalanceSheet::model()->find(array('condition' => 'parent_id = :accid AND yearmonth_periode = :period', 'params' => array(':accid' => $model->account_no_id, ':period' => $_curPeriod)));
            $_debit = $model->debit;
            $_credit = $model->credit;
            $_endbalance = 0;
            $_curdebit = $modelBalanceCurrent->debit - $_debit;
            $_curcredit = $modelBalanceCurrent->credit - $_credit;
            $_curbalance = $modelBalanceCurrent->end_balance;
            if ($model->account->getSideValue() == 1 || isset($model->account->reverse)) {
                //Asset, Expense
                $_endbalance = $_curbalance - $_debit + $_credit;
            } else {
                //Pasiva, Income
                $_endbalance = $_curbalance - $_credit + $_debit;
            }
            $command = Yii::app()->db->createCommand('
					UPDATE  t_balance_sheet SET
					debit = ' . $_curdebit . ',
					credit = ' . $_curcredit . ',
					end_balance = ' . $_endbalance . '
					WHERE yearmonth_periode = ' . $_curPeriod . ' AND parent_id = ' . $model->account_no_id);
            $command->execute();
            //LOG
            $commandLog = Yii::app()->db->createCommand('
					INSERT INTO t_balance_sheet_log (journal_id, yearmonth_periode, type_balance_id, remark, budget, account_no_id, beginning_balance,debit,credit,end_balance) VALUES (' . $model->parent_id . ',' . $_curPeriod . ', 1, \'UNPOSTING LOG\', 0,' . $model->account_no_id . ',' . $_curbalance . ',' . $_debit . ',' . $_credit . ',' . $_endbalance . ')
					');
            $commandLog->execute();
        }
    }