SyncFromInventory.php 5.49 KB
Newer Older
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
<?php

/**
 * gredu_labs.
 *
 * @link https://github.com/eellak/gredu_labs for the canonical source repository
 *
 * @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
 * @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
 */

namespace SchSync;

use Exception;
use GrEduLabs\Schools\Service\AssetServiceInterface;
use GrEduLabs\Schools\Service\LabServiceInterface;
use GrEduLabs\Schools\Service\SchoolServiceInterface;
use Psr\Log\LoggerInterface;
use RedBeanPHP\R;
use SchInventory\ServiceInterface as InventoryService;

class SyncFromInventory
{

    /**
     *
     * @var LabServiceInterface
     */
    protected $labService;

    /**
     *
     * @var AssetServiceInterface
     */
    protected $assetService;

    /**
     *
     * @var InventoryService
     */
    protected $inventoryService;

    /**
     *
     * @var SchoolServiceInterface
     */
    protected $schoolService;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     *
     * @var array
     */
    private $categoryMap;

    public function __construct(
        LabServiceInterface $labService,
        AssetServiceInterface $assetService,
        InventoryService $inventoryService,
        SchoolServiceInterface $schoolService,
        LoggerInterface $logger,
        array $categoryMap = []
    ) {
        $this->labService       = $labService;
        $this->assetService     = $assetService;
        $this->inventoryService = $inventoryService;
        $this->schoolService    = $schoolService;
        $this->logger           = $logger;
        $this->categoryMap      = $categoryMap;
    }

    public function __invoke($school_id)
    {
        $school = $this->schoolService->getSchool($school_id);
        try {
            $equipment = $this->inventoryService->getUnitEquipment($school['registry_no']);
        } catch (Exception $e) {
            $this->logger->error(sprintf('Problem retrieving assets from inventory for school %s', $school_id));
            $this->logger->debug('Exception', [$e->getMessage(), $e->getTraceAsString()]);

            return false;
        }
        $labTypes   = $this->getLabTypes();
        $assetTypes = $this->getAssetTypes();

        try {
            $locations = $this->getLocations($school_id, $equipment, $labTypes, $assetTypes);
            R::storeAll($locations);
            $this->logger->info(sprintf('Add assets from inventory for school %s', $school_id));

            return $this->labService->getLabsBySchoolId($school_id);
        } catch (Exception $e) {
            $this->logger->error(sprintf('Problem inserting assets for school %s in database', $school_id));
            $this->logger->debug('Exception', [$e->getMessage(), $e->getTraceAsString()]);

            return false;
        }
    }

    private function getLabTypes()
    {
        return array_reduce($this->labService->getLabTypes(), function ($map, $type) {
            $map[trim($type['name'])] = $type['id'];

            return $map;
        }, []);
    }

    private function getAssetTypes()
    {
        return array_reduce($this->assetService->getAllItemCategories(), function ($map, $type) {
            $map[trim($type['name'])] = $type['id'];

            return $map;
        }, []);
    }

    private function getLocations($school_id, array $equipment, array $labTypes, array $assetTypes)
    {
        return array_reduce($equipment, function ($uniq, $item) use ($school_id, $labTypes, $assetTypes) {
            if (!isset($uniq[$item['location.id']])) {
                $locationName = $item['location.name'];
                $locationId = $item['location.id'];

                $lab = R::findOne('lab', ' school_id = ? AND (inventory_key = ? OR name = ?)', [
                    $school_id,
                    $locationId,
                    $locationName,
                ]);

                if (null !== $lab) {
                    return $uniq;
                }

                $detected = reset(array_filter(array_keys($labTypes), function ($type) use ($locationName) {
                    return false !== stripos($locationName, $type) ||
                        false !== stripos($type, $locationName);
                }));
                $labType = $detected ? $labTypes[$detected] : end($labTypes);
                $data = [
                    'school_id'     => (int) $school_id,
                    'name'          => $locationName,
                    'labtype_id'    => (int) $labType,
                    'inventory_key' => (int) $locationId,
                ];
                $lab = R::dispense('lab');
                $lab->import($data);
                $uniq[$item['location.id']] = $lab;
            }

            $categoryName = $item['item_template.category.name'];

            $type = reset(array_filter(array_keys($this->categoryMap), function ($type) use ($categoryName) {
                return $type == $categoryName;
            }));
            $type = ($type) ? $assetTypes[$this->categoryMap[$type]] : false;

            if ($type !== false) {
                if (!isset($uniq[$item['location.id']]->ownSchoolAsset[$type])) {
                    $asset = R::dispense('schoolasset');
                    $asset->school_id = (int) $school_id;
                    $asset->itemcategory_id = (int) $type;
                    $uniq[$item['location.id']]->ownSchoolAsset[$type] = $asset;
                }
                $uniq[$item['location.id']]->ownSchoolAsset[$type]->qty += 1;
            }

            return $uniq;
        }, []);
    }
}