From 6bbf3dface8f455f02cd5550c2c8fdf8acd9fa15 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 16 Jul 2023 20:34:31 +1000 Subject: [PATCH] Updates for exchange rate validation --- app/Export/CSV/QuoteItemExport.php | 53 ++++++++++++++----- app/Factory/CreditFactory.php | 2 +- app/Factory/PurchaseOrderFactory.php | 3 +- app/Factory/QuoteFactory.php | 1 + .../Requests/Credit/StoreCreditRequest.php | 9 +++- .../Requests/Credit/UpdateCreditRequest.php | 7 ++- .../Requests/Invoice/StoreInvoiceRequest.php | 3 +- .../Requests/Invoice/UpdateInvoiceRequest.php | 7 ++- .../StorePurchaseOrderRequest.php | 7 ++- .../UpdatePurchaseOrderRequest.php | 5 ++ app/Http/Requests/Quote/StoreQuoteRequest.php | 5 ++ .../Requests/Quote/UpdateQuoteRequest.php | 5 ++ .../StoreRecurringInvoiceRequest.php | 7 ++- .../UpdateRecurringInvoiceRequest.php | 7 ++- 14 files changed, 99 insertions(+), 22 deletions(-) diff --git a/app/Export/CSV/QuoteItemExport.php b/app/Export/CSV/QuoteItemExport.php index 4059174a8af8..06cd71cc97b2 100644 --- a/app/Export/CSV/QuoteItemExport.php +++ b/app/Export/CSV/QuoteItemExport.php @@ -135,25 +135,44 @@ class QuoteItemExport extends BaseExport $transformed_items = []; - foreach ($quote->line_items as $item) { - $item_array = []; + $transformed_items = []; - foreach (array_values($this->input['report_keys']) as $key) { - if (str_contains($key, 'item.')) { - $key = str_replace('item.', '', $key); - $item_array[$key] = $item->{$key}; + foreach ($quote->line_items as $item) { + $item_array = []; + + foreach (array_values($this->input['report_keys']) as $key) { //items iterator produces item array + + if (str_contains($key, "item.")) { + + $key = str_replace("item.", "", $key); + + $keyval = $key; + + $keyval = str_replace("custom_value", "quote", $key); + + if($key == 'type_id') + $keyval = 'type'; + + if($key == 'tax_id') + $keyval = 'tax_category'; + + if (property_exists($item, $key)) { + $item_array[$keyval] = $item->{$key}; + } else { + $item_array[$keyval] = ''; + } } } $entity = []; - foreach (array_values($this->input['report_keys']) as $key) { - $keyval = array_search($key, $this->entity_keys); + foreach (array_values($this->input['report_keys']) as $key) { //create an array of report keys only + $keyval = array_search($key, $this->entity_keys); if (array_key_exists($key, $transformed_items)) { $entity[$keyval] = $transformed_items[$key]; } else { - $entity[$keyval] = ''; + $entity[$keyval] = ""; } } @@ -173,16 +192,26 @@ class QuoteItemExport extends BaseExport foreach (array_values($this->input['report_keys']) as $key) { $keyval = array_search($key, $this->entity_keys); + if(!$keyval) { + $keyval = array_search(str_replace("quote.", "", $key), $this->entity_keys) ?? $key; + } + + if(!$keyval) { + $keyval = $key; + } + if (array_key_exists($key, $transformed_quote)) { $entity[$keyval] = $transformed_quote[$key]; - } else { - $entity[$keyval] = ''; + } elseif (array_key_exists($keyval, $transformed_quote)) { + $entity[$keyval] = $transformed_quote[$keyval]; + } + else { + $entity[$keyval] = $this->resolveKey($keyval, $quote, $this->quote_transformer); } } return $this->decorateAdvancedFields($quote, $entity); } - private function decorateAdvancedFields(Quote $quote, array $entity) :array { if (in_array('currency_id', $this->input['report_keys'])) { diff --git a/app/Factory/CreditFactory.php b/app/Factory/CreditFactory.php index bb3643d469d2..ec1d8341a39b 100644 --- a/app/Factory/CreditFactory.php +++ b/app/Factory/CreditFactory.php @@ -49,7 +49,7 @@ class CreditFactory $credit->user_id = $user_id; $credit->company_id = $company_id; $credit->recurring_id = null; - + $credit->exchange_rate = 1; return $credit; } } diff --git a/app/Factory/PurchaseOrderFactory.php b/app/Factory/PurchaseOrderFactory.php index 31a082ac83b7..4e68f9ad2f0f 100644 --- a/app/Factory/PurchaseOrderFactory.php +++ b/app/Factory/PurchaseOrderFactory.php @@ -49,7 +49,8 @@ class PurchaseOrderFactory $purchase_order->user_id = $user_id; $purchase_order->company_id = $company_id; $purchase_order->recurring_id = null; - + $purchase_order->exchange_rate = 1; + return $purchase_order; } } diff --git a/app/Factory/QuoteFactory.php b/app/Factory/QuoteFactory.php index 64a4b42f244f..d312a6215646 100644 --- a/app/Factory/QuoteFactory.php +++ b/app/Factory/QuoteFactory.php @@ -46,6 +46,7 @@ class QuoteFactory $quote->user_id = $user_id; $quote->company_id = $company_id; $quote->paid_to_date = 0; + $quote->exchange_rate = 1; return $quote; } diff --git a/app/Http/Requests/Credit/StoreCreditRequest.php b/app/Http/Requests/Credit/StoreCreditRequest.php index 13eee9c59852..8616ab177bc0 100644 --- a/app/Http/Requests/Credit/StoreCreditRequest.php +++ b/app/Http/Requests/Credit/StoreCreditRequest.php @@ -67,7 +67,8 @@ class StoreCreditRequest extends Request $rules['tax_name1'] = 'bail|sometimes|string|nullable'; $rules['tax_name2'] = 'bail|sometimes|string|nullable'; $rules['tax_name3'] = 'bail|sometimes|string|nullable'; - + $rules['exchange_rate'] = 'bail|sometimes|gt:0'; + if ($this->invoice_id) { $rules['invoice_id'] = new ValidInvoiceCreditRule(); } @@ -88,7 +89,11 @@ class StoreCreditRequest extends Request $input = $this->decodePrimaryKeys($input); $input['line_items'] = isset($input['line_items']) ? $this->cleanItems($input['line_items']) : []; - //$input['line_items'] = json_encode($input['line_items']); + + if (array_key_exists('exchange_rate', $input) && is_null($input['exchange_rate'])) { + $input['exchange_rate'] = 1; + } + $this->replace($input); } } diff --git a/app/Http/Requests/Credit/UpdateCreditRequest.php b/app/Http/Requests/Credit/UpdateCreditRequest.php index 929ad49e443a..a397d41a8862 100644 --- a/app/Http/Requests/Credit/UpdateCreditRequest.php +++ b/app/Http/Requests/Credit/UpdateCreditRequest.php @@ -67,7 +67,8 @@ class UpdateCreditRequest extends Request $rules['tax_name1'] = 'bail|sometimes|string|nullable'; $rules['tax_name2'] = 'bail|sometimes|string|nullable'; $rules['tax_name3'] = 'bail|sometimes|string|nullable'; - + $rules['exchange_rate'] = 'bail|sometimes|gt:0'; + return $rules; } @@ -81,6 +82,10 @@ class UpdateCreditRequest extends Request $input['line_items'] = isset($input['line_items']) ? $this->cleanItems($input['line_items']) : []; } + if (array_key_exists('exchange_rate', $input) && is_null($input['exchange_rate'])) { + $input['exchange_rate'] = 1; + } + $input['id'] = $this->credit->id; $this->replace($input); diff --git a/app/Http/Requests/Invoice/StoreInvoiceRequest.php b/app/Http/Requests/Invoice/StoreInvoiceRequest.php index 327aa8b96a04..37ff17758412 100644 --- a/app/Http/Requests/Invoice/StoreInvoiceRequest.php +++ b/app/Http/Requests/Invoice/StoreInvoiceRequest.php @@ -72,7 +72,8 @@ class StoreInvoiceRequest extends Request $rules['tax_name1'] = 'bail|sometimes|string|nullable'; $rules['tax_name2'] = 'bail|sometimes|string|nullable'; $rules['tax_name3'] = 'bail|sometimes|string|nullable'; - + $rules['exchange_rate'] = 'bail|sometimes|gt:0'; + return $rules; } diff --git a/app/Http/Requests/Invoice/UpdateInvoiceRequest.php b/app/Http/Requests/Invoice/UpdateInvoiceRequest.php index 0a6992192e93..971de8712cd3 100644 --- a/app/Http/Requests/Invoice/UpdateInvoiceRequest.php +++ b/app/Http/Requests/Invoice/UpdateInvoiceRequest.php @@ -72,7 +72,8 @@ class UpdateInvoiceRequest extends Request $rules['tax_name2'] = 'bail|sometimes|string|nullable'; $rules['tax_name3'] = 'bail|sometimes|string|nullable'; $rules['status_id'] = 'bail|sometimes|not_in:5'; //do not all cancelled invoices to be modfified. - + $rules['exchange_rate'] = 'bail|sometimes|gt:0'; + // not needed. // $rules['partial_due_date'] = 'bail|sometimes|required_unless:partial,0,null'; @@ -95,6 +96,10 @@ class UpdateInvoiceRequest extends Request unset($input['documents']); } + if (array_key_exists('exchange_rate', $input) && is_null($input['exchange_rate'])) { + $input['exchange_rate'] = 1; + } + $this->replace($input); } diff --git a/app/Http/Requests/PurchaseOrder/StorePurchaseOrderRequest.php b/app/Http/Requests/PurchaseOrder/StorePurchaseOrderRequest.php index 91a312203354..71d8fe32de99 100644 --- a/app/Http/Requests/PurchaseOrder/StorePurchaseOrderRequest.php +++ b/app/Http/Requests/PurchaseOrder/StorePurchaseOrderRequest.php @@ -60,7 +60,8 @@ class StorePurchaseOrderRequest extends Request } $rules['status_id'] = 'nullable|integer|in:1,2,3,4,5'; - + $rules['exchange_rate'] = 'bail|sometimes|gt:0'; + return $rules; } @@ -77,6 +78,10 @@ class StorePurchaseOrderRequest extends Request $input['amount'] = 0; $input['balance'] = 0; + if (array_key_exists('exchange_rate', $input) && is_null($input['exchange_rate'])) { + $input['exchange_rate'] = 1; + } + $this->replace($input); } } diff --git a/app/Http/Requests/PurchaseOrder/UpdatePurchaseOrderRequest.php b/app/Http/Requests/PurchaseOrder/UpdatePurchaseOrderRequest.php index e23fa91520b8..f772944ddd53 100644 --- a/app/Http/Requests/PurchaseOrder/UpdatePurchaseOrderRequest.php +++ b/app/Http/Requests/PurchaseOrder/UpdatePurchaseOrderRequest.php @@ -63,6 +63,7 @@ class UpdatePurchaseOrderRequest extends Request } $rules['status_id'] = 'sometimes|integer|in:1,2,3,4,5'; + $rules['exchange_rate'] = 'bail|sometimes|gt:0'; return $rules; } @@ -79,6 +80,10 @@ class UpdatePurchaseOrderRequest extends Request $input['line_items'] = isset($input['line_items']) ? $this->cleanItems($input['line_items']) : []; } + if (array_key_exists('exchange_rate', $input) && is_null($input['exchange_rate'])) { + $input['exchange_rate'] = 1; + } + $this->replace($input); } } diff --git a/app/Http/Requests/Quote/StoreQuoteRequest.php b/app/Http/Requests/Quote/StoreQuoteRequest.php index 387f165de110..39e03dc37e33 100644 --- a/app/Http/Requests/Quote/StoreQuoteRequest.php +++ b/app/Http/Requests/Quote/StoreQuoteRequest.php @@ -55,6 +55,7 @@ class StoreQuoteRequest extends Request $rules['discount'] = 'sometimes|numeric'; $rules['is_amount_discount'] = ['boolean']; + $rules['exchange_rate'] = 'bail|sometimes|gt:0'; // $rules['number'] = new UniqueQuoteNumberRule($this->all()); $rules['line_items'] = 'array'; @@ -72,6 +73,10 @@ class StoreQuoteRequest extends Request $input['amount'] = 0; $input['balance'] = 0; + if (array_key_exists('exchange_rate', $input) && is_null($input['exchange_rate'])) { + $input['exchange_rate'] = 1; + } + $this->replace($input); } } diff --git a/app/Http/Requests/Quote/UpdateQuoteRequest.php b/app/Http/Requests/Quote/UpdateQuoteRequest.php index 07b463936892..b692e82663d4 100644 --- a/app/Http/Requests/Quote/UpdateQuoteRequest.php +++ b/app/Http/Requests/Quote/UpdateQuoteRequest.php @@ -57,6 +57,7 @@ class UpdateQuoteRequest extends Request $rules['line_items'] = 'array'; $rules['discount'] = 'sometimes|numeric'; $rules['is_amount_discount'] = ['boolean']; + $rules['exchange_rate'] = 'bail|sometimes|gt:0'; return $rules; } @@ -75,6 +76,10 @@ class UpdateQuoteRequest extends Request unset($input['documents']); } + if (array_key_exists('exchange_rate', $input) && is_null($input['exchange_rate'])) { + $input['exchange_rate'] = 1; + } + $input['id'] = $this->quote->id; $this->replace($input); diff --git a/app/Http/Requests/RecurringInvoice/StoreRecurringInvoiceRequest.php b/app/Http/Requests/RecurringInvoice/StoreRecurringInvoiceRequest.php index 420afb7c09f7..11c266ac8f46 100644 --- a/app/Http/Requests/RecurringInvoice/StoreRecurringInvoiceRequest.php +++ b/app/Http/Requests/RecurringInvoice/StoreRecurringInvoiceRequest.php @@ -67,7 +67,8 @@ class StoreRecurringInvoiceRequest extends Request $rules['tax_name2'] = 'bail|sometimes|string|nullable'; $rules['tax_name3'] = 'bail|sometimes|string|nullable'; $rules['due_date_days'] = 'bail|sometimes|string'; - + $rules['exchange_rate'] = 'bail|sometimes|gt:0'; + return $rules; } @@ -143,6 +144,10 @@ class StoreRecurringInvoiceRequest extends Request unset($input['number']); } + if (array_key_exists('exchange_rate', $input) && is_null($input['exchange_rate'])) { + $input['exchange_rate'] = 1; + } + $this->replace($input); } diff --git a/app/Http/Requests/RecurringInvoice/UpdateRecurringInvoiceRequest.php b/app/Http/Requests/RecurringInvoice/UpdateRecurringInvoiceRequest.php index f268cbe84288..26cc4e2f44de 100644 --- a/app/Http/Requests/RecurringInvoice/UpdateRecurringInvoiceRequest.php +++ b/app/Http/Requests/RecurringInvoice/UpdateRecurringInvoiceRequest.php @@ -61,7 +61,8 @@ class UpdateRecurringInvoiceRequest extends Request $rules['tax_name1'] = 'bail|sometimes|string|nullable'; $rules['tax_name2'] = 'bail|sometimes|string|nullable'; $rules['tax_name3'] = 'bail|sometimes|string|nullable'; - + $rules['exchange_rate'] = 'bail|sometimes|gt:0'; + return $rules; } @@ -121,6 +122,10 @@ class UpdateRecurringInvoiceRequest extends Request unset($input['documents']); } + if (array_key_exists('exchange_rate', $input) && is_null($input['exchange_rate'])) { + $input['exchange_rate'] = 1; + } + $this->replace($input); }