1+ <?php
2+
3+ namespace App \Http \Livewire ;
4+
5+ use App \Models \Customer ;
6+ use App \Models \Invoice ;
7+ use Livewire \Component ;
8+ use Illuminate \Support \Facades \Log ;
9+
10+ class CreateInvoice extends Component
11+ {
12+ public $ totalSteps = 3 ;
13+ public $ step = 1 ;
14+
15+ //Fields to store
16+ public $ customer_id = '' ;
17+ public $ isFixed = true ;
18+ public $ amount = 0 ;
19+ public $ hours = 0 ;
20+ public $ rate = 0 ;
21+ public $ calculatedAmount = 0 ;
22+ public $ message = '' ;
23+
24+ protected $ rules = [
25+ 'customer_id ' => 'required ' ,
26+ 'isFixed ' => 'required|boolean ' ,
27+ 'amount ' => 'required|numeric|gt:0 ' ,
28+ 'hours ' => 'numeric|gt:0 ' ,
29+ 'rate ' => 'numeric|gt:0 ' ,
30+ 'message ' => 'required|min:20 ' ,
31+ ];
32+
33+ protected $ messages = [
34+ 'customer_id.required ' => 'Please select Customer. ' ,
35+ 'amount.required ' => 'Please enter Amount. ' ,
36+ 'rate.gt ' => 'Hourly Rate must be greater than 0. ' ,
37+ 'message.required ' => 'Please add Message for Customer. ' ,
38+ ];
39+
40+ public function render ()
41+ {
42+
43+ $ customers = Customer::orderBy ('full_name ' )->pluck ('full_name ' , 'id ' );
44+ return view ('livewire.invoices.index ' , [
45+ 'customers ' => $ customers
46+ ]);
47+ }
48+
49+ public function moveAhead ()
50+ {
51+ if ($ this ->step == 1 ) {
52+ //Validate Step 1 Data
53+ $ this ->validateOnly ('customer_id ' );
54+ //Wont reach here if the Validation Fails.
55+ //Reset Error Bag to clear any errors on Step 2.
56+ $ this ->resetErrorBag ();
57+ //Recalculate Amount for Step 2.
58+ $ this ->_calculateAmount ();
59+ }
60+
61+ if ($ this ->step == 2 ) {
62+ if ($ this ->isFixed ) {
63+ //Fixed Invoice Validation
64+ $ this ->validateOnly ('amount ' );
65+ } else {
66+ //Hourly Invoice Validation
67+ $ this ->validateOnly ('hours ' );
68+ $ this ->validateOnly ('rate ' );
69+ }
70+ //Reset Error Bag to clear any errors on Step 3.
71+ $ this ->resetErrorBag ();
72+ }
73+
74+ if ($ this ->step == 3 ) {
75+ $ this ->validateOnly ('message ' );
76+ //Save to the Invoice Model
77+ $ invoice = new Invoice ;
78+ $ invoice ->customer_id = $ this ->customer_id ;
79+ $ invoice ->is_fixed = $ this ->isFixed ;
80+ if ($ invoice ->is_fixed ) {
81+ $ invoice ->amount = $ this ->amount ;
82+ } else {
83+ $ invoice ->hours = $ this ->hours ;
84+ $ invoice ->rate = $ this ->rate ;
85+ $ invoice ->amount = $ this ->calculatedAmount ;
86+ }
87+ $ invoice ->message = $ this ->message ;
88+ $ invoice ->save ();
89+
90+ //redirect
91+ redirect ()->route ('dashboard ' );
92+ }
93+
94+ //Increase Step
95+ $ this ->step += 1 ;
96+ $ this ->_validateStep ();
97+ }
98+
99+ public function moveBack ()
100+ {
101+ $ this ->step -= 1 ;
102+ $ this ->_validateStep ();
103+ }
104+
105+ private function _validateStep ()
106+ {
107+ if ($ this ->step < 1 ) {
108+ $ this ->step = 1 ;
109+ }
110+
111+ if ($ this ->step > $ this ->totalSteps ) {
112+ $ this ->step = $ this ->totalSteps ;
113+ }
114+ }
115+
116+ public function updatedHours ($ value )
117+ {
118+ //
119+ $ this ->calculatedAmount = 0 ;
120+ $ this ->validateOnly ('hours ' );
121+ $ this ->_calculateAmount ();
122+ }
123+
124+ public function updatedRate ($ value )
125+ {
126+ //
127+ $ this ->calculatedAmount = 0 ;
128+ $ this ->validateOnly ('rate ' );
129+ $ this ->_calculateAmount ();
130+
131+ }
132+
133+ private function _calculateAmount ()
134+ {
135+ if (is_numeric ($ this ->hours ) && is_numeric ($ this ->rate )) {
136+ $ this ->calculatedAmount = $ this ->hours * $ this ->rate ;
137+ } else {
138+ $ this ->calculatedAmount = 0 ;
139+ }
140+ }
141+ }
0 commit comments