Advertisement
muhidins

TransaksiController

Apr 29th, 2020
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. // use Illuminate\Support\Facades\Validator;
  7. use App\Pelanggan;
  8. use App\Produk;
  9. use App\ProdukHarga;
  10. use App\Transaksi;
  11. use Carbon\Carbon;
  12. use DB;
  13.  
  14. class TransaksiController extends Controller
  15. {
  16. public function formTransaksi()
  17. {
  18. $pelanggan = Pelanggan::orderBy('nama', 'ASC')->get();
  19. $produk = Produk::orderBy('varian', 'ASC')->get();
  20. return view('transaksi.form', compact('pelanggan', 'produk'));
  21. }
  22.  
  23. public function getHargaProduk()
  24. {
  25. $harga = ProdukHarga::where('produk_id', request()->id)->get();
  26. return response()->json(['status' => 'success', 'data' => $harga]);
  27. }
  28.  
  29. public function saveTransaksi(Request $request)
  30. {
  31. //VALIDASI
  32. $this->validate($request, [
  33. 'tipe_pelanggan' => 'required',
  34. 'produk_id' => 'required|exist:produk,id',
  35. 'layanan' => 'required|exist:produk_harga,id',
  36. 'jaminan' => 'required',
  37. 'foto_jaminan' => 'required|image|mimes:jpg,jpeg,png,gif',
  38. 'tgl_pinjam' => 'required|date',
  39. 'lama_pinjam' => 'required'
  40. ]);
  41.  
  42. $this->validate($request, [
  43. 'varian' => 'required|string|max:50',
  44. 'merk' => 'required|string|max:20',
  45. 'plat' => 'required|string|unique:produk',
  46. 'gambar' => 'required|image|mimes:png,jpg,jpeg,gif'
  47. ]);
  48.  
  49. DB::beginTransaction();
  50. try {
  51. //PELANGGAN BARU
  52. if ($request->tipe_pelanggan == 0) {
  53. //HANDLE FILE UPDATE
  54. $ktpFile = $request->file('foto_ktp');
  55. $filename = $request->nik . '.' . $ktpFile->getClientOriginalExtension();
  56. $ktpFile->storeAs('public/pelanggan', $filename);
  57.  
  58. $pelanggan = Pelanggan::create([
  59. 'nik' => $request->nik,
  60. 'foto_ktp' =>$request->$filename,
  61. 'nama' => $request->nama,
  62. 'notelp' =>$request->nama,
  63. 'alamat' =>$request->alamat
  64. ]);
  65. } else {
  66. //PELANGGAN LAMA
  67. $pelanggan = Pelanggan::find($request->pelanggan_id);
  68. }
  69.  
  70. $existing = Transaksi::orderBy('created_at', 'DESC')->first();
  71. $faktur = 'PP-';
  72. if($existing){
  73. //Nomor Faktur diawali dari PP-1 dst
  74. $explode = explode('-', $existing->faktur);
  75. $faktur = 'PP-' . $explode[1] + 1;
  76. }
  77.  
  78. $jaminanFile = $request->file('foto_jaminan');
  79. $filenameJaminan = $faktur . '.' . $jaminanFile->getClientOriginalExtension();
  80. $jaminanFile->storeAs('public/transaksi', $filenameJaminan);
  81.  
  82. $produkHarga = ProdukHarga::find($request->layanan);
  83. $tgl_pinjam = Carbon::parse($request->tgl_pinjam);
  84. Transaksi::create([
  85. 'faktur' => $faktur,
  86. 'pelanggan_id' => $pelanggan->id,
  87. 'jaminan' => $request->jaminan,
  88. 'foto_jaminan' => $filenameJaminan,
  89. 'tgl_pinjam' => $tgl_pinjam->format('Y-m-d'),
  90. 'tgl_kembali' => $tgl_pinjam->addDays($request->lama_pinjam)->format('Y-m-d'),
  91. 'harga' => $produkHarga->harga,
  92. 'denda' => 0,
  93. 'tgl_dikembalikan' => '',
  94. 'produk_id' => $request->produk_id,
  95. 'user_id' => auth()->user()->id,
  96. 'status' => 0
  97. ]);
  98. DB::commit();
  99. return redirect()->back()->with(['success' => '#' . $faktur . ' Berhasil ditambahkan']);
  100. } catch (\Exception $e) {
  101. DB::rollback();
  102. return $e->getMessage();
  103. }
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement