2023-09-17 19:12:00 +00:00
< ? php
namespace Romanah\Gokbakja\Components ;
use Cms\Classes\ComponentBase ;
2023-09-18 20:22:50 +00:00
use Romanah\Gokbakja\Models\Product as ProductModel ;
2023-09-17 19:12:00 +00:00
use Romanah\Gokbakja\Models\Production as ProductionModel ;
2023-09-23 20:10:26 +00:00
use Romanah\Gokbakja\Models\ProductionMachine as ProductionMachineModel ;
2023-09-17 19:12:00 +00:00
use Romanah\Gokbakja\Models\PivotProduction as PivotProductionModel ;
use Redirect ;
2023-09-30 21:55:49 +00:00
use Carbon\Carbon ;
2023-09-17 19:12:00 +00:00
use Flash ;
2023-10-02 22:15:33 +00:00
use DB ;
2023-09-17 19:12:00 +00:00
class Production extends ComponentBase
{
public function componentDetails ()
{
return [
'name' => 'Production' ,
'description' => 'productions settings'
];
}
2023-10-02 22:15:33 +00:00
public function onCalculateReportDaily ()
{
$currentDate = Carbon :: now () -> timezone ( 'UTC +05:00' );
$currentDateFormat = $currentDate -> format ( 'Y-m-d' );
$data = post ();
$excruiter = $this -> param ( " excruiter " );
$currentDateLast = ProductionModel :: whereDate ( 'created_at' , date ( $currentDateFormat )) -> where ( 'excruiter_id' , $excruiter ) -> orderBy ( 'id' , 'DESC' ) -> first ();
$currentDateFirst = ProductionModel :: whereDate ( 'created_at' , date ( $currentDateFormat )) -> where ( 'excruiter_id' , $excruiter ) -> orderBy ( 'id' , 'ASC' ) -> first ();
$diffMinutes = Carbon :: parse ( $currentDateLast -> time ) -> diffInMinutes ( $currentDateFirst -> time );
$currentDateProductionAll = ProductionModel :: whereDate ( 'created_at' , date ( $currentDateFormat )) -> where ( 'excruiter_id' , $excruiter ) -> avg ( 'all_amount' );
// $testq = PivotProductionModel::whereDate('created_at', date($currentDateFormat))
// ->where('excruiter_id', $excruiter)
// ->select('id', 'product_name', 'excruiter_id',DB::raw('round(AVG(amount),0) as average'), DB::raw('round(AVG(amount_percentage),0) as average_percentage'))
// ->groupBy('product_id')
// ->get();
$pivotFirsts = PivotProductionModel :: where ( 'production_id' , $currentDateFirst -> id ) -> get ();
$pivotLasts = PivotProductionModel :: where ( 'production_id' , $currentDateLast -> id ) -> get ();
$allData = array (
" all " => array (
" hours " => floor ( $diffMinutes / 60 ) . ' sagat ' . ( $diffMinutes - floor ( $diffMinutes / 60 ) * 60 ) . ' minut' ,
" avg_all_amount " => number_format ( $currentDateProductionAll , 2 ),
)
);
2023-10-08 23:04:21 +00:00
$producedAll = $currentDateLast -> all_amount - $currentDateFirst -> all_amount ;
2023-10-02 22:15:33 +00:00
$html_data = '<div class="row">' ;
$html_data .= '<h4 style="margin-top: 20px;margin-bottom: 20px;">IŞLENEN ÇIG MAL</h4>' ;
$html_data .= ' < div class = " col-lg " >
< a class = " card bg-success text-white-50 " >
< div class = " card-body " >
< h5 class = " mb-4 text-white " >< i
class = " mdi mdi-bullseye-arrow me-3 " ></ i > '.$allData["all"]["hours"].' </ h5 >
< hr >
< p class = " card-text " style = " font-size: 15px;color: white; " > JEMI IŞLENEN : '. $producedAll.' kg </ p >
</ div >
</ a >
</ div > ' ;
2023-10-08 23:04:21 +00:00
for ( $x = 0 ; $x < count ( $pivotLasts ); $x ++ ) {
$diff = $pivotLasts [ $x ] -> amount - $pivotFirsts [ $x ] -> amount ;
$diffPerc = ( $diff * 100 ) / $producedAll ;
2023-10-02 22:15:33 +00:00
$html_data .= ' < div class = " col-lg " >
< a class = " card bg-info text-white-50 " >
< div class = " card-body " >
2023-10-08 23:04:21 +00:00
< h5 class = " mb-4 text-white " >< i class = " mdi mdi-bullseye-arrow me-3 " ></ i > '.($diff).' kg </ h5 >
2023-10-02 22:15:33 +00:00
< hr >
2023-10-08 23:04:21 +00:00
< p class = " card-text " style = " font-size: 15px;color: white; " > '.$pivotLasts[$x]->product_name.' : '.($diffPerc).' %</ p >
2023-10-02 22:15:33 +00:00
</ div >
</ a >
</ div > ' ;
}
$html_data .= '</div>' ;
2023-10-08 23:04:21 +00:00
2023-10-02 22:15:33 +00:00
return [
'#calculation_of_day' => $html_data
];
}
2023-09-30 21:55:49 +00:00
2023-10-02 22:15:33 +00:00
function onCalculateAvg ()
{
2023-09-30 21:55:49 +00:00
$currentDate = Carbon :: now () -> timezone ( 'UTC +05:00' );
$currentDateFormat = $currentDate -> format ( 'Y-m-d' );
$data = post ();
2023-10-02 22:15:33 +00:00
$product = ProductModel :: where ( 'id' , $data [ " prod_id " ]) -> first ();
2023-09-30 21:55:49 +00:00
$sumPercentage = PivotProductionModel :: where ( 'product_id' , $data [ " prod_id " ]) -> whereDate ( 'created_at' , date ( $currentDateFormat )) -> sum ( 'amount_percentage' );
$sumPercentageCount = PivotProductionModel :: where ( 'product_id' , $data [ " prod_id " ]) -> whereDate ( 'created_at' , date ( $currentDateFormat )) -> count ( 'amount_percentage' );
2023-10-02 22:15:33 +00:00
2023-09-30 21:55:49 +00:00
$sumAmount = PivotProductionModel :: where ( 'product_id' , $data [ " prod_id " ]) -> whereDate ( 'created_at' , date ( $currentDateFormat )) -> sum ( 'amount' );
2023-09-23 20:10:26 +00:00
2023-09-30 21:55:49 +00:00
$sumAmountcount = PivotProductionModel :: where ( 'product_id' , $data [ " prod_id " ]) -> whereDate ( 'created_at' , date ( $currentDateFormat )) -> count ( 'amount' );
2023-10-02 22:15:33 +00:00
// dd($sumPercentage / $sumPercentageCount);
// dd(($sumPercentage + (int) $data["field_value"]) / ($sumPercentageCount + 1));
if ( $sumPercentage != 0 ) {
2023-09-30 21:55:49 +00:00
2023-10-02 22:15:33 +00:00
$calcAvgPerc = ( $sumPercentage + ( float ) $data [ " field_value " ]) / ( $sumPercentageCount + 1 );
// $convertedToKgAvg = ((int) $data["field_value"] / 100) * ((int) $data["all_amount"]);
$convertedToKgSum = (( float ) $data [ " field_value " ] / 100 ) * (( float ) $data [ " all_amount " ]);
$convertToCount = $sumAmountcount + 1 ;
$calculateAvgAmount = ( $convertedToKgSum + $sumAmount ) / $convertToCount ;
$allData = array (
" product_name " => $product -> name ,
" avg_percentage " => number_format ( $calcAvgPerc , 2 ),
" avg_amount " => number_format ( $calculateAvgAmount , 2 )
);
} else {
$allData = array (
" product_name " => $product -> name ,
" avg_percentage " => number_format ( 0 , 2 ),
" avg_amount " => number_format ( 0 , 2 )
);
}
2023-09-30 21:55:49 +00:00
// $calcAvgAmount = ($sumPercentage + (int) $data["field_value"]) / ($sumPercentageCount + 1);
2023-10-02 22:15:33 +00:00
2023-09-30 21:55:49 +00:00
return $allData ;
// dd($currentDateFormat);
2023-09-23 20:10:26 +00:00
}
2023-09-17 19:12:00 +00:00
2023-10-02 22:15:33 +00:00
public function onUpdatePivotProduction ()
2023-09-17 19:12:00 +00:00
{
$user = \Auth :: user ();
2023-09-30 21:55:49 +00:00
$currentDate = Carbon :: now () -> timezone ( 'UTC +05:00' );
// $this["currentMonth"] = $currentDate->format('m');
2023-10-02 22:15:33 +00:00
$data = post ();
2023-09-30 21:55:49 +00:00
2023-10-02 22:15:33 +00:00
$updatePivotProduction = PivotProductionModel :: where ( " id " , $data [ " product_id " ]) -> first ();
2023-09-30 21:55:49 +00:00
2023-10-02 22:15:33 +00:00
$production = ProductionModel :: where ( " id " , $updatePivotProduction -> production_id ) -> first ();
$fieldName = " product_ " . $data [ " product_id " ];
$updatePivotProduction -> amount_percentage = ( float ) $data [ $fieldName ] ? ? 0 ;
$updatePivotProduction -> amount = ((( float ) $data [ $fieldName ] ? ? 0 ) / 100 ) * (( float ) $production -> all_amount );
$updatePivotProduction -> save ();
2023-09-17 19:12:00 +00:00
2023-10-02 22:15:33 +00:00
if ( $updatePivotProduction ) {
Flash :: success ( " Hasabat Ustunlikli Üýtgedildi " );
return Redirect :: refresh ();
2023-09-18 20:22:50 +00:00
}
2023-10-02 22:15:33 +00:00
}
public function onCreateProduction ()
{
$user = \Auth :: user ();
$currentDate = Carbon :: now () -> timezone ( 'UTC +05:00' );
// $this["currentMonth"] = $currentDate->format('m');
$data = post ();
$excruiter = $this -> param ( " excruiter " );
if ( $excruiter == 0 ) {
Flash :: error ( " Ekskruiter Saýla! " );
2023-09-18 20:22:50 +00:00
return Redirect :: refresh ();
2023-10-02 22:15:33 +00:00
} else {
$createProduction = new ProductionModel ();
$createProduction -> all_amount = $data [ " all_amount " ];
$createProduction -> note = $data [ " note " ];
$createProduction -> user_id = $user -> id ;
$createProduction -> date = $currentDate -> format ( 'Y-m-d' );
$createProduction -> time = $currentDate -> format ( 'H:i:s' );
$createProduction -> excruiter_id = $excruiter ;
$createProduction -> shift_id = $data [ " shift_id " ];
$createProduction -> save ();
$products = ProductModel :: where ( " report " , " simple " ) -> get ();
for ( $x = 0 ; $x < count ( $products ); $x ++ ) {
$fieldName = " product_ " . $products [ $x ] -> code ;
$createPivotProduction = new PivotProductionModel ();
$createPivotProduction -> production_id = $createProduction -> id ;
$createPivotProduction -> product_id = $products [ $x ] -> id ;
$createPivotProduction -> amount_percentage = ( float ) $data [ $fieldName ] ? ? 0 ;
$createPivotProduction -> product_name = $products [ $x ] -> name ;
$createPivotProduction -> product_code = $products [ $x ] -> code ;
$createProduction -> excruiter_id = $excruiter ;
$createPivotProduction -> amount = ( float )((( float ) $data [ $fieldName ] ? ? 0 ) / 100 ) * (( float ) $createProduction -> all_amount );
$createPivotProduction -> date = $currentDate -> format ( 'Y-m-d' );
$createPivotProduction -> time = $currentDate -> format ( 'H:i:s' );
$createPivotProduction -> save ();
}
if ( $createProduction && $createPivotProduction ) {
Flash :: success ( " Hasabat Ustunlikli Goşuldy " );
return Redirect :: refresh ();
}
2023-09-18 20:22:50 +00:00
}
2023-09-17 19:12:00 +00:00
}
public function onUpdateProduction ()
{
$user = \Auth :: user ();
$data = post ();
$updateResult = ActionModel :: where ( 'id' , $data [ " id " ])
-> update ( array (
'product_id' => $data [ " product_id " ],
'amount' => $data [ " quantity " ],
'stock_id' => $data [ " stock_id " ],
'note' => $data [ " note " ],
'transport_no' => $data [ " transport_no " ],
'driver' => $data [ " driver " ],
'user_id' => $user -> id ,
));
// $createResult = DollorModel::create([]);
// dd($data);
if ( $updateResult ) {
Flash :: success ( " Action Ustunlikli Uytgedildi " );
return Redirect :: refresh ();
}
}
public function onGetProduction ()
{
$id = post ( 'id' );
return ActionModel :: where ( 'id' , $id ) -> first ();
}
// public function onDeleteProduction()
// {
// $id = post('id');
// $item = ActionModel::Find($id);
// $item->delete();
// if ($item) {
// Flash::success("Action Ustunlikli Pozuldy");
// return Redirect::refresh();
// }
// }
}