Magento API duplicate SKU's - magento

Using the Magento API to import ~60.000 products I manage to import duplicate SKU's in Magento, normally this throws an 'Duplicate SKU' exception but somehow it just add's double products.
Anyone knows what's going wrong (some index caching or something?)?
This is the (relevant) part of the code of the import script:
<?php
/**
* #param $data
* #param bool $retry
* #return bool
*/
public function updateProduct($data, $retry = true) {
//Calculate product hash
$hash = md5(serialize($data));
try {
$manageStock = 0;
if($data->qty <= 0) {
$manageStock = 1;
}
$result = $this->client->catalogProductUpdate($this->session, $data->prefixed_sku, array(
'categories' => $data->categories,
'status' => empty($data->status) ? '1' : (int) $data->status,
'cost' => $data->cost,
'price' => $data->price,
'tax_class_id' => '2',
'visibility' => '4',
'stock_data' => array(
'qty' => $data->qty,
'is_in_stock' => $data->is_in_stock,
'manage_stock' => $manageStock
),
'additional_attributes' => array('cost' => $data->cost)
), null, 'sku');
}
catch (Exception $e) {
$result = $this->exceptionLogAndRetry($e, 'updateProduct', $retry, $data);
}
return $result;
}
/**
* #param $data
* #param bool $retry
* #return bool
*/
public function addProduct($data, $retry = true) {
//Calculate product hash
$hash = md5(serialize($data));
if(!$this->attributeSet) {
$attributeSets = $this->client->catalogProductAttributeSetList($this->session);
foreach($attributeSets as $attributeSet) {
if(strtolower($attributeSet->name) == 'default') {
$this->attributeSet = $attributeSet;
}
}
if(!$this->attributeSet) {
$this->attributeSet = current($attributeSets);
}
}
try {
$stock = array(
'qty' => $data->qty,
'is_in_stock' => $data->is_in_stock,
'use_config_manage_stock' => $data->use_config_manage_stock
);
if($data->qty <= 0) {
$stock['manage_stock'] = 1;
}
$result = $this->client->catalogProductCreate($this->session, 'simple', $this->attributeSet->set_id, $data->prefixed_sku, array(
'categories' => $data->categories,
'websites' => array($data->website_id),
'name' => $data->name,
'description' => $data->description,
'short_description' => $data->short_description,
'weight' => $data->weight,
'cost' => $data->cost,
'status' => empty($data->status) ? '1' : (int) $data->status,
'url_key' => $data->url_key,
'url_path' => $data->url_path,
'visibility' => '4',
'price' => $data->price,
'tax_class_id' => '2',
'meta_title' => $data->meta_title,
'meta_keyword' => $data->meta_keyword,
'meta_description' => $data->meta_description,
'stock_data' => $stock,
'additional_attributes' => array('cost' => $data->cost)
));
}
catch (Exception $e) {
$result = $this->exceptionLogAndRetry($e, 'addProduct', $retry, $data);
}
for ($i = 1; $i <= 5; $i++)
{
$cur = $i;
if ($cur == 1)
{
$cur = '';
}
if (!empty($data->{"product_image$cur"}) && file_exists($data->{"product_image$cur"}) && is_numeric($result))
{
$file = array(
'content' => base64_encode(#file_get_contents($data->{"product_image$cur"})),
'mime' => $data->{'product_mime'.$cur}
);
try {
$imgRes = $this->client->catalogProductAttributeMediaCreate(
$this->session,
$result,
array('file' => $file, 'label' => '', 'position' => '100', 'types' => array('thumbnail', 'small_image', 'image'), 'exclude' => 0)
);
}
catch (Exception $e) {
//Do nothing
}
}
}
$return = $result;
return $return;
}

Related

where and whereIn suddenly stops working in laravel api

In my laravel api am using eloquent query with where condition like this. it was woriking fine before not it is not returning data . when i remove where and whereIn from query then it works fine.In tinker it is fetching data with where and whereIn .
Here is the query
WorkingOrders::whereIn('status',[8,9])->where('gang_boss',auth()->user()->id)->orderBy('id','desc')->get();
whole controller is here
<?php
namespace App\Http\Controllers\GeneralApi;
use App\Events\ConfirmOrder;
use App\Http\Controllers\Controller;
use App\Models\Admin\Gang;
use App\Models\Admin\RightHandMan;
use App\Models\Admin\Staff;
use App\Models\Admin\StaffPivot;
use App\Models\Admin\Worker;
use App\Models\Admin\WorkerArrival;
use App\Models\Admin\WorkSheet;
use App\Models\WorkingOrders;
use App\Notifications\CustomNotification;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class WorkOrderController extends Controller
{
public function workOrdersCompletedCancelled($user_id)
{
$role_id = Auth::guard('staff_api')->user()->roles()->first()->id;
$work_orders = WorkingOrders::when($role_id == 2, function ($q) use ($user_id) {
return $q->where('ranch', $user_id);
})->when($role_id == 4, function ($q) use ($user_id) {
return $q->where('right_hand_man_id', $user_id);
})->when($role_id == 8, function ($q) use ($user_id) {
return $q->where('supervisor', $user_id);
})->when($role_id == 3, function ($q) use ($user_id) {
$gang_ids = DB::table('worker_gang')->where('worker_id', $user_id)->pluck('gang_id')->toArray();
$boss_ids = Gang::where('id', $gang_ids)->pluck('boss_id')->toArray();
return $q->whereIn('gang_boss', $boss_ids);
})->when($role_id == 10, function ($q) use ($user_id) {
return $q->where('truck', $user_id);
})->when($role_id == 1, function ($q) use ($user_id) {
return $q->where('agent', $user_id);
})->when($role_id == 9, function ($q) use ($user_id) {
return $q->where('gang_boss', $user_id);
})->whereIn('status', [0, 9])->with('gang_boss_details.bosses')
->with('type_of_cut_details')
->with('type_of_damage_details')
->with('number_of_boxes_details')
->with('size_of_boxes_details')
->with('packaging_company_details.packaging_companies')
->with('cutting_company_details:id,name')->with('supervisor_details:id,name')->with('agent_details:id,name')
->with('packaging_company_details:id,name')->with('truck_details:id,name')->with('ranch_details.ranch_owners')->with('right_hand_man:id,name')->with('weighing_machine_details:id,name,address,longitude,latitude')->with('worksheet')->orderBy('id', 'desc')->limit(20)->get();
return response()->json($work_orders);
}
public function workOrders($boss_id)
{
$work_orders = WorkingOrders::where('gang_boss', $boss_id)
->with('type_of_cut_details')
->with('type_of_damage_details')
->with('number_of_boxes_details')
->with('size_of_boxes_details')
->with('packaging_company_details.packaging_companies')
->with('gang_boss_details.bosses')->with('cutting_company_details:id,name')->with('supervisor_details:id,name')->with('agent_details:id,name')
->with('packaging_company_details')->with('truck_details:id,name')->with('ranch_details.ranch_owners')->with('right_hand_man:id,name')->with('weighing_machine_details:id,name,address,longitude,latitude')->with('worksheet')->orderBy('id', 'desc')->limit(20)->get();
return response()->json($work_orders);
}
public function workOrderDetail($work_order_id)
{
$work_order = WorkingOrders::with('cutting_company_details:id,name')
->with('type_of_cut_details')
->with('type_of_damage_details')
->with('number_of_boxes_details')
->with('size_of_boxes_details')
->with('packaging_company_details.packaging_companies')
->with('supervisor_details')->with('gang_boss_details.bosses')->with('agent_details')->with('ranch_details.ranch_owners')
->with('packaging_company_details')->with('weighing_machine_details:id,name,address,longitude,latitude')->with('worksheet')->with('truck_details.trucks')->with('right_hand_man')->find($work_order_id);
return response()->json($work_order);
}
public function markOrderComplete($id)
{
$work_order = WorkingOrders::find($id);
DB::table('notifications')->where('data','like','%'.$id.'%')->delete();
$work_order->update(['status'=>9,'truck_delivered_fruit_at'=>date('Y-m-d H:i:s')]);
}
public function getRightHandManList()
{
$staff_child = StaffPivot::where('staff_id', auth()->user()->id)->pluck('child_id')->toArray();
$staff_role = DB::table('staff_role')->whereIn('staff_id', $staff_child)->where('role_id', 4)->pluck('staff_id')->toArray();
$staffs = Staff::whereIn('id', $staff_role)->pluck('id');
$right_hand_men = RightHandMan::whereIn('staff_id', $staffs)->with('staffs')->get();
return response()->json($right_hand_men);
}
public function saveRightHandManList($right_hand_man, $work_order_id)
{
$work_order = WorkingOrders::find($work_order_id);
if($work_order->status == 8){
$status = 8;
}else{
$status = 3;
}
$work_order->update(['right_hand_man_id' => $right_hand_man, 'status' => $status,'confirmed_by_boss_at'=>date('Y-m-d H:i:s')]);
$working_order = WorkingOrders::find($work_order_id);
$appDetails = [
'actionText' => Auth::user()->name . ' Se te asignó la orden de corte #' . $work_order_id,
'actionURL' => "/work-order-detail/2/{$working_order->longitude}/{$working_order->latitude}",
];
$rhm = Staff::find($right_hand_man);
$rhm->notify(new CustomNotification($appDetails));
event(new ConfirmOrder($working_order));
return response()->json(['success' => 'Bañero asignado correctamente']);
}
public function getWorkSheet($work_order_id,$time)
{
$work_sheet = WorkSheet::where('work_order_id', $work_order_id)->first();
$work_order = WorkingOrders::find($work_order_id);
if($work_order->status == 8){
$status = 8;
}else{
$status = 6;
}
$work_order->update(['status'=>$status,'arrival_to_ranch_at'=>date('Y-m-d H:i:s',strtotime($time))]);
return response()->json($work_sheet);
}
public function saveWorkSheet($work_order_id, $column_name, $column_value)
{
if ($column_value == 'null') {
$column_value = null;
}
$work_sheet = WorkSheet::where('work_order_id', $work_order_id)->update([$column_name => $column_value]);
return response()->json(['status' => true, 'message' => 'Hoja de campo guardada correctamente']);
}
public function addWorker(Request $request)
{
$this->validate($request, [
'name' => 'required',
'password' => 'required',
'location' => 'required',
'longitude' => 'required',
'latitude' => 'required',
'govt_id' => 'required',
'phone_number' => 'required',
'social_security_number' => 'required|unique:staffs,social_security_number',
'time_as_worker' => 'required',
'email' => 'required|email|unique:staffs,email'
]);
$staff = Staff::create([
'name' => $request->name, 'password' => bcrypt($request->password), 'email' => $request->email, 'govt_id' => $request->govt_id,
'social_security_number' => $request->social_security_number, 'phone_number' => $request->phone_number
]);
DB::insert('insert into staff_role (role_id, staff_id) values (?, ?)', [3, $staff->id]);
Worker::create(['staff_id' => $staff->id, 'time_as_worker' => $request->time_as_worker, 'address' => $request->location, 'latitude' => $request->latitude, 'longitude' => $request->longitude]);
StaffPivot::create(['staff_id' => $request->boss_id, 'child_id' => $staff->id]);
return response()->json(['status' => true, 'message' => 'Cortador creado correctamente']);
}
public function addRightHandMan(Request $request)
{
$this->validate($request, [
'name' => 'required',
'password' => 'required',
'location' => 'required',
'longitude' => 'required',
'latitude' => 'required',
'govt_id' => 'required',
'phone_number' => 'required',
'social_security_number' => 'required|unique:staffs,social_security_number',
'time_as_right_hand_man' => 'required',
'email' => 'required|email|unique:staffs,email'
]);
$staff = Staff::create([
'name' => $request->name, 'password' => bcrypt($request->password), 'email' => $request->email, 'govt_id' => $request->govt_id,
'social_security_number' => $request->social_security_number, 'phone_number' => $request->phone_number
]);
DB::insert('insert into staff_role (role_id, staff_id) values (?, ?)', [4, $staff->id]);
RightHandMan::create(['staff_id' => $staff->id, 'time_as_right_hand_man' => $request->time_as_right_hand_man, 'address' => $request->location, 'latitude' => $request->latitude, 'longitude' => $request->longitude]);
StaffPivot::create(['staff_id' => $request->boss_id, 'child_id' => $staff->id]);
return response()->json(['status' => true, 'message' => 'Bañero creado correctamente']);
}
public function getGangWorkers($gang_boss_id, $work_order_id)
{
$gang = Gang::where('boss_id', $gang_boss_id)->first();
$arrived_workers = WorkerArrival::select('worker_id')->where('work_order_id', $work_order_id)->pluck('worker_id');
return response()->json(['workers' => $gang->workers()->get(), 'arrived_workers' => $arrived_workers]);
}
public function getGangWorkersOnly($gang_boss_id)
{
$gang = Gang::where('boss_id', $gang_boss_id)->first();
return response()->json(['workers' => $gang->workers()->get()]);
}
public function storeWorkerArrival(Request $request)
{
WorkerArrival::where('work_order_id', $request->work_order_id)->delete();
foreach ($request->worker_ids as $id) {
if(!WorkerArrival::where('worker_id',$id)->where('work_order_id',$request->work_order_id)->exists()){
WorkerArrival::create(['worker_id' => $id, 'work_order_id' => $request->work_order_id, 'is_reached' => 1]);
}
}
$work_order = WorkingOrders::find($request->work_order_id);
if($work_order->status == 8){
$status = 8;
}else{
$status = 5;
}
$work_order->update(['status'=>$status,'arrival_to_mp_at'=>date('Y-m-d H:i:s',strtotime($request->time))]);
event(new ConfirmOrder(WorkingOrders::find($request->work_order_id)));
return response()->json(['status' => true, 'message' => 'Los cortadores que llegaron han sido registrados']);
}
public function getBossRightHandMan($boss_id)
{
$staff_child = StaffPivot::where('staff_id', $boss_id)->pluck('child_id')->toArray();
$staff_role = DB::table('staff_role')->whereIn('staff_id', $staff_child)->where('role_id', 4)->pluck('staff_id')->toArray();
$staffs = Staff::whereIn('id', $staff_role)->get();
return response()->json($staffs);
}
public function getBossWorkers($boss_id)
{
// $gang = Gang::where('boss_id', $boss_id)->first();
// return response()->json(['workers' => $gang->workers()->get()]);
$staff_child = StaffPivot::where('staff_id', $boss_id)->pluck('child_id')->toArray();
$staff_role = DB::table('staff_role')->whereIn('staff_id', $staff_child)->where('role_id', 3)->pluck('staff_id')->toArray();
$staffs = Staff::whereIn('id', $staff_role)->get();
$gang = Gang::where('boss_id', $boss_id)->first();
return response()->json(['staff'=>$staffs,'workers'=>$gang->workers()->pluck('worker_id')->toArray()]);
}
public function deleteWorker(Request $request)
{
// Staff::find($request->worker_id)->delete();
// Worker::where('staff_id', $request->worker_id)->delete();
// $gang = Gang::where('boss_id', auth()->user()->id)->first();
// DB::table('worker_gang')->where('worker_id', $request->worker_id)->where('gang_id', $gang->id)->delete();
$gang = Gang::where('boss_id', auth()->user()->id)->first();
StaffPivot::where('staff_id', auth()->user()->id)->where('child_id' , $request->worker_id)->delete();
DB::table('worker_gang')->where('worker_id', $request->worker_id)->where('gang_id', $gang->id)->delete();
return response()->json(['success', 'Cortador elimando de tu lista de trabajadores']);
}
public function checkExistingStaff(Request $request)
{
if (Staff::where('social_security_number', $request->social_security_number)->exists()) {
return response()->json(['exists' => true, 'data' => Staff::where('social_security_number', $request->social_security_number)->first()]);
}
return response()->json(['exists' => false]);
}
public function addToMyProfile(Request $request)
{
if (!StaffPivot::where('staff_id', auth()->user()->id)->where('child_id', $request->child_id)->exists()) {
StaffPivot::create(['staff_id' => auth()->user()->id, 'child_id' => $request->child_id]);
return response()->json(['message' => 'Agregado correctamente a tu lista de trabajadores']);
}
return response()->json(['message' => 'Ya existe este usuario en tu lista de trabajadores']);
}
public function satffDetail($staff_id)
{
$satff = Staff::find($staff_id);
return response()->json($satff);
}
public function addToGang(Request $request)
{
$gang = Gang::where('boss_id', auth()->user()->id)->first();
if (!DB::table('worker_gang')->where('worker_id', $request->worker_id)->where('gang_id', $gang->id)->exists()) {
DB::table('worker_gang')->insert(['worker_id' => $request->worker_id, 'gang_id' => $gang->id]);
}
return response()->json(['status' => true, 'message' => 'Cortador asignado a la cuadrilla']);
}
public function removeWorkerFromGang(Request $request)
{
$gang = Gang::where('boss_id', auth()->user()->id)->first();
if (DB::table('worker_gang')->where('worker_id', $request->worker_id)->where('gang_id', $gang->id)->exists()) {
DB::table('worker_gang')->where('worker_id', $request->worker_id)->where('gang_id', $gang->id)->delete();
return response()->json(['status' => true, 'message' => 'Eliminado de la cuadrilla']);
}
return response()->json(['status' => true, 'message' => 'Este cortador no está en la cuadrilla']);
}
public function removeRHMFromWorkOrder(Request $request)
{
StaffPivot::where('child_id', $request->rhm)->where('staff_id', auth()->user()->id)->delete();
return response()->json(['success', 'Bañero elimanado de tu lista de trabajadores']);
}
public function OrderComplete($id,$time)
{
$work_order = WorkingOrders::find($id);
if($work_order->status == 8){
$status = 8;
}else{
$status = 7;
}
$work_order->update(['status' => $status,'boss_marked_completed_at'=>date('Y-m-d H:i:s',strtotime($time))]);
return response()->json(['status' => true, 'message' => 'Orden de corte completada']);
}
public function acceptance(Request $request)
{
if ($request->role == 'ranch') {
WorkingOrders::where('id', $request->work_order_id)->update(['ranch_signature' => $request->acceptance]);
}
if ($request->role == 'supervisor') {
WorkingOrders::where('id', $request->work_order_id)->update(['supervisor_signature' => $request->acceptance]);
}
return response()->json(['success' => 'Acción completada']);
}
public function storeWorkerData(Request $request)
{
WorkingOrders::find($request->work_order_id)->update(['boss_marked_completed_at'=>date('Y-m-d H:i:s'),'arrival_to_ranch_at'=>date('Y-m-d H:i:s')]);
WorkSheet::where('work_order_id', $request->work_order_id)->update(['worker_data' => json_encode($request->worker_data),'number_of_box'=>json_encode($request->number_of_boxes)]);
return response()->json(['success' => 'Datos del cortador guardados']);
}
public function getBossPayments(Request $request)
{
if($request->keyword !='false'){
$staff_ids = Staff::select('id')->where('name','like',"%{$request->keyword}%")->pluck('id');
$work_orders = WorkingOrders::select('id','cutting_company','ranch','cutting_company_amount','final_kg','boss_amount')->with(['cutting_company_details:id,name','ranch_details:id,name','worksheet:work_order_id,worker_data,number_of_box'])->whereIn('status',[8,9])->where('gang_boss',auth()->user()->id)
->where(function($query)use($request,$staff_ids){
$query->orWhere('id',$request->keyword)->orWhereIn('ranch',$staff_ids)->orWhereIn('cutting_company',$staff_ids);
})->orderBy('id','desc')->get();
foreach($work_orders as $work_order)
{
$work_order->setAttribute('payment',$this->getTotalAmountToPayToWorkers($work_order)['total_payment']);
}
return response()->json(['work_orders'=>$work_orders]);
}else{
$work_orders = WorkingOrders::select('id','cutting_company','ranch','cutting_company_amount','final_kg','boss_amount')->with(['cutting_company_details:id,name','ranch_details:id,name','worksheet:work_order_id,worker_data,number_of_box'])->whereIn('status',[8,9])->where('gang_boss',auth()->user()->id)->orderBy('id','desc')->get();
dd($work_orders);
foreach($work_orders as $work_order)
{
$work_order->setAttribute('payment',$this->getTotalAmountToPayToWorkers($work_order)['total_payment']);
}
return response()->json(['work_orders'=>$work_orders]);
}
}
public function getWorkOrderWorkerPayment($work_order_id)
{
$work_order = WorkingOrders::with(['worksheet:work_order_id,worker_data,number_of_box','cutting_company_details:id,name','ranch_details:id,name'])->select('id','boss_amount','boss_marked_completed_at','cutting_company','ranch','date_of_work')->where('id',$work_order_id)->first();
$work_order->setAttribute('total_amount_to_pay', $this->getTotalAmountToPayToWorkers($work_order)['total_payment']);
return response()->json(['worker_payment_data'=>$this->getTotalAmountToPayToWorkers($work_order)['worker_payment_data'],'work_order'=>$work_order]);
}
public function getTotalAmountToPayToWorkers($work_order)
{
$total_amount_to_pay = 0;
$worker_payment_data = [];
$work_sheet= $work_order->worksheet;
$data = json_decode($work_sheet->worker_data,true );
$number_of_boxes = json_decode($work_sheet->number_of_box,true);
foreach ((array)$data as $key => $worker_data){
$box = 0;
if(key_exists($key,$number_of_boxes))
{
$box =(int)$number_of_boxes[$key];
}
array_push($worker_payment_data,array('worker_id'=>explode("-",$key)[0],'photo_url'=>Staff::find(explode("-",$key)[0])->photo_url ?? '','worker_name'=>explode("-",$key)[1],'number_of_boxes'=>$box,'comment'=>$worker_data,
'amount_to_pay'=>$box * (float)$work_order->boss_amount ));
$total_amount_to_pay += ($box * (float)$work_order->boss_amount);
}
$worker_payment_data = collect($worker_payment_data);
return array('total_payment'=>$total_amount_to_pay,'worker_payment_data'=>$worker_payment_data);
}
public function submitBossAmount($work_order_id,Request $request)
{
WorkingOrders::find($work_order_id)->update(['boss_amount'=>$request->boss_amount]);
return response()->json(['status'=>true,'message'=>'Boss amount submitted successfully']);
}
public function getWorkerPayments(Request $request,$worker_id)
{
$staff_ids = Staff::select('id')->where('name','like',"%{$request->keyword}%")->pluck('id');
$work_order_ids = WorkSheet::select('work_order_id')->where('worker_data','like',"%{$worker_id}%")->pluck('work_order_id');
$work_orders = WorkingOrders::select('id','cutting_company','ranch','cutting_company_amount','final_kg','boss_amount','date_of_work','boss_marked_completed_at')
->with(['cutting_company_details:id,name','ranch_details:id,name'])
->whereIn('status',[8,9])->whereIn('id',$work_order_ids)
->where(function($query)use($staff_ids,$request){
$query->where('id',$request->keyword)->orWhereIn('ranch',$staff_ids)->orWhereIn('cutting_company',$staff_ids);
})->orderBy('id','desc')->get();
foreach($work_orders as $work_order)
{
$payment = $this->getTotalAmountToPayToWorkersForWorkPortal($work_order,$worker_id);
$work_order->setAttribute('payment',$payment['total_payment']);
$work_order->setAttribute('worker_payment_data',$payment['worker_payment_data']);
$work_order->setAttribute('total_amount_to_pay',$payment['total_payment']);
}
return response()->json(['work_orders'=>$work_orders,'worker_payment_data']);
}
public function getTotalAmountToPayToWorkersForWorkPortal($work_order,$worker_id)
{
$total_amount_to_pay = 0;
$worker_payment_data = [];
$work_sheet= $work_order->worksheet;
$worker_data = json_decode($work_sheet->worker_data,true );
$number_of_boxes = json_decode($work_sheet->number_of_box,true);
foreach ($worker_data as $key => $worker_data){
$box = 0;
if(key_exists($key,$number_of_boxes))
{
$box =(int)$number_of_boxes[$key];
}
if(explode("-",$key)[0] == $worker_id){
$worker_payment_data = array('worker_id'=>explode("-",$key)[0],'photo_url'=>Staff::find(explode("-",$key)[0])->photo_url ?? '','worker_name'=>explode("-",$key)[1],'number_of_boxes'=> $box ,'comment'=>$worker_data,
'amount_to_pay'=> $box * (float)$work_order->boss_amount );
$total_amount_to_pay += ( $box * (float)$work_order->boss_amount);
}
}
$worker_payment_data = collect($worker_payment_data);
return array('total_payment'=>$total_amount_to_pay,'worker_payment_data'=>$worker_payment_data);
}
}
This is the full controller . I also tried to type cast variable which are using in where condition but nothing works.

Maatwebsite\Excel\Validators\ValidationException: The given data was invalid in Laravel

In my Laravel-5.8 project, I am using Maatwebsites-3.1 to import excel
Imports
<?php
namespace App\Imports;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class LeavesImport implements WithMultipleSheets
{
public function sheets(): array
{
return [
new FirstLeaveSheetImport()
];
}
}
Which calles this:
class FirstLeaveSheetImport implements ToModel, WithHeadingRow, WithBatchInserts, WithValidation
{
protected $staffid, $leavetype, $commencementdate, $resumptiondate, $reliefofficer;
private $errors = []; // array to accumulate errors
use Importable;
// public function onRow(Row $row)
public function model(array $row)
{
$this->staffid = $row['staff_id'];
$this->leavetype = $row['leave_type'];
$this->reliefofficer = $row['relief_officer'];
$this->commencementdate = $row['commencement_date'];
return new HrLeaveRequest([
'employee_id' => $this->getStaffId(),
'leave_type_id' => $this->getLeaveType(),
'commencement_date' => $this->transformDate($row['commencement_date']),
'resumption_date' => $this->transformDate($row['resumption_date']),
'no_of_days' => $row['leave_days'],
'is_adjusted' => 1,
'relief_officer_id' => $this->getReliefOfficer(),
'reason' => $row['reason'] ?? '',
'alternative_email_address' => $row['alternative_email'] ?? '',
'contact_phone_number' => $row['contact_phone'] ?? '',
'contact_address' => $row['contact_address'] ?? '',
'company_id' => Auth::user()->company_id,
'leave_status' => 4,
'is_resumption_activated' => 1,
'resumption_activation_date' => $this->transformDate($row['resumption_date']),
'employee_code' => $row['staff_id'],
'created_by' => Auth::user()->id,
'created_at' => date("Y-m-d H:i:s"),
'is_active' => 1,
]);
}
public function getStaffId(){
if(!empty($this->staffid)){
return HrEmployee::where('employee_code',$this->staffid)->where('company_id',Auth::user()->company_id)->pluck('id')->first();
} else {
return 0;
}
}
public function getLeaveType(){
if(!empty($this->leavetype) || !$this->leavetype){
return HrLeaveType::where('leave_type_name',$this->leavetype)->where('company_id',Auth::user()->company_id)->pluck('id')->first();
} else {
return 0;
}
}
public function getReliefOfficer(){
return HrEmployee::where('employee_code',$this->reliefofficer)->where('company_id',Auth::user()->company_id)->pluck('employee_code')->first();
}
public function getErrors()
{
return $this->errors;
}
public function rules(): array
{
return [
'staff_id' => 'required|max:15',
'leave_type' => 'required|max:255',
'commencement_date' => 'required',
'leave_days' => 'required|numeric',
'resumption_date' => 'required',
'relief_officer' => 'nullable|max:255',
'reason' => 'nullable|max:255',
'alternative_email' => 'nullable|email|max:255',
'contact_phone' => 'nullable|phone:NG,mobile',
'contact_address' => 'nullable|max:255',
];
}
public function customValidationAttributes()
{
return [
'staff_id' => 'Staff ID',
'leave_type' => 'Leave Type',
'commencement_date' => 'Commencement Date',
'leave_days' => 'Leave Days',
'resumption_date' => 'Resumption Date',
'relief_officer' => 'Duty Relief Officer',
'reason' => 'Reason',
'alternative_email' => 'Alternative Email Address',
'contact_phone' => 'Contact Phone No.',
'contact_address' => 'Contact Address',
];
}
public function validationMessages()
{
return [
'staffid.*required' => "Staff ID is required",
];
}
public function transformDate($value, $format = 'Y-m-d')
{
try {
return \Carbon\Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value));
} catch (\ErrorException $e) {
return \Carbon\Carbon::createFromFormat($format, $value);
}
}
public function batchSize(): int
{
return 200;
}
public function headingRow(): int
{
return 1;
}
}
Controller
public function import(Request $request){
$request->validate([
'file' => 'required|max:10000|mimes:xlsx,xls',
]);
$path1 = $request->file('file')->store('temp');
$path=storage_path('app').'/'.$path1;
try{
Excel::import(new LeavesImport, $path);
} catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
$failures = $e->failures();
Log::error($e);
$errormessage = "";
// dd($failures);
foreach ($failures as $failure) {
$errormess = "";
foreach($failure->errors() as $error)
{
$errormess = $errormess.$error;
}
$errormessage = $errormessage." ,\n At Row ".$failure->row().", ".$errormess."<br>";
}
// Session::flash('error', 'Excel file is not imported!');
Session::flash('error', $errormessage);
// return redirect()->route('leave.leave_adjustments.index');
return back();
}catch (\Illuminate\Database\QueryException $e)
{
$errorCode = $e->errorInfo[1];
if($errorCode == 1062){
Log::error($e);
DB::rollback();
Session::flash('error', 'You have a duplicate entry problem!');
}
return back();
}
Session::flash('success', 'Leave Records Imported Successfully');
return redirect()->route('leave.leave_reviews.index_hr');
}
When I tried t upload the excel file it shows that it was successful. But I found that no data is store in the database. So when I checked error Log, I saw these:
[2020-11-16 07:38:53] production.ERROR: Maatwebsite\Excel\Validators\ValidationException: The given data was invalid. in C:\xampp\htdocs\myapp\vendor\maatwebsite\excel\src\Validators\RowValidator.php:62
Stack trace:
#0 C:\xampp\htdocs\myapp\vendor\maatwebsite\excel\src\Imports\ModelManager.php(166): Maatwebsite\Excel\Validators\RowValidator->validate(Array, Object(App\Imports\FirstLeaveSheetImport))
#1 C:\xampp\htdocs\myapp\vendor\maatwebsite\excel\src\Imports\ModelManager.php(53): Maatwebsite\Excel\Imports\ModelManager->validateRows(Object(App\Imports\FirstLeaveSheetImport))
#2 C:\xampp\htdocs\myapp\vendor\maatwebsite\excel\src\Imports\ModelImporter.php(70): Maatwebsite\Excel\Imports\ModelManager->flush(Object(App\Imports\FirstLeaveSheetImport), true)
#3 C:\xampp\htdocs\myapp\vendor\maatwebsite\excel\src\Sheet.php(248): Maatwebsite\Excel\Imports\ModelImporter->import(Object(PhpOffice\PhpSpreadsheet\Worksheet\Worksheet), Object(App\Imports\FirstLeaveSheetImport), 2)
#4 C:\xampp\htdocs\myapp\vendor\maatwebsite\excel\src\Reader.php(111): Maatwebsite\Excel\Sheet->import(Object(App\Imports\FirstLeaveSheetImport), 2)
#5 C:\xampp\htdocs\myapp\vendor\laravel\framework\src\Illuminate\Database\Concerns\ManagesTransactions.php(29): Maatwebsite\Excel\Reader->Maatwebsite\Excel\{closure}(Object(Illuminate\Database\MySqlConnection))
#6 C:\xampp\htdocs\myapp\vendor\maatwebsite\excel\src\Transactions\DbTransactionHandler.php(30): Illuminate\Database\Connection->transaction(Object(Closure))
#7 C:\xampp\htdocs\myapp\vendor\maatwebsite\excel\src\Reader.php(115): Maatwebsite\Excel\Transactions\DbTransactionHandler->__invoke(Object(Closure))
#8 C:\xampp\htdocs\myapp\vendor\maatwebsite\excel\src\Excel.php(146): Maatwebsite\Excel\Reader->read(Object(App\Imports\LeavesImport), 'C:\\xampp\\htdocs...', 'Xlsx', NULL)
#9 C:\xampp\htdocs\myapp\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php(239): Maatwebsite\Excel\Excel->import(Object(App\Imports\LeavesImport), 'C:\\xampp\\htdocs...')
#10 C:\xampp\htdocs\myapp\app\Http\Controllers\Leave\LeaveAdjustmentsController.php(360): Illuminate\Support\Facades\Facade::__callStatic('import', Array)
#11 [internal function]: App\Http\Controllers\Leave\LeaveAdjustmentsController->import(Object(Illuminate\Http\Request))
How do I detect and rectify the error?
Thanks
From the docs
try {
$import->import('import-users.xlsx');
} catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
$failures = $e->failures();
foreach ($failures as $failure) {
$failure->row(); // row that went wrong
$failure->attribute(); // either heading key (if using heading row concern) or column index
$failure->errors(); // Actual error messages from Laravel validator
$failure->values(); // The values of the row that has failed.
}
}

Signin with facebook

In my code I check the account status from users table if it is 1 then shows the detail of user. By default the value of account status is 1. When i run the code that condition doesn't work. Here is my code. Please Help. Thanks in advance.
public function signInFacebook(SignInFacebookUser $request)
{
if($profile_picture = $request->hasFile('profile_picture')) {
$profile_picture = time().'.'.$request->profile_picture->getClientOriginalExtension();
$request->profile_picture->move(public_path('Storage/ProfileImages'), $profile_picture);
$profile_picture = 'Storage/ProfileImages/'.$profile_picture;
} else {
$profile_picture = NULL;
}
try {
$user = User::updateOrCreate([
'facebook_id' => $request->input('facebook_id'),
],
[
'name' => $request->input('name'),
'surname' => $request->input('surname'),
'date_of_birth' => $request->input('date_of_birth'),
'email' => $request->input('email'),
'city' => $request->input('city'),
'university' => $request->input('university'),
'profile_picture' => $profile_picture,
]);
} catch (QueryException $e) {
$errorCode = $e->errorInfo[1];
if($errorCode == 1062){
return response()->json(['message' => 'Duplicate Entry']);
}
}
$token = JWTAuth::fromUser($user);
if($user->account_status == 1) {
$userDetail = $user->where('id', $user->id)->first();
return response()->json(['token' => $token, 'user' => $userDetail], 200);
}
else {
return response()->json(['message' => 'you are not active on app, contact to support team'], 200);
}
}
Here is the changing I done in my code and it works.
public function signInFacebook(SignInFacebookUser $request)
{
if($profile_picture = $request->hasFile('profile_picture')) {
$profile_picture = time().'.'.$request->profile_picture->getClientOriginalExtension();
$request->profile_picture->move(public_path('Storage/ProfileImages'), $profile_picture);
$profile_picture = 'Storage/ProfileImages/'.$profile_picture;
} else {
$profile_picture = NULL;
}
try {
$user = User::updateOrCreate([
'facebook_id' => $request->input('facebook_id'),
],
[
'name' => $request->input('name'),
'surname' => $request->input('surname'),
'date_of_birth' => $request->input('date_of_birth'),
'email' => $request->input('email'),
'city' => $request->input('city'),
'university' => $request->input('university'),
'profile_picture' => $profile_picture,
]);
} catch (QueryException $e) {
$errorCode = $e->errorInfo[1];
if($errorCode == 1062){
return response()->json(['message' => 'Duplicate Entry']);
}
}
if(!$user) {
return response()->json(['message' => 'failed to signin with facebook'], 200);
}
$userDetail = $user->where('id', $user->id)->first();
if ($userDetail->account_status == 1) {
$token = JWTAuth::fromUser($user);
return response()->json(['token' => $token, 'user' => $userDetail], 200);
} else {
return response()->json(['message' => 'you are not active on app, contact to support team'], 200);
}
}

Error adding Custom Option in magento

I have this error "Fatal error: Uncaught exception 'Mage_Eav_Model_Entity_Attribute_Exception' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '6152-0-11-0' for key"
Whe add custom optión programatically in magento, i use this function:
function createCustomOption($value, $customoptions_price, $sku, $title, $type, $noOption = false)
{
$custom_options = array();
if ($type && $value != "" && $value) {
$values = explode(',', $value);
$skus = explode(',', $sku);
$is_required = 1;
$customoptions_prices = explode(',', $customoptions_price);
if (count($values)) {
/**If the custom option has options*/
if (! $noOption) {
$is_required = 1;
$sort_order = 0;
$custom_options[ ] = array(
'is_delete' => 0,
'title' => $title,
'previous_group' => "",
'previous_type' => "",
'type' => $type,
'is_require' => $is_required,
'sort_order' => $sort_order ,
'values' => array()
);
for($i = 0; $i < (count($values)) ; $i++)
{
switch ($type) {
case 'drop_down':
case 'radio':
case 'checkbox':
case 'multiple':
default:
$custom_options[count($custom_options)-1]['values'][ ] = array(
'is_delete' => 0,
'title' => $values[$i],
'option_type_id' => –1,
'price_type' => 'fixed',
'price' => $customoptions_prices[$i],
'sku' => $skus[$i],
'sort_order' => $i);
break;
}
}
return $custom_options;
}
/**If the custom option doesn't have options | Case: area and field*/
else {
$is_required = 1;
$sort_order = 0;
$custom_options[ ] = array(
"is_delete" => 0,
"title" => $title,
"previous_group" => "text",
"price_type" => 'fixed',
"price" => "0",
"type" => $type,
"is_required" => $is_required
);
return $custom_options;
}
}
}
return false;
}
I call it from here:
$product = Mage::getModel('catalog/product')->load(6152);
unset($optionData);
$precio = 100;
$custom_title = "Unidad,Caja de 10 unidades";
$customoptions_price = "0,".$precio."";
$prod_sku = "unidadesku,cajasku";
$customoption_main_title = "Seleccione unidades o cajas";
$option_type = "drop_down";
$optionData[ ] = createCustomOption($custom_title, $customoptions_price, $prod_sku, $customoption_main_title, $option_type);
if(count($product->getOptions()) == 0){
foreach ($optionData as $options) {
foreach ($options as $option) {
$opt = Mage::getModel('catalog/product_option');
$opt->setProduct($product);
$opt->addOption($option);
$opt->saveOptions();
}
}
$product->setHasOptions(1)->save();
I solved it adding in my script this line:
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Exporting and Importing Attributes in Magento

I would love a script or magento extension to allow me to export all the product attributes including values for drop down attributes. Importing would be good too, I've had a look but can't seem to find anything does anyone know how this can be done?
Update: I found a working script in stackexchange and the script worked exactly as it should. All credits to the original poster. I am just copy pasting his reply here for future reference. Link to the original thread: is here.
I've done this to export all attributes and their options (if it is a dropdown attribute) from the source website:
exportAttributes.php in root directory of source website:
<?php
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
Mage::app();
$entity_type_id = Mage::getModel('catalog/product')->getResource()->getTypeId();
prepareCollection($entity_type_id);
function prepareCollection($ent_type_id){
$resource = Mage::getSingleton('core/resource');
$connection = $resource->getConnection('core_read');
$select_attribs = $connection->select()
->from(array('ea'=>$resource->getTableName('eav/attribute')))
->join(array('c_ea'=>$resource->getTableName('catalog/eav_attribute')), 'ea.attribute_id = c_ea.attribute_id');
// ->join(array('e_ao'=>$resource->getTableName('eav/attribute_option'), array('option_id')), 'c_ea.attribute_id = e_ao.attribute_id')
// ->join(array('e_aov'=>$resource->getTableName('eav/attribute_option_value'), array('value')), 'e_ao.option_id = e_aov.option_id and store_id = 0')
$select_prod_attribs = $select_attribs->where('ea.entity_type_id = '.$ent_type_id)
->order('ea.attribute_id ASC');
$product_attributes = $connection->fetchAll($select_prod_attribs);
$select_attrib_option = $select_attribs
->join(array('e_ao'=>$resource->getTableName('eav/attribute_option'), array('option_id')), 'c_ea.attribute_id = e_ao.attribute_id')
->join(array('e_aov'=>$resource->getTableName('eav/attribute_option_value'), array('value')), 'e_ao.option_id = e_aov.option_id and store_id = 0')
->order('e_ao.attribute_id ASC');
$product_attribute_options = $connection->fetchAll($select_attrib_option);
$attributesCollection = mergeCollections($product_attributes, $product_attribute_options);
prepareCsv($attributesCollection);
}
function mergeCollections($product_attributes, $product_attribute_options){
foreach($product_attributes as $key => $_prodAttrib){
$values = array();
$attribId = $_prodAttrib['attribute_id'];
foreach($product_attribute_options as $pao){
if($pao['attribute_id'] == $attribId){
$values[] = $pao['value'];
}
}
if(count($values) > 0){
$values = implode(";", $values);
$product_attributes[$key]['_options'] = $values;
}
else{
$product_attributes[$key]['_options'] = "";
}
/*
temp
*/
$product_attributes[$key]['attribute_code'] = $product_attributes[$key]['attribute_code'];
}
return $product_attributes;
}
function prepareCsv($attributesCollection, $filename = "importAttrib.csv", $delimiter = '|', $enclosure = '"'){
$f = fopen('php://memory', 'w');
$first = true;
foreach ($attributesCollection as $line) {
if($first){
$titles = array();
foreach($line as $field => $val){
$titles[] = $field;
}
fputcsv($f, $titles, $delimiter, $enclosure);
$first = false;
}
fputcsv($f, $line, $delimiter, $enclosure);
}
fseek($f, 0);
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="'.$filename.'"');
fpassthru($f);
}
This will give a csv file [actually i used "|" to separate ;)]
paste this csv file in MAGENTO_ROOT/attribImport directory of the destination website, i.e. website to which attributes need to be imported:
now put the following code in MAGENTO_ROOT/attribImport** directory
of the destination website
<?php
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/../app/Mage.php';
Mage::app();
// $fileName = MAGENTO . '/var/import/importAttrib.csv';
$fileName = 'importAttrib.csv';
// getCsv($fileName);
getAttributeCsv($fileName);
function getAttributeCsv($fileName){
// $csv = array_map("str_getcsv", file($fileName,FILE_SKIP_EMPTY_LINES));
$file = fopen($fileName,"r");
while(!feof($file)){
$csv[] = fgetcsv($file, 0, '|');
}
$keys = array_shift($csv);
foreach ($csv as $i=>$row) {
$csv[$i] = array_combine($keys, $row);
}
foreach($csv as $row){
$labelText = $row['frontend_label'];
$attributeCode = $row['attribute_code'];
if($row['_options'] != "")
$options = explode(";", $row['_options']); // add this to createAttribute parameters and call "addAttributeValue" function.
else
$options = -1;
if($row['apply_to'] != "")
$productTypes = explode(",", $row['apply_to']);
else
$productTypes = -1;
unset($row['frontend_label'], $row['attribute_code'], $row['_options'], $row['apply_to'], $row['attribute_id'], $row['entity_type_id'], $row['search_weight']);
createAttribute($labelText, $attributeCode, $row, $productTypes, -1, $options);
}
}
/**
* Create an attribute.
*
* For reference, see Mage_Adminhtml_Catalog_Product_AttributeController::saveAction().
*
* #return int|false
*/
function createAttribute($labelText, $attributeCode, $values = -1, $productTypes = -1, $setInfo = -1, $options = -1)
{
$labelText = trim($labelText);
$attributeCode = trim($attributeCode);
if($labelText == '' || $attributeCode == '')
{
echo "Can't import the attribute with an empty label or code. LABEL= [$labelText] CODE= [$attributeCode]"."<br/>";
return false;
}
if($values === -1)
$values = array();
if($productTypes === -1)
$productTypes = array();
if($setInfo !== -1 && (isset($setInfo['SetID']) == false || isset($setInfo['GroupID']) == false))
{
echo "Please provide both the set-ID and the group-ID of the attribute-set if you'd like to subscribe to one."."<br/>";
return false;
}
echo "Creating attribute [$labelText] with code [$attributeCode]."."<br/>";
//>>>> Build the data structure that will define the attribute. See
// Mage_Adminhtml_Catalog_Product_AttributeController::saveAction().
$data = array(
'is_global' => '0',
'frontend_input' => 'text',
'default_value_text' => '',
'default_value_yesno' => '0',
'default_value_date' => '',
'default_value_textarea' => '',
'is_unique' => '0',
'is_required' => '0',
'frontend_class' => '',
'is_searchable' => '1',
'is_visible_in_advanced_search' => '1',
'is_comparable' => '1',
'is_used_for_promo_rules' => '0',
'is_html_allowed_on_front' => '1',
'is_visible_on_front' => '0',
'used_in_product_listing' => '0',
'used_for_sort_by' => '0',
'is_configurable' => '0',
'is_filterable' => '0',
'is_filterable_in_search' => '0',
'backend_type' => 'varchar',
'default_value' => '',
'is_user_defined' => '0',
'is_visible' => '1',
'is_used_for_price_rules' => '0',
'position' => '0',
'is_wysiwyg_enabled' => '0',
'backend_model' => '',
'attribute_model' => '',
'backend_table' => '',
'frontend_model' => '',
'source_model' => '',
'note' => '',
'frontend_input_renderer' => '',
);
// Now, overlay the incoming values on to the defaults.
foreach($values as $key => $newValue)
if(isset($data[$key]) == false)
{
echo "Attribute feature [$key] is not valid."."<br/>";
return false;
}
else
$data[$key] = $newValue;
// Valid product types: simple, grouped, configurable, virtual, bundle, downloadable, giftcard
$data['apply_to'] = $productTypes;
$data['attribute_code'] = $attributeCode;
$data['frontend_label'] = array(
0 => $labelText,
1 => '',
3 => '',
2 => '',
4 => '',
);
//<<<<
//>>>> Build the model.
$model = Mage::getModel('catalog/resource_eav_attribute');
$model->addData($data);
if($setInfo !== -1)
{
$model->setAttributeSetId($setInfo['SetID']);
$model->setAttributeGroupId($setInfo['GroupID']);
}
$entityTypeID = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();
$model->setEntityTypeId($entityTypeID);
$model->setIsUserDefined(1);
//<<<<
// Save.
try
{
$model->save();
}
catch(Exception $ex)
{
echo "Attribute [$labelText] could not be saved: " . $ex->getMessage()."<br/>";
return false;
}
if(is_array($options)){
foreach($options as $_opt){
addAttributeValue($attributeCode, $_opt);
}
}
$id = $model->getId();
echo "Attribute [$labelText] has been saved as ID ($id).<br/>";
// return $id;
}
function addAttributeValue($arg_attribute, $arg_value)
{
$attribute_model = Mage::getModel('eav/entity_attribute');
$attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
$attribute = $attribute_model->load($attribute_code);
if(!attributeValueExists($arg_attribute, $arg_value))
{
$value['option'] = array($arg_value,$arg_value);
$result = array('value' => $value);
$attribute->setData('option',$result);
$attribute->save();
}
$attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;
$attribute_table = $attribute_options_model->setAttribute($attribute);
$options = $attribute_options_model->getAllOptions(false);
foreach($options as $option)
{
if ($option['label'] == $arg_value)
{
return $option['value'];
}
}
return false;
}
function attributeValueExists($arg_attribute, $arg_value)
{
$attribute_model = Mage::getModel('eav/entity_attribute');
$attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;
$attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
$attribute = $attribute_model->load($attribute_code);
$attribute_table = $attribute_options_model->setAttribute($attribute);
$options = $attribute_options_model->getAllOptions(false);
foreach($options as $option)
{
if ($option['label'] == $arg_value)
{
return $option['value'];
}
}
return false;
}
NOTE: Allthough exceptions have been handled, Backup your Database
before you import these attributes, to be on safer side. Happy
Importing!
Thanks to :
Programatically create attribute in Magento, useful for the “on the fly” import system
programmatically_adding_attributes_and_attribute_sets
Magento – Programmatically insert new attribute option
I would recommend Boris's uRapidFlow: http://www.unirgy.com/products/urapidflow/ It's one of the better Data Flow Import/Export modules available. Be aware however it does require IonCube Loader, but it is well worth it if you are moving data around a lot.

Resources