April 2026 waren Wirtschaft Feedback
This commit is contained in:
parent
02f2a4c23e
commit
9ce711d6b2
167 changed files with 25278 additions and 8518 deletions
44
app/Repositories/PackagingItemRepository.php
Normal file
44
app/Repositories/PackagingItemRepository.php
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Models\PackagingItem;
|
||||
|
||||
class PackagingItemRepository
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function create(array $data): PackagingItem
|
||||
{
|
||||
return PackagingItem::create($this->extractAttributes($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function update(PackagingItem $packagingItem, array $data): PackagingItem
|
||||
{
|
||||
$packagingItem->update($this->extractAttributes($data));
|
||||
|
||||
return $packagingItem->fresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function extractAttributes(array $data): array
|
||||
{
|
||||
return collect($data)->only([
|
||||
'packaging_material_id',
|
||||
'supplier_id',
|
||||
'name',
|
||||
'category',
|
||||
'weight_grams',
|
||||
'min_stock_alert',
|
||||
'product_id',
|
||||
'active',
|
||||
])->all();
|
||||
}
|
||||
}
|
||||
|
|
@ -2,26 +2,23 @@
|
|||
|
||||
namespace App\Repositories;
|
||||
|
||||
|
||||
|
||||
use App\Models\CountryPrice;
|
||||
use App\Models\Attribute;
|
||||
use App\Models\CountryPrice;
|
||||
use App\Models\Ingredient;
|
||||
use App\Models\Product;
|
||||
use App\Models\ProductAttribute;
|
||||
use App\Models\ProductCategory;
|
||||
use App\Models\ProductImage;
|
||||
use App\Models\ProductIngredient;
|
||||
use App\Services\Slim;
|
||||
|
||||
|
||||
class ProductRepository extends BaseRepository {
|
||||
|
||||
|
||||
class ProductRepository extends BaseRepository
|
||||
{
|
||||
public function __construct(Product $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* refresh.
|
||||
*/
|
||||
|
|
@ -37,59 +34,211 @@ class ProductRepository extends BaseRepository {
|
|||
$data['max_buy'] = isset($data['max_buy']) ? 1 : 0;
|
||||
$data['show_on'] = isset($data['show_on']) ? $data['show_on'] : null;
|
||||
|
||||
if($data['id'] === "new"){
|
||||
$this->model = Product::create($data);
|
||||
if (array_key_exists('shelf_life_type', $data)) {
|
||||
if ($data['shelf_life_type'] === '' || $data['shelf_life_type'] === null) {
|
||||
$data['shelf_life_type'] = null;
|
||||
$data['shelf_life_months'] = null;
|
||||
} elseif ($data['shelf_life_type'] === 'pao') {
|
||||
$data['shelf_life_months'] = null;
|
||||
} elseif ($data['shelf_life_type'] === 'fixed' && array_key_exists('shelf_life_months', $data) && $data['shelf_life_months'] !== '' && $data['shelf_life_months'] !== null) {
|
||||
$data['shelf_life_months'] = (int) $data['shelf_life_months'];
|
||||
}
|
||||
}
|
||||
else{
|
||||
|
||||
if ($data['id'] === 'new') {
|
||||
$this->model = Product::create($data);
|
||||
} else {
|
||||
$this->model = $this->getById($data['id']);
|
||||
$this->model->fill($data);
|
||||
$this->model->save();
|
||||
}
|
||||
|
||||
|
||||
$this->updateCategories(isset($data['categories']) ? $data['categories'] : []);
|
||||
$this->updateAttributes(isset($data['attributes']) ? $data['attributes'] : []);
|
||||
|
||||
|
||||
$this->updateWLVariants(isset($data['whitelabel_variants']) ? $data['whitelabel_variants'] : []);
|
||||
$this->updateWLImageAttributs(isset($data['image_wl_attributes']) ? $data['image_wl_attributes'] : [] , isset($data['whitelabel_variants']) ? $data['whitelabel_variants'] : []);
|
||||
$this->updateWLImageAttributs(isset($data['image_wl_attributes']) ? $data['image_wl_attributes'] : [], isset($data['whitelabel_variants']) ? $data['whitelabel_variants'] : []);
|
||||
|
||||
$this->updateIngredients(isset($data['product_ingredients']) ? $data['product_ingredients'] : []);
|
||||
$this->updateIngredients($data);
|
||||
$this->updateManufacturerIngredients($data);
|
||||
$this->updatePackagings($data);
|
||||
$this->updateCountryPrices($data);
|
||||
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
|
||||
public function updateIngredients($data = array())
|
||||
public function updatePackagings(array $data = []): bool
|
||||
{
|
||||
$ProductIngredient = $this->model->p_ingredients()->pluck('ingredient_id')->toArray();
|
||||
//set attr
|
||||
if(is_array($data)){
|
||||
foreach ($data as $id) {
|
||||
//not use
|
||||
if(!in_array($id, $ProductIngredient)){
|
||||
ProductIngredient::create([
|
||||
'product_id' => $this->model->id,
|
||||
'ingredient_id' => $id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
if (! isset($data['pp_packaging_item_id']) || ! is_array($data['pp_packaging_item_id'])) {
|
||||
$this->model->packagings()->detach();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
$ids = $data['pp_packaging_item_id'];
|
||||
$quantities = $data['pp_quantity'] ?? [];
|
||||
$syncData = [];
|
||||
foreach ($ids as $index => $packagingItemId) {
|
||||
$pid = (int) $packagingItemId;
|
||||
if ($pid <= 0) {
|
||||
continue;
|
||||
}
|
||||
$qtyRaw = $quantities[$index] ?? null;
|
||||
$qty = $this->parseNullableDecimal($qtyRaw);
|
||||
if ($qty === null || $qty <= 0) {
|
||||
$qty = 1.0;
|
||||
}
|
||||
$syncData[$pid] = [
|
||||
'quantity' => $qty,
|
||||
'pos' => (int) $index,
|
||||
];
|
||||
}
|
||||
|
||||
$this->model->packagings()->sync($syncData);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function updateCategories($data = array())
|
||||
public function updateIngredients(array $data = []): bool
|
||||
{
|
||||
if (! array_key_exists('product_inci_sync_sent', $data)) {
|
||||
if (isset($data['product_ingredients']) && is_array($data['product_ingredients'])) {
|
||||
$ids = array_values(array_unique(array_filter(array_map('intval', $data['product_ingredients']), fn (int $id) => $id > 0)));
|
||||
$defaults = Ingredient::whereIn('id', $ids)->pluck('default_factor', 'id');
|
||||
|
||||
ProductIngredient::where('product_id', $this->model->id)
|
||||
->where('recipe_type', 'product')
|
||||
->delete();
|
||||
|
||||
foreach ($ids as $index => $ingredientId) {
|
||||
$factor = $defaults[$ingredientId] ?? 1.10;
|
||||
ProductIngredient::create([
|
||||
'product_id' => $this->model->id,
|
||||
'ingredient_id' => $ingredientId,
|
||||
'pos' => $index,
|
||||
'gram' => null,
|
||||
'factor' => $factor,
|
||||
'recipe_type' => 'product',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isset($data['pi_ingredient_id']) && is_array($data['pi_ingredient_id'])) {
|
||||
$ids = $data['pi_ingredient_id'];
|
||||
$grams = $data['pi_gram'] ?? [];
|
||||
$factors = $data['pi_factor'] ?? [];
|
||||
|
||||
ProductIngredient::where('product_id', $this->model->id)
|
||||
->where('recipe_type', 'product')
|
||||
->delete();
|
||||
|
||||
foreach ($ids as $index => $ingredientId) {
|
||||
$ingredientId = (int) $ingredientId;
|
||||
if ($ingredientId <= 0) {
|
||||
continue;
|
||||
}
|
||||
ProductIngredient::create([
|
||||
'product_id' => $this->model->id,
|
||||
'ingredient_id' => $ingredientId,
|
||||
'pos' => $index,
|
||||
'gram' => $this->parseNullableDecimal($grams[$index] ?? null),
|
||||
'factor' => $this->parseFactor($factors[$index] ?? null, $ingredientId),
|
||||
'recipe_type' => 'product',
|
||||
]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
ProductIngredient::where('product_id', $this->model->id)
|
||||
->where('recipe_type', 'product')
|
||||
->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function updateManufacturerIngredients(array $data = []): bool
|
||||
{
|
||||
if (! array_key_exists('manufacturer_inci_sync_sent', $data)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isset($data['mfg_ingredient_id']) && is_array($data['mfg_ingredient_id'])) {
|
||||
$ids = $data['mfg_ingredient_id'];
|
||||
$grams = $data['mfg_gram'] ?? [];
|
||||
$factors = $data['mfg_factor'] ?? [];
|
||||
|
||||
ProductIngredient::where('product_id', $this->model->id)
|
||||
->where('recipe_type', 'manufacturer')
|
||||
->delete();
|
||||
|
||||
foreach ($ids as $index => $ingredientId) {
|
||||
$ingredientId = (int) $ingredientId;
|
||||
if ($ingredientId <= 0) {
|
||||
continue;
|
||||
}
|
||||
ProductIngredient::create([
|
||||
'product_id' => $this->model->id,
|
||||
'ingredient_id' => $ingredientId,
|
||||
'pos' => $index,
|
||||
'gram' => $this->parseNullableDecimal($grams[$index] ?? null),
|
||||
'factor' => $this->parseFactor($factors[$index] ?? null, $ingredientId),
|
||||
'recipe_type' => 'manufacturer',
|
||||
]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
ProductIngredient::where('product_id', $this->model->id)
|
||||
->where('recipe_type', 'manufacturer')
|
||||
->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function parseNullableDecimal(mixed $value): ?float
|
||||
{
|
||||
if ($value === null || $value === '') {
|
||||
return null;
|
||||
}
|
||||
if (is_numeric($value)) {
|
||||
return (float) $value;
|
||||
}
|
||||
$normalized = reFormatNumber((string) $value);
|
||||
|
||||
return $normalized !== null && $normalized !== '' ? (float) $normalized : null;
|
||||
}
|
||||
|
||||
private function parseFactor(mixed $value, int $ingredientId): float
|
||||
{
|
||||
if ($value === null || $value === '') {
|
||||
$default = Ingredient::whereKey($ingredientId)->value('default_factor');
|
||||
|
||||
return $default !== null ? (float) $default : 1.10;
|
||||
}
|
||||
if (is_numeric($value)) {
|
||||
return (float) $value;
|
||||
}
|
||||
$normalized = reFormatNumber((string) $value);
|
||||
|
||||
return $normalized !== null && $normalized !== '' ? (float) $normalized : 1.10;
|
||||
}
|
||||
|
||||
public function updateCategories($data = [])
|
||||
{
|
||||
foreach ($this->model->categories as $category) {
|
||||
if(($pos = array_search($category->category_id, $data)) !== FALSE){
|
||||
if (($pos = array_search($category->category_id, $data)) !== false) {
|
||||
unset($data[$pos]);
|
||||
}else{
|
||||
} else {
|
||||
$category->delete();
|
||||
}
|
||||
}
|
||||
//set attr
|
||||
if(is_array($data)){
|
||||
// set attr
|
||||
if (is_array($data)) {
|
||||
foreach ($data as $id) {
|
||||
ProductCategory::create([
|
||||
'product_id' => $this->model->id,
|
||||
|
|
@ -97,20 +246,21 @@ class ProductRepository extends BaseRepository {
|
|||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function updateAttributes($data = array())
|
||||
public function updateAttributes($data = [])
|
||||
{
|
||||
foreach ($this->model->attributes as $attribute) {
|
||||
if(($pos = array_search($attribute->attribute_id, $data)) !== FALSE){
|
||||
if (($pos = array_search($attribute->attribute_id, $data)) !== false) {
|
||||
unset($data[$pos]);
|
||||
}else{
|
||||
} else {
|
||||
$attribute->delete();
|
||||
}
|
||||
}
|
||||
//set attr
|
||||
if(is_array($data)){
|
||||
// set attr
|
||||
if (is_array($data)) {
|
||||
foreach ($data as $id) {
|
||||
$attribute = Attribute::findOrFail($id);
|
||||
ProductAttribute::create([
|
||||
|
|
@ -120,20 +270,21 @@ class ProductRepository extends BaseRepository {
|
|||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function updateWLVariants($data = array())
|
||||
public function updateWLVariants($data = [])
|
||||
{
|
||||
foreach ($this->model->attribute_variants as $variant) {
|
||||
if(($pos = array_search($variant->attribute_id, $data)) !== FALSE){
|
||||
if (($pos = array_search($variant->attribute_id, $data)) !== false) {
|
||||
unset($data[$pos]);
|
||||
}else{
|
||||
} else {
|
||||
$variant->delete();
|
||||
}
|
||||
}
|
||||
//set attr
|
||||
if(is_array($data)){
|
||||
// set attr
|
||||
if (is_array($data)) {
|
||||
foreach ($data as $id) {
|
||||
$attribute = Attribute::findOrFail($id);
|
||||
ProductAttribute::create([
|
||||
|
|
@ -143,34 +294,32 @@ class ProductRepository extends BaseRepository {
|
|||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function updateWLImageAttributs($attributes = [], $variants = [])
|
||||
{
|
||||
//abgleich der attributes gegen die variants
|
||||
// abgleich der attributes gegen die variants
|
||||
foreach ($attributes as $image => $value) {
|
||||
foreach ($value as $k => $val) {
|
||||
if(!is_array($variants) || !in_array($val, $variants)){
|
||||
if (! is_array($variants) || ! in_array($val, $variants)) {
|
||||
unset($attributes[$image][$k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($this->model->whitelabel_images as $image) {
|
||||
$image->update([
|
||||
'attributes' => isset($attributes[$image->id]) ? $attributes[$image->id] : NULL,
|
||||
]);
|
||||
$image->update([
|
||||
'attributes' => isset($attributes[$image->id]) ? $attributes[$image->id] : null,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public function updateCountryPrices($data)
|
||||
{
|
||||
if(!isset($data['country_prices']) || !is_array($data['country_prices'])){
|
||||
if (! isset($data['country_prices']) || ! is_array($data['country_prices'])) {
|
||||
return false;
|
||||
}
|
||||
foreach ($data['country_prices'] as $k => $country_id) {
|
||||
|
|
@ -178,61 +327,77 @@ class ProductRepository extends BaseRepository {
|
|||
'country_id' => $country_id,
|
||||
'product_id' => $this->model->id,
|
||||
],
|
||||
[
|
||||
'c_price' => isset($data['c_price'][$country_id]) ? reFormatNumber($data['c_price'][$country_id]) : null,
|
||||
'c_tax' => isset($data['c_tax'][$country_id]) ? reFormatNumber($data['c_tax'][$country_id]) : null,
|
||||
'c_price_old' => isset($data['c_price_old'][$country_id]) ? reFormatNumber($data['c_price_old'][$country_id]) : null,
|
||||
'c_currency' => isset($data['c_currency'][$country_id]) ? reFormatNumber($data['c_currency'][$country_id]) : null,
|
||||
]);
|
||||
[
|
||||
'c_price' => isset($data['c_price'][$country_id]) ? reFormatNumber($data['c_price'][$country_id]) : null,
|
||||
'c_tax' => isset($data['c_tax'][$country_id]) ? reFormatNumber($data['c_tax'][$country_id]) : null,
|
||||
'c_price_old' => isset($data['c_price_old'][$country_id]) ? reFormatNumber($data['c_price_old'][$country_id]) : null,
|
||||
'c_currency' => isset($data['c_currency'][$country_id]) ? reFormatNumber($data['c_currency'][$country_id]) : null,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public function copy($model)
|
||||
{
|
||||
$this->model = $model->replicate();
|
||||
$this->model->name = "Kopie: ".$this->model->name;
|
||||
$this->model->name = 'Kopie: '.$this->model->name;
|
||||
$this->model->wp_number = null;
|
||||
$this->model->save();
|
||||
|
||||
//categories
|
||||
foreach ($model->categories as $category){
|
||||
// categories
|
||||
foreach ($model->categories as $category) {
|
||||
ProductCategory::create([
|
||||
'product_id' => $this->model->id,
|
||||
'category_id' => $category->category_id,
|
||||
]);
|
||||
}
|
||||
|
||||
//attributes
|
||||
foreach ($model->attributes as $attribute){
|
||||
// attributes
|
||||
foreach ($model->attributes as $attribute) {
|
||||
ProductAttribute::create([
|
||||
'product_id' => $this->model->id,
|
||||
'type_id' => $this->model->attribute_type_id,
|
||||
'type_id' => $attribute->type_id,
|
||||
'attribute_id' => $attribute->attribute_id,
|
||||
]);
|
||||
}
|
||||
//INCS
|
||||
$ingredients = $model->p_ingredients()->pluck('ingredient_id')->toArray();
|
||||
if(is_array($ingredients)){
|
||||
foreach ($ingredients as $incs_id){
|
||||
ProductIngredient::create([
|
||||
'product_id' => $this->model->id,
|
||||
'ingredient_id' => $incs_id,
|
||||
]);
|
||||
}
|
||||
foreach ($model->p_ingredients()->orderByPivot('pos')->get() as $ing) {
|
||||
ProductIngredient::create([
|
||||
'product_id' => $this->model->id,
|
||||
'ingredient_id' => $ing->id,
|
||||
'pos' => (int) ($ing->pivot->pos ?? 0),
|
||||
'gram' => $ing->pivot->gram,
|
||||
'factor' => $ing->pivot->factor !== null ? (float) $ing->pivot->factor : 1.10,
|
||||
'recipe_type' => 'product',
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
//images
|
||||
foreach ($model->images as $image){
|
||||
$name = \App\Services\Slim::sanitizeFileName($image->original_name);
|
||||
$name = uniqid() . '_' . $name;
|
||||
foreach ($model->manufacturer_ingredients()->orderByPivot('pos')->get() as $ing) {
|
||||
ProductIngredient::create([
|
||||
'product_id' => $this->model->id,
|
||||
'ingredient_id' => $ing->id,
|
||||
'pos' => (int) ($ing->pivot->pos ?? 0),
|
||||
'gram' => $ing->pivot->gram,
|
||||
'factor' => $ing->pivot->factor !== null ? (float) $ing->pivot->factor : 1.10,
|
||||
'recipe_type' => 'manufacturer',
|
||||
]);
|
||||
}
|
||||
|
||||
//copy
|
||||
$packSync = [];
|
||||
foreach ($model->packagings()->orderByPivot('pos')->get() as $pack) {
|
||||
$packSync[$pack->id] = [
|
||||
'quantity' => $pack->pivot->quantity !== null ? (float) $pack->pivot->quantity : 1.0,
|
||||
'pos' => (int) ($pack->pivot->pos ?? 0),
|
||||
];
|
||||
}
|
||||
$this->model->packagings()->sync($packSync);
|
||||
|
||||
// images
|
||||
foreach ($model->images as $image) {
|
||||
$name = Slim::sanitizeFileName($image->original_name);
|
||||
$name = uniqid().'_'.$name;
|
||||
|
||||
// copy
|
||||
$data = \Storage::disk('public')->copy(
|
||||
'images/product/'.$image->product_id.'/'.$image->filename,
|
||||
'images/product/'.$this->model->id.'/'.$name
|
||||
|
|
@ -246,7 +411,7 @@ class ProductRepository extends BaseRepository {
|
|||
'ext' => $image->ext,
|
||||
'mine' => $image->mine,
|
||||
'size' => $image->size,
|
||||
'attributes' => $image->attributes
|
||||
'attributes' => $image->attributes,
|
||||
|
||||
]);
|
||||
}
|
||||
|
|
@ -254,10 +419,5 @@ class ProductRepository extends BaseRepository {
|
|||
return $this->model;
|
||||
}
|
||||
|
||||
|
||||
public function delete()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
public function delete() {}
|
||||
}
|
||||
|
|
|
|||
21
app/Repositories/ProductionRepository.php
Normal file
21
app/Repositories/ProductionRepository.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Models\Production;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class ProductionRepository
|
||||
{
|
||||
/**
|
||||
* @return Collection<int, Production>
|
||||
*/
|
||||
public function listForIndex(): Collection
|
||||
{
|
||||
return Production::query()
|
||||
->with(['product', 'location', 'producedByUser'])
|
||||
->orderByDesc('produced_at')
|
||||
->orderByDesc('id')
|
||||
->get();
|
||||
}
|
||||
}
|
||||
92
app/Repositories/StockEntryRepository.php
Normal file
92
app/Repositories/StockEntryRepository.php
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Models\StockEntry;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class StockEntryRepository
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function create(array $data): StockEntry
|
||||
{
|
||||
$data['unit'] = ($data['entry_type'] ?? '') === 'ingredient' ? 'gram' : 'piece';
|
||||
|
||||
return StockEntry::query()->create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function update(StockEntry $stockEntry, array $data): StockEntry
|
||||
{
|
||||
if (array_key_exists('entry_type', $data)) {
|
||||
$data['unit'] = ($data['entry_type'] ?? '') === 'ingredient' ? 'gram' : 'piece';
|
||||
}
|
||||
|
||||
$stockEntry->update($data);
|
||||
|
||||
return $stockEntry->fresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function receive(StockEntry $stockEntry, array $data): StockEntry
|
||||
{
|
||||
$data['status'] = 'received';
|
||||
$data['received_by'] = auth()->id();
|
||||
|
||||
$stockEntry->update($data);
|
||||
|
||||
return $stockEntry->fresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, StockEntry>
|
||||
*/
|
||||
public function getByStatus(string $status): Collection
|
||||
{
|
||||
return StockEntry::query()
|
||||
->where('status', $status)
|
||||
->orderByDesc('ordered_at')
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, StockEntry>
|
||||
*/
|
||||
public function getForIngredient(int $ingredientId): Collection
|
||||
{
|
||||
return StockEntry::query()
|
||||
->where('ingredient_id', $ingredientId)
|
||||
->where('status', 'received')
|
||||
->orderByDesc('received_at')
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Liste: Pending zuerst (neuestes Bestelldatum), dann Received (neuester Eingang).
|
||||
*
|
||||
* @return Collection<int, StockEntry>
|
||||
*/
|
||||
public function listForIndex(): Collection
|
||||
{
|
||||
$with = [
|
||||
'ingredient',
|
||||
'packagingItem',
|
||||
'supplier',
|
||||
'location',
|
||||
'quality',
|
||||
'orderedByUser',
|
||||
'receivedByUser',
|
||||
];
|
||||
|
||||
$pending = StockEntry::query()->with($with)->where('status', 'pending')->orderByDesc('ordered_at')->get();
|
||||
$received = StockEntry::query()->with($with)->where('status', 'received')->orderByDesc('received_at')->get();
|
||||
|
||||
return $pending->concat($received)->values();
|
||||
}
|
||||
}
|
||||
58
app/Repositories/SupplierRepository.php
Normal file
58
app/Repositories/SupplierRepository.php
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Models\Supplier;
|
||||
|
||||
class SupplierRepository
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function create(array $data): Supplier
|
||||
{
|
||||
$supplier = Supplier::create($this->extractSupplierAttributes($data));
|
||||
$this->syncCategories($supplier, $data['supplier_category_ids'] ?? []);
|
||||
|
||||
return $supplier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function update(Supplier $supplier, array $data): Supplier
|
||||
{
|
||||
$supplier->update($this->extractSupplierAttributes($data));
|
||||
$this->syncCategories($supplier, $data['supplier_category_ids'] ?? []);
|
||||
|
||||
return $supplier->fresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int|string>|null $categoryIds
|
||||
*/
|
||||
public function syncCategories(Supplier $supplier, array $categoryIds): void
|
||||
{
|
||||
$ids = array_values(array_filter(array_map('intval', $categoryIds)));
|
||||
|
||||
$supplier->supplierCategories()->sync($ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function extractSupplierAttributes(array $data): array
|
||||
{
|
||||
return collect($data)->only([
|
||||
'name',
|
||||
'url',
|
||||
'contact_person',
|
||||
'email',
|
||||
'phone',
|
||||
'country_id',
|
||||
'notes',
|
||||
'active',
|
||||
])->all();
|
||||
}
|
||||
}
|
||||
|
|
@ -2,60 +2,54 @@
|
|||
|
||||
namespace App\Repositories;
|
||||
|
||||
use Str;
|
||||
use App\User;
|
||||
use stdClass;
|
||||
use Validator;
|
||||
use Carbon\Carbon;
|
||||
use App\Models\PaymentMethod;
|
||||
use App\Models\UserAccount;
|
||||
use App\Models\UserRegister;
|
||||
use App\Models\PaymentMethod;
|
||||
use App\Services\UserService;
|
||||
use App\User;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use stdClass;
|
||||
use Validator;
|
||||
|
||||
class UserRepository extends BaseRepository {
|
||||
|
||||
|
||||
class UserRepository extends BaseRepository
|
||||
{
|
||||
public function __construct(User $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function update($data)
|
||||
{
|
||||
|
||||
if($data['user_id'] === "new" || $data['user_id'] == 0){
|
||||
if ($data['user_id'] === 'new' || $data['user_id'] == 0) {
|
||||
|
||||
$this->model = User::create([
|
||||
'email' => $data['email'],
|
||||
'password' => env('APP_KEY'),
|
||||
'password' => Hash::make(config('app.key')),
|
||||
]);
|
||||
$this->model->payment_methods = PaymentMethod::getDefaultAsArray();
|
||||
$this->model->save();
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
$this->model = $this->getById($data['user_id']);
|
||||
}
|
||||
|
||||
if(!$this->model->account_id){
|
||||
$account = new UserAccount();
|
||||
}else{
|
||||
if (! $this->model->account_id) {
|
||||
$account = new UserAccount;
|
||||
} else {
|
||||
$account = $this->model->account;
|
||||
}
|
||||
|
||||
$data['same_as_billing'] = !isset($data['same_as_billing']) ? 0 : 1;
|
||||
$data['same_as_billing'] = ! isset($data['same_as_billing']) ? 0 : 1;
|
||||
|
||||
$data['birthday_day'] = isset($data['birthday_day']) ? $data['birthday_day'] : 1;
|
||||
$data['birthday_month'] = isset($data['birthday_month']) ? $data['birthday_month'] : 1;
|
||||
$data['birthday_year'] = isset($data['birthday_year']) ? $data['birthday_year'] : 1900;
|
||||
$data['birthday'] = $data['birthday_day'].".".$data['birthday_month'].".".$data['birthday_year'];
|
||||
$data['birthday'] = $data['birthday'] == "1.1.1900" ? null : $data['birthday'];
|
||||
|
||||
$data['birthday'] = $data['birthday_day'].'.'.$data['birthday_month'].'.'.$data['birthday_year'];
|
||||
$data['birthday'] = $data['birthday'] == '1.1.1900' ? null : $data['birthday'];
|
||||
|
||||
$account->fill($data)->save();
|
||||
|
||||
if(!$this->model->account_id){
|
||||
if (! $this->model->account_id) {
|
||||
$this->model->account_id = $account->id;
|
||||
$this->model->save();
|
||||
}
|
||||
|
|
@ -63,9 +57,10 @@ class UserRepository extends BaseRepository {
|
|||
return true;
|
||||
}
|
||||
|
||||
public function createUserRegister($data){
|
||||
public function createUserRegister($data)
|
||||
{
|
||||
|
||||
$obj = new stdClass();
|
||||
$obj = new stdClass;
|
||||
|
||||
$obj->email = $data['email'];
|
||||
$obj->password = Hash::make($data['password']);
|
||||
|
|
@ -76,11 +71,11 @@ class UserRepository extends BaseRepository {
|
|||
$obj->first_name = $data['first_name'];
|
||||
$obj->last_name = $data['last_name'];
|
||||
$obj->data_protection = now()->toDateTimeString();
|
||||
|
||||
|
||||
$obj->confirmation_code_to = date('Y-m-d H:i:s', strtotime('+1 week'));
|
||||
$obj->confirmation_code_remider = 0;
|
||||
$obj->m_sponsor = config('app.main_user_id');
|
||||
if(isset($data['from_member_id'])){
|
||||
if (isset($data['from_member_id'])) {
|
||||
$obj->m_sponsor = (int) str_replace('gs', '', $data['from_member_id']) - config('main.add_number_id');
|
||||
}
|
||||
$confirmation_code = UserService::createConfirmationCode();
|
||||
|
|
@ -88,24 +83,26 @@ class UserRepository extends BaseRepository {
|
|||
UserRegister::create([
|
||||
'identifier' => $data['email'],
|
||||
'instance' => $confirmation_code,
|
||||
'content' => $obj
|
||||
'content' => $obj,
|
||||
]);
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function clearUserRegister(){
|
||||
$cleartime = date('Y-m-d H:i:s', strtotime('-1 day')); //gestern -24h
|
||||
public function clearUserRegister()
|
||||
{
|
||||
$cleartime = date('Y-m-d H:i:s', strtotime('-1 day')); // gestern -24h
|
||||
UserRegister::where('created_at', '<', $cleartime)->delete();
|
||||
}
|
||||
|
||||
public function create($UserRegister){
|
||||
public function create($UserRegister)
|
||||
{
|
||||
|
||||
|
||||
$userObj = $UserRegister->content;
|
||||
|
||||
$user = User::create([
|
||||
'email' => $userObj->email,
|
||||
'password' =>$userObj->password,
|
||||
'password' => $userObj->password,
|
||||
]);
|
||||
|
||||
$account = UserAccount::create([
|
||||
|
|
@ -122,17 +119,17 @@ class UserRepository extends BaseRepository {
|
|||
$user->confirmation_code_to = null;
|
||||
$user->confirmation_code_remider = 0;
|
||||
$user->confirmation_date = now();
|
||||
$user->lang = !empty(\App::getLocale()) ? \App::getLocale() : "de";
|
||||
$user->lang = ! empty(\App::getLocale()) ? \App::getLocale() : 'de';
|
||||
$user->m_sponsor = $userObj->m_sponsor;
|
||||
|
||||
$user->account_id = $account->id;
|
||||
$user->payment_methods = PaymentMethod::getDefaultAsArray();
|
||||
|
||||
$user->save();
|
||||
|
||||
|
||||
$user = User::find($user->id);
|
||||
|
||||
//clear
|
||||
// clear
|
||||
$identifier = $UserRegister->identifier;
|
||||
UserRegister::where('identifier', $identifier)->delete();
|
||||
|
||||
|
|
@ -141,13 +138,13 @@ class UserRepository extends BaseRepository {
|
|||
|
||||
public function deleteUser(User $user)
|
||||
{
|
||||
if($user->account){
|
||||
if ($user->account) {
|
||||
$user->account->delete();
|
||||
}
|
||||
$user->email = "delete".time();
|
||||
$user->password = "delete".time();
|
||||
$user->email = 'delete'.time();
|
||||
$user->password = 'delete'.time();
|
||||
$user->confirmed = 0;
|
||||
$user->confirmation_code = "delete".time();
|
||||
$user->confirmation_code = 'delete'.time();
|
||||
$user->confirmation_date = null;
|
||||
$user->confirmation_code_to = null;
|
||||
$user->confirmation_code_remider = 2;
|
||||
|
|
@ -162,31 +159,35 @@ class UserRepository extends BaseRepository {
|
|||
return true;
|
||||
}
|
||||
|
||||
public function reverse_charge_validate($data, $user, $route){
|
||||
public function reverse_charge_validate($data, $user, $route)
|
||||
{
|
||||
|
||||
if(isset($data['action']) && $data['action'] == 'reverse_charge_validate'){
|
||||
$rules = array(
|
||||
if (isset($data['action']) && $data['action'] == 'reverse_charge_validate') {
|
||||
$rules = [
|
||||
'tax_identification_number' => 'required',
|
||||
);
|
||||
];
|
||||
$validator = Validator::make($data, $rules);
|
||||
if ($validator->fails()) {
|
||||
$data = [
|
||||
'user' => $user,
|
||||
];
|
||||
|
||||
return redirect($route)->withErrors($validator)->withInput($data);
|
||||
}
|
||||
}
|
||||
$ret = $this->reverse_charge_activate($data, $user);
|
||||
if($ret === 'error'){
|
||||
if ($ret === 'error') {
|
||||
$validator = Validator::make($data, []);
|
||||
$validator->errors()->add('tax_identification_number_validated', __('msg.VATID_could_not_be_validated'));
|
||||
$data['reverse_charge'] = 0;
|
||||
$data = [
|
||||
'user' => $user,
|
||||
];
|
||||
|
||||
return redirect($route.'#user-vat-validation')->withErrors($validator)->withInput($data);
|
||||
}
|
||||
if($ret === 'valid'){
|
||||
if ($ret === 'valid') {
|
||||
\Session()->flash('alert-success', __('msg.VATID_successfully_entered'));
|
||||
|
||||
return redirect($route.'#user-vat-validation')->withInput($data);
|
||||
|
||||
return redirect($route.'#user-vat-validation')->withInput($data);
|
||||
|
|
@ -194,20 +195,23 @@ class UserRepository extends BaseRepository {
|
|||
}
|
||||
}
|
||||
|
||||
public function reverse_charge_delete($data, $user, $route){
|
||||
if(isset($data['action']) && $data['action'] == 'reverse_charge_delete'){
|
||||
public function reverse_charge_delete($data, $user, $route)
|
||||
{
|
||||
if (isset($data['action']) && $data['action'] == 'reverse_charge_delete') {
|
||||
$user->account->tax_identification_number = '';
|
||||
$user->account->reverse_charge = 0;
|
||||
$user->account->reverse_charge_code = null;
|
||||
$user->account->reverse_charge_code = null;
|
||||
$user->account->reverse_charge_valid = null;
|
||||
$user->account->save();
|
||||
$data['tax_identification_number'] = '';
|
||||
\Session()->flash('alert-success', __('msg.reverse_charge_procedure_and_VATID_deleted'));
|
||||
|
||||
return redirect($route.'#user-vat-validation')->withInput($data);
|
||||
}
|
||||
}
|
||||
|
||||
public function reverse_charge_activate($data, $user){
|
||||
public function reverse_charge_activate($data, $user)
|
||||
{
|
||||
|
||||
/* 'AT' => 'AT-Oesterreich',
|
||||
'BE' => 'BE-Belgien',
|
||||
|
|
@ -237,44 +241,43 @@ class UserRepository extends BaseRepository {
|
|||
'SI' => 'SI-Slowenien',
|
||||
'SK' => 'SK-Slowakei',
|
||||
'XI' => 'XI-Nordirland', */
|
||||
$countryCode = 'DE';
|
||||
|
||||
if($user->account->country_id){
|
||||
$countryCode = $user->account->country->code;
|
||||
}
|
||||
|
||||
$vatid = str_replace(array(' ', '.', '-', ',', ', '), '', trim($data['tax_identification_number']));
|
||||
$cc = substr($vatid, 0, 2);
|
||||
$vatNo = substr($vatid, 2);
|
||||
|
||||
$options = [
|
||||
'cache_wsdl' => WSDL_CACHE_NONE,
|
||||
'trace' => 1,
|
||||
'stream_context' => stream_context_create(
|
||||
[
|
||||
'ssl' => [
|
||||
'verify_peer' => false,
|
||||
'verify_peer_name' => false,
|
||||
'allow_self_signed' => true
|
||||
]
|
||||
]
|
||||
)
|
||||
];
|
||||
|
||||
$client = new \SoapClient("https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl", $options);
|
||||
$result = $client->checkVat(['countryCode' => $countryCode, 'vatNumber' => $vatNo]);
|
||||
|
||||
if($result->valid == true) {
|
||||
$user->account->tax_identification_number = $data['tax_identification_number'];
|
||||
$user->account->reverse_charge = 1;
|
||||
$user->account->reverse_charge_code = $countryCode;
|
||||
$user->account->reverse_charge_valid = now();
|
||||
$user->account->save();
|
||||
return 'valid';
|
||||
} else {
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
$countryCode = 'DE';
|
||||
|
||||
if ($user->account->country_id) {
|
||||
$countryCode = $user->account->country->code;
|
||||
}
|
||||
|
||||
}
|
||||
$vatid = str_replace([' ', '.', '-', ',', ', '], '', trim($data['tax_identification_number']));
|
||||
$cc = substr($vatid, 0, 2);
|
||||
$vatNo = substr($vatid, 2);
|
||||
|
||||
$options = [
|
||||
'cache_wsdl' => WSDL_CACHE_NONE,
|
||||
'trace' => 1,
|
||||
'stream_context' => stream_context_create(
|
||||
[
|
||||
'ssl' => [
|
||||
'verify_peer' => false,
|
||||
'verify_peer_name' => false,
|
||||
'allow_self_signed' => true,
|
||||
],
|
||||
]
|
||||
),
|
||||
];
|
||||
|
||||
$client = new \SoapClient('https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl', $options);
|
||||
$result = $client->checkVat(['countryCode' => $countryCode, 'vatNumber' => $vatNo]);
|
||||
|
||||
if ($result->valid == true) {
|
||||
$user->account->tax_identification_number = $data['tax_identification_number'];
|
||||
$user->account->reverse_charge = 1;
|
||||
$user->account->reverse_charge_code = $countryCode;
|
||||
$user->account->reverse_charge_valid = now();
|
||||
$user->account->save();
|
||||
|
||||
return 'valid';
|
||||
} else {
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue