SHARE
TWEET

Untitled

a guest Sep 17th, 2019 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. exports.kurangStokObat = async (uid_pesanan, id_lokasi) => {
  2.   try {
  3.     let isi_pesanan = await listPesananObat({
  4.       uid_pesanan,
  5.       status_pesanan: "MENUNGGU"
  6.     })
  7.     // console.log("miaw", isi_pesanan)
  8.     // console.log(uid_pesanan)
  9.     let isiDetailPesanan = isi_pesanan.detail_pesanan
  10.     // console.log(isiDetailPesanan)
  11.     let nik_karyawan = isi_pesanan.nik_dokter
  12.     // console.log(nik_karyawan)
  13.     for (var i = 0; i < isiDetailPesanan.length; i++) {
  14.       uid_obat = isiDetailPesanan[i].uid_obat
  15.       jumlah_keluar = isiDetailPesanan[i].jumlah_obat
  16.       // console.log(uid_obat)
  17.       await connection.query("begin");
  18.       let {
  19.         uid,
  20.         stok
  21.       } = await connection
  22.         .query(
  23.           `select uid, stok from stok_obat where uid_obat = $1 and id_lokasi = $2 order by kadaluarsa asc limit 1`,
  24.           [uid_obat, id_lokasi]
  25.         )
  26.         .then(({
  27.           rows
  28.         }) => rows[0]);
  29.       // console.log(uid, stok)
  30.  
  31.  
  32.       if (stok < jumlah_keluar) {
  33.         let {
  34.           rows
  35.         } = await connection.query(
  36.           "select uid, stok from stok_obat where uid_obat = $1 and id_lokasi = $2 order by kadaluarsa asc offset 1 limit 1",
  37.           [uid_obat, id_lokasi]
  38.         );
  39.         uid = rows[0].uid;
  40.         stok = rows[0].stok;
  41.       } else {
  42.         // console.log(uid, stok - jumlah_keluar)
  43.         let sisastok = stok - jumlah_keluar
  44.         let testing = await connection.query(
  45.           `update stok_obat set stok = $1 where uid = $2 returning*`,
  46.           [sisastok, uid]
  47.         ).then(newStok => console.log("miaw", newStok.rows[0])).catch(err => console.log(err));
  48.         console.log("wakwau", testing)
  49.  
  50.       }
  51.  
  52.     }
  53.     //   await logTransaksiObat({
  54.     //     uid_obat,
  55.     //     uid_stok_obat: uid,
  56.     //     jumlah: jumlah_keluar * -1,
  57.     //     nik_karyawan,
  58.     //     id_lokasi: id_lokasi
  59.     //   });
  60.     // }
  61.     // await connection.query("delete from stok_obat where stok = 0");
  62.     // await connection.query("commit");
  63.     // return isi_pesanan;
  64.   } catch (error) {
  65.     connection.query("rollback");
  66.     throw error;
  67.   }
  68. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top