module/Space/src/Space/Controller/SpaceItemController.php
/**
     * synchronize installation ppu
     * @return boolean
     * @throws \Exception
     */
synchronize installation ppu
@return boolean
@throws \Exception
synchronize installation ppu @return boolean @throws \ Exception
private function synchroniseInstallation ($productId=false) {
        try {
            $query = $this->getEntityManager()->createQuery("SELECT SUM(s.ippu * s.quantity) AS price FROM Space\Entity\System s WHERE s.space = {$this->getSpace()->getSpaceId()}");
            $sum = $query->getSingleScalarResult();
            $sum = round($sum, 2);
            $systems=$this->getEntityManager()->getRepository('Space\Entity\System')->findBySpaceId($this->getSpace()->getSpaceId(), array('locked'=>true,'type'=>100));
            if (!empty($systems)) {
                $systemInstall = array_shift($systems);
            }
            if (empty($sum)) {
                if (!empty($systemInstall)) {
                    $this->getEntityManager()->remove($systemInstall);
                    $this->getEntityManager()->flush();
                } 
            } else {
                if (empty($systemInstall)) {
                    $products=$this->getEntityManager()->getRepository('Product\Entity\Product')->findByType(100);
                    if (empty($products)) {
                        throw new \Exception('Could not find installation product');
                    }
                    $product = array_shift($products);
                    $systemInstall = new \Space\Entity\System();
                    $systemInstall
                            ->setSpace($this->getSpace())
                            ->setQuantity(1)
                            ->setIppu(0)
                            ->setHours(0)
                            ->setLux(0)
                            ->setOccupancy(0)
                            ->setLocked(true)
                            ->setLegacy(null)
                            ->setProduct($product);
                } else {
                    if ($systemInstall->getPpu()==$sum) {
                        return true;
                    }
                }
                $systemInstall
                        ->setCpu($sum)
                        ->setPpu($sum);
                $this->getEntityManager()->persist($systemInstall);
                $this->getEntityManager()->flush();
            }
            if (!empty($productId)) {
                $product = $this->getEntityManager()->find('Product\Entity\Product', $productId);
                if (!$product instanceof \Product\Entity\Product) {
                    throw new \Exception('Product could not be found');
                }
                if (!$product->getType()->getService()) {
                    $query = $this->getEntityManager()->createQuery("SELECT SUM(s.quantity) AS products FROM Space\Entity\System s JOIN s.space sp WHERE sp.project = {$this->getProject()->getProjectId()} AND s.product = {$productId}");
                    $sum = $query->getSingleScalarResult();
                    if (!empty($sum) && (count($product->getPricepoints())>0)) {
                        $ppu = $product->getppu();
                        $cpu = $product->getcpu();
                        $pricing_id = 'NULL';
                        foreach($product->getPricepoints() as $pricing) {
                            if (($sum>=$pricing->getMin()) && ($sum<=$pricing->getMax())) {
                                $ppu = $pricing->getppu();
                                $cpu = $pricing->getcpu();
                                $pricing_id = $pricing->getPricingId();
                                break;
                            }
                        }
                        $sql = "UPDATE `System` s "
                                . "INNER JOIN `Space` sp ON sp.`space_id` = s.`space_id` "
                                . "SET s.`cpu`={$cpu}, s.ppu={$ppu}, s.`pricing_id`= {$pricing_id} "
                                . "WHERE sp.`project_id`={$this->getProject()->getProjectId()} AND s.`product_id`={$productId}";
                        $stmt = $this->getEntityManager()->getConnection()->prepare($sql);
                        $stmt->execute();
                    }
                }
            }
            return true;
        } catch (\Exception $ex) {
            throw $ex;
            return false;
        }
    }
private function synchroniseInstallation ( $ productId = false ) { try { $ query = $ this -> getEntityManager ( ) -> createQuery ( " SELECT SUM(s.ippu * s.quantity) AS price FROM Space\Entity\System s WHERE s.space =  { $ this -> getSpace ( ) -> getSpaceId ( ) } " ) ; $ sum = $ query -> getSingleScalarResult ( ) ; $ sum = round ( $ sum , 2 ) ; $ systems = $ this -> getEntityManager ( ) -> getRepository ( 'Space\Entity\System' ) -> findBySpaceId ( $ this -> getSpace ( ) -> getSpaceId ( ) , array ( 'locked' => true , 'type' => 100 ) ) ; if ( ! empty ( $ systems ) ) { $ systemInstall = array_shift ( $ systems ) ; } if ( empty ( $ sum ) ) { if ( ! empty ( $ systemInstall ) ) { $ this -> getEntityManager ( ) -> remove ( $ systemInstall ) ; $ this -> getEntityManager ( ) -> flush ( ) ; } } else { if ( empty ( $ systemInstall ) ) { $ products = $ this -> getEntityManager ( ) -> getRepository ( 'Product\Entity\Product' ) -> findByType ( 100 ) ; if ( empty ( $ products ) ) { throw new \ Exception ( 'Could not find installation product' ) ; } $ product = array_shift ( $ products ) ; $ systemInstall = new \ Space \ Entity \ System ( ) ; $ systemInstall -> setSpace ( $ this -> getSpace ( ) ) -> setQuantity ( 1 ) -> setIppu ( 0 ) -> setHours ( 0 ) -> setLux ( 0 ) -> setOccupancy ( 0 ) -> setLocked ( true ) -> setLegacy ( null ) -> setProduct ( $ product ) ; } else { if ( $ systemInstall -> getPpu ( ) == $ sum ) { return true ; } } $ systemInstall -> setCpu ( $ sum ) -> setPpu ( $ sum ) ; $ this -> getEntityManager ( ) -> persist ( $ systemInstall ) ; $ this -> getEntityManager ( ) -> flush ( ) ; } if ( ! empty ( $ productId ) ) { $ product = $ this -> getEntityManager ( ) -> find ( 'Product\Entity\Product' , $ productId ) ; if ( ! $ product instanceof \ Product \ Entity \ Product ) { throw new \ Exception ( 'Product could not be found' ) ; } if ( ! $ product -> getType ( ) -> getService ( ) ) { $ query = $ this -> getEntityManager ( ) -> createQuery ( " SELECT SUM(s.quantity) AS products FROM Space\Entity\System s JOIN s.space sp WHERE sp.project =  { $ this -> getProject ( ) -> getProjectId ( ) }  AND s.product =  { $ productId } " ) ; $ sum = $ query -> getSingleScalarResult ( ) ; if ( ! empty ( $ sum ) && ( count ( $ product -> getPricepoints ( ) ) > 0 ) ) { $ ppu = $ product -> getppu ( ) ; $ cpu = $ product -> getcpu ( ) ; $ pricing_id = 'NULL' ; foreach ( $ product -> getPricepoints ( ) as $ pricing ) { if ( ( $ sum >= $ pricing -> getMin ( ) ) && ( $ sum <= $ pricing -> getMax ( ) ) ) { $ ppu = $ pricing -> getppu ( ) ; $ cpu = $ pricing -> getcpu ( ) ; $ pricing_id = $ pricing -> getPricingId ( ) ; break ; } } $ sql = " UPDATE `System` s  " . " INNER JOIN `Space` sp ON sp.`space_id` = s.`space_id`  " . " SET s.`cpu`= { $ cpu } , s.ppu= { $ ppu } , s.`pricing_id`=  { $ pricing_id } " . " WHERE sp.`project_id`= { $ this -> getProject ( ) -> getProjectId ( ) }  AND s.`product_id`= { $ productId } " ; $ stmt = $ this -> getEntityManager ( ) -> getConnection ( ) -> prepare ( $ sql ) ; $ stmt -> execute ( ) ; } } } return true ; } catch ( \ Exception $ ex ) { throw $ ex ; return false ; } }
synchronize installation ppu
 @return boolean
 @throws \Exception
synchronize installation ppu @return boolean @throws \ Exception
// synchronize product price point according to quantities // find total number of items