April 2026 waren Wirtschaft Feedback

This commit is contained in:
Kevin Adametz 2026-04-10 17:14:38 +02:00
parent 02f2a4c23e
commit 9ce711d6b2
167 changed files with 25278 additions and 8518 deletions

View file

@ -1,76 +1,77 @@
<?php
namespace App\Services;
use App\User;
use App\Models\Country;
use App\Models\Product;
use App\Models\Category;
use App\Models\LeadType;
use App\Models\Attribute;
use App\Models\UserLevel;
use App\Models\Ingredient;
use App\Models\ShoppingUser;
use App\Models\AttributeType;
use App\Models\Category;
use App\Models\Country;
use App\Models\Ingredient;
use App\Models\LeadType;
use App\Models\Product;
use App\Models\ShippingCountry;
use Illuminate\Support\Facades\Auth;
use App\Models\ShoppingUser;
use App\Models\UserLevel;
use App\User;
class HTMLHelper
{
public static $months = [
1 => 'January',
2 => 'February',
3 => 'March',
4 => 'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 => 'September',
10 => 'October',
11 => 'November',
12 => 'December'
2 => 'February',
3 => 'March',
4 => 'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 => 'September',
10 => 'October',
11 => 'November',
12 => 'December',
];
private static $roles = [
0 => 'Kunde',
1 => 'Redakteur',
7 => 'Admin',
8 => 'SuperAdmin',
9 => 'SySAdmin',
//10 => "API",
// 10 => "API",
];
public static function getMonth($i){
public static function getMonth($i)
{
return self::$months[intval($i)];
}
public static function getTransMonths($full = false){
public static function getTransMonths($full = false)
{
$ret = [];
foreach(self::$months as $key=>$val){
$ret[$key] = trans('cal.months.'.$val);
}
if($full){ //ganzes Jahr
foreach (self::$months as $key => $val) {
$ret[$key] = trans('cal.months.'.$val);
}
if ($full) { // ganzes Jahr
$ret[13] = trans('cal.months.full_year');
}
return $ret;
return $ret;
}
public static function getYearRange($start = 2021)
{
$end = date("Y");
$end = date('Y');
return array_reverse(range($start, $end));
}
public static function getRoleLabel($role_id = 0){
public static function getRoleLabel($role_id = 0)
{
return '<span class="badge badge-pill '.self::getLabel($role_id).'">'.self::$roles[$role_id].'</span>';
}
public static function getLabel($id){
public static function getLabel($id)
{
switch ($id) {
case 0:
return 'badge-default';
@ -94,31 +95,32 @@ class HTMLHelper
}
public static function getCustomListOf($name, $select){
$ret = "";
if($name === 'day'){
public static function getCustomListOf($name, $select)
{
$ret = '';
if ($name === 'day') {
$start = 1;
$end = 31;
$values = range($start, $end);
$ret = '<option value="">'.__('Tag').'</option>\n';
foreach ($values as $value){
foreach ($values as $value) {
$attr = ($value == $select) ? 'selected="selected"' : '';
$ret .= '<option value="'.$value.'" '.$attr.'>'.$value.'</option>\n';
}
}
if($name === 'month'){
if ($name === 'month') {
$ret = '<option value="">'.__('Monat').'</option>\n';
foreach (self::$months as $key=>$value){
foreach (self::$months as $key => $value) {
$attr = ($key == $select) ? 'selected="selected"' : '';
$ret .= '<option value="'.$key.'" '.$attr.'>'.$value.'</option>\n';
}
}
if($name === 'year'){
$start = date("Y", strtotime("-5 years", time()));
$end = date("Y", strtotime("-90 years", time()));
if ($name === 'year') {
$start = date('Y', strtotime('-5 years', time()));
$end = date('Y', strtotime('-90 years', time()));
$values = range($start, $end);
$ret = '<option value="">'.__('Jahr').'</option>\n';
foreach ($values as $value){
foreach ($values as $value) {
$attr = ($value == $select) ? 'selected="selected"' : '';
$ret .= '<option value="'.$value.'" '.$attr.'>'.$value.'</option>\n';
}
@ -127,14 +129,16 @@ class HTMLHelper
return $ret;
}
public static function setContentReadMore($content){
public static function setContentReadMore($content)
{
$sep = '##mehr lesen##';
if(strpos($content, $sep) !== false){
if (strpos($content, $sep) !== false) {
$name = 'collapse_'.random_int(1000, 10000);
$split = explode($sep, $content);
$first = isset($split[0]) ? $split[0] : "";
$text = isset($split[1]) ? $split[1] : "";
$first = isset($split[0]) ? $split[0] : '';
$text = isset($split[1]) ? $split[1] : '';
$content = $first;
$content .= '<br><a class="btn btn-primary btn-sm mt-2 collapsed" data-toggle="collapse" href="#'.$name.'" role="button" aria-expanded="false" aria-controls="'.$name.'">
@ -147,382 +151,436 @@ class HTMLHelper
</div>
</div>';
}
return $content;
}
public static function getRolesOptions(){
$ret = "";
foreach (self::$roles as $role_id => $value){
public static function getRolesOptions()
{
$ret = '';
foreach (self::$roles as $role_id => $value) {
$ret .= '<option value="'.$role_id.'">'.$value.'</option>\n';
}
return $ret;
}
public static function getYearSelectOptions(){
$start = date("Y", strtotime("-5 years", time()));
$end = date("Y", strtotime("+1 years", time()));
public static function getYearSelectOptions()
{
$start = date('Y', strtotime('-5 years', time()));
$end = date('Y', strtotime('+1 years', time()));
$values = range($start, $end);
$now = date("Y", time());
$ret = "";
foreach ($values as $value){
$now = date('Y', time());
$ret = '';
foreach ($values as $value) {
$attr = ($value == $now) ? 'selected="selected"' : '';
$ret .= '<option value="'.$value.'" '.$attr.'>'.$value.'</option>\n';
}
return $ret;
}
public static function getAttributeTypes($id = false){
public static function getAttributeTypes($id = false)
{
$values = AttributeType::where('parent_id', null)->where('active', 1)->orderBy('pos', 'asc')->get();
$ret = "";
if($id === false){
$ret = '';
if ($id === false) {
$val = $values->first();
$id = $val->id;
}
foreach ($values as $value){
foreach ($values as $value) {
$attr = ($value->id == $id) ? 'selected="selected"' : '';
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->name.'</option>\n';
}
return $ret;
}
public static function getProductsWhiteLabelOptions($ids = [], $unsets = [], $type_id = false){
public static function getProductsWhiteLabelOptions($ids = [], $unsets = [], $type_id = false)
{
$values = Product::where('whitelabel', 1)->where('active', 1)->get();
$ret = "";
foreach ($values as $value){
if(is_array($unsets) && in_array($value->id, $unsets)){
$ret = '';
foreach ($values as $value) {
if (is_array($unsets) && in_array($value->id, $unsets)) {
continue;
}
$attr = (is_array($ids) && in_array($value->id, $ids)) ? 'selected="selected"' : '';
$attr = (is_array($ids) && in_array($value->id, $ids)) ? 'selected="selected"' : '';
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->name.'</option>\n';
}
return $ret;
}
public static function getAttributesOptions($ids = array(), $all = true, $type_id = false){
if($type_id){
public static function getAttributesOptions($ids = [], $all = true, $type_id = false)
{
if ($type_id) {
$values = Attribute::where('active', 1)->where('attribute_type_id', $type_id)->get();
}else{
} else {
$values = Attribute::where('active', 1)->get();
}
$ret = "";
if($all){
$ret = '';
if ($all) {
$ret .= '<option value="">'.__('no').'</option>\n';
}
foreach ($values as $value){
$attr = in_array($value->id, $ids) ? 'selected="selected"' : '';
foreach ($values as $value) {
$attr = in_array($value->id, $ids) ? 'selected="selected"' : '';
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->name.'</option>\n';
}
return $ret;
}
public static function getProductAttributesOptions($product_attributes, $ids = [], $all = true, $type_id = false){
$ret = "";
if($all){
public static function getProductAttributesOptions($product_attributes, $ids = [], $all = true, $type_id = false)
{
$ret = '';
if ($all) {
$ret .= '<option value="">'.__('no').'</option>\n';
}
foreach ($product_attributes as $product_attribute){
if($product_attribute->attribute){
$attr = (is_array($ids) && in_array($product_attribute->attribute_id, $ids)) ? 'selected="selected"' : '';
foreach ($product_attributes as $product_attribute) {
if ($product_attribute->attribute) {
$attr = (is_array($ids) && in_array($product_attribute->attribute_id, $ids)) ? 'selected="selected"' : '';
$ret .= '<option value="'.$product_attribute->attribute_id.'" '.$attr.'>'.$product_attribute->attribute->name.'</option>\n';
}
}
return $ret;
}
public static function getCategoriesWithoutParents($id = false, $sameId = false, $all = true){
public static function getCategoriesWithoutParents($id = false, $sameId = false, $all = true)
{
$values = Category::where('parent_id', null)->get();
$ret = "";
if($all){
$ret = '';
if ($all) {
$ret .= '<option value="">'.__('no').'</option>\n';
}
foreach ($values as $value){
if($sameId == $value->id){
foreach ($values as $value) {
if ($sameId == $value->id) {
continue;
}
$attr = ($value->id == $id) ? 'selected="selected"' : '';
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->name.'</option>\n';
}
return $ret;
}
public static function getProductsOptions($ids = array(), $all = true){
if($ids == null){
$ids = array();
public static function getProductsOptions($ids = [], $all = true)
{
if ($ids == null) {
$ids = [];
}
$values = Product::where('active', 1)->get();
$ret = "";
if($all){
$ret = '';
if ($all) {
$ret .= '<option value="">'.__('no').'</option>\n';
}
foreach ($values as $value){
$attr = in_array($value->id, $ids) ? 'selected="selected"' : '';
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->name.'</option>\n';
}
return $ret;
}
public static function getCategoriesOptions($ids = array(), $all = true){
$values = Category::where('active', 1)->get();
$ret = "";
if($all){
$ret .= '<option value="">'.__('no').'</option>\n';
}
foreach ($values as $value){
foreach ($values as $value) {
$attr = in_array($value->id, $ids) ? 'selected="selected"' : '';
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->name.'</option>\n';
}
return $ret;
}
public static function getCategoriesByShowOn($show_on = []){
$values = Category::where('active', true)->whereJsonContains('show_on', $show_on)->orderBy('pos', 'ASC')->get();
public static function getCategoriesOptions($ids = [], $all = true)
{
$values = Category::where('active', 1)->get();
$ret = '';
if ($all) {
$ret .= '<option value="">'.__('no').'</option>\n';
}
foreach ($values as $value) {
$attr = in_array($value->id, $ids) ? 'selected="selected"' : '';
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->name.'</option>\n';
}
return $ret;
}
public static function getCategoriesByShowOn($show_on = [])
{
$values = Category::where('active', true)->whereJsonContains('show_on', $show_on)->orderBy('pos', 'ASC')->get();
$ret = [];
foreach ($values as $value){
foreach ($values as $value) {
$ret[$value->id] = ['name' => $value->name, 'count' => $value->getProductsCountOn($show_on)];
}
return $ret;
}
public static function getCategoriesOptionsByShowOn($ids = array(), $all = false, $show_on = []){
$values = Category::where('active', true)->whereJsonContains('show_on', $show_on)->orderBy('pos', 'ASC')->get();
$ret = "";
if($all){
$ret .= '<option value="">'.$all.'</option>\n';
}
foreach ($values as $value){
$attr = in_array($value->id, $ids) ? 'selected="selected"' : '';
$count = $value->getProductsCountOn($show_on);
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->name.' ('.$count.')</option>\n';
}
return $ret;
}
public static function getProductIngredientsOptions($has_ids = array(), $all = true){
$values = Ingredient::where('active', 1)->get();
$ret = "";
$attr = "";
foreach ($values as $value){
if(!in_array($value->id, $has_ids)){
public static function getCategoriesOptionsByShowOn($ids = [], $all = false, $show_on = [])
{
$values = Category::where('active', true)->whereJsonContains('show_on', $show_on)->orderBy('pos', 'ASC')->get();
$ret = '';
if ($all) {
$ret .= '<option value="">'.$all.'</option>\n';
}
foreach ($values as $value) {
$attr = in_array($value->id, $ids) ? 'selected="selected"' : '';
$count = $value->getProductsCountOn($show_on);
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->name.' ('.$count.')</option>\n';
}
return $ret;
}
public static function getProductIngredientsOptions($has_ids = [], $all = true)
{
$values = Ingredient::where('active', 1)->orderBy('name')->get();
$ret = '';
$attr = '';
foreach ($values as $value) {
if (! in_array($value->id, $has_ids)) {
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->name.'</option>\n';
}
}
return $ret;
}
public static function getLeadTypeOptions($id = false, $all = true){
public static function getLeadTypeOptions($id = false, $all = true)
{
$values = LeadType::where('active', 1)->get();
$ret = "";
if($all){
$ret = '';
if ($all) {
$ret .= '<option value="">'.__('no').'</option>\n';
}
foreach ($values as $value){
foreach ($values as $value) {
$attr = ($value->id == $id) ? 'selected="selected"' : '';
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->name.'</option>\n';
}
return $ret;
}
public static function getUserLevelOptions($id = false, $all = true){
public static function getUserLevelOptions($id = false, $all = true)
{
$values = UserLevel::where('active', 1)->get();
$ret = "";
if($all){
$ret = '';
if ($all) {
$ret .= '<option value="">'.__('no').'</option>\n';
}
foreach ($values as $value){
foreach ($values as $value) {
$attr = ($value->id == $id) ? 'selected="selected"' : '';
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->name.'</option>\n';
}
return $ret;
}
public static function getCompanyOptions($company){
$options = array(1 => __('business'), 0 => __('private'), );
$ret = "";
foreach ($options as $id => $value){
public static function getCompanyOptions($company)
{
$options = [1 => __('business'), 0 => __('private')];
$ret = '';
foreach ($options as $id => $value) {
$attr = ($id == $company) ? 'selected="selected"' : '';
$ret .= '<option value="'.$id.'" '.$attr.'>'.$value.'</option>\n';
}
return $ret;
}
public static function getContriesWithMore($id, $all=true){#
public static function getContriesWithMore($id, $all = true) //
{
$values = Country::all();
$counter = 1;
$ret = "";
if($all){
$ret = '';
if ($all) {
$ret .= '<option value="">'.__('please select').'</option>\n';
}
foreach ($values as $value){
if( $counter == 7){
foreach ($values as $value) {
if ($counter == 7) {
$ret .= '<optgroup label="'.__('further countrie').'">';
}
$attr = ($value->id == $id) ? 'selected="selected"' : '';
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->getLocated().'</option>\n';
$counter ++;
$counter++;
}
$ret .= '</optgroup>';
return $ret;
}
public static function getContriesCodes($id, $all=true){#
public static function getContriesCodes($id, $all = true) //
{
$values = Country::all();
$counter = 1;
$ret = "";
if($all){
$ret = '';
if ($all) {
$ret .= '<option value="">'.__('please select').'</option>\n';
}
foreach ($values as $value){
foreach ($values as $value) {
if(!$value->phone) continue;
if( $counter == 7){
if (! $value->phone) {
continue;
}
if ($counter == 7) {
$ret .= '<optgroup label="'.__('further countrie').'">';
}
$attr = ($value->id == $id) ? 'selected="selected"' : '';
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->phone.'('.$value->getLocated().')</option>\n';
$counter ++;
$counter++;
}
$ret .= '</optgroup>';
return $ret;
}
public static function getCountriesWithoutUsedShippings($all=true){#
public static function getCountriesWithoutUsedShippings($all = true) //
{
$values = Country::all();
$country_ids = ShippingCountry::all()->pluck('country_id')->toArray();
$ret = "";
if($all){
$ret = '';
if ($all) {
$ret .= '<option value="">'.__('please select').'</option>\n';
}
foreach ($values as $value){
if(!in_array($value->id, $country_ids)){
foreach ($values as $value) {
if (! in_array($value->id, $country_ids)) {
$ret .= '<option value="'.$value->id.'">'.$value->getLocated().'</option>\n';
}
}
return $ret;
}
public static function getCountryNameFormShipping($id){
public static function getCountryNameFormShipping($id)
{
$value = ShippingCountry::find($id);
if($value){
if ($value) {
return $value->country->getLocated();
}
return "not defined";
return 'not defined';
}
public static function getCountriesForShipping($id, $all=false, $shipping_for = false){#
public static function getCountriesForShipping($id, $all = false, $shipping_for = false) //
{
$values = ShippingCountry::all();
if($shipping_for){
if ($shipping_for) {
$temp = [];
foreach($values as $value){
if($shipping = $value->shipping){
if($shipping->getShippingPricesFirstBy($shipping_for)){
foreach ($values as $value) {
if ($shipping = $value->shipping) {
if ($shipping->getShippingPricesFirstBy($shipping_for)) {
$temp[] = $value;
}
}
}
$values = $temp;
}
$ret = "";
if($all){
$ret = '';
if ($all) {
$ret .= '<option value="">'.__('please select').'</option>\n';
}
foreach ($values as $value){
foreach ($values as $value) {
$attr = ($value->id == $id) ? 'selected="selected"' : '';
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->country->getLocated().'</option>\n';
}
return $ret;
}
public static function getSalutation($id){
$values = array('mr' => __('MR'), 'ms' => __('MS'));
$ret = "";
public static function getSalutation($id)
{
$values = ['mr' => __('MR'), 'ms' => __('MS')];
$ret = '';
$ret .= '<option value="">'.__('please select').'</option>\n';
foreach ($values as $key => $value){
foreach ($values as $key => $value) {
$attr = ($key == $id) ? 'selected="selected"' : '';
$ret .= '<option value="'.$key.'" '.$attr.'>'.$value.'</option>\n';
}
return $ret;
}
public static function getSalutationLang($id){
$values = array('mr' => __('MR'), 'ms' => __('MS'));
return (!empty($values[$id]) ? $values[$id] : '');
public static function getSalutationLang($id)
{
$values = ['mr' => __('MR'), 'ms' => __('MS')];
return ! empty($values[$id]) ? $values[$id] : '';
}
public static function getTaxSaleOptions($id){
$values = array('1' => __('taxable_sales_1'), '2' => __('taxable_sales_2'));
$ret = "";
public static function getTaxSaleOptions($id)
{
$values = ['1' => __('taxable_sales_1'), '2' => __('taxable_sales_2')];
$ret = '';
$ret .= '<option value="">'.__('please select').'</option>\n';
foreach ($values as $key => $value){
foreach ($values as $key => $value) {
$attr = ($key == $id) ? 'selected="selected"' : '';
$ret .= '<option value="'.$key.'" '.$attr.'>'.$value.'</option>\n';
}
return $ret;
}
public static function getMembersOptions($id, $all=false){
public static function getMembersOptions($id, $all = false)
{
$values = User::where('active', '=', true)->where('blocked', '=', false)->where('payment_account', '>=', now())->get();
$ret = "";
if($all){
$ret = '';
if ($all) {
$ret .= '<option value="">'.__('please select').'</option>\n';
}
foreach ($values as $value){
foreach ($values as $value) {
$attr = ($value->id == $id) ? 'selected="selected"' : '';
$to="";
if($value->account){
$to = $value->account->first_name." ".$value->account->last_name." | ";
$to = '';
if ($value->account) {
$to = $value->account->first_name.' '.$value->account->last_name.' | ';
}
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$to.$value->email.' #'.$value->account->m_account.'</option>\n';
}
return $ret;
}
public static function getUserCustomerOptions($id, $all=false){
public static function getUserCustomerOptions($id, $all = false)
{
$values = ShoppingUser::select(['id', 'billing_firstname', 'billing_lastname', 'billing_email', 'number'])
->where('shopping_users.member_id', '=', \Auth::user()->id)->get();
$ret = "";
if($all){
$ret = '';
if ($all) {
$ret .= '<option value="">'.__('please select').'</option>\n';
}
foreach ($values as $value){
foreach ($values as $value) {
$attr = ($value->id == $id) ? 'selected="selected"' : '';
$to = $value->billing_firstname." ".$value->billing_lastname." | ".$value->billing_email;
$to = $value->billing_firstname.' '.$value->billing_lastname.' | '.$value->billing_email;
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$to.' #'.$value->account->m_account.'</option>\n';
}
return $ret;
}
public static function getAnyOptions($id, $options = [], $all=true){#
$ret = "";
if($all){
public static function getAnyOptions($id, $options = [], $all = true) //
{
$ret = '';
if ($all) {
$ret .= '<option value="">'.__('please select').'</option>\n';
}
foreach ($options as $key=>$value){
foreach ($options as $key => $value) {
$attr = ($key == $id) ? 'selected="selected"' : '';
$ret .= '<option value="'.$key.'" '.$attr.'>'.$value.'</option>\n';
}
return $ret;
}
public static function getOptionRange($select, $from=1, $to=50){
public static function getOptionRange($select, $from = 1, $to = 50)
{
$values = range($from, $to);
$ret = "";
foreach ($values as $value){
$ret = '';
foreach ($values as $value) {
$attr = ($value == $select) ? 'selected="selected"' : '';
$ret .= '<option value="'.$value.'" '.$attr.'>'.$value.'</option>\n';
}
return $ret;
}
}
}

View file

@ -1,127 +1,199 @@
<?php
namespace App\Services;
use App\Mail\MailInvoice;
use App\Mail\MailLogistic;
use App\Services\Util;
use App\Models\Setting;
use App\Models\ShoppingOrder;
use App\Models\UserCredit;
use Illuminate\Support\Facades\Mail;
class Invoice
{
public static function getInvoiceNumber(){
public static function getInvoiceNumber()
{
return (int) Setting::getContentBySlug('invoice-number');
}
public static function makeNextInvoiceNumber(){
public static function makeNextInvoiceNumber()
{
$invoice_number = self::getInvoiceNumber();
$invoice_number = $invoice_number+1;
$invoice_number = $invoice_number + 1;
Setting::setContentBySlug('invoice-number', $invoice_number, 'int');
return $invoice_number;
}
public static function createInvoiceNumber($invoice_number, $invoice_date){
public static function createInvoiceNumber($invoice_number, $invoice_date)
{
$prefix = \Carbon::parse($invoice_date)->format('Ym');
return $prefix.$invoice_number;
}
public static function getInvoiceStorageDir($invoice_date){
return "/invoice/".\Carbon::parse($invoice_date)->format('Y/m/');
public static function getInvoiceStorageDir($invoice_date)
{
return '/invoice/'.\Carbon::parse($invoice_date)->format('Y/m/');
}
public static function getDeliveryStorageDir($invoice_date){
return "/delivery/".\Carbon::parse($invoice_date)->format('Y/m/');
public static function getDeliveryStorageDir($invoice_date)
{
return '/delivery/'.\Carbon::parse($invoice_date)->format('Y/m/');
}
public static function makeInvoiceFilename($invoice_number){
return "Rechnung-".$invoice_number.".pdf";
public static function makeInvoiceFilename($invoice_number)
{
return 'Rechnung-'.$invoice_number.'.pdf';
}
public static function makeDeliveryFilename($invoice_number){
return "Lieferschein-".$invoice_number.".pdf";
public static function makeDeliveryFilename($invoice_number)
{
return 'Lieferschein-'.$invoice_number.'.pdf';
}
//invoice
public static function isInvoice(ShoppingOrder $shopping_order){
public static function makeCancellationFilename($invoice_number)
{
return 'Stornorechnung-'.$invoice_number.'.pdf';
}
public static function getCancellationStorageDir($invoice_date)
{
return '/cancellation/'.\Carbon::parse($invoice_date)->format('Y/m/');
}
// invoice
public static function isInvoice(ShoppingOrder $shopping_order)
{
return isset($shopping_order->invoice['filename']) ? true : false;
}
public static function getFilename($shopping_order){
public static function getFilename($shopping_order)
{
return isset($shopping_order->invoice['filename']) ? $shopping_order->invoice['filename'] : false;
}
public static function getDir($shopping_order){
public static function getDir($shopping_order)
{
return isset($shopping_order->invoice['dir']) ? $shopping_order->invoice['dir'] : false;
}
public static function getDate($shopping_order){
public static function getDate($shopping_order)
{
return isset($shopping_order->invoice['invoice_date']) ? $shopping_order->invoice['invoice_date'] : false;
}
public static function getNumber($shopping_order){
public static function getNumber($shopping_order)
{
return isset($shopping_order->invoice['invoice_number']) ? $shopping_order->invoice['invoice_number'] : false;
}
//delivery
public static function isDelivery(ShoppingOrder $shopping_order){
// cancellation
public static function isCancellationInvoice(ShoppingOrder $shopping_order)
{
return isset($shopping_order->cancellation_invoice['filename']) ? true : false;
}
public static function getCancellationFilename($shopping_order)
{
return isset($shopping_order->cancellation_invoice['filename']) ? $shopping_order->cancellation_invoice['filename'] : false;
}
public static function getCancellationDir($shopping_order)
{
return isset($shopping_order->cancellation_invoice['dir']) ? $shopping_order->cancellation_invoice['dir'] : false;
}
public static function getCancellationDate($shopping_order)
{
return isset($shopping_order->cancellation_invoice['cancellation_date']) ? $shopping_order->cancellation_invoice['cancellation_date'] : false;
}
public static function getCancellationNumber($shopping_order)
{
return isset($shopping_order->cancellation_invoice['cancellation_number']) ? $shopping_order->cancellation_invoice['cancellation_number'] : false;
}
public static function getCancellationDownloadPath(ShoppingOrder $shopping_order, $full = false)
{
$dir = self::getCancellationDir($shopping_order);
$filename = self::getCancellationFilename($shopping_order);
if (! $full) {
return $dir.$filename;
}
return \Storage::disk('public')->path($dir.$filename);
}
// delivery
public static function isDelivery(ShoppingOrder $shopping_order)
{
return isset($shopping_order->delivery['filename']) ? true : false;
}
public static function getDeliveryFilename($shopping_order){
public static function getDeliveryFilename($shopping_order)
{
return isset($shopping_order->delivery['filename']) ? $shopping_order->delivery['filename'] : self::makeDeliveryFilename(self::getDeliveryNumber($shopping_order));
}
public static function getDeliveryDir($shopping_order){
public static function getDeliveryDir($shopping_order)
{
return isset($shopping_order->delivery['dir']) ? $shopping_order->delivery['dir'] : self::getDeliveryStorageDir(self::getDeliveryDate($shopping_order));
}
public static function getDeliveryDate($shopping_order){
public static function getDeliveryDate($shopping_order)
{
return isset($shopping_order->delivery['date']) ? $shopping_order->delivery['date'] : self::getDate($shopping_order);
}
public static function getDeliveryNumber($shopping_order){
public static function getDeliveryNumber($shopping_order)
{
return isset($shopping_order->delivery['number']) ? $shopping_order->delivery['number'] : self::getNumber($shopping_order);
}
public static function getDownloadPath(ShoppingOrder $shopping_order, $full = false){
public static function getDownloadPath(ShoppingOrder $shopping_order, $full = false)
{
$dir = self::getDir($shopping_order);
$filename = self::getFilename($shopping_order);
if(!$full){
if (! $full) {
return $dir.$filename;
}
return \Storage::disk('public')->path($dir.$filename);
}
public static function getDownloadPathDelivery(ShoppingOrder $shopping_order, $full = false){
public static function getDownloadPathDelivery(ShoppingOrder $shopping_order, $full = false)
{
$dir = self::getDeliveryDir($shopping_order);
$filename = self::getDeliveryFilename($shopping_order);
if(!$full){
if (! $full) {
return $dir.$filename;
}
return \Storage::disk('public')->path($dir.$filename);
}
public static function sendInvoiceMail($shopping_order){
public static function sendInvoiceMail($shopping_order)
{
$bcc = [];
$billing_email = $shopping_order->shopping_user->billing_email;
if(!$billing_email){
if($shopping_order->mode === 'test'){
if (! $billing_email) {
if ($shopping_order->mode === 'test') {
$billing_email = config('app.checkout_test_mail');
}else{
} else {
$billing_email = config('app.checkout_mail');
}
}
if($shopping_order->mode === 'test'){
if ($shopping_order->mode === 'test') {
$bcc[] = config('app.checkout_test_mail');
}else{
} else {
$bcc[] = config('app.checkout_mail');
}
Mail::to($billing_email)->bcc($bcc)->send(new MailInvoice($shopping_order));
}
public static function sendLogisticMail(ShoppingOrder $shopping_order){
$to = [config('app.logistic_mail')]; //['versand@aloe-vera.bio'];
public static function sendLogisticMail(ShoppingOrder $shopping_order)
{
$to = [config('app.logistic_mail')]; // ['versand@aloe-vera.bio'];
Mail::to($to)->send(new MailLogistic($shopping_order));
}
}

View file

@ -2,11 +2,10 @@
namespace App\Services;
use App\Mail\PaymentReminderEmail;
use App\Models\Logger;
use App\Models\PaymentReminder;
use App\Models\ShoppingPayment;
use App\Mail\PaymentReminderEmail;
use App\Services\Util;
use Carbon\Carbon;
use Illuminate\Support\Facades\Mail;
@ -31,19 +30,23 @@ class PaymentReminderService
'action' => $action,
'channel' => 'payment_reminder',
'message' => $message,
'level' => $level
'level' => $level,
]);
}
/**
* Hole alle aktiven Intervalle für Zahlungserinnerungen
* Hole alle aktiven Intervalle für Zahlungserinnerungen, immer mit dem kleinsten intervall
*/
public function getActiveIntervals()
{
$intervals = [];
$payment_reminders = PaymentReminder::where('active', true)->get();
$payment_reminders = PaymentReminder::where('active', true)->orderBy('interval', 'asc')->get();
foreach($payment_reminders as $reminder) {
foreach ($payment_reminders as $reminder) {
if (isset($intervals[$reminder->clearingtype]) && $intervals[$reminder->clearingtype] < $reminder->interval) {
continue;
}
$intervals[$reminder->clearingtype] = $reminder->interval;
}
@ -64,7 +67,7 @@ class PaymentReminderService
->where('shopping_payments.created_at', '<', $date)
->where('shopping_payments.amount', '>', 0)
->whereNull('shopping_orders.deleted_at')
->whereIn('shopping_payments.id', function($query) use ($clearingtype, $date) {
->whereIn('shopping_payments.id', function ($query) use ($clearingtype, $date) {
$query->selectRaw('MAX(shopping_payments.id)')
->from('shopping_payments')
->join('shopping_orders', 'shopping_payments.shopping_order_id', '=', 'shopping_orders.id')
@ -79,7 +82,6 @@ class PaymentReminderService
->select('shopping_payments.*')
->get();
return $payments;
}
@ -91,15 +93,15 @@ class PaymentReminderService
$intervals = $this->getActiveIntervals();
$results = [];
foreach($intervals as $clearingtype => $interval){
foreach ($intervals as $clearingtype => $interval) {
$date = Carbon::now()->subDays($interval);
$payments = $this->getOpenPaymentsForClearingType($clearingtype, $interval);
$results[$clearingtype] = [
'interval' => $interval,
'date_limit' => $date,
'payments' => $payments,
'count' => $payments->count()
'count' => $payments->count(),
];
}
@ -114,18 +116,18 @@ class PaymentReminderService
$intervals = $this->getActiveIntervals();
$detailedData = [];
foreach($intervals as $clearingtype => $interval){
foreach ($intervals as $clearingtype => $interval) {
$date = Carbon::now()->subDays($interval);
$payments = $this->getOpenPaymentsForClearingType($clearingtype, $interval);
foreach($payments as $payment){
$name = !isset($payment->shopping_order->shopping_user) ? 'Kein Name' : $payment->shopping_order->shopping_user->billing_firstname.' '.$payment->shopping_order->shopping_user->billing_lastname;
$email = !isset($payment->shopping_order->shopping_user) ? 'Keine Email' : $payment->shopping_order->shopping_user->billing_email;
foreach ($payments as $payment) {
$name = ! isset($payment->shopping_order->shopping_user) ? 'Kein Name' : $payment->shopping_order->shopping_user->billing_firstname.' '.$payment->shopping_order->shopping_user->billing_lastname;
$email = ! isset($payment->shopping_order->shopping_user) ? 'Keine Email' : $payment->shopping_order->shopping_user->billing_email;
$shipped = '<span class="badge badge-pill badge-'.$payment->shopping_order->getShippedColor().'">'.$payment->shopping_order->getShippedType().'</span>';
// Countdown für nächste Erinnerung berechnen
$countdown = $this->getNextReminderCountdown($payment);
$detailedData[] = [
'clearingtype' => $clearingtype,
'clearingtype_name' => $this->getClearingtype($clearingtype),
@ -157,32 +159,31 @@ class PaymentReminderService
*/
public function sendReminder($payment)
{
//holen der nächsten zahlungserinnerung
// holen der nächsten zahlungserinnerung
$payment_reminder = $this->getReminder((int) $payment->reminder, $payment->clearingtype);
if(!$payment_reminder){
if (! $payment_reminder) {
return false;
}
//zahlungserinnerung Platzhalter ersetzen.
// zahlungserinnerung Platzhalter ersetzen.
$payment_reminder = $this->replacePlaceholder($payment, $payment_reminder);
//zahlungserinnerung senden
// zahlungserinnerung senden
$emailSent = $this->sendReminderEmail($payment, $payment_reminder);
if ($emailSent) {
$this->createLog(
'email_sent',
"Zahlungserinnerung E-Mail gesendet an: {$payment->shopping_order->shopping_user->billing_email}, Subject: {$payment_reminder->subject}",
'ShoppingOrder',
'ShoppingOrder',
$payment->shopping_order_id,
3
);
}
//action ausführen
if($payment_reminder->action === 'set_order_status_cancelled'){
// action ausführen
if ($payment_reminder->action === 'set_order_status_cancelled') {
$this->setNoNPayment($payment);
$payment->shopping_order->shipped = 10;
$payment->shopping_order->save();
@ -194,10 +195,10 @@ class PaymentReminderService
3
);
}
//reminder setzen +1
$payment->reminder = (int) $payment->reminder + 1;
$payment->reminder_date = Carbon::now();
// reminder setzen +1
$payment->reminder = (int) $payment->reminder + 1; // anzahl der Reminder
$payment->reminder_date = Carbon::now(); // wann wurde der letzte reminder gesendet?
$payment->save();
$this->createLog(
@ -225,7 +226,7 @@ class PaymentReminderService
public function getClearingtype($clearingtype)
{
return isset($this->clearingtypes[$clearingtype]) ? $this->clearingtypes[$clearingtype] : $clearingtype;
return isset($this->clearingtypes[$clearingtype]) ? $this->clearingtypes[$clearingtype] : $clearingtype;
}
public function getReminder($reminder, $clearingtype)
@ -235,13 +236,14 @@ class PaymentReminderService
->orderBy('interval', 'asc')
->get();
if($payment_reminders->isEmpty()) {
if ($payment_reminders->isEmpty()) {
return false;
}
// Wenn reminder größer ist als Anzahl der Erinnerungen
if($reminder >= $payment_reminders->count()) {
if ($reminder >= $payment_reminders->count()) {
return false;
}
// Hole die Erinnerung an Position $reminder (0,1,2,3...)
return $payment_reminders[$reminder];
}
@ -256,12 +258,12 @@ class PaymentReminderService
'{billing_last_name}' => $shopping_user->billing_lastname,
'{order_number}' => '<b>'.$shopping_order->getLastShoppingPayment('reference').'</b>',
'{order_date}' => '<b>'.$shopping_order->created_at->format('d.m.Y').'</b>',
'{order_total}' => '<b>'.$shopping_order->getFormattedTotalShipping().'</b>'
'{order_total}' => '<b>'.$shopping_order->getFormattedTotalShipping().'</b>',
];
$payment_reminder->subject = str_replace(
array_keys($replacements),
array_values($replacements),
array_values($replacements),
$payment_reminder->subject
);
@ -280,29 +282,30 @@ class PaymentReminderService
$email = $payment->shopping_order->shopping_user->billing_email;
$subject = $payment_reminder->subject;
$message = $payment_reminder->message;
if(Util::isTestSystem()){
if (Util::isTestSystem()) {
$email = config('app.checkout_test_mail');
}
if($payment->shopping_order->mode === 'test' || Util::isTestSystem()){
if ($payment->shopping_order->mode === 'test' || Util::isTestSystem()) {
$bcc[] = config('app.checkout_test_mail');
}else{
} else {
$bcc[] = config('app.checkout_mail');
}
Mail::to($email)->bcc($bcc)->send(new PaymentReminderEmail($subject, $message, $payment->shopping_order));
return true;
} catch (\Exception $e) {
\Log::error('Fehler beim E-Mail-Versand: ' . $e->getMessage());
\Log::error('Fehler beim E-Mail-Versand: '.$e->getMessage());
$this->createLog(
'email_exception',
"E-Mail Exception: " . $e->getMessage() . " für Payment ID: {$payment->id}",
'E-Mail Exception: '.$e->getMessage()." für Payment ID: {$payment->id}",
'ShoppingOrder',
$payment->shopping_order_id,
5
);
return false;
}
}
@ -332,39 +335,39 @@ class PaymentReminderService
return [
'type' => 'completed',
'message' => 'Alle Erinnerungen gesendet',
'days_left' => 0
'days_left' => 0,
];
}
// Hole die nächste Erinnerung
$next_reminder = $payment_reminders[$payment->reminder];
// Berechne die Differenz zwischen aktuellem und nächstem Reminder
$current_reminder = $payment_reminders[$payment->reminder - 1];
$interval_difference = $next_reminder->interval - $current_reminder->interval;
// Berechne das Datum der nächsten Erinnerung
$next_reminder_date = Carbon::parse($payment->reminder_date)->addDays($interval_difference);
// Berechne die verbleibenden Tage
$days_left = Carbon::now()->diffInDays($next_reminder_date, false);
// Wenn die nächste Erinnerung bereits fällig ist
if ($days_left <= 0) {
return [
'type' => 'overdue',
'message' => 'Nächste Erinnerung fällig',
'days_left' => 0,
'next_reminder_date' => $next_reminder_date
'next_reminder_date' => $next_reminder_date,
];
}
return [
'type' => 'countdown',
'message' => 'Nächste Erinnerung in ' . $days_left . ' Tagen',
'message' => 'Nächste Erinnerung in '.$days_left.' Tagen',
'days_left' => $days_left,
'next_reminder_date' => $next_reminder_date,
'next_reminder_interval' => $interval_difference
'next_reminder_interval' => $interval_difference,
];
}
@ -374,7 +377,7 @@ class PaymentReminderService
public function getPaymentReminderLogs($limit = 100, $paymentId = null, $action = null)
{
$query = Logger::where('channel', 'payment_reminder')
->orderBy('created_at', 'desc');
->orderBy('created_at', 'desc');
if ($paymentId) {
$query->where('model_id', $paymentId);
@ -393,9 +396,9 @@ class PaymentReminderService
public function getLogsForPayment($orderId)
{
return Logger::where('channel', 'payment_reminder')
->where('model_id', $orderId)
->orderBy('created_at', 'desc')
->get();
->where('model_id', $orderId)
->orderBy('created_at', 'desc')
->get();
}
/**
@ -404,9 +407,9 @@ class PaymentReminderService
public function getLogsForDateRange($startDate, $endDate)
{
return Logger::where('channel', 'payment_reminder')
->whereBetween('created_at', [$startDate, $endDate])
->orderBy('created_at', 'desc')
->get();
->whereBetween('created_at', [$startDate, $endDate])
->orderBy('created_at', 'desc')
->get();
}
/**
@ -415,39 +418,38 @@ class PaymentReminderService
public function getLogStatistics($days = 30)
{
$startDate = Carbon::now()->subDays($days);
$stats = Logger::where('channel', 'payment_reminder')
->where('created_at', '>=', $startDate)
->selectRaw('action, level, COUNT(*) as count')
->groupBy('action', 'level')
->get();
->where('created_at', '>=', $startDate)
->selectRaw('action, level, COUNT(*) as count')
->groupBy('action', 'level')
->get();
$summary = [
'total_logs' => Logger::where('channel', 'payment_reminder')
->where('created_at', '>=', $startDate)
->count(),
->where('created_at', '>=', $startDate)
->count(),
'emails_sent' => Logger::where('channel', 'payment_reminder')
->where('action', 'email_sent')
->where('created_at', '>=', $startDate)
->count(),
->where('action', 'email_sent')
->where('created_at', '>=', $startDate)
->count(),
'emails_failed' => Logger::where('channel', 'payment_reminder')
->where('action', 'email_exception')
->where('created_at', '>=', $startDate)
->count(),
->where('action', 'email_exception')
->where('created_at', '>=', $startDate)
->count(),
'reminders_completed' => Logger::where('channel', 'payment_reminder')
->where('action', 'reminder_completed')
->where('created_at', '>=', $startDate)
->count(),
->where('action', 'reminder_completed')
->where('created_at', '>=', $startDate)
->count(),
'actions_executed' => Logger::where('channel', 'payment_reminder')
->where('action', 'action_completed')
->where('created_at', '>=', $startDate)
->count(),
->where('action', 'action_completed')
->where('created_at', '>=', $startDate)
->count(),
];
return [
'summary' => $summary,
'detailed_stats' => $stats
'detailed_stats' => $stats,
];
}
}
}

View file

@ -0,0 +1,343 @@
<?php
namespace App\Services;
use App\Models\Product;
use App\Models\Production;
use App\Models\StockEntry;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
class ProductionService
{
/**
* @param array<string, mixed> $data
* @param array<int, array{ingredient_id: int, stock_entry_id: int, quantity_used: float|int|string}> $ingredientLines
*/
public function store(array $data, array $ingredientLines, int $userId): Production
{
return DB::transaction(function () use ($data, $ingredientLines, $userId) {
$product = Product::query()
->with(['p_ingredients', 'packagings'])
->findOrFail($data['product_id']);
$locationId = (int) $data['location_id'];
$producedQty = (int) $data['quantity'];
if ($producedQty < 1) {
throw ValidationException::withMessages(['quantity' => __('Die Stückzahl muss mindestens 1 sein.')]);
}
if ($product->p_ingredients->isEmpty()) {
throw ValidationException::withMessages(['product_id' => __('Das Produkt hat keine Rezeptur (Inhaltsstoffe).')]);
}
$requiredGrams = $this->requiredGramsByIngredient($product, $producedQty);
$sums = [];
foreach ($ingredientLines as $line) {
$iid = (int) $line['ingredient_id'];
$used = $this->parseQuantity($line['quantity_used'] ?? null);
$sums[$iid] = ($sums[$iid] ?? 0) + $used;
}
foreach ($requiredGrams as $iid => $req) {
$sum = $sums[$iid] ?? 0;
if (abs($sum - $req) > 0.02) {
throw ValidationException::withMessages([
'ingredient_lines' => __('Summe der Chargen-Mengen pro INCI muss dem Soll-Verbrauch entsprechen (INCI :id: Soll :req g, Ist :sum g).', [
'id' => $iid,
'req' => number_format($req, 2, ',', '.'),
'sum' => number_format($sum, 2, ',', '.'),
]),
]);
}
}
foreach ($sums as $iid => $_sum) {
if (! isset($requiredGrams[$iid])) {
throw ValidationException::withMessages([
'ingredient_lines' => __('Unerwarteter Inhaltsstoff in den Chargen-Zeilen.'),
]);
}
}
foreach ($ingredientLines as $line) {
$this->assertStockEntryMatchesLine($line, $locationId);
}
$mhdWarning = $this->computeMhdWarning($product, $data['produced_at'], $ingredientLines);
$production = Production::query()->create([
'product_id' => $product->id,
'location_id' => $locationId,
'produced_by' => $userId,
'produced_at' => $data['produced_at'],
'quantity' => $producedQty,
'notes' => $data['notes'] ?? null,
'mhd_warning' => $mhdWarning,
]);
foreach ($ingredientLines as $line) {
$production->productionIngredients()->create([
'ingredient_id' => (int) $line['ingredient_id'],
'stock_entry_id' => (int) $line['stock_entry_id'],
'quantity_used' => $this->parseQuantity($line['quantity_used'] ?? null),
]);
}
foreach ($product->packagings as $bom) {
$perUnit = (float) ($bom->pivot->quantity ?? 1);
$pieces = (int) round($perUnit * $producedQty);
if ($pieces < 1) {
$pieces = 1;
}
$production->productionPackagings()->create([
'packaging_item_id' => $bom->id,
'quantity_used' => $pieces,
]);
}
return $production->fresh(['product', 'location', 'productionIngredients', 'productionPackagings']);
});
}
/**
* @param array<string, mixed> $data
* @param array<int, array{ingredient_id: int, stock_entry_id: int, quantity_used: float|int|string}> $ingredientLines
*/
public function updateProduction(Production $production, array $data, array $ingredientLines, int $userId): Production
{
return DB::transaction(function () use ($production, $data, $ingredientLines) {
$product = Product::query()
->with(['p_ingredients', 'packagings'])
->findOrFail($data['product_id']);
$locationId = (int) $data['location_id'];
$producedQty = (int) $data['quantity'];
if ($producedQty < 1) {
throw ValidationException::withMessages(['quantity' => __('Die Stückzahl muss mindestens 1 sein.')]);
}
if ($product->p_ingredients->isEmpty()) {
throw ValidationException::withMessages(['product_id' => __('Das Produkt hat keine Rezeptur (Inhaltsstoffe).')]);
}
$requiredGrams = $this->requiredGramsByIngredient($product, $producedQty);
$sums = [];
foreach ($ingredientLines as $line) {
$iid = (int) $line['ingredient_id'];
$used = $this->parseQuantity($line['quantity_used'] ?? null);
$sums[$iid] = ($sums[$iid] ?? 0) + $used;
}
foreach ($requiredGrams as $iid => $req) {
$sum = $sums[$iid] ?? 0;
if (abs($sum - $req) > 0.02) {
throw ValidationException::withMessages([
'ingredient_lines' => __('Summe der Chargen-Mengen pro INCI muss dem Soll-Verbrauch entsprechen (INCI :id: Soll :req g, Ist :sum g).', [
'id' => $iid,
'req' => number_format($req, 2, ',', '.'),
'sum' => number_format($sum, 2, ',', '.'),
]),
]);
}
}
foreach ($ingredientLines as $line) {
$this->assertStockEntryMatchesLine($line, $locationId);
}
$mhdWarning = $this->computeMhdWarning($product, $data['produced_at'], $ingredientLines);
$production->update([
'product_id' => $product->id,
'location_id' => $locationId,
'produced_at' => $data['produced_at'],
'quantity' => $producedQty,
'notes' => $data['notes'] ?? null,
'mhd_warning' => $mhdWarning,
]);
$production->productionIngredients()->delete();
foreach ($ingredientLines as $line) {
$production->productionIngredients()->create([
'ingredient_id' => (int) $line['ingredient_id'],
'stock_entry_id' => (int) $line['stock_entry_id'],
'quantity_used' => $this->parseQuantity($line['quantity_used'] ?? null),
]);
}
$production->productionPackagings()->delete();
foreach ($product->packagings as $bom) {
$perUnit = (float) ($bom->pivot->quantity ?? 1);
$pieces = (int) round($perUnit * $producedQty);
if ($pieces < 1) {
$pieces = 1;
}
$production->productionPackagings()->create([
'packaging_item_id' => $bom->id,
'quantity_used' => $pieces,
]);
}
return $production->fresh(['product', 'location', 'productionIngredients', 'productionPackagings']);
});
}
/**
* @return array<int, float>
*/
public function requiredGramsByIngredient(Product $product, int $producedQuantity): array
{
$required = [];
foreach ($product->p_ingredients as $ing) {
$gram = $ing->pivot->gram;
if ($gram === null || $gram === '') {
throw ValidationException::withMessages([
'product_id' => __('Für „:name“ fehlt Gramm in der Rezeptur.', ['name' => $ing->name]),
]);
}
$factor = (float) ($ing->pivot->factor ?? 1.1);
$required[(int) $ing->id] = (float) $gram * $factor * $producedQuantity;
}
return $required;
}
/**
* @param array{ingredient_id: int, stock_entry_id: int, quantity_used?: mixed} $line
*/
public function assertStockEntryMatchesLine(array $line, int $locationId): void
{
$entry = StockEntry::query()->findOrFail((int) $line['stock_entry_id']);
if ($entry->status !== 'received') {
throw ValidationException::withMessages([
'ingredient_lines' => __('Wareneingang :id ist nicht als eingegangen gebucht.', ['id' => $entry->id]),
]);
}
if ((int) $entry->ingredient_id !== (int) $line['ingredient_id']) {
throw ValidationException::withMessages(['ingredient_lines' => __('Charge passt nicht zum Inhaltsstoff.')]);
}
if ((int) $entry->location_id !== $locationId) {
throw ValidationException::withMessages(['ingredient_lines' => __('Charge gehört zu einem anderen Lagerort.')]);
}
}
/**
* @param array<int, array{ingredient_id: int, stock_entry_id: int, quantity_used?: mixed}> $ingredientLines
*/
public function computeMhdWarning(Product $product, string $producedAt, array $ingredientLines): bool
{
if ($product->shelf_life_type !== 'fixed' || ! $product->shelf_life_months) {
return false;
}
$productEnd = Carbon::parse($producedAt)->addMonths((int) $product->shelf_life_months)->startOfDay();
foreach ($ingredientLines as $line) {
$entry = StockEntry::query()->find((int) $line['stock_entry_id']);
if (! $entry || ! $entry->best_before) {
continue;
}
if ($entry->best_before->lt($productEnd)) {
return true;
}
}
return false;
}
/**
* Letzte empfangene Wareneingänge pro Inhaltsstoff am Standort (max. 3).
*
* @return Collection<int, StockEntry>
*/
public function latestStockEntriesForIngredient(int $ingredientId, int $locationId, int $limit = 3)
{
return StockEntry::query()
->where('status', 'received')
->where('entry_type', 'ingredient')
->where('ingredient_id', $ingredientId)
->where('location_id', $locationId)
->orderByDesc('received_at')
->orderByDesc('id')
->limit($limit)
->get();
}
/**
* @return array<string, mixed>
*/
public function buildRecipePayload(Product $product, int $locationId, int $productionQuantity): array
{
$product->loadMissing(['p_ingredients', 'packagings.packagingMaterial']);
$qty = max(1, $productionQuantity);
$ingredients = [];
foreach ($product->p_ingredients as $ing) {
$gram = $ing->pivot->gram;
$factor = (float) ($ing->pivot->factor ?? 1.1);
$req = ($gram !== null && $gram !== '') ? (float) $gram * $factor * $qty : null;
$ingredients[] = [
'id' => $ing->id,
'name' => $ing->name,
'gram' => $gram !== null && $gram !== '' ? (float) $gram : null,
'factor' => $factor,
'required_grams_total' => $req,
'stock_entries' => $this->latestStockEntriesForIngredient((int) $ing->id, $locationId)->map(function (StockEntry $se) {
return [
'id' => $se->id,
'batch_number' => $se->batch_number,
'best_before' => $se->best_before?->format('Y-m-d'),
'received_at' => $se->received_at?->format('Y-m-d'),
'received_quantity' => $se->received_quantity !== null ? (float) $se->received_quantity : null,
];
})->values()->all(),
];
}
$packagings = [];
foreach ($product->packagings as $pk) {
$perUnit = (float) ($pk->pivot->quantity ?? 1);
$packagings[] = [
'id' => $pk->id,
'name' => $pk->name,
'quantity_per_product' => $perUnit,
'total_pieces' => (int) round($perUnit * max(1, $productionQuantity)),
'weight_grams' => $pk->weight_grams !== null ? (float) $pk->weight_grams : null,
'material_name' => $pk->packagingMaterial?->name,
];
}
return [
'product' => [
'id' => $product->id,
'name' => $product->name,
'shelf_life_type' => $product->shelf_life_type,
'shelf_life_months' => $product->shelf_life_months,
],
'location_id' => $locationId,
'production_quantity' => $productionQuantity,
'ingredients' => $ingredients,
'packagings' => $packagings,
];
}
private function parseQuantity(mixed $value): float
{
if ($value === null || $value === '') {
return 0.0;
}
if (is_numeric($value)) {
return (float) $value;
}
$n = reFormatNumber((string) $value);
return $n !== null && $n !== '' ? (float) $n : 0.0;
}
}