예제 #1
0
 /**
  * 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)) {
         return $this->redirect(['config']);
     }
     $model = new Sales();
     $model->status = Sales::STATUS_RELEASED;
     $model->date = date('Y-m-d');
     $error = false;
     $payments = [];
     if ($model->load(Yii::$app->request->post())) {
         $transaction = Yii::$app->db->beginTransaction();
         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]);
                     if ($movement && $movement->save()) {
                         $invoice = $movement->createInvoice();
                         if ($invoice && $invoice->save()) {
                             /* @var $payment Payment */
                             $success = true;
                             $total = 0;
                             $paymentData = ['vendor_id' => $invoice->vendor_id, 'date' => date('Y-m-d'), 'type' => $invoice->type];
                             foreach ($payments as $payment) {
                                 $payment->attributes = $paymentData;
                                 $payment->status = Payment::STATUS_RELEASED;
                                 $payItems = $payment->items;
                                 $payItems[0]->invoice_id = $invoice->id;
                                 $total += $payItems[0]->value;
                                 $payment->items = $payItems;
                             }
                             if ($invoice->value == $total) {
                                 foreach ($payments as $i => $payment) {
                                     if (!$payment->save()) {
                                         $success = false;
                                         $firstErrors = $payment->firstErrors;
                                         $error = "Payment {$i}: " . reset($firstErrors);
                                         break;
                                     }
                                 }
                             } else {
                                 $success = false;
                                 $error = 'Total payment tidak sama dengan invoice';
                             }
                             if ($success) {
                                 $transaction->commit();
                                 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]);
 }
예제 #2
0
 /**
  * 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]);
 }