| Conditions | 15 |
| Paths | 240 |
| Total Lines | 116 |
| 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 |
||
| 62 | public function store(Request $request) |
||
| 63 | { |
||
| 64 | |||
| 65 | // receive the client data and create a invoice with status = pending |
||
| 66 | $invoice = Invoice::create([ |
||
| 67 | 'client_name' => $request->client_name, |
||
| 68 | 'client_idnumber' => $request->client_idnumber, |
||
| 69 | 'client_address' => $request->client_address, |
||
| 70 | 'client_email' => $request->client_email, |
||
| 71 | 'client_phone' => $request->client_phone, |
||
| 72 | 'invoice_type_id' => $request->invoicetype, |
||
| 73 | 'receipt_number' => $request->receiptnumber, |
||
| 74 | 'date' => $request->has('date') ? Carbon::parse($request->date) : Carbon::now(), |
||
| 75 | ]); |
||
| 76 | |||
| 77 | $invoice->setNumber(); // TODO extract this to model events. |
||
| 78 | |||
| 79 | // persist the products |
||
| 80 | foreach ($request->products as $f => $product) { |
||
| 81 | $productType = match ($product['type']) { |
||
| 82 | 'enrollment' => Enrollment::class, |
||
| 83 | 'scheduledPayment' => ScheduledPayment::class, |
||
| 84 | 'fee' => Fee::class, |
||
| 85 | 'book' => Book::class, |
||
| 86 | }; |
||
| 87 | |||
| 88 | $productFinalPrice = 0; // used to compute the final price with taxes and discounts |
||
| 89 | |||
| 90 | $productFinalPrice += $product['price'] * ($product['quantity'] ?? 1) * 100; |
||
| 91 | |||
| 92 | // 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 |
||
| 93 | if (isset($product['discounts'])) { |
||
| 94 | foreach ($product['discounts'] as $d => $discount) { |
||
| 95 | InvoiceDetail::create([ |
||
| 96 | 'invoice_id' => $invoice->id, |
||
| 97 | 'product_name' => $discount['name'], |
||
| 98 | 'product_id' => $discount['id'], |
||
| 99 | 'product_type' => Discount::class, |
||
| 100 | 'price' => -($discount['value'] / 100) * $product['price'] * ($product['quantity'] ?? 1), |
||
| 101 | ]); |
||
| 102 | |||
| 103 | $productFinalPrice -= (($discount['value']) * $product['price']) * ($product['quantity'] ?? 1); // no need to multiply by 100 because discount is in % |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | if (isset($product['taxes'])) { |
||
| 108 | foreach ($product['taxes'] as $d => $tax) { |
||
| 109 | $productFinalPrice += (($tax['value']) * $product['price']) * ($product['quantity'] ?? 1); // no need to multiply by 100 because discount is in % |
||
| 110 | |||
| 111 | InvoiceDetail::create([ |
||
| 112 | 'invoice_id' => $invoice->id, |
||
| 113 | 'product_name' => $tax['name'], |
||
| 114 | 'product_id' => $tax['id'], |
||
| 115 | 'product_type' => Tax::class, |
||
| 116 | 'price' => $product['price'] * ($tax['value'] / 100) * ($product['quantity'] ?? 1), |
||
| 117 | ]); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | InvoiceDetail::create([ |
||
| 122 | 'invoice_id' => $invoice->id, |
||
| 123 | 'product_name' => $product['name'], |
||
| 124 | 'product_code' => $product['product_code'], |
||
| 125 | 'product_id' => $product['id'], |
||
| 126 | 'product_type' => $productType, |
||
| 127 | 'price' => $product['price'], |
||
| 128 | 'final_price' => $productFinalPrice, |
||
| 129 | 'quantity' => $product['quantity'] ?? 1, |
||
| 130 | ]); |
||
| 131 | } |
||
| 132 | |||
| 133 | foreach ($request->payments as $p => $payment) { |
||
| 134 | Payment::create([ |
||
| 135 | 'responsable_id' => backpack_user()->id, |
||
|
|
|||
| 136 | 'invoice_id' => $invoice->id, |
||
| 137 | 'payment_method' => $payment['method'] ?? null, |
||
| 138 | 'value' => $payment['value'], |
||
| 139 | 'date' => isset($payment['date']) ? Carbon::parse($payment['date']) : Carbon::now(), |
||
| 140 | ]); |
||
| 141 | } |
||
| 142 | |||
| 143 | // send the details to Accounting |
||
| 144 | // and receive and store the invoice number |
||
| 145 | if ($request->sendinvoice == true && config('invoicing.invoicing_system')) { |
||
| 146 | try { |
||
| 147 | $invoiceNumber = $this->invoicingService->saveInvoice($invoice); |
||
| 148 | Log::info($invoiceNumber); |
||
| 149 | if ($invoiceNumber !== null) { |
||
| 150 | $invoice->receipt_number = $invoiceNumber; |
||
| 151 | $invoice->save(); |
||
| 152 | $success = true; |
||
| 153 | } else { |
||
| 154 | Invoice::where('id', $invoice->id)->delete(); |
||
| 155 | abort(500); |
||
| 156 | } |
||
| 157 | } catch (Exception $exception) { |
||
| 158 | Log::error('Data could not be sent to accounting'); |
||
| 159 | Log::error($exception); |
||
| 160 | } |
||
| 161 | } else { |
||
| 162 | $success = true; |
||
| 163 | } |
||
| 164 | if (isset($success)) { |
||
| 165 | $this->ifTheInvoiceIsFullyPaidMarkItsProductsAsSuch($invoice); |
||
| 166 | |||
| 167 | if (isset($request->comment)) { |
||
| 168 | Comment::create([ |
||
| 169 | 'commentable_id' => $invoice->id, |
||
| 170 | 'commentable_type' => Invoice::class, |
||
| 171 | 'body' => $request->comment, |
||
| 172 | 'author_id' => backpack_user()->id, |
||
| 173 | ]); |
||
| 174 | } |
||
| 175 | } else { |
||
| 176 | Invoice::where('id', $invoice->id)->delete(); |
||
| 177 | abort(500); |
||
| 178 | } |
||
| 260 |