invoiceninja/app/Ninja/Repositories/ProductRepository.php

112 lines
3.3 KiB
PHP
Raw Permalink Normal View History

2017-01-30 21:40:43 +02:00
<?php
namespace App\Ninja\Repositories;
2015-11-06 00:37:04 +02:00
2016-05-02 16:12:37 +03:00
use App\Models\Product;
use App\Events\ProductWasCreated;
use App\Events\ProductWasUpdated;
use Utils;
2017-01-30 21:40:43 +02:00
use DB;
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()
->where('is_deleted', '=', false)
2016-05-31 23:15:55 +03:00
->get();
}
2016-10-18 21:15:08 +03:00
public function find($accountId, $filter = null)
2015-11-06 00:37:04 +02:00
{
2016-09-23 17:00:47 +03:00
$query = DB::table('products')
2015-11-06 00:37:04 +02: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 19:47:26 +02:00
'products.deleted_at',
2018-02-14 13:25:23 +02:00
'products.is_deleted',
'products.custom_value1',
'products.custom_value2'
2015-11-06 00:37:04 +02:00
);
2016-09-23 17:00:47 +03:00
2016-10-18 21:15:08 +03:00
if ($filter) {
$query->where(function ($query) use ($filter) {
$query->where('products.product_key', 'like', '%'.$filter.'%')
2018-02-21 16:25:05 +02:00
->orWhere('products.notes', 'like', '%'.$filter.'%')
->orWhere('products.custom_value1', 'like', '%'.$filter.'%')
->orWhere('products.custom_value2', 'like', '%'.$filter.'%');
2016-10-18 21:15:08 +03:00
});
}
2016-11-18 15:31:43 +02:00
$this->applyFilters($query, ENTITY_PRODUCT);
2016-09-23 17:00:47 +03:00
return $query;
2015-11-06 00:37:04 +02:00
}
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) {
$product = Product::scope($publicId)->withArchived()->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);
2017-03-09 13:38:17 +02: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 12:23:56 +02:00
$product->qty = isset($data['qty']) ? Utils::parseFloat($data['qty']) : 1;
2016-05-02 16:12:37 +03:00
$product->save();
if ($publicId) {
event(new ProductWasUpdated($product, $data));
} else {
event(new ProductWasCreated($product, $data));
}
2016-05-02 16:12:37 +03:00
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;
$products = Product::scope()->get();
2016-08-06 20:54:56 +03:00
foreach ($products as $product) {
2017-01-30 18:05:31 +02:00
if (! $product->product_key) {
2016-08-06 20:54:56 +03: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 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
}