| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace LWS\ImportExport\Jobs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Carbon\Carbon; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Bus\Queueable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Maatwebsite\Excel\Facades\Excel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Queue\SerializesModels; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Queue\InteractsWithQueue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Contracts\Queue\ShouldQueue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Illuminate\Foundation\Bus\Dispatchable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use LWS\ImportExport\Models\Export; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class ExportJob implements ShouldQueue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public $tries = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $export; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * Create a new job instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function __construct(Export $export) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->export = $export; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Execute the job. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     public function handle() | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         // Custom file path | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $file_path = date('Y/m'); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         // Export instance assign to variable | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $export = $this->export; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         // Store total row count | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $export->result_rows = $export->query->count(); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $export->save(); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         // Create export file | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $excel = Excel::create('export-'.date('dmYhis'), function ($excel) use ($export) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             // Create new sheet | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             $excel->sheet('export', function ($sheet) use ($export) { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                 // Retrive data in chunk | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 $export->query->chunk(10, function ($data) use ($export, $sheet) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                     // Process chunk data | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                     foreach ($data as $row) { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                         // Append row to sheet | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                         $sheet->appendRow($row->toArray()); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                     // Store processed row count | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                     $export->row_processed += 10; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                     $export->save(); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |                 }); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             }); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         })->store($this->export->type, storage_path('app/exports/'.$file_path), true); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         // Update export data | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         $export->file = $file_path.'/'.$excel['file']; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         $export->completed_at = Carbon::now(); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         $export->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  |