invoiceninja/app/Ninja/Repositories/ProductRepository.php

100 lines
2.8 KiB
PHP
Raw 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 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()
->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',
'products.is_deleted'
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.'%')
->orWhere('products.notes', 'like', '%'.$filter.'%');
});
}
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) {
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);
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();
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
}