Fixed an API issue when only setting product_key in an invoice

This commit is contained in:
Hillel Coren 2016-02-11 19:24:41 +02:00
parent a30153cabe
commit 0a2a97d89a
2 changed files with 16 additions and 14 deletions

View File

@ -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;
}

View File

@ -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;
});