| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace App\Imports;
- use App\DischargeTask;
- use App\Facilitator;
- use App\Owner;
- use App\Warehouse;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Cache;
- use Maatwebsite\Excel\Concerns\ToCollection;
- use Maatwebsite\Excel\Concerns\WithHeadingRow;
- use Maatwebsite\Excel\Imports\HeadingRowFormatter;
- HeadingRowFormatter::default('none');
- class DischargeTaskCostEntryImport implements ToCollection,WithHeadingRow
- {
- /**
- * @param Collection $collection
- */
- public function collection(Collection $collection)
- {
- $exception = [];
- $numbers = [];
- foreach ($collection as $row => $item) {
- $index = $row + 1; // 行数
- $message = ''; // 信息
- $type = array_search(trim($item['作业名称']), DischargeTask::types);
- $created_at = $item['日期'] ?? ''; // 导入的日期为创建时间
- $number = $item['入库单号'] ?? '';
- $owner_name = $item['客户名称'] ?? '';
- $owner = Owner::query()->where('name',$owner_name)->first();
- $ware_house = Warehouse::query()->where('name',$item['仓库'])->first();
- $facilitator = Facilitator::query()->where('name', $item['服务商'] ?? '')->first();
- if ($created_at == '') $message .= '日期为空,';
- if ($number == '')$message .= '入库单号为空,';
- if ($owner_name =='' || $owner == null) $message .= '货主未找到,';
- if ($ware_house == null) $message .= '仓库未找到,';
- $expenditure_unit_price = $item['成本单价'];
- $expenditure_amount = $item['成本数量'];
- $dischargeTask = DischargeTask::query()->where([
- 'owner_id' => $owner['id'],
- 'numbers' => $number,
- 'created_at' => $created_at,
- 'type' => $type,
- 'warehouse_id' =>$ware_house['id']
- ])->first();
- if (isset($item['成本单位'])){
- $expenditure_unit = array_search(trim($item['成本单位']), DischargeTask::units);
- if (trim($expenditure_unit) == 'm3'){
- $expenditure_unit= array_search('m³', DischargeTask::types);
- }
- }else{
- $expenditure_unit = $dischargeTask['income_unit'];
- }
- if ($expenditure_unit_price == 0 || $expenditure_unit_price == null) $message .= '成本单价为空,';
- if ($expenditure_unit != 0 && ($expenditure_unit == '' || $expenditure_unit == null ) ) $message .= '成本单位为空,';
- if ($expenditure_amount == '' || $expenditure_unit_price == null) $message .= '成本数量为空,';
- if ($facilitator == null) $message .= '未指定服务商,';
- if (strlen($message) != 0) {
- $exception[] = "第{$index}行成本编辑异常:" . $message;
- continue;
- }
- $params = [
- 'expenditure_unit_price' => $expenditure_unit_price,
- 'expenditure_unit' => $expenditure_unit,
- 'expenditure_amount'=>$expenditure_amount,
- 'facilitator_id' => $facilitator['id'],
- 'status' => array_search('接单',DischargeTask::status),
- 'expenditure_at' => $dischargeTask->expenditure_at,
- ];
- $params['expenditure_total_cost'] = $expenditure_unit_price * $expenditure_amount;
- $dischargeTask->update($params);
- $numbers[] = $number;
- }
- Cache::put('exception', $exception, 86400);
- Cache::put('numbers', $numbers, 86400);
- }
- }
|