mirror of
https://github.com/grocy/grocy.git
synced 2026-04-04 20:06:16 +02:00
Support associating a Shopping List with a Store
This commit is contained in:
parent
6b18b0a7be
commit
4fb70f8a89
|
|
@ -20,13 +20,18 @@ class StockApiController extends BaseApiController
|
||||||
$requestBody = $this->GetParsedAndFilteredRequestBody($request);
|
$requestBody = $this->GetParsedAndFilteredRequestBody($request);
|
||||||
|
|
||||||
$listId = 1;
|
$listId = 1;
|
||||||
|
|
||||||
if (array_key_exists('list_id', $requestBody) && !empty($requestBody['list_id']) && is_numeric($requestBody['list_id']))
|
if (array_key_exists('list_id', $requestBody) && !empty($requestBody['list_id']) && is_numeric($requestBody['list_id']))
|
||||||
{
|
{
|
||||||
$listId = intval($requestBody['list_id']);
|
$listId = intval($requestBody['list_id']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->getStockService()->AddMissingProductsToShoppingList($listId);
|
$checkDefaultShoppingLocation = false;
|
||||||
|
if (array_key_exists('check_default_shopping_location', $requestBody) && filter_var($requestBody['check_default_shopping_location'], FILTER_VALIDATE_BOOLEAN) !== false)
|
||||||
|
{
|
||||||
|
$checkDefaultShoppingLocation = boolval($requestBody['check_default_shopping_location']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->getStockService()->AddMissingProductsToShoppingList($listId, $checkDefaultShoppingLocation);
|
||||||
return $this->EmptyApiResponse($response);
|
return $this->EmptyApiResponse($response);
|
||||||
}
|
}
|
||||||
catch (\Exception $ex)
|
catch (\Exception $ex)
|
||||||
|
|
|
||||||
|
|
@ -441,6 +441,7 @@ class StockController extends BaseController
|
||||||
{
|
{
|
||||||
return $this->renderPage($response, 'shoppinglistform', [
|
return $this->renderPage($response, 'shoppinglistform', [
|
||||||
'mode' => 'create',
|
'mode' => 'create',
|
||||||
|
'shoppinglocations' => $this->getDatabase()->shopping_locations()->where('active = 1')->orderBy('name', 'COLLATE NOCASE'),
|
||||||
'userfields' => $this->getUserfieldsService()->GetFields('shopping_lists')
|
'userfields' => $this->getUserfieldsService()->GetFields('shopping_lists')
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
@ -449,6 +450,7 @@ class StockController extends BaseController
|
||||||
return $this->renderPage($response, 'shoppinglistform', [
|
return $this->renderPage($response, 'shoppinglistform', [
|
||||||
'shoppingList' => $this->getDatabase()->shopping_lists($args['listId']),
|
'shoppingList' => $this->getDatabase()->shopping_lists($args['listId']),
|
||||||
'mode' => 'edit',
|
'mode' => 'edit',
|
||||||
|
'shoppinglocations' => $this->getDatabase()->shopping_locations()->where('active = 1')->orderBy('name', 'COLLATE NOCASE'),
|
||||||
'userfields' => $this->getUserfieldsService()->GetFields('shopping_lists')
|
'userfields' => $this->getUserfieldsService()->GetFields('shopping_lists')
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2470,3 +2470,6 @@ msgstr ""
|
||||||
|
|
||||||
msgid "List actions"
|
msgid "List actions"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
msgid "Add products that are below defined min. stock amount from matching store"
|
||||||
|
msgstr ""
|
||||||
|
|
|
||||||
2
migrations/0254.sql
Normal file
2
migrations/0254.sql
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
ALTER TABLE shopping_lists
|
||||||
|
ADD shopping_location_id REFERENCES shopping_locations(id);
|
||||||
|
|
@ -156,6 +156,20 @@ $(document).on('click', '#add-products-below-min-stock-amount', function(e)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$(document).on('click', '#add-products-below-min-stock-amount-by-shopping-location', function(e)
|
||||||
|
{
|
||||||
|
Grocy.Api.Post('stock/shoppinglist/add-missing-products', { "list_id": $("#selected-shopping-list").val(), "check_default_shopping_location": true },
|
||||||
|
function(result)
|
||||||
|
{
|
||||||
|
window.location.href = U('/shoppinglist?list=' + $("#selected-shopping-list").val());
|
||||||
|
},
|
||||||
|
function(xhr)
|
||||||
|
{
|
||||||
|
console.error(xhr);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
$(document).on('click', '#add-overdue-expired-products', function(e)
|
$(document).on('click', '#add-overdue-expired-products', function(e)
|
||||||
{
|
{
|
||||||
Grocy.Api.Post('stock/shoppinglist/add-overdue-products', { "list_id": $("#selected-shopping-list").val() },
|
Grocy.Api.Post('stock/shoppinglist/add-overdue-products', { "list_id": $("#selected-shopping-list").val() },
|
||||||
|
|
|
||||||
|
|
@ -18,41 +18,46 @@ class StockService extends BaseService
|
||||||
const TRANSACTION_TYPE_TRANSFER_FROM = 'transfer_from';
|
const TRANSACTION_TYPE_TRANSFER_FROM = 'transfer_from';
|
||||||
const TRANSACTION_TYPE_TRANSFER_TO = 'transfer_to';
|
const TRANSACTION_TYPE_TRANSFER_TO = 'transfer_to';
|
||||||
|
|
||||||
public function AddMissingProductsToShoppingList($listId = 1)
|
public function AddMissingProductsToShoppingList($listId = 1, $checkDefaultShoppingLocation = False)
|
||||||
{
|
{
|
||||||
if (!$this->ShoppingListExists($listId))
|
if (!$this->ShoppingListExists($listId))
|
||||||
{
|
{
|
||||||
throw new \Exception('Shopping list does not exist');
|
throw new \Exception('Shopping list does not exist');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$shoppingList = $this->getDatabase()->shopping_lists()->where('id', $listId)->fetch();
|
||||||
$missingProducts = $this->GetMissingProducts();
|
$missingProducts = $this->GetMissingProducts();
|
||||||
foreach ($missingProducts as $missingProduct)
|
foreach ($missingProducts as $missingProduct)
|
||||||
{
|
{
|
||||||
$product = $this->getDatabase()->products()->where('id', $missingProduct->id)->fetch();
|
$product = $this->getDatabase()->products()->where('id', $missingProduct->id)->fetch();
|
||||||
$amountToAdd = round($missingProduct->amount_missing, 2);
|
|
||||||
|
|
||||||
$alreadyExistingEntry = $this->getDatabase()->shopping_list()->where('product_id', $missingProduct->id)->fetch();
|
if (!$checkDefaultShoppingLocation || ($product->shopping_location_id == $shoppingList->shopping_location_id))
|
||||||
if ($alreadyExistingEntry)
|
|
||||||
{
|
{
|
||||||
// Update
|
$amountToAdd = round($missingProduct->amount_missing, 2);
|
||||||
if ($alreadyExistingEntry->amount < $amountToAdd)
|
|
||||||
|
$alreadyExistingEntry = $this->getDatabase()->shopping_list()->where('product_id', $missingProduct->id)->fetch();
|
||||||
|
if ($alreadyExistingEntry)
|
||||||
{
|
{
|
||||||
$alreadyExistingEntry->update([
|
// Update
|
||||||
'amount' => $amountToAdd,
|
if ($alreadyExistingEntry->amount < $amountToAdd)
|
||||||
'shopping_list_id' => $listId
|
{
|
||||||
]);
|
$alreadyExistingEntry->update([
|
||||||
|
'amount' => $amountToAdd,
|
||||||
|
'shopping_list_id' => $listId
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Insert
|
||||||
|
$shoppinglistRow = $this->getDatabase()->shopping_list()->createRow([
|
||||||
|
'product_id' => $missingProduct->id,
|
||||||
|
'amount' => $amountToAdd,
|
||||||
|
'shopping_list_id' => $listId,
|
||||||
|
'qu_id' => $product->qu_id_purchase
|
||||||
|
]);
|
||||||
|
$shoppinglistRow->save();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Insert
|
|
||||||
$shoppinglistRow = $this->getDatabase()->shopping_list()->createRow([
|
|
||||||
'product_id' => $missingProduct->id,
|
|
||||||
'amount' => $amountToAdd,
|
|
||||||
'shopping_list_id' => $listId,
|
|
||||||
'qu_id' => $product->qu_id_purchase
|
|
||||||
]);
|
|
||||||
$shoppinglistRow->save();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -144,6 +144,9 @@ $listItem->last_price_total = $listItem->price * $listItem->amount;
|
||||||
<a id="add-products-below-min-stock-amount"
|
<a id="add-products-below-min-stock-amount"
|
||||||
class="dropdown-item"
|
class="dropdown-item"
|
||||||
href="#">{{ $__t('Add products that are below defined min. stock amount') }}</a>
|
href="#">{{ $__t('Add products that are below defined min. stock amount') }}</a>
|
||||||
|
<a id="add-products-below-min-stock-amount-by-shopping-location"
|
||||||
|
class="dropdown-item"
|
||||||
|
href="#">{{ $__t('Add products that are below defined min. stock amount from matching store') }}</a>
|
||||||
@endif
|
@endif
|
||||||
<a id="add-overdue-expired-products"
|
<a id="add-overdue-expired-products"
|
||||||
class="dropdown-item"
|
class="dropdown-item"
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,20 @@
|
||||||
<div class="invalid-feedback">{{ $__t('A name is required') }}</div>
|
<div class="invalid-feedback">{{ $__t('A name is required') }}</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@php $prefillById = ''; if($mode=='edit') { $prefillById = $shoppingList->shopping_location_id; } @endphp
|
||||||
|
@if(GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING)
|
||||||
|
@include('components.shoppinglocationpicker', array(
|
||||||
|
'label' => 'Associated store',
|
||||||
|
'prefillById' => $prefillById,
|
||||||
|
'shoppinglocations' => $shoppinglocations
|
||||||
|
))
|
||||||
|
@else
|
||||||
|
<input type="hidden"
|
||||||
|
name="shopping_location_id"
|
||||||
|
id="shopping_location_id"
|
||||||
|
value="1">
|
||||||
|
@endif
|
||||||
|
|
||||||
@include('components.userfieldsform', array(
|
@include('components.userfieldsform', array(
|
||||||
'userfields' => $userfields,
|
'userfields' => $userfields,
|
||||||
'entity' => 'shopping_lists'
|
'entity' => 'shopping_lists'
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user