50 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			50 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Imports\Purchases\Sheets;
 | |
| 
 | |
| use App\Abstracts\Import;
 | |
| use App\Http\Requests\Document\Document as Request;
 | |
| use App\Models\Document\Document as Model;
 | |
| use Illuminate\Support\Str;
 | |
| 
 | |
| class Bills extends Import
 | |
| {
 | |
|     public function model(array $row)
 | |
|     {
 | |
|         return new Model($row);
 | |
|     }
 | |
| 
 | |
|     public function map($row): array
 | |
|     {
 | |
|         if ($this->isEmpty($row, 'bill_number')) {
 | |
|             return [];
 | |
|         }
 | |
| 
 | |
|         $row = parent::map($row);
 | |
| 
 | |
|         $country = array_search($row['contact_country'], trans('countries'));
 | |
| 
 | |
|         $row['document_number'] = $row['bill_number'];
 | |
|         $row['issued_at'] = $row['billed_at'];
 | |
|         $row['category_id'] = $this->getCategoryId($row, 'expense');
 | |
|         $row['contact_id'] = $this->getContactId($row, 'vendor');
 | |
|         $row['type'] = Model::BILL_TYPE;
 | |
|         $row['contact_country'] = !empty($country) ? $country : null;
 | |
| 
 | |
|         return $row;
 | |
|     }
 | |
| 
 | |
|     public function rules(): array
 | |
|     {
 | |
|         $rules = (new Request())->rules();
 | |
| 
 | |
|         $rules['bill_number'] = Str::replaceFirst('unique:documents,NULL', 'unique:documents,document_number', $rules['document_number']);
 | |
|         $rules['billed_at'] = $rules['issued_at'];
 | |
|         $rules['currency_rate'] = 'required';
 | |
| 
 | |
|         unset($rules['document_number'], $rules['issued_at'], $rules['type']);
 | |
| 
 | |
|         return $this->replaceForBatchRules($rules);
 | |
|     }
 | |
| }
 |