126 lines
3.9 KiB
PHP
126 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Transaction;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Barang;
|
|
use App\Models\Supplier;
|
|
use App\Models\ListItemPembelian;
|
|
use App\Models\Pembelian;
|
|
use App\Models\SettingPrinter;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Carbon\Carbon;
|
|
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
|
|
use Mike42\Escpos\Printer;
|
|
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
|
|
|
|
class PembelianController extends Controller
|
|
{
|
|
private function generateNoTransaksi()
|
|
{
|
|
return 'TRN' . time();
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$data = [
|
|
'title' => 'Data Pembelian',
|
|
'pembelian' => Pembelian::with('supplier',"listItem","listItem.barang")->get(),
|
|
];
|
|
|
|
if (!$request->ajax() && $request->isMethod('get')) {
|
|
return view('page.dashboard.pembelian.index', compact('data'));
|
|
}
|
|
|
|
if ($request->ajax() && $request->isMethod('get')) {
|
|
$perPage = $request->input('per_page', 10);
|
|
$query = Pembelian::query()->with('supplier');
|
|
|
|
$searchTerm = $request->input('search');
|
|
|
|
if ($searchTerm) {
|
|
$query->where(function($q) use ($searchTerm) {
|
|
$q->where('no_transaksi', 'like', "%$searchTerm%")
|
|
->orWhere('supplier.nama_supplier', 'like', "%$searchTerm%")
|
|
->orWhere('created_at', 'like', "%$searchTerm%");
|
|
});
|
|
}
|
|
|
|
|
|
$data = $query->paginate($perPage);
|
|
|
|
if ($data->isEmpty()) {
|
|
return response()->json(['message' => 'Tidak ada data!'], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'message' => $data,
|
|
], 200);
|
|
}
|
|
|
|
|
|
}
|
|
public function tambah(Request $request)
|
|
{
|
|
$data = [
|
|
'title' => 'Tambah Pembelian',
|
|
'supplier' => Supplier::all()
|
|
];
|
|
if (!$request->ajax()) {
|
|
return view('page.dashboard.pembelian.tambah', compact('data'));
|
|
}
|
|
|
|
if ($request->isMethod('get')) {
|
|
$barang = Pembelian::all();
|
|
return response()->json(['message' => $barang], 200);
|
|
}
|
|
|
|
$noTransaksi = $this->generateNoTransaksi();
|
|
$items = $request->items;
|
|
|
|
$pembelian = Pembelian::create([
|
|
'operator' => Auth::user()->id,
|
|
'no_transaksi' => $noTransaksi,
|
|
'total_harga'=> $request->total,
|
|
'bayar'=> $request->bayar,
|
|
'sisa'=> $request->sisa,
|
|
'supplier_id'=> $request->supplier
|
|
]);
|
|
|
|
foreach ($items as $item) {
|
|
$barang = Barang::where('kode', $item['kode'])->first();
|
|
|
|
if ($barang) {
|
|
ListItemPembelian::create([
|
|
'pembelian_id' => $pembelian->id,
|
|
'barang_id' => $barang->id,
|
|
'qty_beli' => $item['qty'],
|
|
'harga_beli' => $barang->harga_beli,
|
|
'harga_jual' => $barang->harga_jual,
|
|
'nama_barang' => $barang->nama,
|
|
'ukuran_barang' => $barang->ukuran
|
|
]);
|
|
} else {
|
|
return response()->json(['message' => 'Opps! theres been an error.'], 404);
|
|
}
|
|
}
|
|
|
|
return response()->json(['message' => 'Ok'], 200);
|
|
}
|
|
|
|
public function delete(Request $request, $id)
|
|
{
|
|
if ($request->ajax()) {
|
|
$data = ListItemPembelian::where("pembelian_id","=",$id);
|
|
if ($data->delete()) {
|
|
$data = Pembelian::findOrFail($id)->delete();
|
|
return response()->json(['message' => 'Berhasil hapus data!'], 200);
|
|
} else {
|
|
return response()->json(['message' => 'Gagal hapus data!'], 404);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
} |