|
|
@@ -2,22 +2,80 @@
|
|
|
|
|
|
namespace App\Imports;
|
|
|
|
|
|
+use App\DischargeTask;
|
|
|
+use App\Owner;
|
|
|
+use App\Services\DischargeTaskService;
|
|
|
+use App\Warehouse;
|
|
|
+use Illuminate\Support\Carbon;
|
|
|
use Illuminate\Support\Collection;
|
|
|
+use Illuminate\Support\Facades\Cache;
|
|
|
+use Illuminate\Support\Str;
|
|
|
use Maatwebsite\Excel\Concerns\ToCollection;
|
|
|
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
|
|
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
|
|
|
+use PhpMyAdmin\Plugins\Export\ExportExcel;
|
|
|
|
|
|
HeadingRowFormatter::default('none');
|
|
|
-class DischargeTaskImport implements ToCollection,WithHeadingRow
|
|
|
+
|
|
|
+class DischargeTaskImport implements ToCollection, WithHeadingRow
|
|
|
{
|
|
|
/**
|
|
|
- * @param Collection $collection
|
|
|
- */
|
|
|
+ * @param Collection $collection
|
|
|
+ */
|
|
|
public function collection(Collection $collection)
|
|
|
{
|
|
|
- dd($collection->first());
|
|
|
-// foreach ($collection as $item) {
|
|
|
+ /** @var DischargeTaskService $service */
|
|
|
+ $service = app(DischargeTaskService::class);
|
|
|
+ $exception = [];
|
|
|
+ $numbers = [];
|
|
|
+ foreach ($collection as $row => $item) {
|
|
|
+ $index = $row + 1;
|
|
|
+ $message = '';
|
|
|
+ $owner = Owner::query()->where('name', trim($item['货主']))->first();
|
|
|
+ $waveHouse = Warehouse::query()->where('name', trim($item['仓库']))->first();
|
|
|
+ $type = array_search(trim($item['作业名称']), DischargeTask::types);
|
|
|
+
|
|
|
+ if (array_search(trim($item['入库单']), $numbers)) $message .= '入库单号重复;';
|
|
|
+
|
|
|
+ if (!$owner) $message .= '对应货主不存在;';
|
|
|
+
|
|
|
+ if (!$waveHouse) $message .= '对应仓库不存在;';
|
|
|
+
|
|
|
+ if (!$item['入库单'] || strlen(trim('入库单')) == 0) $message .= '未输入入库单;';
|
|
|
+
|
|
|
+ if (!$item['数量'] || strlen(trim('数量')) == 0) $message .= '未输入数量;';
|
|
|
+
|
|
|
+ if (!$item['单价'] || strlen(trim('单价')) == 0) $message .= '未输入单价;';
|
|
|
+
|
|
|
+ if (isset($type) && $type != 0) $message .= '指定作业类型错误;';
|
|
|
+
|
|
|
+ if (!$item['作业名称']) $message .= '未指定作业类型';
|
|
|
+
|
|
|
+ if (DischargeTask::query()->where('numbers',$item['入库单'])->exists()) $message .= '入库单号已存在;';
|
|
|
|
|
|
-// }
|
|
|
+ if (strlen($message) > 0) {
|
|
|
+ $exception[] = "第{$index}行卸货任务创建失败:" . $message;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $params = [
|
|
|
+ 'owner_id' => $owner->id,
|
|
|
+ 'warehouse_id' => $waveHouse->id,
|
|
|
+ 'type' => $type,
|
|
|
+ 'numbers' => trim($item['入库单']),
|
|
|
+ 'income_amount' => $item['数量'],
|
|
|
+ 'income_unit_price' => $item['单价'],
|
|
|
+ 'income_total_cost' => $item['数量'] * $item['单价'],
|
|
|
+ 'status' => 0,
|
|
|
+ 'income_remark' => $item['备注'],
|
|
|
+ 'income_at' => $item['预约日期'] ?formatExcelDateTime($item['预约日期']): Carbon::now()->format(Carbon::DEFAULT_TO_STRING_FORMAT)
|
|
|
+ ];
|
|
|
+ try {
|
|
|
+ $service->createTask($params, false);
|
|
|
+ $numbers[] = trim($item['入库单']);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ $exception[] = "第{$index}行创建失败:创建异常";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Cache::put('exception', $exception, 86400);
|
|
|
}
|
|
|
}
|