public function exportPdf($id, $kertas = ReturPembelian::KERTAS_A4, $draft = false)
 {
     $modelHeader = $this->loadModel($id);
     $configs = Config::model()->findAll();
     /*
      * Ubah config (object) jadi array
      */
     $branchConfig = array();
     foreach ($configs as $config) {
         $branchConfig[$config->nama] = $config->nilai;
     }
     /*
      * Data Supplier
      */
     $profil = Profil::model()->findByPk($modelHeader->profil_id);
     /*
      * Retur Pembelian Detail
      */
     $returPembelianDetail = ReturPembelianDetail::model()->with('inventoryBalance', 'inventoryBalance.barang')->findAll(array('condition' => "retur_pembelian_id={$id}", 'order' => 'barang.nama'));
     /*
      * Persiapan render PDF
      */
     $listNamaKertas = ReturPembelian::listNamaKertas();
     $mPDF1 = Yii::app()->ePdf->mpdf('', $listNamaKertas[$kertas]);
     $viewCetak = '_pdf';
     if ($draft) {
         $viewCetak = '_pdf_draft';
     }
     $mPDF1->WriteHTML($this->renderPartial($viewCetak, array('modelHeader' => $modelHeader, 'branchConfig' => $branchConfig, 'profil' => $profil, 'returPembelianDetail' => $returPembelianDetail), true));
     $mPDF1->SetDisplayMode('fullpage');
     $mPDF1->pagenumSuffix = ' dari ';
     $mPDF1->pagenumPrefix = 'Halaman ';
     // Render PDF
     $mPDF1->Output("{$modelHeader->nomor}.pdf", 'I');
 }
Example #2
0
 public function simpanReturPembelian()
 {
     $this->scenario = 'simpanReturBeli';
     $transaction = $this->dbConnection->beginTransaction();
     try {
         /*
          * Save sekaligus mengubah status dari draft jadi piutang
          */
         if ($this->save()) {
             $details = ReturPembelianDetail::model()->findAll("retur_pembelian_id={$this->id}");
             if (is_null($details)) {
                 throw new Exception("Tidak ada detail");
             }
             foreach ($details as $detail) {
                 $inventoryTerpakai = InventoryBalance::model()->returBeli($detail);
                 $count = 1;
                 foreach ($inventoryTerpakai as $layer) {
                     if ($count > 1) {
                         $detailBaru = new ReturPembelianDetail();
                         $detailBaru->retur_pembelian_id = $this->id;
                         $detailBaru->inventory_balance_id = $layer['id'];
                         $detailBaru->qty = $layer['qtyTerpakai'];
                         if (!$detailBaru->save()) {
                             throw new Exception('Gagal simpan retur pembelian detail (layer baru)', 500);
                         }
                     } else {
                         $detail->qty = $layer['qtyTerpakai'];
                         if (!$detail->save()) {
                             throw new Exception("Gagal simpan detail", 500);
                         }
                     }
                     $count++;
                 }
             }
             $jumlahReturBeli = $this->ambilTotal();
             /*
              * Create (piutang)
              */
             $hutang = new HutangPiutang();
             $hutang->profil_id = $this->profil_id;
             $hutang->jumlah = $jumlahReturBeli;
             $hutang->tipe = HutangPiutang::TIPE_PIUTANG;
             $hutang->asal = HutangPiutang::DARI_RETUR_BELI;
             $hutang->nomor_dokumen_asal = $this->nomor;
             if (!$hutang->save()) {
                 throw new Exception("Gagal simpan hutang");
             }
             /*
              * Hutang Detail
              */
             $hutangDetail = new HutangPiutangDetail();
             $hutangDetail->hutang_piutang_id = $hutang->id;
             $hutangDetail->keterangan = 'Retur Beli: ' . $this->nomor;
             $hutangDetail->jumlah = $jumlahReturBeli;
             if (!$hutangDetail->save()) {
                 throw new Exception("Gagal simpan hutang detail");
             }
             /*
              * Simpan hutang_id ke retur pembelian
              */
             if (!ReturPembelian::model()->updateByPk($this->id, array('hutang_piutang_id' => $hutang->id)) > 1) {
                 throw new Exception("Gagal simpan hutang_id");
             }
             $transaction->commit();
             return ['sukses' => true];
         } else {
             throw new Exception("Gagal Simpan Retur Pembelian");
         }
     } catch (Exception $ex) {
         $transaction->rollback();
         return ['sukses' => false, 'error' => ['msg' => $ex->getMessage(), 'code' => $ex->getCode()]];
     }
 }