public function saveClients($rpf) { // $payments = RoyaltyPayment::groupedByClientCode($rpf->id)->get(); // foreach ($payments as $payment) // $client = Client::create(["name" => $payment->client_name, "code" => $payment->client_code]); DB::connection()->getpdo()->exec('INSERT IGNORE INTO ' . Client::table() . '(`name`, `code`) SELECT `client_name`, `client_code` FROM ' . RoyaltyPayment::table() . ' WHERE `royalty_payment_file_id` = ' . $rpf->id . ' AND `payee_payment_id` IS NULL GROUP BY `client_code` ORDER BY `client_code`'); }
public static function loadFromFile(RoyaltyPaymentFile $rpf) { $firstLine = CsvFileService::getFileFirstLine($rpf->path); $header = explode(',', strtolower(trim($firstLine))); $fieldsToFileHeaderPos = RoyaltyPayment::fieldsToFileHeaderPositions(); if (count($header) == count(RoyaltyPayment::fieldsToFileHeaderPositionsOld())) { $fieldsToFileHeaderPos = RoyaltyPayment::fieldsToFileHeaderPositionsOld(); } $query = "LOAD DATA LOCAL INFILE " . DB::connection()->getPdo()->quote($rpf->path) . "\n INTO TABLE " . RoyaltyPayment::table() . "\n CHARACTER SET 'utf8'\n FIELDS TERMINATED BY ','\n ENCLOSED BY '\"' " . RoyaltyPayment::detectNewLineChar($firstLine) . "\n IGNORE 1 LINES\n (@col" . implode(",@col", array_keys($header)) . ")\n SET "; foreach ($fieldsToFileHeaderPos as $position => $field) { if (array_key_exists($field, self::fieldFilters())) { $query .= " {$field}=" . self::fieldFilter($field, "@col" . ($position + 2)) . ", "; } else { $query .= " {$field}=TRIM(@col" . ($position + 2) . "), "; } } $query = rtrim($query, ", "); $query .= ", created_at = now(), updated_at = now(), company_id = " . $rpf->company_id . ", royalty_payment_file_id = " . $rpf->id; DB::connection()->getpdo()->exec($query); }