/** * Creates a new Sales model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $profile = Yii::$app->profile; if (!isset($profile->branch_id, $profile->warehouse_id) || $profile->branch_id == '') { Yii::$app->getSession()->setFlash('_config_return_url', Yii::$app->getRequest()->getUrl()); return $this->redirect(['config']); } $model = new Sales(); $model->status = Sales::STATUS_RELEASED; $model->date = date('Y-m-d'); $model->branch_id = Yii::$app->profile->branch_id; $model->vendor_id = !isset(Yii::$app->params['default_cust']) ? Sales::DEFAULT_VENDOR : Yii::$app->params['default_cust']; $model->vendor_name = $model->vendor != null ? $model->vendor->name : ''; $error = false; $whse = $this->findWarehouse(Yii::$app->profile->warehouse_id); $payments = []; if ($model->load(Yii::$app->request->post())) { $transaction = Yii::$app->db->beginTransaction(); $model->branch_id = $profile->branch_id; try { $payments = Helper::createMultiple(Payment::className(), Yii::$app->request->post()); if (!empty($payments)) { $model->items = Yii::$app->request->post('SalesDtl', []); if ($model->save()) { $movement = $model->createMovement(['warehouse_id' => $profile->warehouse_id, 'description' => 'Sales Retail']); $glHeader = new GlHeader(['status' => GlHeader::STATUS_RELEASED, 'reff_type' => GlHeader::REFF_SALES, 'reff_id' => $model->id, 'date' => date('Y-m-d'), 'branch_id' => $model->branch_id, 'periode_id' => $this->findPeriode(), 'description' => "Sales POS [{$model->number}]"]); //Create Jurnal $coa_sales = ['penjualan' => 16, 'persediaan' => 32, 'hpp' => 19, 'diskon' => 18]; $tcogs = 0; $penjualan = 0; $diskon = 0; foreach ($model->items as $item) { $tcogs += $item->cogs * $item->qty * $item->productUom->isi; $penjualan += $item->price * $item->qty * $item->productUom->isi; $diskon += 0.01 * $item->discount * $item->qty * $item->productUom->isi * $item->price; } $glDetails = []; // Hpp(D) Vs Persediaan(K) $glDetails[] = ['coa_id' => $coa_sales['hpp'], 'amount' => $tcogs]; $glDetails[] = ['coa_id' => $coa_sales['persediaan'], 'amount' => -$tcogs]; if ($movement && $movement->save()) { $invoice = $movement->createInvoice(); if ($invoice && $invoice->save()) { /* @var $payment Payment */ $success = true; $total = 0; $invoiceTotal = $invoice->value; $model->reff_type = Sales::REFF_INVOICE; $model->reff_id = $invoice->id; $model->save(); $paymentData = ['vendor_id' => $invoice->vendor_id, 'date' => date('Y-m-d'), 'type' => $invoice->type]; $totalPaid = 0; foreach ($payments as $payment) { $payment->attributes = $paymentData; $payment->status = Payment::STATUS_RELEASED; $payItems = $payment->items; $payItems[0]->invoice_id = $invoice->id; if ($invoiceTotal - $total >= $payItems[0]->value) { $total += $payItems[0]->value; } else { $payItems[0]->value = $invoiceTotal - $total; $total = $invoiceTotal; } // payment $bayar = $payItems[0]->value * (1 - $payment->paymentMethod->potongan); $glDetails[] = ['coa_id' => $payment->paymentMethod->coa_id, 'amount' => $bayar]; // potongan payment method $potongan_cc = 0; if ($payment->paymentMethod->potongan > 0) { $potongan_cc = $payItems[0]->value * $payment->paymentMethod->potongan; $glDetails[] = ['coa_id' => $payment->paymentMethod->coa_id_potongan, 'amount' => $potongan_cc]; } $totalPaid += $bayar + $potongan_cc; $payment->items = $payItems; } // Penjualan(K) Vs [Payment(D) + Diskon(D)] if ($diskon != 0) { $glDetails[] = ['coa_id' => $coa_sales['diskon'], 'amount' => $diskon]; } $glDetails[] = ['coa_id' => $coa_sales['penjualan'], 'amount' => -1 * $penjualan]; if ($invoice->value >= $total) { foreach ($payments as $i => $payment) { if (!$payment->save()) { $success = false; $firstErrors = $payment->firstErrors; $error = "Payment {$i}: " . reset($firstErrors); break; } } if ($success) { $glHeader->glDetails = $glDetails; if (!$glHeader->save()) { $success = false; $firstErrors = $glHeader->firstErrors; $error = "Journal: " . reset($firstErrors); } } } else { $success = false; //seharusnya muncul cash back jika berlebih, bukan error $error = 'Kurang bayar'; } if ($success) { $transaction->commit(); //return $this->redirect(['create']); return $this->redirect(['view', 'id' => $model->id]); } } else { if ($invoice) { $firstErrors = $invoice->firstErrors; $error = "Invoice: " . reset($firstErrors); } else { $error = 'Cannot create invoice'; } } } else { if ($movement) { $firstErrors = $movement->firstErrors; $error = "GI: " . reset($firstErrors); } else { $error = 'Cannot create GI'; } } } } else { $error = "Payment can not empty"; } if ($error !== false) { $model->addError('related', $error); } } catch (\Exception $exc) { $transaction->rollBack(); throw $exc; } $transaction->rollBack(); } return $this->render('create', ['model' => $model, 'payments' => $payments, 'warehouse' => $whse->name]); }
public function actionTestEntriJournal() { $model = new EntriSheet(); $dPost = Yii::$app->request->post(); if (!empty($dPost)) { $model = EntriSheet::findOne($dPost['EntriSheet']['id']); $model->amount = $dPost['EntriSheet']['amount']; $newGl = new GlHeader(); $newGl->reff_type = 0; $newGl->reff_id = null; $newGl->date = date('Y-m-d'); $newDtls = []; foreach ($model->entriSheetDtls as $ddtl) { $ndtl = new \backend\models\accounting\GlDetail(); $ndtl->coa_id = $ddtl->coa_id; $ndtl->header_id = null; $ndtl->amount = $ddtl->dk == $ddtl::DK_CREDIT ? -1 * $model->amount : $model->amount; $newDtls[] = $ndtl; } $newGl->status = $newGl::STATUS_RELEASED; $activePeriode = \backend\models\accounting\AccPeriode::find()->active()->one(); $newGl->periode_id = $activePeriode->id; $newGl->branch_id = 1; $newGl->description = $model->name; $newGl->glDetails = $newDtls; if (!$newGl->save()) { // print_r($newGl->getErrors()); // print_r($newGl->getRelatedErrors()); return $this->redirect(['/accounting/general-ledger/view', 'id' => $model->id]); } } return $this->render('test', ['model' => $model]); }
/** * Deletes an existing GlHeader model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionReverse($id) { $oldGl = $this->findModel($id); $trans = \Yii::$app->db->beginTransaction(); try { $newGl = new GlHeader(); $newGl->attributes = $oldGl->attributes; $newGl->date = date('Y-m-d'); $newDtls = []; foreach ($oldGl->glDetails as $ddtl) { $ndtl = new \backend\models\accounting\GlDetail(); $ndtl->attributes = $ddtl->attributes; $ndtl->header_id = null; $ndtl->amount = -1 * $ddtl->amount; $newDtls[] = $ndtl; } $newGl->status = $newGl::STATUS_CANCELED; $newGl->reff_type = $newGl::REFF_JOURNAL; $newGl->reff_id = $oldGl->id; $newGl->number = null; $newGl->description = 'Reverse of ' . $oldGl->number; $newGl->glDetails = $newDtls; $newGl->created_at = null; $newGl->created_by = null; $newGl->updated_at = null; $newGl->updated_by = null; if ($newGl->save()) { $oldGl->status = $oldGl::STATUS_CANCELED; // $oldGl->reff_type = $oldGl::REFF_JOURNAL; // $oldGl->reff_id = $newGl->id; $oldGl->description = $oldGl->description . ' canceled by ' . $newGl->number; if ($oldGl->save()) { $trans->commit(); $this->redirect(['index']); } } else { print_r($newGl->getErrors()); print_r($newGl->getRelatedErrors()); } } catch (Exception $ex) { $trans->rollBack(); } return; //$this->redirect(['view','id'=>$id]); }
/** * * @return boolean */ public function postGL($factor) { /* * Header Journal */ $model_journal = new \backend\models\accounting\GlHeader(); $model_journal->periode_id = $this->findActivePeriode(); $model_journal->date = date('Y-m-d'); $model_journal->status = \backend\models\accounting\GlHeader::STATUS_RELEASED; $model_journal->reff_type = \backend\models\accounting\GlHeader::REFF_GOODS_MOVEMENT; $model_journal->reff_id = $this->id; $model_journal->branch_id = isset(Yii::$app->profile->branch_id) ? Yii::$app->profile->branch_id : -1; $esheet = null; switch ($this->reff_type) { case self::REFF_TRANSFER: $esheet = $this->type == self::TYPE_ISSUE ? EntriSheet::find()->where('code=:dcode', [':dcode' => 'ES004'])->one() : EntriSheet::find()->where('code=:dcode', [':dcode' => 'ES005'])->one(); break; default: $esheet = $factor == 1 ? EntriSheet::find()->where('code=:dcode', [':dcode' => 'ES001'])->one() : EntriSheet::find()->where('code=:dcode', [':dcode' => 'ES003'])->one(); } $model_journal->description = $esheet->name; /* * Detail Journal */ $newDtls = []; $ndtl = new \backend\models\accounting\GlDetail(); $ndtl->coa_id = $esheet->d_coa_id; $ndtl->header_id = null; $ndtl->amount = $this->totalValue; $newDtls[] = $ndtl; $ndtl1 = new \backend\models\accounting\GlDetail(); $ndtl1->coa_id = $esheet->k_coa_id; $ndtl1->header_id = null; $ndtl1->amount = $this->totalValue * -1; $newDtls[] = $ndtl1; $model_journal->glDetails = $newDtls; if (!$model_journal->save()) { print_r($model_journal->getErrors()); print_r($model_journal->getRelatedErrors()); return false; } return true; }