diff --git a/Core/Lib/AjaxForms/PurchasesController.php b/Core/Lib/AjaxForms/PurchasesController.php index dce39d33ee..d7d8137ec4 100644 --- a/Core/Lib/AjaxForms/PurchasesController.php +++ b/Core/Lib/AjaxForms/PurchasesController.php @@ -19,7 +19,6 @@ namespace FacturaScripts\Core\Lib\AjaxForms; -use FacturaScripts\Core\Base\DataBase\DataBaseWhere; use FacturaScripts\Core\DataSrc\Series; use FacturaScripts\Core\Lib\Calculator; use FacturaScripts\Core\Lib\ExtendedController\BaseView; @@ -29,7 +28,10 @@ use FacturaScripts\Core\Model\Base\BusinessDocumentLine; use FacturaScripts\Core\Model\Base\PurchaseDocument; use FacturaScripts\Core\Tools; +use FacturaScripts\Core\Where; use FacturaScripts\Dinamic\Lib\AssetManager; +use FacturaScripts\Dinamic\Model\Producto; +use FacturaScripts\Dinamic\Model\ProductoProveedor; use FacturaScripts\Dinamic\Model\Proveedor; use FacturaScripts\Dinamic\Model\Variante; use Throwable; @@ -118,8 +120,8 @@ protected function autocompleteProductAction(): bool $variante = new Variante(); $query = (string)$this->request->queryOrInput('term'); $where = [ - new DataBaseWhere('p.bloqueado', 0), - new DataBaseWhere('p.secompra', 1) + Where::eq('p.bloqueado', 0), + Where::eq('p.secompra', 1) ]; foreach ($variante->codeModelSearch($query, 'referencia', $where) as $value) { $list[] = [ @@ -128,6 +130,36 @@ protected function autocompleteProductAction(): bool ]; } + // buscar tambiƩn por referencia del proveedor + if (!empty($query)) { + $addedRefs = array_column($list, 'key'); + $codproveedor = $this->request->queryOrInput('codproveedor'); + $whereProv = [ + Where::like('LOWER(pp.refproveedor)', strtolower($query) . '%'), + Where::eq('p.bloqueado', 0), + Where::eq('p.secompra', 1) + ]; + if (!empty($codproveedor)) { + $whereProv[] = Where::eq('pp.codproveedor', $codproveedor); + } + $sql = 'SELECT pp.referencia, pp.refproveedor, p.descripcion' + . ' FROM ' . ProductoProveedor::tableName() . ' pp' + . ' INNER JOIN ' . Variante::tableName() . ' v ON v.referencia = pp.referencia' + . ' INNER JOIN ' . Producto::tableName() . ' p ON p.idproducto = v.idproducto' + . Where::multiSql($whereProv) + . ' ORDER BY pp.refproveedor ASC'; + foreach ($this->db()->selectLimit($sql, 10) as $row) { + if (in_array($row['referencia'], $addedRefs)) { + continue; + } + $addedRefs[] = $row['referencia']; + $list[] = [ + 'key' => Tools::fixHtml($row['referencia']), + 'value' => Tools::fixHtml($row['refproveedor'] . ' - ' . $row['descripcion']) + ]; + } + } + if (empty($list)) { $list[] = ['key' => null, 'value' => Tools::trans('no-data')]; } @@ -346,6 +378,7 @@ protected function recalculateAction(bool $renderLines): bool $formData = json_decode($this->request->input('data'), true); PurchasesHeaderHTML::apply($model, $formData); PurchasesFooterHTML::apply($model, $formData); + $this->resolveSupplierRefs($formData, $model->codproveedor); PurchasesLineHTML::apply($model, $lines, $formData); Calculator::calculate($model, $lines, false); @@ -515,6 +548,42 @@ protected function saveStatusAction(): bool return false; } + private function resolveSupplierRef(string $reference, string $codproveedor): string + { + $variant = new Variante(); + if ($variant->loadWhere([Where::eq('referencia', $reference)])) { + return $reference; + } + + $where = [Where::eq('refproveedor', $reference)]; + if (!empty($codproveedor)) { + $where[] = Where::eq('codproveedor', $codproveedor); + } + $rows = ProductoProveedor::all($where, [], 0, 1); + return empty($rows) ? $reference : $rows[0]->referencia; + } + + private function resolveSupplierRefs(array &$formData, string $codproveedor): void + { + foreach ($formData as $key => $value) { + if (!empty($value) && str_starts_with($key, 'referencia_n')) { + $formData[$key] = $this->resolveSupplierRef((string)$value, $codproveedor); + } + } + + if (!empty($formData['selectedLine'])) { + $formData['selectedLine'] = $this->resolveSupplierRef((string)$formData['selectedLine'], $codproveedor); + } + + if (($formData['action'] ?? '') === 'fast-line' && !empty($formData['fastli'])) { + $resolved = $this->resolveSupplierRef((string)$formData['fastli'], $codproveedor); + if ($resolved !== $formData['fastli']) { + $formData['action'] = 'fast-product'; + $formData['selectedLine'] = $resolved; + } + } + } + private function sendJsonWithLogs(array $data): void { $data['messages'] = Tools::log()::read('master', $this->logLevels); diff --git a/Core/View/Tab/PurchasesDocument.html.twig b/Core/View/Tab/PurchasesDocument.html.twig index 922c1e9887..daf6b158c4 100644 --- a/Core/View/Tab/PurchasesDocument.html.twig +++ b/Core/View/Tab/PurchasesDocument.html.twig @@ -37,7 +37,7 @@ $.ajax({ method: "POST", url: '{{ fsc.url() }}', - data: {action: 'autocomplete-product', term: request.term}, + data: {action: 'autocomplete-product', term: request.term, codproveedor: document.forms['purchasesForm']['codproveedor'] ? document.forms['purchasesForm']['codproveedor'].value : ''}, dataType: "json", success: function (results) { let values = [];