/** * 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]); }
protected function createInvoice($params) { $invoice = new Invoice(); $invoice->id_vendor = $params['id_vendor']; $invoice->invoice_date = $params['date']; $invoice->invoice_value = $params['value']; $invoice->invoice_type = $params['invoice_type']; $invoice->due_date = date('Y-m-d', strtotime('+1 month')); $invoice->status = 0; if (!$invoice->save()) { throw new UserException(implode("\n", $invoice->getFirstErrors())); } $invDtl = new InvoiceDtl(); $invDtl->id_invoice = $invoice->id_invoice; $invDtl->id_reff = $params['id_ref']; $invDtl->trans_value = $params['value']; if (!$invDtl->save()) { throw new UserException(implode("\n", $invDtl->getFirstErrors())); } }
public function search($params) { $query = InvoiceModel::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { $query->where('1=0'); return $dataProvider; } $query->andFilterWhere(['id_invoice' => $this->id_invoice, 'invoice_type' => $this->invoice_type, 'invoice_date' => $this->invoice_date, 'due_date' => $this->due_date, 'id_vendor' => $this->id_vendor, 'status' => $this->status, 'create_by' => $this->create_by, 'update_by' => $this->update_by]); $query->andFilterWhere(['like', 'invoice_num', $this->invoice_num])->andFilterWhere(['like', 'invoice_value', $this->invoice_value])->andFilterWhere(['like', 'create_at', $this->create_at])->andFilterWhere(['like', 'update_at', $this->update_at]); return $dataProvider; }
public function search($params) { $query = InvoiceModel::find()->leftJoin('{{%payment_dtl}}', '{{%payment_dtl.id_invoice}}={{%invoice.id_invoice}}')->groupBy('{{%invoice.id_invoice}}')->having('{{%invoice.invoice_value}}>sum({{%payment_dtl.payment_value}})'); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { $query->where('1=0'); return $dataProvider; } $query->andFilterWhere(['id_invoice' => $this->id_invoice, 'invoice_type' => $this->invoice_type, 'invoice_date' => $this->invoice_date, 'due_date' => $this->due_date, 'id_vendor' => $this->id_vendor, 'status' => $this->status, 'create_by' => $this->create_by, 'update_by' => $this->update_by]); $query->andFilterWhere(['like', 'invoice_num', $this->invoice_num])->andFilterWhere(['like', 'invoice_value', $this->invoice_value])->andFilterWhere(['like', 'create_at', $this->create_at])->andFilterWhere(['like', 'update_at', $this->update_at]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getIdInvoice() { return $this->hasOne(Invoice::className(), ['id_invoice' => 'id_invoice']); }
public function createFromInvoice($data, $model = null) { $pay_vals = ArrayHelper::map($data['details'], 'id_invoice', 'value'); $ids = array_keys($pay_vals); $invoice_values = Invoice::find()->where(['id_invoice' => $ids])->indexBy('id_invoice')->asArray()->all(); $vendor = $inv_type = null; $vendors = $inv_types = []; foreach ($invoice_values as $row) { $vendor = $row['id_vendor']; $vendors[$vendor] = true; $inv_type = $row['invoice_type']; $inv_types[$inv_type] = true; } if (count($vendors) !== 1) { throw new UserException('Vendor harus sama'); } if (count($inv_types) !== 1) { throw new UserException('Type invoice harus sama'); } $invoice_paid = PaymentDtl::find()->select(['id_invoice', 'total' => 'sum(payment_value)'])->joinWith('idPayment')->where(['payment_type' => $inv_type, 'id_invoice' => $ids])->groupBy('id_invoice')->indexBy('id_invoice')->asArray()->all(); $data['id_vendor'] = $vendor; $data['payment_type'] = $inv_type; $details = []; foreach ($inv_vals as $id => $value) { $sisa = $invoice_values[$id]['invoice_value']; if (isset($invoice_paid[$id])) { $sisa -= $invoice_paid[$id]['total']; } if ($value > $sisa) { throw new UserException('Tagihan lebih besar dari sisa'); } $details[] = ['id_invoice' => $id, 'payment_value' => $value]; } $data['details'] = $details; return static::create($data, $model); }
public static function modelClass() { return MInvoice::className(); }
/** * Finds the Invoice model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Invoice the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Invoice::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getIdInvoices() { return $this->hasMany(Invoice::className(), ['id_invoice' => 'id_invoice'])->viaTable('payment_dtl', ['id_payment' => 'id_payment']); }