Conditions | 15 |
Paths | 240 |
Total Lines | 117 |
Code Lines | 79 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
55 | public function store(Request $request) |
||
56 | { |
||
57 | |||
58 | // receive the client data and create a invoice with status = pending |
||
59 | $invoice = Invoice::create([ |
||
60 | 'client_name' => $request->client_name, |
||
61 | 'client_idnumber' => $request->client_idnumber, |
||
62 | 'client_address' => $request->client_address, |
||
63 | 'client_email' => $request->client_email, |
||
64 | 'client_phone' => $request->client_phone, |
||
65 | 'invoice_type_id' => $request->invoicetype, |
||
66 | 'receipt_number' => $request->receiptnumber, |
||
67 | 'date' => $request->has('date') ? Carbon::parse($request->date) : Carbon::now(), |
||
68 | ]); |
||
69 | |||
70 | $invoice->setNumber(); // TODO extract this to model events. |
||
71 | |||
72 | // persist the products |
||
73 | foreach ($request->products as $f => $product) { |
||
74 | $productType = match ($product['type']) { |
||
75 | 'enrollment' => Enrollment::class, |
||
76 | 'scheduledPayment' => ScheduledPayment::class, |
||
77 | 'fee' => Fee::class, |
||
78 | 'book' => Book::class, |
||
79 | }; |
||
80 | |||
81 | $productFinalPrice = 0; // used to compute the final price with taxes and discounts |
||
82 | |||
83 | $productFinalPrice += $product['price'] * ($product['quantity'] ?? 1) * 100; |
||
84 | |||
85 | // The front end sends the discounts value as percent, but for the invoice we want to store their actual value relative to the product they were applied on |
||
86 | if (isset($product['discounts'])) { |
||
87 | foreach ($product['discounts'] as $d => $discount) { |
||
88 | InvoiceDetail::create([ |
||
89 | 'invoice_id' => $invoice->id, |
||
90 | 'product_name' => $discount['name'], |
||
91 | 'product_id' => $discount['id'], |
||
92 | 'product_type' => Discount::class, |
||
93 | 'price' => -($discount['value'] / 100) * $product['price'] * ($product['quantity'] ?? 1), |
||
94 | ]); |
||
95 | |||
96 | $productFinalPrice -= (($discount['value']) * $product['price']) * ($product['quantity'] ?? 1); // no need to multiply by 100 because discount is in % |
||
97 | } |
||
98 | } |
||
99 | |||
100 | if (isset($product['taxes'])) { |
||
101 | foreach ($product['taxes'] as $d => $tax) { |
||
102 | $productFinalPrice += (($tax['value']) * $product['price']) * ($product['quantity'] ?? 1); // no need to multiply by 100 because discount is in % |
||
103 | |||
104 | InvoiceDetail::create([ |
||
105 | 'invoice_id' => $invoice->id, |
||
106 | 'product_name' => $tax['name'], |
||
107 | 'product_id' => $tax['id'], |
||
108 | 'product_type' => Tax::class, |
||
109 | 'price' => $product['price'] * ($tax['value'] / 100) * ($product['quantity'] ?? 1), |
||
110 | ]); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | InvoiceDetail::create([ |
||
115 | 'invoice_id' => $invoice->id, |
||
116 | 'product_name' => $product['name'], |
||
117 | 'product_code' => $product['product_code'], |
||
118 | 'product_id' => $product['id'], |
||
119 | 'product_type' => $productType, |
||
120 | 'price' => $product['price'], |
||
121 | 'final_price' => $productFinalPrice, |
||
122 | 'quantity' => $product['quantity'] ?? 1, |
||
123 | ]); |
||
124 | } |
||
125 | |||
126 | foreach ($request->payments as $p => $payment) { |
||
127 | Payment::create([ |
||
128 | 'responsable_id' => backpack_user()->id, |
||
|
|||
129 | 'invoice_id' => $invoice->id, |
||
130 | 'payment_method' => $payment['method'] ?? null, |
||
131 | 'value' => $payment['value'], |
||
132 | 'date' => isset($payment['date']) ? Carbon::parse($payment['date']) : Carbon::now(), |
||
133 | ]); |
||
134 | } |
||
135 | |||
136 | // send the details to Accounting |
||
137 | // and receive and store the invoice number |
||
138 | if ($request->sendinvoice == true && config('invoicing.invoicing_system')) { |
||
139 | try { |
||
140 | $invoiceNumber = $this->invoicingService->saveInvoice($invoice); |
||
141 | Log::info($invoiceNumber); |
||
142 | if ($invoiceNumber !== null) { |
||
143 | $invoice->receipt_number = $invoiceNumber; |
||
144 | $invoice->save(); |
||
145 | $success = true; |
||
146 | } else { |
||
147 | Invoice::where('id', $invoice->id)->delete(); |
||
148 | abort(500); |
||
149 | } |
||
150 | } catch (Exception $exception) { |
||
151 | Log::error('Data could not be sent to accounting'); |
||
152 | Log::error($exception); |
||
153 | } |
||
154 | } else { |
||
155 | $success = true; |
||
156 | } |
||
157 | if (isset($success)) { |
||
158 | |||
159 | $this->ifTheInvoiceIsFullyPaidMarkItsProductsAsSuch($invoice); |
||
160 | |||
161 | if (isset($request->comment)) { |
||
162 | Comment::create([ |
||
163 | 'commentable_id' => $invoice->id, |
||
164 | 'commentable_type' => Invoice::class, |
||
165 | 'body' => $request->comment, |
||
166 | 'author_id' => backpack_user()->id, |
||
167 | ]); |
||
168 | } |
||
169 | } else { |
||
170 | Invoice::where('id', $invoice->id)->delete(); |
||
171 | abort(500); |
||
172 | } |
||
253 |