From 65016b0454d89ce8b9a2bf1684090509176203b2 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Thu, 27 Apr 2017 11:41:16 +0700 Subject: [PATCH] Added save draft transaction to database --- app/Cart/TransactionDraft.php | 57 ++++++++++++++++++++++ app/Http/Controllers/CartController.php | 23 ++++++++- app/Transaction.php | 13 +++++ ...2017_04_27_121204_create_transactions_table.php | 38 +++++++++++++++ .../views/cart/partials/draft-confirm.blade.php | 2 +- .../cart/partials/form-draft-detail.blade.php | 4 +- routes/web.php | 1 + tests/Unit/Integration/TransactionDraftTest.php | 51 ++++++++++++++++++- 8 files changed, 183 insertions(+), 6 deletions(-) create mode 100644 app/Transaction.php create mode 100644 database/migrations/2017_04_27_121204_create_transactions_table.php diff --git a/app/Cart/TransactionDraft.php b/app/Cart/TransactionDraft.php index 36a8478..4600078 100644 --- a/app/Cart/TransactionDraft.php +++ b/app/Cart/TransactionDraft.php @@ -3,6 +3,7 @@ namespace App\Cart; use App\Product; +use App\Transaction; /** * Transaction Draft Interface. @@ -101,4 +102,60 @@ abstract class TransactionDraft { return $this->payment - $this->getTotal(); } + + public function store() + { + $transaction = new Transaction; + $transaction->invoice_no = $this->getNewInvoiceNo(); + $transaction->items = $this->getItemsArray(); + $transaction->customer = $this->customer; + $transaction->payment = $this->payment; + $transaction->total = $this->getTotal(); + $transaction->notes = $this->notes; + $transaction->user_id = auth()->id() ?: 1; + + $transaction->save(); + + return $transaction; + } + + public function getNewInvoiceNo() + { + $prefix = date('ym'); + + $lastTransaction = Transaction::orderBy('invoice_no','desc')->first(); + + if (! is_null($lastTransaction)) { + $lastInvoiceNo = $lastTransaction->invoice_no; + if (substr($lastInvoiceNo, 0, 4) == $prefix) { + return ++$lastInvoiceNo; + } + } + + return $prefix . '0001'; + } + + protected function getItemsArray() + { + $items = []; + foreach ($this->items as $item) { + $items[] = [ + 'id' => $item->product->id, + 'name' => $item->name, + 'price' => $item->price, + 'qty' => $item->qty, + 'item_discount' => $item->item_discount, + 'item_discount_subtotal' => $item->item_discount_subtotal, + 'subtotal' => $item->subtotal, + ]; + } + + return $items; + } + + public function destroy() + { + $cart = app(CartCollection::class); + return $cart->removeDraft($this->draftKey); + } } diff --git a/app/Http/Controllers/CartController.php b/app/Http/Controllers/CartController.php index 0818db5..dc46896 100644 --- a/app/Http/Controllers/CartController.php +++ b/app/Http/Controllers/CartController.php @@ -28,6 +28,10 @@ class CartController extends Controller public function show(Request $request, $draftKey) { + $draft = $this->cart->get($draftKey); + if (is_null($draft)) + return redirect()->route('cart.index'); + $query = $request->get('query'); $queriedProducts = []; if ($query) { @@ -36,8 +40,6 @@ class CartController extends Controller })->get(); } - $draft = $this->cart->get($draftKey); - return view('cart.index', compact('draft', 'queriedProducts')); } @@ -101,7 +103,24 @@ class CartController extends Controller public function proccess(Request $request, $draftKey) { + $this->validate($request, [ + 'customer.name' => 'required|string|max:30', + 'customer.phone' => 'nullable|string|max:20', + 'payment' => 'required|numeric', + 'notes' => 'nullable|string|max:100', + ]); $this->cart->updateDraftAttributes($draftKey, $request->only('customer','notes','payment')); return redirect()->route('cart.show', [$draftKey, 'action' => 'confirm']); } + + public function store(Request $request, $draftKey) + { + $draft = $this->cart->get($draftKey); + if (is_null($draft)) + return redirect()->route('cart.index'); + + $draft->store(); + $draft->destroy(); + return redirect()->route('cart.index'); + } } diff --git a/app/Transaction.php b/app/Transaction.php new file mode 100644 index 0000000..864e2b5 --- /dev/null +++ b/app/Transaction.php @@ -0,0 +1,13 @@ + 'array', + 'customer' => 'array', + ]; +} diff --git a/database/migrations/2017_04_27_121204_create_transactions_table.php b/database/migrations/2017_04_27_121204_create_transactions_table.php new file mode 100644 index 0000000..2b50456 --- /dev/null +++ b/database/migrations/2017_04_27_121204_create_transactions_table.php @@ -0,0 +1,38 @@ +increments('id'); + $table->char('invoice_no', 8); + $table->text('items'); + $table->string('customer'); + $table->unsignedInteger('payment'); + $table->unsignedInteger('total'); + $table->string('notes')->nullable(); + $table->unsignedInteger('user_id'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('transactions'); + } +} diff --git a/resources/views/cart/partials/draft-confirm.blade.php b/resources/views/cart/partials/draft-confirm.blade.php index 5c69e20..570be31 100644 --- a/resources/views/cart/partials/draft-confirm.blade.php +++ b/resources/views/cart/partials/draft-confirm.blade.php @@ -63,7 +63,7 @@