|
1
|
|
|
<?php namespace App\Http\Controllers; |
|
2
|
|
|
|
|
3
|
|
|
use App\Repositories\CategoryRepository; |
|
4
|
|
|
use App\Repositories\ProductRepository; |
|
5
|
|
|
use App\Repositories\SaleDetailsRepository; |
|
6
|
|
|
use App\Repositories\SaleRepository; |
|
7
|
|
|
use App\Repositories\SnapshotDetailsRepository; |
|
8
|
|
|
use App\Repositories\SnapshotRepository; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Response; |
|
11
|
|
|
use DB; |
|
12
|
|
|
|
|
13
|
|
|
class BarController extends Controller |
|
14
|
|
|
{ |
|
15
|
|
|
private $categoryRepository; |
|
16
|
|
|
|
|
17
|
|
|
private $saleRepository; |
|
18
|
|
|
|
|
19
|
|
|
private $saleDetailsRepository; |
|
20
|
|
|
|
|
21
|
|
|
private $snapshotRepository; |
|
22
|
|
|
|
|
23
|
|
|
private $snapshotDetailsRepository; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(CategoryRepository $categoryRepository, |
|
26
|
|
|
SaleRepository $saleRepository, |
|
27
|
|
|
SaleDetailsRepository $saleDetailsRepository, |
|
28
|
|
|
SnapshotRepository $snapshotRepository, |
|
29
|
|
|
SnapshotDetailsRepository $snapshotDetailsRepository) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->categoryRepository = $categoryRepository; |
|
32
|
|
|
$this->saleRepository = $saleRepository; |
|
33
|
|
|
$this->saleDetailsRepository = $saleDetailsRepository; |
|
34
|
|
|
$this->snapshotDetailsRepository = $snapshotDetailsRepository; |
|
35
|
|
|
$this->snapshotRepository = $snapshotRepository; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function app() |
|
39
|
|
|
{ |
|
40
|
|
|
$stock = $this->categoryRepository->allWithProducts(); |
|
41
|
|
|
|
|
42
|
|
|
return view('app.app')->with('stock', $stock); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function registerSale(Request $request, ProductRepository $productRepository) |
|
46
|
|
|
{ |
|
47
|
|
|
$currentSnapshotId = $this->snapshotRepository->current()->cs_id; |
|
48
|
|
|
|
|
49
|
|
|
foreach ($request->all() as $sale) { |
|
50
|
|
|
|
|
51
|
|
|
DB::beginTransaction(); |
|
52
|
|
|
|
|
53
|
|
|
$formattedSaleDate = ['time' => $sale['timestamp'], |
|
54
|
|
|
'sum' => $sale['price'], |
|
55
|
|
|
'paid' => $sale['cash']]; |
|
56
|
|
|
|
|
57
|
|
|
$saleId = $this->saleRepository->register($formattedSaleDate); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($sale['items'] as $item) { |
|
60
|
|
|
$formattedSaleDetail = ['sale_id' => $saleId, |
|
61
|
|
|
'product_id' => $item['id'], |
|
62
|
|
|
'quantity' => $item['quantity'], |
|
63
|
|
|
'current_price' => $item['price']]; |
|
64
|
|
|
|
|
65
|
|
|
$this->saleDetailsRepository->store($formattedSaleDetail); |
|
66
|
|
|
|
|
67
|
|
|
$productRepository->decrementQuantity($item['id'], $item['quantity']); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->snapshotDetailsRepository->store(['type' => 'SALE', |
|
71
|
|
|
'sum' => min($sale['price'], $sale['cash']), |
|
72
|
|
|
'time' => $sale['timestamp'], |
|
73
|
|
|
'sale_id' => $saleId, |
|
74
|
|
|
'cs_id' => $currentSnapshotId]); |
|
75
|
|
|
|
|
76
|
|
|
DB::commit(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return Response::json(['status' => 1], 200); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|