/** * Creates a new Payment model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate($type, $ids) { $model = new Payment(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { try { $transaction = Yii::$app->db->beginTransaction(); $model->save(false); $details = $model->saveRelated('paymentDtl'); if ($details === true) { $transaction->commit(); return $this->redirect(['view', 'id' => $model->id_payment]); } else { $transaction->rollBack(); } } catch (\Exception $e) { $transaction->rollBack(); $model->addError('', $e->getMessage()); } } $query = Invoice::find()->where(['invoice_type' => $type, 'id_invoice' => explode(',', $ids)]); $jmlInv = (new Query())->select('sum({{dtl.trans_value}})')->from(['dtl' => '{{%invoice_dtl}}'])->innerJoin(['q' => $query], '{{q.id_invoice}}={{dtl.id_invoice}}')->scalar(); $jmlPaid = (new Query())->select('sum({{dtl.payment_value}})')->from(['dtl' => '{{%payment_dtl}}'])->innerJoin(['q' => $query], '{{q.id_invoice}}={{dtl.id_invoice}}')->scalar(); if (!isset($details)) { $details = []; foreach ($query->all() as $hdr) { $detail = new PaymentDtl(['id_invoice' => $hdr->id_invoice]); $details[] = $detail; } } return $this->render('create', ['model' => $model, 'details' => $details, 'type' => $type, 'jmlInv' => $jmlInv, 'jmlPaid' => $jmlPaid, 'jmlRemain' => $jmlInv - $jmlPaid]); }