DischargeTaskImport.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Imports;
  3. use App\DischargeTask;
  4. use App\Owner;
  5. use App\Services\DischargeTaskService;
  6. use App\Warehouse;
  7. use Illuminate\Support\Carbon;
  8. use Illuminate\Support\Collection;
  9. use Illuminate\Support\Facades\Cache;
  10. use Maatwebsite\Excel\Concerns\ToCollection;
  11. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  12. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  13. HeadingRowFormatter::default('none');
  14. class DischargeTaskImport implements ToCollection, WithHeadingRow
  15. {
  16. /**
  17. * @param Collection $collection
  18. */
  19. public function collection(Collection $collection)
  20. {
  21. /** @var DischargeTaskService $service */
  22. $service = app(DischargeTaskService::class);
  23. $exception = [];
  24. $numbers = [];
  25. foreach ($collection as $row => $item) {
  26. $index = $row + 1;
  27. $message = '';
  28. $owner = Owner::query()->where('name', trim($item['货主']))->first();
  29. $waveHouse = Warehouse::query()->where('name', trim($item['仓库']))->first();
  30. $type = array_search(trim($item['作业名称']), DischargeTask::types);
  31. $unit = array_search(trim($item['单位']), DischargeTask::units);
  32. if (trim($item['单位']) == 'm3'){
  33. $unit= array_search('m³', DischargeTask::types);
  34. }
  35. if (array_search(trim($item['入库单']), $numbers)) $message .= '入库单号重复;';
  36. if (!$owner) $message .= '对应货主不存在;';
  37. if (!$waveHouse) $message .= '对应仓库不存在;';
  38. if (!$item['入库单'] || strlen(trim('入库单')) == 0) $message .= '未输入入库单;';
  39. if (!$item['数量'] || strlen(trim('数量')) == 0) $message .= '未输入数量;';
  40. if (!$item['单价'] || strlen(trim('单价')) == 0) $message .= '未输入单价;';
  41. if (!$item['单位'] || strlen(trim('单位')) == 0) $message .= '未输入单位;';
  42. if (!isset($type)) $message .= '指定作业类型错误;';
  43. if (!$item['作业名称']) $message .= '未指定作业类型';
  44. if (DischargeTask::query()->where('numbers',$item['入库单'])->exists()) $message .= '入库单号已存在;';
  45. if (strlen($message) > 0) {
  46. $exception[] = "第{$index}行卸货任务创建失败:" . $message;
  47. continue;
  48. }
  49. $params = [
  50. 'owner_id' => $owner->id,
  51. 'warehouse_id' => $waveHouse->id,
  52. 'type' => $type,
  53. 'numbers' => trim($item['入库单']),
  54. 'income_amount' => $item['数量'],
  55. 'income_unit_price' => $item['单价'],
  56. 'income_unit' => $unit,
  57. 'income_total_cost' => $item['数量'] * $item['单价'],
  58. 'status' => 0,
  59. 'income_remark' => $item['备注'] ?? '',
  60. 'income_at' => $item['预约日期'] ?formatExcelDateTime($item['预约日期']): Carbon::now()->format(Carbon::DEFAULT_TO_STRING_FORMAT)
  61. ];
  62. try {
  63. $service->createTask($params, false);
  64. $numbers[] = trim($item['入库单']);
  65. } catch (\Exception $e) {
  66. $exception[] = "第{$index}行创建失败:创建异常";
  67. }
  68. }
  69. Cache::put('exception', $exception, 86400);
  70. }
  71. }