new: allow to delete seuil lines in price-list's pice modal
This commit is contained in:
@@ -71,7 +71,10 @@ class PriceListValueController extends Controller
|
|||||||
|
|
||||||
public function addPrice($index)
|
public function addPrice($index)
|
||||||
{
|
{
|
||||||
$data['index'] = $index;
|
$data = [
|
||||||
|
'index' => $index,
|
||||||
|
'taxes' => Taxes::getOptions(),
|
||||||
|
];
|
||||||
|
|
||||||
return view('Admin.Shop.PriceListValues.partials.row_price', $data);
|
return view('Admin.Shop.PriceListValues.partials.row_price', $data);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,6 +52,17 @@ class PriceListValues
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function purgeRemovedValues($price_list_id, array $ids)
|
||||||
|
{
|
||||||
|
if (! count($ids)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
PriceListValue::byPriceList($price_list_id)
|
||||||
|
->whereIn('id', $ids)
|
||||||
|
->delete();
|
||||||
|
}
|
||||||
|
|
||||||
protected static function hasPrice($value): bool
|
protected static function hasPrice($value): bool
|
||||||
{
|
{
|
||||||
if (! array_key_exists('price', $value)) {
|
if (! array_key_exists('price', $value)) {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class PriceLists
|
|||||||
'taxes' => Taxes::getOptions(),
|
'taxes' => Taxes::getOptions(),
|
||||||
'price_list' => [
|
'price_list' => [
|
||||||
'tariff_id' => $tariffId,
|
'tariff_id' => $tariffId,
|
||||||
'price_list_values' => array_fill(0, 3, ''),
|
'price_list_values' => [],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -50,9 +50,8 @@ class PriceLists
|
|||||||
public static function edit($id)
|
public static function edit($id)
|
||||||
{
|
{
|
||||||
$price_list = self::getFull($id)->toArray();
|
$price_list = self::getFull($id)->toArray();
|
||||||
$n = count($price_list['price_list_values']);
|
if (count($price_list['price_list_values']) === 0) {
|
||||||
if ($n <= 3) {
|
$price_list['price_list_values'][] = [];
|
||||||
$price_list['price_list_values'] += array_fill($n, 3 - $n, '');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $price_list;
|
return $price_list;
|
||||||
@@ -71,9 +70,14 @@ class PriceLists
|
|||||||
public static function store($data)
|
public static function store($data)
|
||||||
{
|
{
|
||||||
$id = $data['id'] ?? false;
|
$id = $data['id'] ?? false;
|
||||||
$price_list_values = $data['price_list_values'] ?? false;
|
$price_list_values = $data['price_list_values'] ?? [];
|
||||||
|
$deleted_values = array_map('intval', array_filter($data['deleted_price_list_value_ids'] ?? [], function ($value) {
|
||||||
|
return $value !== null && $value !== '';
|
||||||
|
}));
|
||||||
unset($data['price_list_values']);
|
unset($data['price_list_values']);
|
||||||
|
unset($data['deleted_price_list_value_ids']);
|
||||||
$price_list = $id ? self::update($data) : self::create($data);
|
$price_list = $id ? self::update($data) : self::create($data);
|
||||||
|
PriceListValues::purgeRemovedValues($price_list->id, $deleted_values);
|
||||||
PriceListValues::storePrices($price_list->id, $price_list_values);
|
PriceListValues::storePrices($price_list->id, $price_list_values);
|
||||||
|
|
||||||
return $price_list;
|
return $price_list;
|
||||||
|
|||||||
@@ -15,4 +15,9 @@
|
|||||||
<td>
|
<td>
|
||||||
@include('components.form.inputs.money', ['name' => 'price_list_values[' . $index . '][price_taxed]', 'value' => $price_list_value['price_taxed'] ?? null, 'required' => true, 'class' => 'price_taxed'])
|
@include('components.form.inputs.money', ['name' => 'price_list_values[' . $index . '][price_taxed]', 'value' => $price_list_value['price_taxed'] ?? null, 'required' => true, 'class' => 'price_taxed'])
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
<td class="text-center align-middle">
|
||||||
|
<button type="button" class="btn btn-outline-danger btn-xs remove-price" title="{{ __('Supprimer') }}">
|
||||||
|
<i class="fa fa-trash"></i>
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|||||||
@@ -30,12 +30,18 @@
|
|||||||
<th>Unit. HT</th>
|
<th>Unit. HT</th>
|
||||||
<th>TVA</th>
|
<th>TVA</th>
|
||||||
<th>Unit. TTC</th>
|
<th>Unit. TTC</th>
|
||||||
|
<th class="text-center" style="width: 60px;">Actions</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@foreach ($price_list['price_list_values'] as $price_list_value)
|
@php($priceListValues = $price_list['price_list_values'] ?? [])
|
||||||
@include('Admin.Shop.PriceListValues.partials.row_price', ['index' => $loop->index])
|
@php($nextIndex = count($priceListValues))
|
||||||
|
|
||||||
|
@foreach ($priceListValues as $index => $price_list_value)
|
||||||
|
@include('Admin.Shop.PriceListValues.partials.row_price', ['index' => $index, 'price_list_value' => $price_list_value])
|
||||||
@endforeach
|
@endforeach
|
||||||
|
|
||||||
|
@include('Admin.Shop.PriceListValues.partials.row_price', ['index' => $nextIndex, 'price_list_value' => []])
|
||||||
</tbody>
|
</tbody>
|
||||||
<tfoot>
|
<tfoot>
|
||||||
<tr>
|
<tr>
|
||||||
@@ -49,21 +55,30 @@
|
|||||||
</tfoot>
|
</tfoot>
|
||||||
</table>
|
</table>
|
||||||
@endcomponent
|
@endcomponent
|
||||||
|
<div id="deleted-price-list-values"></div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
var priceRowIndex = 0;
|
||||||
|
var lastRemovedIndex = null;
|
||||||
|
|
||||||
function handleAddPrice() {
|
function handleAddPrice() {
|
||||||
$('#add_price').click( function () {
|
$('#add_price').click( function () {
|
||||||
var index = $('#prices-table tbody tr').length;
|
addEmptyPriceRow();
|
||||||
$.get("{{ route('Admin.Shop.PriceListValues.addPrice') }}/" + index, function(data) {
|
|
||||||
$("#prices-table").append(data);
|
|
||||||
})
|
|
||||||
handlePrices();
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function addEmptyPriceRow() {
|
||||||
|
var index = nextPriceRowIndex();
|
||||||
|
$.get("{{ route('Admin.Shop.PriceListValues.addPrice') }}/" + index, function(data) {
|
||||||
|
$("#prices-table tbody").append(data);
|
||||||
|
handlePrices();
|
||||||
|
handleRemovePrice();
|
||||||
|
lastRemovedIndex = null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function handle_prices() {
|
function handle_prices() {
|
||||||
$('.price').change(function() {
|
$('.price').change(function() {
|
||||||
$col_tax = $(this).parent().parent().find('.tax');
|
$col_tax = $(this).parent().parent().find('.tax');
|
||||||
@@ -101,9 +116,82 @@
|
|||||||
handle_prices_taxed();
|
handle_prices_taxed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleRemovePrice() {
|
||||||
|
$('#prices-table').off('click', '.remove-price').on('click', '.remove-price', function() {
|
||||||
|
var $row = $(this).closest('tr');
|
||||||
|
var idx = extractRowIndex($row);
|
||||||
|
var id = $row.find('input[name$="[id]"]').val();
|
||||||
|
if (id) {
|
||||||
|
registerDeletedPrice(id);
|
||||||
|
}
|
||||||
|
$row.remove();
|
||||||
|
lastRemovedIndex = idx;
|
||||||
|
ensureAtLeastOneRow();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function registerDeletedPrice(id) {
|
||||||
|
var $container = $('#deleted-price-list-values');
|
||||||
|
if ($container.find('input[value="' + id + '"]').length === 0) {
|
||||||
|
$container.append('<input type="hidden" name="deleted_price_list_value_ids[]" value="' + id + '">');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function ensureAtLeastOneRow() {
|
||||||
|
if ($('#prices-table tbody tr').length === 0) {
|
||||||
|
if (lastRemovedIndex !== null) {
|
||||||
|
$.get("{{ route('Admin.Shop.PriceListValues.addPrice') }}/" + lastRemovedIndex, function(data) {
|
||||||
|
$("#prices-table tbody").append(data);
|
||||||
|
handlePrices();
|
||||||
|
handleRemovePrice();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
addEmptyPriceRow();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function computeStartingIndex() {
|
||||||
|
var maxIndex = -1;
|
||||||
|
$('#prices-table tbody tr').each(function() {
|
||||||
|
var $idInput = $(this).find('input[name$="[id]"]');
|
||||||
|
var name = $idInput.attr('name');
|
||||||
|
if (name) {
|
||||||
|
var matches = name.match(/price_list_values\[(\d+)\]\[id\]/);
|
||||||
|
if (matches && matches[1]) {
|
||||||
|
var idx = parseInt(matches[1], 10);
|
||||||
|
if (!isNaN(idx) && idx > maxIndex) {
|
||||||
|
maxIndex = idx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return maxIndex + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function nextPriceRowIndex() {
|
||||||
|
return priceRowIndex++;
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractRowIndex($row) {
|
||||||
|
var $idInput = $row.find('input[name$="[id]"]');
|
||||||
|
var name = $idInput.attr('name');
|
||||||
|
if (!name) {
|
||||||
|
return priceRowIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
var matches = name.match(/price_list_values\[(\d+)\]\[id\]/);
|
||||||
|
|
||||||
|
return matches && matches[1] ? parseInt(matches[1], 10) : priceRowIndex;
|
||||||
|
}
|
||||||
|
|
||||||
$(function() {
|
$(function() {
|
||||||
|
priceRowIndex = computeStartingIndex();
|
||||||
handleAddPrice();
|
handleAddPrice();
|
||||||
handlePrices();
|
handlePrices();
|
||||||
|
handleRemovePrice();
|
||||||
|
ensureAtLeastOneRow();
|
||||||
});
|
});
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user