Skip to content

Commit

Permalink
prima implementazione gdxp 2
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Nov 26, 2023
1 parent 4fa0a48 commit 518589b
Show file tree
Hide file tree
Showing 20 changed files with 582 additions and 86 deletions.
43 changes: 9 additions & 34 deletions code/app/Console/Commands/FixDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Artisan;

use App\Config;
use App\User;
use App\Supplier;
use App\Gas;
use App\ModifierType;

class FixDatabase extends Command
{
Expand All @@ -37,38 +34,16 @@ public function handle()
Artisan::call('db:seed', ['--force' => true, '--class' => 'MovementTypesSeeder']);
Artisan::call('db:seed', ['--force' => true, '--class' => 'ModifierTypesSeeder']);

foreach(Supplier::all() as $supplier) {
$attachments = $supplier->attachments;

$pdf = $attachments->firstWhere('name', 'Listino PDF (autogenerato)');
if ($pdf) {
$pdf->url = route('suppliers.catalogue', ['id' => $supplier->id, 'format' => 'pdf']);
$pdf->save();
}

$csv = $attachments->firstWhere('name', 'Listino CSV (autogenerato)');
if ($csv) {
$csv->url = route('suppliers.catalogue', ['id' => $supplier->id, 'format' => 'csv']);
$csv->save();
}
$mod = ModifierType::where('name', 'Sconto')->where('system', true)->where('identifier', '')->first();
if ($mod) {
$mod->identifier = 'discount';
$mod->save();
}

User::query()->update(['tour' => true]);

foreach(Gas::all() as $gas) {
$registrations_info = $gas->public_registrations;
if (isset($registrations_info['manual']) == false) {
$registrations_info['manual'] = false;
$gas->setConfig('public_registrations', $registrations_info);
}

$satispay_info = (object) [
'public' => '',
'secret' => '',
'key' => '',
];

$gas->setConfig('satispay', $satispay_info);
$mod = ModifierType::where('name', 'Spese Trasporto')->where('system', true)->where('identifier', '')->first();
if ($mod) {
$mod->identifier = 'shipping';
$mod->save();
}
}
}
14 changes: 9 additions & 5 deletions code/app/Delivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,27 @@ private static function sortByPlace($bookings)
$a_place = $a->shipping_place;
$b_place = $b->shipping_place;

$ret = 0;

if (is_null($a_place) && is_null($b_place)) {
return $a->user->printableName() <=> $b->user->printableName();
$ret = $a->user->printableName() <=> $b->user->printableName();
}
else if (is_null($a_place)) {
return -1;
$ret = -1;
}
else if (is_null($b_place)) {
return 1;
$ret = 1;
}
else {
if ($a_place->id != $b_place->id) {
return $a_place->name <=> $b_place->name;
$ret = $a_place->name <=> $b_place->name;
}
else {
return $a->user->printableName() <=> $b->user->printableName();
$ret = $a->user->printableName() <=> $b->user->printableName();
}
}

return $ret;
});

return $bookings;
Expand Down
9 changes: 8 additions & 1 deletion code/app/Helpers/Components.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,14 @@ function formatUpdater($buttons, $params)

function formatInnerLastUpdater($component, $params)
{
$params['buttons'] = formatUpdater($params['buttons'], $params);
if (isset($params['inner_form_managed'])) {
unset($params['inner_form_managed']);
}
else {
$params['inner_form_managed'] = 'ongoing';
$params['buttons'] = formatUpdater($params['buttons'], $params);
}

return $params;
}

Expand Down
40 changes: 30 additions & 10 deletions code/app/Helpers/Percentages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,55 @@

function normalizePercentage($value)
{
if (is_null($value))
if (is_null($value)) {
return '';
else
}
else {
return str_replace(' ', '', $value);
}
}

function isPercentage($value)
{
return (strpos($value, '%') !== false);
}

function formatPercentage($value, $percentage)
{
if ($percentage) {
return sprintf('%s%%', $value);
}
else {
return (string) $value;
}
}

function printablePercentage($value)
{
if (empty($value))
if (empty($value)) {
return printablePriceCurrency(0);
}

if (isPercentage($value))
if (isPercentage($value)) {
return $value;
else
}
else {
return printablePriceCurrency($value);
}
}

function readPercentage($value)
{
if (empty($value))
if (empty($value)) {
return [printablePrice(0), false];
}

if (isPercentage($value))
if (isPercentage($value)) {
return [(float) $value, true];
else
}
else {
return [printablePrice($value), false];
}
}

function savingPercentage($request, $name)
Expand All @@ -52,10 +70,12 @@ function savingPercentage($request, $name)
$is_percentage = $request->input($name . '_percentage_type', 'euro');
}

if ($is_percentage == 'percentage')
if ($is_percentage == 'percentage') {
return $value . '%';
else
}
else {
return $value;
}
}

function applyPercentage($original, $percentage, $op = '-')
Expand Down
1 change: 0 additions & 1 deletion code/app/Http/Controllers/CategoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ private function updateRecursive($data, $parent, &$accumulator)

if (isset($category['id'])) {
if (in_array($category['id'], $accumulator)) {
Log::error('Itero ricorsivamente categoria già gestita, salto');
continue;
}

Expand Down
4 changes: 3 additions & 1 deletion code/app/Http/Controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private function readGdxpFile($path, $execute, $supplier_replace)

public function postGdxp(Request $request)
{
try {
// try {
$archivepath = '';
$working_dir = sys_get_temp_dir();
$step = $request->input('step', 'read');
Expand Down Expand Up @@ -181,10 +181,12 @@ public function postGdxp(Request $request)

return view('import.gdxpfinal', ['data' => $data]);
}
/*
}
catch(\Exception $e) {
Log::error(_i('Errore importando file GDXP: %s', $e->getMessage()));
return view('import.gdxperror');
}
*/
}
}
55 changes: 55 additions & 0 deletions code/app/Importers/GDXP/Attachments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Importers\GDXP;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;

use App\Product;

class Attachments extends GDXPImporter
{
public static function readXML($xml)
{
return null;
}

public static function importXML($xml, $replace)
{
return null;
}

public static function readJSON($json)
{
return null;
}

public static function importJSON($target, $json)
{
$contents = base64_decode($json->contents);

if (is_a($target, Product::class)) {
$filename = Str::random(30);
$fullpath = sprintf('%s/%s', gas_storage_path('app'), $filename);
file_put_contents($fullpath, $contents);

$mime = mime_content_type($fullpath);
if (str_starts_with($mime, 'image')) {
$target->picture = $fullpath;
}
else {
/*
TODO: al momento non viene gestito nessun altro file
allegato ai prodotti oltre all'immagine
*/
@unlink($fullpath);
}

return null;
}
else {
$existing = $target->attachments()->where('name', $json->name)->first();
return $target->attachByContents($json->name, $contents, $existing ? $existing->id : null);
}
}
}
18 changes: 18 additions & 0 deletions code/app/Importers/GDXP/GDXPImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ protected static function xmlDateFormat($string)
return sprintf('%d-%d-%d', $year, $month, $day);
}

protected static function handleTransformations($parent, $json)
{
if (isset($json->transformations)) {
foreach($json->transformations as $json_transformation) {
Transformations::importJSON($parent, $json_transformation);
}
}
}

protected static function handleAttachments($parent, $json)
{
if (isset($json->attachments)) {
foreach($json->attachments as $json_attachment) {
Attachments::importJSON($parent, $json_attachment);
}
}
}

abstract public static function readXML($xml);
abstract public static function readJSON($json);
}
14 changes: 7 additions & 7 deletions code/app/Importers/GDXP/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static function readJSON($json)
return $product;
}

public static function importJSON($master, $json, $replace)
public static function importJSON($supplier, $json, $replace)
{
if (is_null($replace)) {
$product = new Product();
Expand All @@ -132,6 +132,7 @@ public static function importJSON($master, $json, $replace)
$product = Product::findOrFail($replace);
}

$product->supplier_id = $supplier->id;
$product->name = $json->name;
$product->supplier_code = $json->sku ?? '';
$product->description = $json->description ?? '';
Expand All @@ -148,12 +149,6 @@ public static function importJSON($master, $json, $replace)
$product->multiple = (float) ($json->orderInfo->mulQty ?? 0);
$product->max_available = (float) ($json->orderInfo->availableQty ?? 0);

/*
TODO: agganciare un modificatore che rappresenti il costo di
trasporto statico, col valore di
$json->orderInfo->shippingCost
*/

$name = $json->category ?? '';
if (!empty($name)) {
$category = Category::firstOrCreate(['name' => $name]);
Expand All @@ -179,6 +174,11 @@ public static function importJSON($master, $json, $replace)
$product->vat_rate_id = $vat_rate->id;
}

$product->save();

self::handleTransformations($product, $json);
self::handleAttachments($supplier, $json);

return $product;
}
}
7 changes: 4 additions & 3 deletions code/app/Importers/GDXP/Suppliers.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,12 @@ public static function importJSON($master, $json, $replace)
$ex_product = $supplier->products()->where('name', $pname)->first();
}

$product = Products::importJSON($master, $json_product, $ex_product->id ?? null);
$product->supplier_id = $supplier->id;
$product->save();
Products::importJSON($supplier, $json_product, $ex_product->id ?? null);
}

self::handleTransformations($supplier, $json);
self::handleAttachments($supplier, $json);

return $supplier;
}
}
Loading

0 comments on commit 518589b

Please sign in to comment.