| Conditions | 20 |
| Paths | 2176 |
| Total Lines | 135 |
| Code Lines | 89 |
| 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 | 'total_price' => $request->total_price, |
||
| 66 | 'invoice_type_id' => $request->invoicetype, |
||
| 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' => Enrollment::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 | if ($product['type'] === 'enrollment') { |
||
| 84 | Enrollment::find($product['id'])->invoices()->attach($invoice, ['scheduled_payment_id' => $request->scheduled_payment_id ?? null]); |
||
| 85 | } |
||
| 86 | |||
| 87 | if ($product['type'] === 'scheduledPayment') { |
||
| 88 | $scheduledPayment = ScheduledPayment::find($product['id']); |
||
| 89 | $scheduledPayment->enrollment->invoices()->attach($invoice, ['scheduled_payment_id' => $product['id']]); |
||
| 90 | // Reset status to default value |
||
| 91 | $scheduledPayment->update(['status' => null]); |
||
| 92 | } |
||
| 93 | |||
| 94 | $productFinalPrice += $product['price'] * ($product['quantity'] ?? 1) * 100; |
||
| 95 | |||
| 96 | if (isset($product['discounts'])) { |
||
| 97 | foreach ($product['discounts'] as $d => $discount) { |
||
| 98 | $productFinalPrice -= (($discount['value']) * $product['price']) * ($product['quantity'] ?? 1); // no need to multiply by 100 because discount is in % |
||
| 99 | |||
| 100 | InvoiceDetail::create([ |
||
| 101 | 'invoice_id' => $invoice->id, |
||
| 102 | 'product_name' => $discount['name'], |
||
| 103 | 'product_id' => $discount['id'], |
||
| 104 | 'product_type' => Discount::class, |
||
| 105 | 'price' => -$discount['value'] * ($product['quantity'] ?? 1), |
||
| 106 | ]); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | if (isset($product['taxes'])) { |
||
| 111 | foreach ($product['taxes'] as $d => $tax) { |
||
| 112 | $productFinalPrice += (($tax['value']) * $product['price']) * ($product['quantity'] ?? 1); // no need to multiply by 100 because discount is in % |
||
| 113 | |||
| 114 | InvoiceDetail::create([ |
||
| 115 | 'invoice_id' => $invoice->id, |
||
| 116 | 'product_name' => $tax['name'], |
||
| 117 | 'product_id' => $tax['id'], |
||
| 118 | 'product_type' => Tax::class, |
||
| 119 | 'price' => $product['price'] * ($tax['value'] / 100) * ($product['quantity'] ?? 1), |
||
| 120 | ]); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | InvoiceDetail::create([ |
||
| 125 | 'invoice_id' => $invoice->id, |
||
| 126 | 'product_name' => $product['name'], |
||
| 127 | 'product_code' => $product['product_code'], |
||
| 128 | 'product_id' => $product['id'], |
||
| 129 | 'product_type' => $productType, |
||
| 130 | 'price' => $product['price'], |
||
| 131 | 'final_price' => $productFinalPrice, |
||
| 132 | 'quantity' => $product['quantity'] ?? 1, |
||
| 133 | //'tax_rate' => collect($product['taxes'] ?? [])->sum('value'), |
||
| 134 | ]); |
||
| 135 | } |
||
| 136 | |||
| 137 | foreach ($request->payments as $p => $payment) { |
||
| 138 | Payment::create([ |
||
| 139 | 'responsable_id' => backpack_user()->id, |
||
| 140 | 'invoice_id' => $invoice->id, |
||
| 141 | 'payment_method' => $payment['method'] ?? null, |
||
| 142 | 'value' => $payment['value'], |
||
| 143 | 'date' => isset($payment['date']) ? Carbon::parse($payment['date']) : Carbon::now(), |
||
| 144 | ]); |
||
| 145 | } |
||
| 146 | |||
| 147 | // send the details to Accounting |
||
| 148 | // and receive and store the invoice number |
||
| 149 | if ($request->sendinvoice == true && config('invoicing.invoicing_system')) { |
||
| 150 | try { |
||
| 151 | $invoiceNumber = $this->invoicingService->saveInvoice($invoice); |
||
| 152 | Log::info($invoiceNumber); |
||
| 153 | if ($invoiceNumber !== null) { |
||
| 154 | $invoice->receipt_number = $invoiceNumber; |
||
| 155 | $invoice->save(); |
||
| 156 | $success = true; |
||
| 157 | } else { |
||
| 158 | Invoice::where('id', $invoice->id)->delete(); |
||
| 159 | abort(500); |
||
| 160 | } |
||
| 161 | } catch (Exception $exception) { |
||
| 162 | Log::error('Data could not be sent to accounting'); |
||
| 163 | Log::error($exception); |
||
| 164 | } |
||
| 165 | } else { |
||
| 166 | $success = true; |
||
| 167 | } |
||
| 168 | |||
| 169 | if (isset($success)) { |
||
| 170 | // if the value of payments matches the total due price, |
||
| 171 | // mark the invoice and associated enrollments as paid. |
||
| 172 | foreach ($invoice->enrollments as $enrollment) { |
||
| 173 | if ($enrollment->price == $invoice->paidTotal()) { |
||
| 174 | $enrollment->markAsPaid(); |
||
| 175 | } elseif ($enrollment->scheduledPayments->where('computed_status', '!==', 2)->count() === 0) { |
||
| 176 | $enrollment->markAsPaid(); |
||
| 177 | } |
||
| 178 | if (isset($request->comment)) { |
||
| 179 | Comment::create([ |
||
| 180 | 'commentable_id' => $enrollment->id, |
||
| 181 | 'commentable_type' => Enrollment::class, |
||
| 182 | 'body' => $request->comment, |
||
| 183 | 'author_id' => backpack_user()->id, |
||
| 184 | ]); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } else { |
||
| 188 | Invoice::where('id', $invoice->id)->delete(); |
||
| 189 | abort(500); |
||
| 190 | } |
||
| 274 |