From 0a2a97d89a94fd2b26963c36e1ff3d71e6bc5203 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 11 Feb 2016 19:24:41 +0200 Subject: [PATCH] Fixed an API issue when only setting product_key in an invoice --- app/Http/Controllers/InvoiceApiController.php | 26 +++++++++---------- app/Providers/AppServiceProvider.php | 4 ++- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/InvoiceApiController.php b/app/Http/Controllers/InvoiceApiController.php index 5789388c18c4..3f7af9721876 100644 --- a/app/Http/Controllers/InvoiceApiController.php +++ b/app/Http/Controllers/InvoiceApiController.php @@ -220,6 +220,19 @@ class InvoiceApiController extends BaseAPIController private function prepareItem($item) { + // if only the product key is set we'll load the cost and notes + if ($item['product_key'] && empty($item['cost']) && empty($item['notes'])) { + $product = Product::findProductByKey($item['product_key']); + if ($product) { + if (empty($item['cost'])) { + $item['cost'] = $product->cost; + } + if (empty($item['notes'])) { + $item['notes'] = $product->notes; + } + } + } + $fields = [ 'cost' => 0, 'product_key' => '', @@ -233,19 +246,6 @@ class InvoiceApiController extends BaseAPIController } } - // if only the product key is set we'll load the cost and notes - if ($item['product_key'] && (is_null($item['cost']) || is_null($item['notes']))) { - $product = Product::findProductByKey($item['product_key']); - if ($product) { - if (is_null($item['cost'])) { - $item['cost'] = $product->cost; - } - if (is_null($item['notes'])) { - $item['notes'] = $product->notes; - } - } - } - return $item; } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index c9b163f109a0..0309b6a8fe83 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -199,7 +199,9 @@ class AppServiceProvider extends ServiceProvider { Validator::extend('valid_invoice_items', function($attribute, $value, $parameters) { $total = 0; foreach ($value as $item) { - $total += $item['qty'] * $item['cost']; + $qty = isset($item['qty']) ? $item['qty'] : 1; + $cost = isset($item['cost']) ? $item['cost'] : 1; + $total += $qty * $cost; } return $total <= MAX_INVOICE_AMOUNT; });