invoiceninja/app/Ninja/Repositories/ProductRepository.php

90 lines
2.4 KiB
PHP
Raw Normal View History

2016-07-21 15:35:23 +03:00
<?php namespace App\Ninja\Repositories;
2015-11-06 00:37:04 +02:00
use DB;
2016-05-02 16:12:37 +03:00
use App\Models\Product;
2015-11-06 00:37:04 +02:00
class ProductRepository extends BaseRepository
{
public function getClassName()
{
return 'App\Models\Product';
}
2016-05-31 23:15:55 +03:00
public function all()
{
return Product::scope()
->withTrashed()
->get();
}
2015-11-06 00:37:04 +02:00
public function find($accountId)
{
return DB::table('products')
->leftJoin('tax_rates', function($join) {
$join->on('tax_rates.id', '=', 'products.default_tax_rate_id')
->whereNull('tax_rates.deleted_at');
})
->where('products.account_id', '=', $accountId)
->where('products.deleted_at', '=', null)
->select(
'products.public_id',
'products.product_key',
'products.notes',
'products.cost',
'tax_rates.name as tax_name',
'tax_rates.rate as tax_rate',
'products.deleted_at'
);
}
2016-05-31 23:15:55 +03:00
2016-07-21 15:35:23 +03:00
public function save($data, $product = null)
2016-05-02 16:12:37 +03:00
{
$publicId = isset($data['public_id']) ? $data['public_id'] : false;
2016-05-31 23:15:55 +03:00
if ($product) {
// do nothing
} elseif ($publicId) {
2016-05-02 16:12:37 +03:00
$product = Product::scope($publicId)->firstOrFail();
\Log::warning('Entity not set in product repo save');
2016-05-02 16:12:37 +03:00
} else {
$product = Product::createNew();
}
$product->fill($data);
$product->save();
return $product;
}
2016-08-06 20:54:56 +03:00
public function findPhonetically($productName)
{
$productNameMeta = metaphone($productName);
$map = [];
2016-08-13 22:19:37 +03:00
$max = SIMILAR_MIN_THRESHOLD;
2016-08-06 20:54:56 +03:00
$productId = 0;
2016-08-13 22:19:37 +03:00
$products = Product::scope()
->with('default_tax_rate')
->get();
2016-08-06 20:54:56 +03:00
foreach ($products as $product) {
if ( ! $product->product_key) {
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 22:19:37 +03:00
return ($productId && isset($map[$productId])) ? $map[$productId] : null;
2016-08-06 20:54:56 +03:00
}
2016-05-31 23:15:55 +03:00
}