Skip to content

Instantly share code, notes, and snippets.

@jongravois
Created February 27, 2021 14:22
Show Gist options
  • Save jongravois/4f4e5b48de9ae378dbb5cc6336bb7ac2 to your computer and use it in GitHub Desktop.
Save jongravois/4f4e5b48de9ae378dbb5cc6336bb7ac2 to your computer and use it in GitHub Desktop.
<?php
namespace App\Models;
use App\Pipes\Invoices\BandedRatesProfit;
use App\Pipes\Invoices\FixedRateProfit;
use App\Pipes\Invoices\NoConsignmentAbort;
use App\Pipes\LegacyInvoices\LegacyBandedRatesProfit;
use App\Pipes\LegacyInvoices\LegacyFixedRateProfit;
use App\Pipes\LegacyInvoices\NoConsignmentLegacyAbort;
use App\Traits\Posted;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pipeline\Pipeline;
class PostedInvoice extends Model
{
use HasFactory;
use Posted;
protected $table = 'posted_invoices';
protected $guarded = [];
protected $appends = [
'invoice_date_for_editing',
'post_date_for_editing',
'ship_date_for_editing'
];
protected $casts = [
'processed' => 'boolean',
'split_bands' => 'boolean',
'is_arg' => 'boolean',
'gross' => 'double',
'net' => 'double',
'profit' => 'double',
'gp_percent_used' => 'double',
'adjusted_net' => 'integer',
'qty_invoiced' => 'integer',
'unit_price' => 'double',
'unit_cost' => 'double',
'unit_freight_cost' => 'double',
'cost' => 'double',
'invoice_date' => 'date',
'post_date' => 'date',
'ship_date' => 'date',
];
/* RELATIONSHIPS */
/* RELATIONSHIPS */
/* MODEL EVENTS */
/* MODEL EVENTS */
/* ACCESSORS / MUTATORS */
public function getInvoiceDateForEditingAttribute()
{
return optional($this->invoice_date)->format('m/d/Y');
} // end function
public function getPostDateForEditingAttribute()
{
return optional($this->post_date)->format('m/d/Y');
} // end function
public function getShipDateForEditingAttribute()
{
return optional($this->ship_date)->format('m/d/Y');
} // end function
public function setInvoiceDateForEditingAttribute($value)
{
$this->invoice_date = Carbon::parse($value);
} // end function
public function setPostDateForEditingAttribute($value)
{
$this->post_date = Carbon::parse($value);
} // end function
public function setShipDateForEditingAttribute($value)
{
$this->ship_date = Carbon::parse($value);
} // end function
/* ACCESSORS / MUTATORS */
/* SCOPES */
public function scopeDay($query, $dt=null)
{
if(!$dt) { $dt = today(); } // end if
return $query->whereDate('post_date', $dt);
} // end function
public function scopeWeek($query, $dt=null)
{
if(!$dt) { $dt = today(); } // end if
$start = $dt->firstOfWeek();
$end = $dt->endOfWeek();
return $query->whereBetween('post_date', [$start, $end]);
} // end function
public function scopeMonth($query, $dt=null)
{
if(!$dt) {
$dt = today();
} elseif(! $dt instanceof Carbon) {
$dt = Carbon::parse($dt);
} // end if
return $query->whereBetween('post_date', [$dt->startOfMonth(), $dt->endOfMonth()]);
} // end function
public function scopeYear($query, $dt=null)
{
if(!$dt) {
$dt = today();
} elseif(! $dt instanceof Carbon) {
$dt = Carbon::parse($dt);
} // end if
return $query->whereBetween('post_date', [today()->startOfYear(), today()->endOfYear()]);
} // end function
public function scopePartsales($query)
{
return $query->where('route_code', '===', 'S');
} // end function
public function scopeArggrossmonth($query, $dt=null)
{
if(!$dt) {
$dt = today();
} elseif(! $dt instanceof Carbon) {
$dt = Carbon::parse($dt);
} // end if
return $query->where('route_code', '!=', 'M')
->whereBetween('post_date', [$dt->startOfMonth(), $dt->endOfMonth()])
->whereIn('consignment_code', ['AP-BLJ','CS-TOE', '2-AERC', 'OE-IHA']);
} // end function
public function scopeArggrossyear($query, $dt=null)
{
if(!$dt) {
$dt = today();
} elseif(! $dt instanceof Carbon) {
$dt = Carbon::parse($dt);
} // end if
return $query->where('route_code', '!=', 'M')
->whereBetween('post_date', [$dt->startOfYear(), $dt->endOfYear()])
->whereIn('consignment_code', ['AP-BLJ','CS-TOE', '2-AERC', 'OE-IHA']);
} // end function
public function scopeArgreturnsmonth($query, $dt=null)
{
if(!$dt) {
$dt = today();
} elseif(! $dt instanceof Carbon) {
$dt = Carbon::parse($dt);
} // end if
return $query->where('route_code', '==', 'M')
->whereBetween('post_date', [$dt->startOfMonth(), $dt->endOfMonth()])
->whereIn('consignment_code', ['AP-BLJ','CS-TOE', '2-AERC', 'OE-IHA']);
} // end function
public function scopeArgreturnsyear($query, $dt=null)
{
if(!$dt) {
$dt = today();
} elseif(! $dt instanceof Carbon) {
$dt = Carbon::parse($dt);
} // end if
return $query->where('route_code', '==', 'M')
->whereBetween('post_date', [$dt->startOfYear(), $dt->endOfYear()])
->whereIn('consignment_code', ['AP-BLJ','CS-TOE', '2-AERC', 'OE-IHA']);
} // end function
public function scopeCattypemonth($query, $dt, $catid)
{
$dt = Carbon::parse($dt);
return $query->where('route_code', '!=', 'M')
->where('cat', $catid)
->whereYear('post_date', $dt->year)
->whereMonth('post_date', $dt->month);
} // end function
public function scopeCattypeyear($query, $dt, $catid)
{
$dt = Carbon::parse($dt);
return $query->where('route_code', '!=', 'M')
->where('cat', $catid)
->whereYear('post_date', $dt->year);
} // end function
public function scopeGrosstypemonth($query, $dt, $typeid)
{
$dt = Carbon::parse($dt);
return $query->where('route_code', '!=', 'M')
->where('customer_type', $typeid)
->whereYear('post_date', $dt->year)
->whereMonth('post_date', $dt->month);
} // end function
public function scopeGrosstypeyear($query, $dt, $typeid)
{
$dt = Carbon::parse($dt);
return $query->where('route_code', '!=', 'M')
->where('customer_type', $typeid)
->whereYear('post_date', $dt->year);
} // end function
public function scopeGrossregionmonth($query, $dt, $regionid)
{
$dt = Carbon::parse($dt);
return $query->where('route_code', '!=', 'M')
->whereRegion('company_code', $regionid)
->whereYear('post_date', $dt->year)
->whereMonth('post_date', $dt->month);
} // end function
public function scopeGrossregionyear($query, $dt, $regionid)
{
$dt = Carbon::parse($dt);
return $query->where('route_code', '!=', 'M')
->whereRegion('company_code', $regionid)
->whereYear('post_date', $dt->year);
} // end function
public function scopeAccountant($query, $sp)
{
return $query->whereSalespersonCode($sp);
} // end function
public function scopeByLot($query, $lot)
{
return $query->whereConsignmentCode($lot);
} // end function
public function scopeChronological($query)
{
return $query->orderBy('post_date')
->orderBy('invc_number');
} // end function
public function scopeGrossmonth($query, $dt=null)
{
$dt = Carbon::parse($dt);
return $query->where('route_code', '!=', 'M')
->whereYear('post_date', $dt->year)
->whereMonth('post_date', $dt->month);
} // end function
public function scopeGrossyear($query, $dt=null)
{
$dt = Carbon::parse($dt);
return $query->where('route_code', '!=', 'M')
->whereYear('post_date', $dt->year);
} // end function
public function scopeMtdAll($query, $dt=null)
{
$dt = Carbon::parse($dt);
return $query->whereYear('post_date', $dt->year)
->whereMonth('post_date', $dt->month);
} // end function
public function scopeMonthrevenue($query, $dt=null)
{
$dt = Carbon::parse($dt);
return $query->where('route_code', '!=', 'M')
->whereYear('post_date', $dt->year)
->whereMonth('post_date', $dt->month);
} // end function
public function scopeMtdSales($query, $dt=null)
{
$dt = Carbon::parse($dt);
return $query->where('route_code', '!=', 'M')
->whereYear('post_date', $dt->year)
->whereMonth('post_date', $dt->month);
} // end function
public function scopeMonthsales($query, $dt=null)
{
$dt = Carbon::parse($dt);
return $query->where('route_code', '!=', 'M')
->where('consignment_code', '!=', 'N351BU')
->whereYear('post_date', $dt->year)
->whereMonth('post_date', $dt->month);
} // end function
public function scopeOther($query)
{
$sales = User::sales()->get()->pluck('salesperson');
return $query->whereNotIn('salesperson_code', $sales);
} // end function
public function scopeReturnsmonth($query, $dt=null)
{
$dt = Carbon::parse($dt);
return $query->where('route_code', 'M')
->whereYear('post_date', $dt->year)
->whereMonth('post_date', $dt->month);
} // end function
public function scopeReturnsyear($query, $dt=null)
{
$dt = Carbon::parse($dt);
return $query->where('route_code', 'M')
->whereYear('post_date', $dt->year);
} // end function
public function scopeSeller($query, $sp)
{
if($sp === 'N351BU') {
return $query->whereConsignmentCode($sp);
} // end if
return $query->whereSalespersonCode($sp); // end if
} // end function
public function scopeSpan($query, Carbon $dt)
{
return $query->whereYear('post_date', $dt->year)->whereMonth('post_date', $dt->month);
} // end function
public function scopeSpecial($query, $dt=null)
{
$dt = Carbon::parse($dt);
return $query->where('route_code', '!=', 'M')
->where('consignment_code', '==', 'N351BU')
->whereYear('post_date', $dt->year)
->whereMonth('post_date', $dt->month);
} // end function
/* SCOPES */
/* STATIC METHODS */
public static function fullSearch($query)
{
return empty($query) ? static::query()
: static::where('company_name', 'like', '%'.$query.'%')
->orWhere('invc_number', 'like', '%'.$query.'%')
->orWhere('consignment_code', 'like', '%'.$query.'%')
->orWhere('so_number', 'like', '%'.$query.'%')
->orWhere('salesperson_code', 'like', '%'.$query.'%');
} // end function
public static function byDay($dt=null): array
{
if($dt) {
$dt = Carbon::parse($dt);
} else {
$dt = today();
} // end if
$reporter = [];
$salesTeam = User::sales()->select(['id', 'last_name', 'salesperson'])->get();
foreach ($salesTeam as $sales) {
$reporter[] = [
'salesperson' => $sales->salesperson,
'last_name' => $sales->last_name,
'count' => static::day()->seller($sales->salesperson)->count(),
'gross' => static::day($dt)->seller($sales->salesperson)->sum('gross'),
'net' => static::day($dt)->seller($sales->salesperson)->sum('net'),
'profit' => static::day($dt)->seller($sales->salesperson)->sum('profit')
];
} // end foreach
return collect($reporter)->sortBy('last_name')->values()->all();
} // end function
public static function byMonth($dt=null)
{
$firstOfMonth = Carbon::parse($dt);
$reporter = [];
$salesTeam = User::sales()->select(['id', 'last_name', 'salesperson'])->get();
foreach ($salesTeam as $sales) {
$reporter[] = [
'salesperson' => $sales->salesperson,
'last_name' => $sales->last_name,
'count' => static::month()->seller($sales->salesperson)->count(),
'gross' => static::month()->seller($sales->salesperson)->sum('gross'),
'net' => static::month()->seller($sales->salesperson)->sum('net'),
'profit' => static::month()->seller($sales->salesperson)->sum('profit')
];
} // end foreach
return collect($reporter)->sortBy('last_name')->values()->all();
} // end function
public static function byMonthSales($dt=null)
{
$firstOfMonth = Carbon::parse($dt);
$reporter = [];
$salesTeam = User::sales()->select(['id', 'last_name', 'salesperson'])->get();
$specials = self::whereConsignmentCode('N351BU')
->whereRouteCode('S')
->whereYear('post_date', today()->year)
->whereMonth('post_date', today()->month)
->get();
$reporter[] = [
'salesperson' => '777-300ER',
'last_name' => 'N351BU',
'count' => $specials->count(),
'gross' => $specials->sum('gross'),
'net' => $specials->sum('net'),
'profit' => $specials->sum('profit')
];
foreach ($salesTeam as $sales) {
$reporter[] = [
'salesperson' => $sales->salesperson,
'last_name' => $sales->last_name,
'count' => static::monthsales()->seller($sales->salesperson)->count(),
'gross' => static::monthsales()->seller($sales->salesperson)->sum('gross'),
'net' => static::monthsales()->seller($sales->salesperson)->sum('net'),
'profit' => static::monthsales()->seller($sales->salesperson)->sum('profit')
];
} // end foreach
return collect($reporter)->sortBy('last_name')->values()->all();
} // end function
public static function byYear($dt=null)
{
$firstOfYear = Carbon::parse($dt);
$reporter = [];
$salesTeam = User::sales()->select(['id', 'last_name', 'salesperson'])->get();
foreach ($salesTeam as $sales) {
$reporter[] = [
'salesperson' => $sales->salesperson,
'last_name' => $sales->last_name,
'count' => static::year()->seller($sales->salesperson)->count(),
'gross' => static::year()->seller($sales->salesperson)->sum('gross'),
'net' => static::year()->seller($sales->salesperson)->sum('net'),
'profit' => static::year()->seller($sales->salesperson)->sum('profit')
];
} // end foreach
return collect($reporter)->sortBy('last_name')->values()->all();
} // end function
public static function calcGrossByMonth($dt): float
{
if($dt) {
$dt = Carbon::parse($dt);
} else {
$dt = today();
} // end if
return self::mtdsales()->sum('gross');
} // end function
public static function calcRepairsByMonthBySales($mo, $sp): float
{
return self::whereYear('post_date', today()->format('Y'))
->whereMonth('post_date', $mo)
->whereSalespersonCode($sp)
->sum('cost');
} // end function
public static function calcTotalReturnsByLot($lot)
{
return static::whereRouteCode('M')
->whereConsignmentCode($lot)->sum('net');
} // end function
public static function countPartsSoldByLot($lot)
{
return static::whereRouteCode('S')
->whereConsignmentCode($lot)->sum('qty_invoiced');
} // end function
public static function countTotalReturnsByLot($lot)
{
return static::whereRouteCode('M')->whereConsignmentCode($lot)->count();
} // end function
public static function getArgGrossMonthly(): array
{
return [
'lastName' => 'ARG Gross',
'jan' => self::arggrossmonth('2021-01-01')->sum('gross'),
'feb' => self::arggrossmonth('2021-02-01')->sum('gross'),
'mar' => self::arggrossmonth('2021-03-01')->sum('gross'),
'apr' => self::arggrossmonth('2021-04-01')->sum('gross'),
'may' => self::arggrossmonth('2021-05-01')->sum('gross'),
'jun' => self::arggrossmonth('2021-06-01')->sum('gross'),
'jul' => self::arggrossmonth('2021-07-01')->sum('gross'),
'aug' => self::arggrossmonth('2021-08-01')->sum('gross'),
'sep' => self::arggrossmonth('2021-09-01')->sum('gross'),
'oct' => self::arggrossmonth('2021-10-01')->sum('gross'),
'nov' => self::arggrossmonth('2021-11-01')->sum('gross'),
'dec' => self::arggrossmonth('2021-12-01')->sum('gross'),
'total' => self::arggrossyear('2021-12-01')->sum('gross'),
];
} // end function
public static function getArgCostMonthly(): array
{
return [
'lastName' => 'ARG Cost',
'jan' => self::arggrossmonth('2021-01-01')->sum('cost'),
'feb' => self::arggrossmonth('2021-02-01')->sum('cost'),
'mar' => self::arggrossmonth('2021-03-01')->sum('cost'),
'apr' => self::arggrossmonth('2021-04-01')->sum('cost'),
'may' => self::arggrossmonth('2021-05-01')->sum('cost'),
'jun' => self::arggrossmonth('2021-06-01')->sum('cost'),
'jul' => self::arggrossmonth('2021-07-01')->sum('cost'),
'aug' => self::arggrossmonth('2021-08-01')->sum('cost'),
'sep' => self::arggrossmonth('2021-09-01')->sum('cost'),
'oct' => self::arggrossmonth('2021-10-01')->sum('cost'),
'nov' => self::arggrossmonth('2021-11-01')->sum('cost'),
'dec' => self::arggrossmonth('2021-12-01')->sum('cost'),
'total' => self::arggrossyear('2021-12-01')->sum('cost'),
];
} // end function
public static function getArgReturnsMonthly(): array
{
return [
'lastName' => 'ARG Returns',
'jan' => self::argreturnsmonth('2021-01-01')->sum('gross'),
'feb' => self::argreturnsmonth('2021-02-01')->sum('gross'),
'mar' => self::argreturnsmonth('2021-03-01')->sum('gross'),
'apr' => self::argreturnsmonth('2021-04-01')->sum('gross'),
'may' => self::argreturnsmonth('2021-05-01')->sum('gross'),
'jun' => self::argreturnsmonth('2021-06-01')->sum('gross'),
'jul' => self::argreturnsmonth('2021-07-01')->sum('gross'),
'aug' => self::argreturnsmonth('2021-08-01')->sum('gross'),
'sep' => self::argreturnsmonth('2021-09-01')->sum('gross'),
'oct' => self::argreturnsmonth('2021-10-01')->sum('gross'),
'nov' => self::argreturnsmonth('2021-11-01')->sum('gross'),
'dec' => self::argreturnsmonth('2021-12-01')->sum('gross'),
'total' => self::argreturnsyear('2021-12-01')->sum('gross'),
];
} // end function
public static function getCategoryDistributionMonthly(): array
{
$reporter = [];
$team = [
[
'id' => 1,
'title' => 'CAT ONE'
],
[
'id' => 2,
'title' => 'CAT TWO'
],
[
'id' => 3,
'title' => 'CAT THREE'
]
];
foreach($team as $row) {
$mgjan = self::calcGrossByMonth('2021-01-01');
$mgfeb = self::calcGrossByMonth('2021-02-01');
$mgmar = self::calcGrossByMonth('2021-03-01');
$mgapr = self::calcGrossByMonth('2021-04-01');
$mgmay = self::calcGrossByMonth('2021-05-01');
$mgjun = self::calcGrossByMonth('2021-06-01');
$mgjul = self::calcGrossByMonth('2021-07-01');
$mgaug = self::calcGrossByMonth('2021-08-01');
$mgsep = self::calcGrossByMonth('2021-09-01');
$mgoct = self::calcGrossByMonth('2021-10-01');
$mgnov = self::calcGrossByMonth('2021-11-01');
$mgdec = self::calcGrossByMonth('2021-12-01');
$reporter[] = [
'lastName' => $row['title'],
'jan' => $jan = self::grosscatmonth('2021-01-01', $row['id'])->sum('gross'),
'jan_per' => $mgjan === 0 ? 0 : $jan/$mgjan,
'feb' => $feb = self::grosscatmonth('2021-02-01', $row['id'])->sum('gross'),
'feb_per' => $mgfeb === 0 ? 0 : $feb/$mgfeb,
'mar' => $mar = self::grosscatmonth('2021-03-01', $row['id'])->sum('gross'),
'mar_per' => $mgmar === 0 ? 0 : $mar/$mgmar,
'apr' => $apr = self::grosscatmonth('2021-04-01', $row['id'])->sum('gross'),
'apr_per' => $mgapr === 0 ? 0 : $apr/$mgapr,
'may' => $may = self::grosscatmonth('2021-05-01', $row['id'])->sum('gross'),
'may_per' => $mgmay === 0 ? 0 : $may/$mgmay,
'jun' => $jun = self::grosscatmonth('2021-06-01', $row['id'])->sum('gross'),
'jun_per' => $mgjun === 0 ? 0 : $jun/$mgjun,
'jul' => $jul = self::grosscatmonth('2021-07-01', $row['id'])->sum('gross'),
'jul_per' => $mgjul === 0 ? 0 : $jul/$mgjul,
'aug' => $aug = self::grosscatmonth('2021-08-01', $row['id'])->sum('gross'),
'aug_per' => $mgaug === 0 ? 0 : $aug/$mgaug,
'sep' => $sep = self::grosscatmonth('2021-09-01', $row['id'])->sum('gross'),
'sep_per' => $mgsep === 0 ? 0 : $sep/$mgsep,
'oct' => $oct = self::grosscatmonth('2021-10-01', $row['id'])->sum('gross'),
'oct_per' => $mgoct === 0 ? 0 : $oct/$mgoct,
'nov' => $nov = self::grosscatmonth('2021-11-01', $row['id'])->sum('gross'),
'nov_per' => $mgnov === 0 ? 0 : $nov/$mgnov,
'dec' => $dec = self::grosscatmonth('2021-12-01', $row['id'])->sum('gross'),
'dec_per' => $mgdec === 0 ? 0 : $dec/$mgdec,
'total' => $tot = self::grosscatyear('2021-12-01', $row['id'])->sum('gross'),
'total_per' => 0,
];
} // end foreach
return $reporter;
} // end function
public static function getCategoryMarginMonthly(): array
{
return [
[
'lastName' => 'CAT 1',
'jan' => 10000,
'feb' => 20000,
'mar' => 30000,
'apr' => 40000,
'may' => 50000,
'jun' => 0,
'jul' => 0,
'aug' => 0,
'sep' => 0,
'oct' => 0,
'nov' => 0,
'dec' => 0,
'total' => 150000
],
[
'lastName' => 'CAT 2',
'jan' => 10000,
'feb' => 20000,
'mar' => 30000,
'apr' => 40000,
'may' => 50000,
'jun' => 0,
'jul' => 0,
'aug' => 0,
'sep' => 0,
'oct' => 0,
'nov' => 0,
'dec' => 0,
'total' => 150000,
],
[
'lastName' => 'CAT 3',
'jan' => 10000,
'feb' => 20000,
'mar' => 30000,
'apr' => 40000,
'may' => 50000,
'jun' => 0,
'jul' => 0,
'aug' => 0,
'sep' => 0,
'oct' => 0,
'nov' => 0,
'dec' => 0,
'total' => 150000,
],
[
'lastName' => 'Other',
'jan' => 10000,
'feb' => 20000,
'mar' => 30000,
'apr' => 40000,
'may' => 50000,
'jun' => 0,
'jul' => 0,
'aug' => 0,
'sep' => 0,
'oct' => 0,
'nov' => 0,
'dec' => 0,
'total' => 150000,
],
];
} // end function
public static function getCustomerDistributionMonthly(): array
{
$reporter = [];
$team = CompanyType::whereReportVisible(true)->orderBy('sort_order')->get();
foreach($team as $row) {
$mgjan = self::calcGrossByMonth('2021-01-01');
$mgfeb = self::calcGrossByMonth('2021-02-01');
$mgmar = self::calcGrossByMonth('2021-03-01');
$mgapr = self::calcGrossByMonth('2021-04-01');
$mgmay = self::calcGrossByMonth('2021-05-01');
$mgjun = self::calcGrossByMonth('2021-06-01');
$mgjul = self::calcGrossByMonth('2021-07-01');
$mgaug = self::calcGrossByMonth('2021-08-01');
$mgsep = self::calcGrossByMonth('2021-09-01');
$mgoct = self::calcGrossByMonth('2021-10-01');
$mgnov = self::calcGrossByMonth('2021-11-01');
$mgdec = self::calcGrossByMonth('2021-12-01');
$reporter[] = [
'lastName' => $row->title,
'jan' => $jan = self::grosstypemonth('2021-01-01', $row->id)->sum('gross'),
'jan_per' => $mgjan === 0 ? 0 : $jan/$mgjan,
'feb' => $feb = self::grosstypemonth('2021-02-01', $row->id)->sum('gross'),
'feb_per' => $mgfeb === 0 ? 0 : $feb/$mgfeb,
'mar' => $mar = self::grosstypemonth('2021-03-01', $row->id)->sum('gross'),
'mar_per' => $mgmar === 0 ? 0 : $mar/$mgmar,
'apr' => $apr = self::grosstypemonth('2021-04-01', $row->id)->sum('gross'),
'apr_per' => $mgapr === 0 ? 0 : $apr/$mgapr,
'may' => $may = self::grosstypemonth('2021-05-01', $row->id)->sum('gross'),
'may_per' => $mgmay === 0 ? 0 : $may/$mgmay,
'jun' => $jun = self::grosstypemonth('2021-06-01', $row->id)->sum('gross'),
'jun_per' => $mgjun === 0 ? 0 : $jun/$mgjun,
'jul' => $jul = self::grosstypemonth('2021-07-01', $row->id)->sum('gross'),
'jul_per' => $mgjul === 0 ? 0 : $jul/$mgjul,
'aug' => $aug = self::grosstypemonth('2021-08-01', $row->id)->sum('gross'),
'aug_per' => $mgaug === 0 ? 0 : $aug/$mgaug,
'sep' => $sep = self::grosstypemonth('2021-09-01', $row->id)->sum('gross'),
'sep_per' => $mgsep === 0 ? 0 : $sep/$mgsep,
'oct' => $oct = self::grosstypemonth('2021-10-01', $row->id)->sum('gross'),
'oct_per' => $mgoct === 0 ? 0 : $oct/$mgoct,
'nov' => $nov = self::grosstypemonth('2021-11-01', $row->id)->sum('gross'),
'nov_per' => $mgnov === 0 ? 0 : $nov/$mgnov,
'dec' => $dec = self::grosstypemonth('2021-12-01', $row->id)->sum('gross'),
'dec_per' => $mgdec === 0 ? 0 : $dec/$mgdec,
'total' => $tot = self::grosstypeyear('2021-12-01', $row->id)->sum('gross'),
'total_per' => 0,
];
} // end foreach
return $reporter;
} // end function
public static function getCustomerMarginMonthly(): array
{
$reporter = [];
$team = CompanyType::whereReportVisible(true)->orderBy('sort_order')->get();
foreach($team as $row) {
$reporter[] = [
'lastName' => $row->title,
'jan' => $jan = self::grosstypemonth('2021-01-01', $row->id)->sum('profit'),
'feb' => $feb = self::grosstypemonth('2021-02-01', $row->id)->sum('profit'),
'mar' => $mar = self::grosstypemonth('2021-03-01', $row->id)->sum('profit'),
'apr' => $apr = self::grosstypemonth('2021-04-01', $row->id)->sum('profit'),
'may' => $may = self::grosstypemonth('2021-05-01', $row->id)->sum('profit'),
'jun' => $jun = self::grosstypemonth('2021-06-01', $row->id)->sum('profit'),
'jul' => $jul = self::grosstypemonth('2021-07-01', $row->id)->sum('profit'),
'aug' => $aug = self::grosstypemonth('2021-08-01', $row->id)->sum('profit'),
'sep' => $sep = self::grosstypemonth('2021-09-01', $row->id)->sum('profit'),
'oct' => $oct = self::grosstypemonth('2021-10-01', $row->id)->sum('profit'),
'nov' => $nov = self::grosstypemonth('2021-11-01', $row->id)->sum('profit'),
'dec' => $dec = self::grosstypemonth('2021-12-01', $row->id)->sum('profit'),
'total' => $tot = self::grosstypeyear('2021-12-01', $row->id)->sum('profit'),
];
} // end foreach
return $reporter;
} // end function
public static function getCustomerRegionMonthly(): array
{
$reporter = [];
$team = Region::orderBy('sort_order')->get();
foreach($team as $row) {
$mgjan = self::calcGrossByMonth('2021-01-01');
$mgfeb = self::calcGrossByMonth('2021-02-01');
$mgmar = self::calcGrossByMonth('2021-03-01');
$mgapr = self::calcGrossByMonth('2021-04-01');
$mgmay = self::calcGrossByMonth('2021-05-01');
$mgjun = self::calcGrossByMonth('2021-06-01');
$mgjul = self::calcGrossByMonth('2021-07-01');
$mgaug = self::calcGrossByMonth('2021-08-01');
$mgsep = self::calcGrossByMonth('2021-09-01');
$mgoct = self::calcGrossByMonth('2021-10-01');
$mgnov = self::calcGrossByMonth('2021-11-01');
$mgdec = self::calcGrossByMonth('2021-12-01');
$reporter[] = [
'lastName' => $row->title,
'jan' => $jan = self::grossregionmonth('2021-01-01', $row->id)->sum('gross'),
'jan_per' => $mgjan === 0 ? 0 : $jan/$mgjan,
'feb' => $feb = self::grossregionmonth('2021-02-01', $row->id)->sum('gross'),
'feb_per' => $mgfeb === 0 ? 0 : $feb/$mgfeb,
'mar' => $mar = self::grossregionmonth('2021-03-01', $row->id)->sum('gross'),
'mar_per' => $mgmar === 0 ? 0 : $mar/$mgmar,
'apr' => $apr = self::grossregionmonth('2021-04-01', $row->id)->sum('gross'),
'apr_per' => $mgapr === 0 ? 0 : $apr/$mgapr,
'may' => $may = self::grossregionmonth('2021-05-01', $row->id)->sum('gross'),
'may_per' => $mgmay === 0 ? 0 : $may/$mgmay,
'jun' => $jun = self::grossregionmonth('2021-06-01', $row->id)->sum('gross'),
'jun_per' => $mgjun === 0 ? 0 : $jun/$mgjun,
'jul' => $jul = self::grossregionmonth('2021-07-01', $row->id)->sum('gross'),
'jul_per' => $mgjul === 0 ? 0 : $jul/$mgjul,
'aug' => $aug = self::grossregionmonth('2021-08-01', $row->id)->sum('gross'),
'aug_per' => $mgaug === 0 ? 0 : $aug/$mgaug,
'sep' => $sep = self::grossregionmonth('2021-09-01', $row->id)->sum('gross'),
'sep_per' => $mgsep === 0 ? 0 : $sep/$mgsep,
'oct' => $oct = self::grossregionmonth('2021-10-01', $row->id)->sum('gross'),
'oct_per' => $mgoct === 0 ? 0 : $oct/$mgoct,
'nov' => $nov = self::grossregionmonth('2021-11-01', $row->id)->sum('gross'),
'nov_per' => $mgnov === 0 ? 0 : $nov/$mgnov,
'dec' => $dec = self::grossregionmonth('2021-12-01', $row->id)->sum('gross'),
'dec_per' => $mgdec === 0 ? 0 : $dec/$mgdec,
'total' => $tot = self::grossregionyear('2021-12-01', $row->id)->sum('gross'),
'total_per' => 0,
];
} // end foreach
return $reporter;
} // end function
public static function getRegionMarginMonthly(): array
{
$reporter = [];
$team = Region::orderBy('sort_order')->get();
foreach($team as $row) {
$reporter[] = [
'lastName' => $row->title,
'jan' => $jan = self::grossregionmonth('2021-01-01', $row->id)->sum('profit'),
'feb' => $feb = self::grossregionmonth('2021-02-01', $row->id)->sum('profit'),
'mar' => $mar = self::grossregionmonth('2021-03-01', $row->id)->sum('profit'),
'apr' => $apr = self::grossregionmonth('2021-04-01', $row->id)->sum('profit'),
'may' => $may = self::grossregionmonth('2021-05-01', $row->id)->sum('profit'),
'jun' => $jun = self::grossregionmonth('2021-06-01', $row->id)->sum('profit'),
'jul' => $jul = self::grossregionmonth('2021-07-01', $row->id)->sum('profit'),
'aug' => $aug = self::grossregionmonth('2021-08-01', $row->id)->sum('profit'),
'sep' => $sep = self::grossregionmonth('2021-09-01', $row->id)->sum('profit'),
'oct' => $oct = self::grossregionmonth('2021-10-01', $row->id)->sum('profit'),
'nov' => $nov = self::grossregionmonth('2021-11-01', $row->id)->sum('profit'),
'dec' => $dec = self::grossregionmonth('2021-12-01', $row->id)->sum('profit'),
'total' => $tot = self::grossregionyear('2021-12-01', $row->id)->sum('profit'),
];
} // end foreach
return $reporter;
} // end function
public static function getRepairCostsMonthly(): array
{
$reporter = [];
$team = ReportsSalesTeam::orderBy('sort_order')->get();
foreach($team as $row) {
$reporter[] = [
'lastName' => $row->title,
'jan' => self::grossmonth('2021-01-01')->seller($row->salesperson_code)->sum('cost'),
'feb' => self::grossmonth('2021-02-01')->seller($row->salesperson_code)->sum('cost'),
'mar' => self::grossmonth('2021-03-01')->seller($row->salesperson_code)->sum('cost'),
'apr' => self::grossmonth('2021-04-01')->seller($row->salesperson_code)->sum('cost'),
'may' => self::grossmonth('2021-05-01')->seller($row->salesperson_code)->sum('cost'),
'jun' => self::grossmonth('2021-06-01')->seller($row->salesperson_code)->sum('cost'),
'jul' => self::grossmonth('2021-07-01')->seller($row->salesperson_code)->sum('cost'),
'aug' => self::grossmonth('2021-08-01')->seller($row->salesperson_code)->sum('cost'),
'sep' => self::grossmonth('2021-09-01')->seller($row->salesperson_code)->sum('cost'),
'oct' => self::grossmonth('2021-10-01')->seller($row->salesperson_code)->sum('cost'),
'nov' => self::grossmonth('2021-11-01')->seller($row->salesperson_code)->sum('cost'),
'dec' => self::grossmonth('2021-12-01')->seller($row->salesperson_code)->sum('cost'),
'total' => self::grossyear('2021-12-01')->seller($row->salesperson_code)->sum('cost'),
];
} // end foreach
return $reporter;
} // end function
public static function getSalesCreditsMonthly(): array
{
$reporter = [];
$team = ReportsSalesTeam::orderBy('sort_order')->get();
foreach($team as $row) {
$reporter[] = [
'lastName' => $row->title,
'jan' => self::returnsmonth('2021-01-01')->seller($row->salesperson_code)->sum('gross'),
'feb' => self::returnsmonth('2021-02-01')->seller($row->salesperson_code)->sum('gross'),
'mar' => self::returnsmonth('2021-03-01')->seller($row->salesperson_code)->sum('gross'),
'apr' => self::returnsmonth('2021-04-01')->seller($row->salesperson_code)->sum('gross'),
'may' => self::returnsmonth('2021-05-01')->seller($row->salesperson_code)->sum('gross'),
'jun' => self::returnsmonth('2021-06-01')->seller($row->salesperson_code)->sum('gross'),
'jul' => self::returnsmonth('2021-07-01')->seller($row->salesperson_code)->sum('gross'),
'aug' => self::returnsmonth('2021-08-01')->seller($row->salesperson_code)->sum('gross'),
'sep' => self::returnsmonth('2021-09-01')->seller($row->salesperson_code)->sum('gross'),
'oct' => self::returnsmonth('2021-10-01')->seller($row->salesperson_code)->sum('gross'),
'nov' => self::returnsmonth('2021-11-01')->seller($row->salesperson_code)->sum('gross'),
'dec' => self::returnsmonth('2021-12-01')->seller($row->salesperson_code)->sum('gross'),
'total' => self::returnsyear('2021-12-01')->seller($row->salesperson_code)->sum('gross'),
];
} // end foreach
return $reporter;
} // end function
public static function getSalesProfitMonthly(): array
{
$reporter = [];
$team = ReportsSalesTeam::orderBy('sort_order')->get();
foreach($team as $row) {
$reporter[] = [
'lastName' => $row->title,
'jan' => self::grossmonth('2021-01-01')->seller($row->salesperson_code)->sum('profit'),
'feb' => self::grossmonth('2021-02-01')->seller($row->salesperson_code)->sum('profit'),
'mar' => self::grossmonth('2021-03-01')->seller($row->salesperson_code)->sum('profit'),
'apr' => self::grossmonth('2021-04-01')->seller($row->salesperson_code)->sum('profit'),
'may' => self::grossmonth('2021-05-01')->seller($row->salesperson_code)->sum('profit'),
'jun' => self::grossmonth('2021-06-01')->seller($row->salesperson_code)->sum('profit'),
'jul' => self::grossmonth('2021-07-01')->seller($row->salesperson_code)->sum('profit'),
'aug' => self::grossmonth('2021-08-01')->seller($row->salesperson_code)->sum('profit'),
'sep' => self::grossmonth('2021-09-01')->seller($row->salesperson_code)->sum('profit'),
'oct' => self::grossmonth('2021-10-01')->seller($row->salesperson_code)->sum('profit'),
'nov' => self::grossmonth('2021-11-01')->seller($row->salesperson_code)->sum('profit'),
'dec' => self::grossmonth('2021-12-01')->seller($row->salesperson_code)->sum('profit'),
'total' => self::grossyear('2021-12-01')->seller($row->salesperson_code)->sum('profit'),
];
} // end foreach
return $reporter;
} // end function
public static function getSalesRevenueMonthly(): array
{
$reporter = [];
$team = ReportsSalesTeam::orderBy('sort_order')->get();
foreach($team as $row) {
$reporter[] = [
'lastName' => $row->title,
'jan' => self::grossmonth('2021-01-01')->seller($row->salesperson_code)->sum('gross'),
'feb' => self::grossmonth('2021-02-01')->seller($row->salesperson_code)->sum('gross'),
'mar' => self::grossmonth('2021-03-01')->seller($row->salesperson_code)->sum('gross'),
'apr' => self::grossmonth('2021-04-01')->seller($row->salesperson_code)->sum('gross'),
'may' => self::grossmonth('2021-05-01')->seller($row->salesperson_code)->sum('gross'),
'jun' => self::grossmonth('2021-06-01')->seller($row->salesperson_code)->sum('gross'),
'jul' => self::grossmonth('2021-07-01')->seller($row->salesperson_code)->sum('gross'),
'aug' => self::grossmonth('2021-08-01')->seller($row->salesperson_code)->sum('gross'),
'sep' => self::grossmonth('2021-09-01')->seller($row->salesperson_code)->sum('gross'),
'oct' => self::grossmonth('2021-10-01')->seller($row->salesperson_code)->sum('gross'),
'nov' => self::grossmonth('2021-11-01')->seller($row->salesperson_code)->sum('gross'),
'dec' => self::grossmonth('2021-12-01')->seller($row->salesperson_code)->sum('gross'),
'total' => self::grossyear('2021-12-01')->seller($row->salesperson_code)->sum('gross'),
];
} // end foreach
return $reporter;
} // end function
public static function getCategoryDistributionWeekly(): array
{
return [
[
'lastName' => 'CAT 1',
'total' => 0
],
[
'lastName' => 'CAT 2',
'total' => 0
],
[
'lastName' => 'CAT 3',
'total' => 0
],
[
'lastName' => 'Other',
'total' => 0
],
];
} // end function
public static function getCategoryMarginWeekly(): array
{
return [
[
'lastName' => 'CAT 1',
'total' => 0
],
[
'lastName' => 'CAT 2',
'total' => 0
],
[
'lastName' => 'CAT 3',
'total' => 0
],
[
'lastName' => 'Other',
'total' => 0
],
];
} // end function
public static function getCustomerDistributionWeekly(): array
{
return [
[
'lastName' => 'MRO',
'total' => 0
],
[
'lastName' => 'Airlines',
'total' => 0
],
[
'lastName' => 'OEM',
'total' => 0
],
[
'lastName' => 'Others',
'total' => 0
],
];
} // end function
public static function getCustomerMarginWeekly(): array
{
return [
[
'lastName' => 'MRO',
'total' => 0
],
[
'lastName' => 'Airlines',
'total' => 0
],
[
'lastName' => 'OEM',
'total' => 0
],
[
'lastName' => 'Others',
'total' => 0
],
];
} // end function
public static function getCustomerRegionWeekly(): array
{
return [
[
'lastName' => 'US',
'total' => 0
],
[
'lastName' => 'EMEA',
'total' => 0
],
[
'lastName' => 'ASIA',
'total' => 0
],
];
} // end function
public static function getRegionMarginWeekly(): array
{
return [
[
'lastName' => 'US',
'total' => 0
],
[
'lastName' => 'EMEA',
'total' => 0
],
[
'lastName' => 'ASIA',
'total' => 0
],
];
} // end function
public static function getRepairCostsWeekly(): array
{
return [
[
'lastName' => '777-300ER',
'total' => 0
],
[
'lastName' => 'Duncan',
'total' => 0
],
[
'lastName' => 'Henderson',
'total' => 0
],
[
'lastName' => 'Hudson',
'total' => 0
],
[
'lastName' => 'Muller',
'total' => 0
],
[
'lastName' => 'Other',
'total' => 0
],
[
'lastName' => 'Qin',
'total' => 0
],
[
'lastName' => 'Song',
'total' => 0
],
[
'lastName' => 'Walker',
'total' => 0
],
];
} // end function
public static function getSalesCreditsWeekly(): array
{
return [
[
'lastName' => '777-300ER',
'total' => 0
],
[
'lastName' => 'Duncan',
'total' => 0
],
[
'lastName' => 'Henderson',
'total' => 0
],
[
'lastName' => 'Hudson',
'total' => 0
],
[
'lastName' => 'Muller',
'total' => 0
],
[
'lastName' => 'Other',
'total' => 0
],
[
'lastName' => 'Qin',
'total' => 0
],
[
'lastName' => 'Song',
'total' => 0
],
[
'lastName' => 'Walker',
'total' => 0
],
];
} // end function
public static function getSalesProfitWeekly(): array
{
return [
[
'lastName' => '777-300ER',
'total' => 0
],
[
'lastName' => 'Duncan',
'total' => 0
],
[
'lastName' => 'Henderson',
'total' => 0
],
[
'lastName' => 'Hudson',
'total' => 0
],
[
'lastName' => 'Muller',
'total' => 0
],
[
'lastName' => 'Other',
'total' => 0
],
[
'lastName' => 'Qin',
'total' => 0
],
[
'lastName' => 'Song',
'total' => 0
],
[
'lastName' => 'Walker',
'total' => 0
],
];
} // end function
public static function getSalesRevenueWeekly(): array
{
return [
[
'lastName' => '777-300ER',
'total' => 0
],
[
'lastName' => 'Duncan',
'total' => 0
],
[
'lastName' => 'Henderson',
'total' => 0
],
[
'lastName' => 'Hudson',
'total' => 0
],
[
'lastName' => 'Muller',
'total' => 0
],
[
'lastName' => 'Other',
'total' => 0
],
[
'lastName' => 'Qin',
'total' => 0
],
[
'lastName' => 'Song',
'total' => 0
],
[
'lastName' => 'Walker',
'total' => 0
],
];
} // end function
public static function lotGross($lotCode): int
{
return static::byLot($lotCode)->pluck('gross')->sum();
} // end function
public static function lotNet($lotCode): int
{
return static::byLot($lotCode)->pluck('net')->sum();
} // end function
public static function sumNet($invoices, $user = null)
{
if (!$user) {
return collect($invoices)->pluck('net')->sum();
} else {
return collect($invoices)->filter(function ($i) use ($user) {
return $i->salesperson_code == $user;
})->pluck('net')->sum();
} // end if
} // end function
public static function sumGross($invoices, $user = null)
{
if (!$user) {
return collect($invoices)->pluck('gross')->sum();
} else {
return collect($invoices)->filter(function ($i) use ($user) {
return $i->salesperson_code == $user;
})->pluck('gross')->sum();
} // end if
} // end function
public static function sumProfit($invoices, $user = null)
{
if (!$user) {
return collect($invoices)->pluck('profit')->sum();
} else {
return collect($invoices)->filter(function ($i) use ($user) {
return $i->salesperson_code == $user;
})->pluck('profit')->sum();
} // end if
} // end function
public static function reportAirlineGross($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereCustomerType('Airline')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('gross');
} // end function
public static function reportAirlineProfit($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereCustomerType('Airline')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('profit');
} // end function
public static function reportAmerGross($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereRegion('AMER')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('gross');
} // end function
public static function reportAmerProfit($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereRegion('AMER')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('profit');
} // end function
public static function reportArgGross($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereIn('consignment_code', ['AP-BLJ','CS-TOE', '2-AERC', 'OE-IHA'])
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('gross');
} // end function
public static function reportArgProfit($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereIn('consignment_code', ['AP-BLJ','CS-TOE', '2-AERC', 'OE-IHA'])
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('profit');
} // end function
public static function reportAsiaGross($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereRegion('ASIA')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('gross');
} // end function
public static function reportAsiaProfit($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereRegion('ASIA')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('profit');
} // end function
public static function reportB77Gross($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereConsignmentCode('N351BU')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('gross');
} // end function
public static function reportB77Profits($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereConsignmentCode('N351BU')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('profit');
} // end function
public static function reportB77Repair($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereConsignmentCode('N351BU')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('cost');
} // end function
public static function reportB77Returns($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('M')
->whereConsignmentCode('N351BU')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('cost');
} // end function
public static function reportCatGross($mo, $cat=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereCat($cat)
->whereYear('post_date', $yr)
->whereMonth('post_date', $mo)
->sum('gross');
} // end function
public static function reportCat1Gross($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereCustomerType('Cat1')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('gross');
} // end function
public static function reportCat1Profit($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereCat('1')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('profit');
} // end function
public static function reportCat2Gross($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereCat('2')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('gross');
} // end function
public static function reportCat2Profit($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereCat('2')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('profit');
} // end function
public static function reportCat3Gross($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereCat('3')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('gross');
} // end function
public static function reportCat3Profit($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereCat('3')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('profit');
} // end function
public static function reportCatOtherGross($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereNotIn('cat', ['1', '2', '3'])
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('gross');
} // end function
public static function reportCatOtherProfit($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereNotIn('cat', ['1', '2', '3'])
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('profit');
} // end function
public static function reportCreditReturns($sp=null, $mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('M')
->whereYear('post_date', $yr)
->when($sp, function($query) use($sp) {
$query->whereSalespersonCode($sp);
})
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('gross');
} // end function
public static function reportEmeaGross($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereRegion('EMEA')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('gross');
} // end function
public static function reportEmeaProfit($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereRegion('EMEA')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('profit');
} // end function
public static function reportGrossSales($sp=null, $mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereYear('post_date', $yr)
->when($sp, function($query) use($sp) {
$query->whereSalespersonCode($sp);
})
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('gross');
} // end function
public static function reportOverallGross($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('gross');
} // end function
public static function reportMroGross($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereCustomerType('MRO')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('gross');
} // end function
public static function reportMroProfit($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereCustomerType('MRO')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('profit');
} // end function
public static function reportNoTypeGross($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereNotIn('customer_type', ['MRO', 'Airline', 'OEM'])
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('gross');
} // end function
public static function reportNoTypeProfit($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereNotIn('customer_type', ['MRO', 'Airline', 'OEM'])
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('profit');
} // end function
public static function reportOemGross($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereCustomerType('OEM')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('gross');
} // end function
public static function reportOemProfit($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereCustomerType('OEM')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('profit');
} // end function
public static function reportOverallNet($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('net');
} // end function
public static function reportOverallProfit($mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereYear('post_date', $yr)
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('profit');
} // end function
public static function reportRepairCosts($sp=null, $mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereYear('post_date', $yr)
->when($sp, function($query) use($sp) {
$query->whereSalespersonCode($sp);
})
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('cost');
} // end function
public static function reportSalesProfits($sp=null, $mo=null, $yr=null): float
{
if(!$yr) { $yr = today()->format('Y'); } // end if
return self::whereRouteCode('S')
->whereYear('post_date', $yr)
->when($sp, function($query) use($sp) {
$query->whereSalespersonCode($sp);
})
->when($mo, function($query) use($mo) {
$query->whereMonth('post_date', $mo);
})
->sum('profit');
} // end function
/* STATIC METHODS */
public function processInvoiceProfit(): void
{
if(!$this->processed) {
if($this->post_date->gte(Carbon::parse(2021-01-01))) {
app(Pipeline::class)
->send($this)
->through([
NoConsignmentAbort::class,
FixedRateProfit::class,
BandedRatesProfit::class
])
->thenReturn();
} else {
app(Pipeline::class)
->send($this)
->through([
NoConsignmentLegacyAbort::class,
LegacyFixedRateProfit::class,
LegacyBandedRatesProfit::class
])
->thenReturn();
} // end if
} // end if
} // end function
public function setAdditionalFields(): void
{
//cat
$cmp = Company::whereCompanyCode($this->company_code)->first();
if(isset($cmp)) {
$this->update([
'region' => $cmp->region,
'customer_type' => $cmp->company_type
]);
} // end if
$pm = PartMaster::wherePartNumber($this->pn)->first();
if(isset($pm)) {
$this->update([ 'cat' => $pm->cat ]);
} // end if
} // end function
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment