1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Ninja/Repositories/ProductRepository.php

101 lines
2.8 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Ninja\Repositories;
2015-11-05 23:37:04 +01:00
2016-05-02 15:12:37 +02:00
use App\Models\Product;
use Utils;
2017-01-30 20:40:43 +01:00
use DB;
2015-11-05 23:37:04 +01:00
class ProductRepository extends BaseRepository
{
public function getClassName()
{
return 'App\Models\Product';
}
2016-05-31 22:15:55 +02:00
public function all()
{
return Product::scope()
->withTrashed()
->where('is_deleted', '=', false)
2016-05-31 22:15:55 +02:00
->get();
}
2016-10-18 20:15:08 +02:00
public function find($accountId, $filter = null)
2015-11-05 23:37:04 +01:00
{
2016-09-23 16:00:47 +02:00
$query = DB::table('products')
2015-11-05 23:37:04 +01:00
->where('products.account_id', '=', $accountId)
->select(
'products.public_id',
'products.product_key',
'products.notes',
'products.cost',
'products.tax_name1 as tax_name',
'products.tax_rate1 as tax_rate',
2016-11-29 18:47:26 +01:00
'products.deleted_at',
'products.is_deleted'
2015-11-05 23:37:04 +01:00
);
2016-09-23 16:00:47 +02:00
2016-10-18 20:15:08 +02:00
if ($filter) {
$query->where(function ($query) use ($filter) {
$query->where('products.product_key', 'like', '%'.$filter.'%')
->orWhere('products.notes', 'like', '%'.$filter.'%');
});
}
2016-11-18 14:31:43 +01:00
$this->applyFilters($query, ENTITY_PRODUCT);
2016-09-23 16:00:47 +02:00
return $query;
2015-11-05 23:37:04 +01:00
}
2016-05-31 22:15:55 +02:00
2016-07-21 14:35:23 +02:00
public function save($data, $product = null)
2016-05-02 15:12:37 +02:00
{
$publicId = isset($data['public_id']) ? $data['public_id'] : false;
2016-05-31 22:15:55 +02:00
if ($product) {
// do nothing
} elseif ($publicId) {
$product = Product::scope($publicId)->withArchived()->firstOrFail();
\Log::warning('Entity not set in product repo save');
2016-05-02 15:12:37 +02:00
} else {
$product = Product::createNew();
}
$product->fill($data);
2017-03-09 12:38:17 +01:00
$product->product_key = isset($data['product_key']) ? trim($data['product_key']) : '';
$product->notes = isset($data['notes']) ? trim($data['notes']) : '';
$product->cost = isset($data['cost']) ? Utils::parseFloat($data['cost']) : 0;
2017-03-20 11:23:56 +01:00
$product->qty = isset($data['qty']) ? Utils::parseFloat($data['qty']) : 1;
2016-05-02 15:12:37 +02:00
$product->save();
return $product;
}
2016-08-06 19:54:56 +02:00
public function findPhonetically($productName)
{
$productNameMeta = metaphone($productName);
$map = [];
2016-08-13 21:19:37 +02:00
$max = SIMILAR_MIN_THRESHOLD;
2016-08-06 19:54:56 +02:00
$productId = 0;
$products = Product::scope()->get();
2016-08-06 19:54:56 +02:00
foreach ($products as $product) {
2017-01-30 17:05:31 +01:00
if (! $product->product_key) {
2016-08-06 19:54:56 +02:00
continue;
}
$map[$product->id] = $product;
$similar = similar_text($productNameMeta, metaphone($product->product_key), $percent);
if ($percent > $max) {
$productId = $product->id;
$max = $percent;
}
}
2016-08-13 21:19:37 +02:00
return ($productId && isset($map[$productId])) ? $map[$productId] : null;
2016-08-06 19:54:56 +02:00
}
2016-05-31 22:15:55 +02:00
}