g_sto/plugins/romanah/gokbakja/components/Production.php

180 lines
6.2 KiB
PHP
Raw Normal View History

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;
class Production extends ComponentBase
{
public function componentDetails()
{
return [
'name' => 'Production',
'description' => 'productions settings'
];
}
2023-09-23 20:10:26 +00:00
public function onCreateMachineProduction()
{
$user = \Auth::user();
$data = post();
$createProductionMachine = new ProductionMachineModel();
$createProductionMachine->type_id = $data["type_id"];
$createProductionMachine->size_id = $data["size_id"];
$createProductionMachine->machine_id = $data["machine_id"];
$createProductionMachine->produced_weight = $data["produced_weight"];
$createProductionMachine->note = $data["note"];
$createProductionMachine->user_id = $user->id;
$createProductionMachine->save();
$productionMachine = ProductionMachineModel::where('id', $createProductionMachine->id)->with(['machine.employee', 'machine.mechanic', 'machine.building'])->first();
// dd($productionMachine->machine->name);
2023-09-30 21:55:49 +00:00
if ($productionMachine) {
2023-09-23 20:10:26 +00:00
$updateResult = ProductionMachineModel::where('id', $productionMachine->id)
2023-09-30 21:55:49 +00:00
->update(array(
'employee_id' => $productionMachine->machine->employee->id,
'mechanic_id' => $productionMachine->machine->mechanic->id,
'employee_name' => $productionMachine->machine->employee->name,
'mechanic_name' => $productionMachine->machine->mechanic->name,
'building_name' => $productionMachine->machine->building->name,
));
2023-09-23 20:10:26 +00:00
}
if ($createProductionMachine && $updateResult) {
Flash::success("Hasabat Ustunlikli Goşuldy");
return Redirect::refresh();
}
2023-09-30 21:55:49 +00:00
}
function onCalculateAvg(){
$currentDate = Carbon::now()->timezone('UTC +05:00');
$currentDateFormat = $currentDate->format('Y-m-d');
$data = post();
$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');
$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');
dd($sumPercentage / $sumPercentageCount);
dd(($sumPercentage + (int) $data["field_value"]) / ($sumPercentageCount + 1));
$calcAvgPerc = ($sumPercentage + (int) $data["field_value"]) / ($sumPercentageCount + 1);
// $calcAvgAmount = ($sumPercentage + (int) $data["field_value"]) / ($sumPercentageCount + 1);
$allData = array(
"avg_percentage" => $calcAvgPerc,
"avg_amount" => '000'
);
return $allData;
// dd($currentDateFormat);
2023-09-23 20:10:26 +00:00
}
2023-09-17 19:12:00 +00:00
public function onCreateProduction()
{
$user = \Auth::user();
2023-09-30 21:55:49 +00:00
$currentDate = Carbon::now()->timezone('UTC +05:00');
// $this["currentMonth"] = $currentDate->format('m');
2023-09-17 19:12:00 +00:00
$data = post();
2023-09-18 20:22:50 +00:00
$createProduction = new ProductionModel();
$createProduction->all_amount = $data["all_amount"];
$createProduction->note = $data["note"];
$createProduction->user_id = $user->id;
2023-09-30 21:55:49 +00:00
$createProduction->date = $currentDate->format('Y-m-d');
$createProduction->time = $currentDate->format('H:i:s');
$createProduction->excruiter_id = 1;
2023-09-18 20:22:50 +00:00
$createProduction->save();
$products = ProductModel::where("report", "simple")->get();
for ($x = 0; $x < count($products); $x++) {
2023-09-30 21:55:49 +00:00
$fieldName = "product_" . $products[$x]->code;
2023-09-18 20:22:50 +00:00
$createPivotProduction = new PivotProductionModel();
$createPivotProduction->production_id = $createProduction->id;
$createPivotProduction->product_id = $products[$x]->id;
$createPivotProduction->amount_percentage = $data[$fieldName];
$createPivotProduction->product_name = $products[$x]->name;
$createPivotProduction->product_code = $products[$x]->code;
$createPivotProduction->amount = ($data[$fieldName] / 100) * ($createProduction->all_amount);
$createPivotProduction->save();
}
if ($createProduction && $createPivotProduction) {
Flash::success("Hasabat Ustunlikli Goşuldy");
return Redirect::refresh();
}
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();
// }
// }
}