Beispiel #1
0
 public function simpan()
 {
     $this->scenario = 'simpan';
     $transaction = $this->dbConnection->beginTransaction();
     try {
         /*
          * Save sekaligus mengubah status dari draft jadi piutang
          */
         if ($this->save()) {
             $details = ReturPenjualanDetail::model()->findAll("retur_penjualan_id={$this->id}");
             foreach ($details as $detail) {
                 /*
                  * Untuk setiap item, tambahkan qty ke inventory baru
                  */
                 InventoryBalance::model()->returJual($detail);
             }
             // Total dari retur penjualan
             // Jika nanti ada item lain, misal: Transport, pajak, dll
             // ditambahkan di sini
             $jumlahReturJual = $this->ambilTotal();
             /*
              * Create hutang
              */
             $hutang = new HutangPiutang();
             $hutang->profil_id = $this->profil_id;
             $hutang->jumlah = $jumlahReturJual;
             $hutang->tipe = HutangPiutang::TIPE_HUTANG;
             $hutang->asal = HutangPiutang::DARI_RETUR_JUAL;
             $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 Jual: ' . $this->nomor;
             $hutangDetail->jumlah = $jumlahReturJual;
             if (!$hutangDetail->save()) {
                 throw new Exception("Gagal simpan hutang detail");
             }
             /*
              * Simpan hutang_id ke pembelian
              */
             if (!ReturPenjualan::model()->updateByPk($this->id, array('hutang_piutang_id' => $hutang->id)) > 1) {
                 throw new Exception("Gagal simpan hutang_id");
             }
             $transaction->commit();
             return true;
         } else {
             throw new Exception("Gagal Simpan Retur Penjualan");
         }
     } catch (Exception $e) {
         $transaction->rollback();
         throw $e;
     }
 }
Beispiel #2
0
 /**
  * Simpan pembelian:
  * 1. update status dari draft menjadi pembelian.
  * 2. update stock.
  * 3. update harga jual & rrp
  * 4. create hutang.
  * 5. update stok minus. BELUM !!
  * @return boolean True jika proses berhasil
  * @throws Exception
  */
 public function simpanPembelian()
 {
     $this->scenario = 'simpanPembelian';
     $transaction = $this->dbConnection->beginTransaction();
     /* Untuk jumlah pembelian yang sangat banyak, misal: init data */
     if ($this->profil_id == 1) {
         ini_set('memory_limit', '-1');
         set_time_limit(0);
     }
     try {
         if ($this->save()) {
             /*
              * Ambil data barang, dan data inventory terakhir/terbaru nya
              * Jika inventory tidak ada, maka ib.* nilainya null
              */
             $details = PembelianDetail::model()->findAll('pembelian_id=:pembelianId', array(':pembelianId' => $this->id));
             foreach ($details as $detail) {
                 /* Untuk setiap barang yang dibeli */
                 /* Sesuaikan inventory */
                 $inventoryBalance = new InventoryBalance();
                 $inventoryBalance->beli(InventoryBalance::ASAL_PEMBELIAN, $this->nomor, $detail->id, $detail->barang_id, $detail->harga_beli, $detail->qty);
                 /*
                  * Update harga jual
                  */
                 if (!HargaJual::model()->updateHarga($detail->barang_id, $detail->harga_jual)) {
                     throw new Exception("Gagal Update Harga Jual");
                 }
                 /*
                  * Update Rrp
                  */
                 if (!HargaJualRekomendasi::model()->updateHarga($detail->barang_id, $detail->harga_jual_rekomendasi)) {
                     throw new Exception("Gagal Update RRP");
                 }
                 /* Tambahkan supplier ke barang ini, jika belum ada */
                 $supplierBarangAda = SupplierBarang::model()->find("supplier_id={$this->profil_id} and barang_id = {$detail->barang_id}");
                 if (is_null($supplierBarangAda)) {
                     $supplierBarang = new SupplierBarang();
                     $supplierBarang->barang_id = $detail->barang_id;
                     $supplierBarang->supplier_id = $this->profil_id;
                     if (!$supplierBarang->save()) {
                         throw new Exception("Gagal simpan supplier barang");
                     }
                 }
             }
             // Total dari pembelian barang
             // Jika nanti ada item lain, misal: Transport, pajak, dll
             // ditambahkan di sini
             $jumlahPembelian = $this->ambilTotal();
             /*
              * Create (hutang)
              */
             $hutang = new HutangPiutang();
             $hutang->profil_id = $this->profil_id;
             $hutang->jumlah = $jumlahPembelian;
             $hutang->tipe = HutangPiutang::TIPE_HUTANG;
             $hutang->asal = HutangPiutang::DARI_PEMBELIAN;
             $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 = 'Pembelian: ' . $this->nomor;
             $hutangDetail->jumlah = $jumlahPembelian;
             if (!$hutangDetail->save()) {
                 throw new Exception("Gagal simpan hutang detail");
             }
             /*
              * Simpan hutang_id ke pembelian
              */
             if (!Pembelian::model()->updateByPk($this->id, array('hutang_piutang_id' => $hutang->id)) > 1) {
                 throw new Exception("Gagal simpan hutang_id");
             }
             $transaction->commit();
             return array('sukses' => true);
         } else {
             throw new Exception("Gagal Simpan Pembelian");
         }
     } catch (Exception $ex) {
         $transaction->rollback();
         // throw $up;
         return array('sukses' => false, 'error' => array('msg' => $ex->getMessage(), 'code' => $ex->getCode()));
     }
 }
Beispiel #3
0
 /**
  * Proses simpan penjualan.
  * Jika piutang, terbit nota debit (gudang)
  *
  * Simpan penjualan:
  * 1. Update status dari draft menjadi piutang.
  * 2. Update stock
  * 3. Catat harga beli dan harga jual
  * 4. Jika stok minus harga beli adalah harga beli terakhir
  * 5. Buat nota debit (piutang)
  *
  */
 public function simpanPenjualan()
 {
     if (!$this->save()) {
         throw new Exception('Gagal simpan penjualan', 500);
     }
     $details = PenjualanDetail::model()->findAll('penjualan_id=:penjualanId', array(':penjualanId' => $this->id));
     foreach ($details as $detail) {
         $inventoryTerpakai = InventoryBalance::model()->jual($detail->barang_id, $detail->qty);
         $count = 1;
         foreach ($inventoryTerpakai as $layer) {
             $hpp = new HargaPokokPenjualan();
             $hpp->penjualan_detail_id = $detail->id;
             $hpp->pembelian_detail_id = $layer['pembelianDetailId'];
             $hpp->qty = $layer['qtyTerpakai'];
             $hpp->harga_beli = $layer['hargaBeli'];
             // Jika negatif simpan juga di harga_beli_temp
             // FIX ME, jika pembelian harga beli nya beda
             if (isset($layer['negatif']) && $layer['negatif']) {
                 $hpp->harga_beli_temp = $layer['hargaBeli'];
             }
             if (!$hpp->save()) {
                 throw new Exception("Gagal simpan HPP", 500);
             }
             /* Tambahan untuk transfer mode,
              * cek apakah harga jual masih sama dengan inventory
              * jika beda, maka tambahkan juga detail penjualannya
              * ctt: transfer mode, harga jual = harga beli, jadi
              * hpp = penjualan_detail
              */
             if ($this->transfer_mode && $count > 1) {
                 $this->tambahDetailTransferBarang($detail, HargaPokokPenjualan::model()->findByPk($hpp->id));
             }
             $count++;
         }
     }
     $jumlahPenjualan = $this->ambilTotal();
     // Buat Hutang Piutang
     $piutang = new HutangPiutang();
     $piutang->profil_id = $this->profil_id;
     $piutang->jumlah = $jumlahPenjualan;
     $piutang->tipe = HutangPiutang::TIPE_PIUTANG;
     $piutang->asal = HutangPiutang::DARI_PENJUALAN;
     $piutang->nomor_dokumen_asal = $this->nomor;
     if (!$piutang->save()) {
         throw new Exception("Gagal simpan piutang", 500);
     }
     /*
      * Piutang Detail
      */
     $piutangDetail = new HutangPiutangDetail();
     $piutangDetail->hutang_piutang_id = $piutang->id;
     $piutangDetail->keterangan = 'Pembelian: ' . $this->nomor;
     $piutangDetail->jumlah = $jumlahPenjualan;
     if (!$piutangDetail->save()) {
         throw new Exception("Gagal simpan piutang detail", 500);
     }
     /*
      * Simpan hutang_piutang_id ke penjualan
      */
     if (!Penjualan::model()->updateByPk($this->id, array('hutang_piutang_id' => $piutang->id)) > 1) {
         throw new Exception("Gagal simpan piutang_id", 500);
     }
     /* Simpan poin jika ada */
     if ($this->getCurPoin() > 0) {
         $penjualanMember = new PenjualanMember();
         $penjualanMember->penjualan_id = $this->id;
         $penjualanMember->profil_id = $this->profil_id;
         $penjualanMember->poin = $this->getCurPoin();
         if (!$penjualanMember->save()) {
             throw new Exception("Gagal simpan poin ke penjualan", 500);
         }
     }
 }
Beispiel #4
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()]];
     }
 }