DischargeTaskCostEntryImport.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Imports;
  3. use App\DischargeTask;
  4. use App\Facilitator;
  5. use App\Owner;
  6. use App\Warehouse;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Facades\Cache;
  9. use Maatwebsite\Excel\Concerns\ToCollection;
  10. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  11. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  12. HeadingRowFormatter::default('none');
  13. class DischargeTaskCostEntryImport implements ToCollection,WithHeadingRow
  14. {
  15. /**
  16. * @param Collection $collection
  17. */
  18. public function collection(Collection $collection)
  19. {
  20. $exception = [];
  21. $numbers = [];
  22. foreach ($collection as $row => $item) {
  23. $index = $row + 1; // 行数
  24. $message = ''; // 信息
  25. $type = array_search(trim($item['作业名称']), DischargeTask::types);
  26. $created_at = $item['日期'] ?? ''; // 导入的日期为创建时间
  27. $number = $item['入库单号'] ?? '';
  28. $owner_name = $item['客户名称'] ?? '';
  29. $owner = Owner::query()->where('name',$owner_name)->first();
  30. $ware_house = Warehouse::query()->where('name',$item['仓库'])->first();
  31. $facilitator = Facilitator::query()->where('name', $item['服务商'] ?? '')->first();
  32. if ($created_at == '') $message .= '日期为空,';
  33. if ($number == '')$message .= '入库单号为空,';
  34. if ($owner_name =='' || $owner == null) $message .= '货主未找到,';
  35. if ($ware_house == null) $message .= '仓库未找到,';
  36. $expenditure_unit_price = $item['成本单价'];
  37. $expenditure_amount = $item['成本数量'];
  38. $dischargeTask = DischargeTask::query()->where([
  39. 'owner_id' => $owner['id'],
  40. 'numbers' => $number,
  41. 'created_at' => $created_at,
  42. 'type' => $type,
  43. 'warehouse_id' =>$ware_house['id']
  44. ])->first();
  45. if (isset($item['成本单位'])){
  46. $expenditure_unit = array_search(trim($item['成本单位']), DischargeTask::units);
  47. if (trim($expenditure_unit) == 'm3'){
  48. $expenditure_unit= array_search('m³', DischargeTask::types);
  49. }
  50. }else{
  51. $expenditure_unit = $dischargeTask['income_unit'];
  52. }
  53. if ($expenditure_unit_price == 0 || $expenditure_unit_price == null) $message .= '成本单价为空,';
  54. if ($expenditure_unit != 0 && ($expenditure_unit == '' || $expenditure_unit == null ) ) $message .= '成本单位为空,';
  55. if ($expenditure_amount == '' || $expenditure_unit_price == null) $message .= '成本数量为空,';
  56. if ($facilitator == null) $message .= '未指定服务商,';
  57. if (strlen($message) != 0) {
  58. $exception[] = "第{$index}行成本编辑异常:" . $message;
  59. continue;
  60. }
  61. $params = [
  62. 'expenditure_unit_price' => $expenditure_unit_price,
  63. 'expenditure_unit' => $expenditure_unit,
  64. 'expenditure_amount'=>$expenditure_amount,
  65. 'facilitator_id' => $facilitator['id'],
  66. 'status' => array_search('接单',DischargeTask::status),
  67. 'expenditure_at' => $dischargeTask->expenditure_at,
  68. ];
  69. $params['expenditure_total_cost'] = $expenditure_unit_price * $expenditure_amount;
  70. $dischargeTask->update($params);
  71. $numbers[] = $number;
  72. }
  73. Cache::put('exception', $exception, 86400);
  74. Cache::put('numbers', $numbers, 86400);
  75. }
  76. }