|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
|
|
+use App\Models\Product;
|
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
@@ -16,8 +17,8 @@ class GenerateSeparateDocxJob implements ShouldQueue
|
|
|
{
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
- protected $vars;
|
|
|
|
|
- protected $products_count;
|
|
|
|
|
|
|
+ protected $ids;
|
|
|
|
|
+ protected $descr;
|
|
|
protected $filename;
|
|
protected $filename;
|
|
|
protected $template;
|
|
protected $template;
|
|
|
|
|
|
|
@@ -26,10 +27,10 @@ class GenerateSeparateDocxJob implements ShouldQueue
|
|
|
*
|
|
*
|
|
|
* @return void
|
|
* @return void
|
|
|
*/
|
|
*/
|
|
|
- public function __construct($vars, $products_count, $filename, $template)
|
|
|
|
|
|
|
+ public function __construct($ids, $descr, $filename, $template)
|
|
|
{
|
|
{
|
|
|
- $this->vars = $vars;
|
|
|
|
|
- $this->products_count = $products_count;
|
|
|
|
|
|
|
+ $this->ids = $ids;
|
|
|
|
|
+ $this->descr = $descr;
|
|
|
$this->filename = $filename;
|
|
$this->filename = $filename;
|
|
|
$this->template = $template;
|
|
$this->template = $template;
|
|
|
}
|
|
}
|
|
@@ -42,18 +43,46 @@ class GenerateSeparateDocxJob implements ShouldQueue
|
|
|
public function handle()
|
|
public function handle()
|
|
|
{
|
|
{
|
|
|
$tmpfile = public_path('exported/docx/' . Str::replace('.docx', '.zip', $this->filename) . '.txt');
|
|
$tmpfile = public_path('exported/docx/' . Str::replace('.docx', '.zip', $this->filename) . '.txt');
|
|
|
-
|
|
|
|
|
|
|
+ $products = Product::query()->whereIn('id', $this->ids)->orderBy('series', 'asc')->get();
|
|
|
|
|
+ $i = 1;
|
|
|
$file_for_arch = [];
|
|
$file_for_arch = [];
|
|
|
- for ($i = 0; $i < $this->products_count; $i++) {
|
|
|
|
|
- $my_template = new TemplateProcessor(storage_path('templates/' . $this->template));
|
|
|
|
|
- $tmp_vars = [];
|
|
|
|
|
- foreach ($this->vars as $k => $v) {
|
|
|
|
|
- if (Str::endsWith($k, '#' . $i + 1)) {
|
|
|
|
|
- $tmp_vars[Str::replace('#' . $i + 1, '#1', $k)] = $v;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($products as $product){
|
|
|
|
|
+ switch ($this->descr) {
|
|
|
|
|
+ case 1:
|
|
|
|
|
+ $descr = $product->characteristics . "\r\n" . $product->tech_description_short;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 2:
|
|
|
|
|
+ $descr = $product->characteristics . "\r\n" . $product->tech_description;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 3:
|
|
|
|
|
+ $descr = $product->tech_description_short;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 4:
|
|
|
|
|
+ $descr = $product->tech_description;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 5:
|
|
|
|
|
+ default:
|
|
|
|
|
+ $descr = $product->characteristics;
|
|
|
|
|
+ break;
|
|
|
}
|
|
}
|
|
|
- $my_template->cloneBlock('product_block', 1, true, true, $tmp_vars);
|
|
|
|
|
- foreach ($tmp_vars as $k => $v) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ $vars_for_template = [
|
|
|
|
|
+ 'series#1' => $product->series,
|
|
|
|
|
+ 'article#1' => $product->article,
|
|
|
|
|
+ 'product_group#1' => $product->product_group,
|
|
|
|
|
+ 'num#1' => 1, // $i,
|
|
|
|
|
+ 'name#1' => $product->name,
|
|
|
|
|
+ 'name_for_form#1' => $product->name_for_form,
|
|
|
|
|
+ 'price#1' => number_format($product->price, 2, '.', ' '),
|
|
|
|
|
+ 'image#1' => $product->image_path,
|
|
|
|
|
+ 'description#1' => str_replace("\n", '</w:t><w:br/><w:t xml:space="preserve">', $descr),
|
|
|
|
|
+ 'page_br#1' => ''
|
|
|
|
|
+ ];
|
|
|
|
|
+ $article = '';
|
|
|
|
|
+ $my_template = new TemplateProcessor(storage_path('templates/' . $this->template));
|
|
|
|
|
+ $my_template->cloneBlock('product_block', 1, true, true, $vars_for_template);
|
|
|
|
|
+ foreach ($vars_for_template as $k => $v) {
|
|
|
$file = public_path() . '/' . env('IMAGES_PATH') . '/' . $v;
|
|
$file = public_path() . '/' . env('IMAGES_PATH') . '/' . $v;
|
|
|
if (str_starts_with($k, 'image') && file_exists($file) && $v !== '') {
|
|
if (str_starts_with($k, 'image') && file_exists($file) && $v !== '') {
|
|
|
$my_template->setImageValue($k, ['path' => $file, 'width' => 270, 'height' => 180]);
|
|
$my_template->setImageValue($k, ['path' => $file, 'width' => 270, 'height' => 180]);
|
|
@@ -61,23 +90,31 @@ class GenerateSeparateDocxJob implements ShouldQueue
|
|
|
$my_template->setValue($k, 1);
|
|
$my_template->setValue($k, 1);
|
|
|
} elseif (Str::startsWith($k, 'page_br')) {
|
|
} elseif (Str::startsWith($k, 'page_br')) {
|
|
|
$my_template->setValue($k, '');
|
|
$my_template->setValue($k, '');
|
|
|
|
|
+ } elseif(Str::startsWith($k, 'article')){
|
|
|
|
|
+ $article = $v;
|
|
|
} else {
|
|
} else {
|
|
|
$my_template->setValue($k, $v);
|
|
$my_template->setValue($k, $v);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ $filename = Str::replace('Наш_Двор_', 'Наш_Двор_' . $article . '_', $this->filename);
|
|
|
|
|
+ $filename = Str::replace('.docx', '_' . str_pad($i + 1, 4, 0, STR_PAD_LEFT) . '.docx', $filename);
|
|
|
|
|
+
|
|
|
|
|
+ $my_template->saveAs(public_path('exported/docx/') . $filename);
|
|
|
|
|
+ $file_for_arch[] = $filename;
|
|
|
|
|
+
|
|
|
if (($i % 10) == 0) {
|
|
if (($i % 10) == 0) {
|
|
|
$f = fopen($tmpfile, 'w+');
|
|
$f = fopen($tmpfile, 'w+');
|
|
|
- $percent = round(($i - 2) / ($this->products_count / 100), 0);
|
|
|
|
|
|
|
+ $percent = round(($i - 2) / (count($this->ids) / 100), 0);
|
|
|
fwrite($f, $percent);
|
|
fwrite($f, $percent);
|
|
|
fclose($f);
|
|
fclose($f);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- $filename = Str::replace('.docx', '_' . str_pad($i + 1, 4, 0, STR_PAD_LEFT) . '.docx', $this->filename);
|
|
|
|
|
- $my_template->saveAs(public_path('exported/docx/') . $filename);
|
|
|
|
|
- $file_for_arch[] = $filename;
|
|
|
|
|
|
|
+ $i++;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // creating zip file
|
|
|
$zip_file = public_path('exported/docx/') . Str::replace('.docx', '.zip', $this->filename);
|
|
$zip_file = public_path('exported/docx/') . Str::replace('.docx', '.zip', $this->filename);
|
|
|
$zip = new \ZipArchive();
|
|
$zip = new \ZipArchive();
|
|
|
$zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
|
|
$zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
|
|
@@ -91,6 +128,6 @@ class GenerateSeparateDocxJob implements ShouldQueue
|
|
|
}
|
|
}
|
|
|
unlink($tmpfile);
|
|
unlink($tmpfile);
|
|
|
|
|
|
|
|
- Log::notice('Generation finished: ' . $i . ' / ' . $this->products_count . ' Execution time: ' . microtime(true) - LARAVEL_START);
|
|
|
|
|
|
|
+ Log::notice('Generation finished: ' . $i . ' / ' . count($this->ids) . ' Execution time: ' . microtime(true) - LARAVEL_START);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|