Example #1
0
    protected function addOrder($Data, $clientId = 0, $orginalOrderId = NULL)
    {
        $selectedOption = Session::getActiveDispatchmethodOption();
        $globalPrice = 0;
        $globalNetto = 0;
        $price = 0;
        $sql = 'INSERT INTO `order` (
					price, 
					dispatchmethodprice, 
					globalprice, 
					dispatchmethodname, 
					paymentmethodname, 
					orderstatusid,
					dispatchmethodid, 
					paymentmethodid, 
					clientid, 
					globalpricenetto, 
					viewid, 
					pricebeforepromotion, 
					currencyid, 
					currencysymbol, 
					currencyrate,
					rulescartid,
					sessionid,
					customeropinion
				)
				VALUES (
					:price, 
					:dispatchmethodprice, 
					:globalprice, 
					:dispatchmethodname, 
					:paymentmethodname,
					(SELECT idorderstatus FROM orderstatus WHERE `default` = 1), 
					:dispatchmethodid, 
					:paymentmethodid, 
					:clientid, 
					:globalpricenetto, 
					:viewid, 
					:pricebeforepromotion, 
					:currencyid, 
					:currencysymbol, 
					:currencyrate,
					:rulescartid,
					:sessionid,
					:customeropinion
				)';
        $stmt = Db::getInstance()->prepare($sql);
        $stmt->bindValue('dispatchmethodprice', $Data['dispatchmethod']['dispatchmethodcost']);
        $stmt->bindValue('dispatchmethodname', $Data['dispatchmethod']['dispatchmethodname']);
        $stmt->bindValue('dispatchmethodid', $Data['dispatchmethod']['dispatchmethodid']);
        $stmt->bindValue('paymentmethodname', $Data['payment']['paymentmethodname']);
        $stmt->bindValue('paymentmethodid', $Data['payment']['idpaymentmethod']);
        $stmt->bindValue('clientid', $clientId);
        $stmt->bindValue('sessionid', session_id());
        $stmt->bindValue('customeropinion', $Data['customeropinion']);
        $shopCurrency = Session::getActiveShopCurrencyId();
        $clientCurrency = Session::getActiveCurrencyId();
        if ($shopCurrency !== $clientCurrency) {
            $stmt->bindValue('currencyid', $clientCurrency);
            $stmt->bindValue('currencysymbol', Session::getActiveCurrencySymbol());
            $stmt->bindValue('currencyrate', Session::getActiveCurrencyRate());
        } else {
            $stmt->bindValue('currencyid', $shopCurrency);
            $stmt->bindValue('currencysymbol', $this->layer['currencysymbol']);
            $stmt->bindValue('currencyrate', Session::getActiveCurrencyRate());
        }
        if (isset($Data['priceWithDispatchMethodPromo']) && $Data['priceWithDispatchMethodPromo'] > 0) {
            $stmt->bindValue('pricebeforepromotion', $Data['priceWithDispatchMethod']);
            if ($globalPrice == 0) {
                $globalPrice = $Data['priceWithDispatchMethodPromo'];
                $globalNetto = $Data['priceWithDispatchMethodNettoPromo'];
                $price = $Data['globalPricePromo'];
            }
        } else {
            $stmt->bindValue('pricebeforepromotion', 0);
        }
        if ($globalPrice == 0 || $globalNetto == 0) {
            $globalPrice = $Data['priceWithDispatchMethod'];
            $globalNetto = $Data['globalPriceWithoutVat'];
            $price = $Data['globalPrice'];
        }
        if (isset($Data['rulescartid']) && !empty($Data['rulescartid'])) {
            $stmt->bindValue('rulescartid', $Data['rulescartid']);
        } else {
            $stmt->bindValue('rulescartid', NULL);
        }
        $stmt->bindValue('globalprice', $globalPrice);
        $stmt->bindValue('globalpricenetto', $globalNetto);
        $stmt->bindValue('price', $price);
        $stmt->bindValue('viewid', Helper::getViewId());
        try {
            $stmt->execute();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
        return Db::getInstance()->lastInsertId();
    }
Example #2
0
    protected function addUpdateProduct($Data)
    {
        Db::getInstance()->beginTransaction();
        $viewid = Helper::getViewId() > 0 ? Helper::getViewId() : $this->registry->loader->getParam('idview');
        $sql = 'SELECT idproduct FROM product WHERE migrationid = :migrationid';
        $stmt = Db::getInstance()->prepare($sql);
        $stmt->bindValue('migrationid', $Data['id']);
        $stmt->execute();
        $rs = $stmt->fetch();
        $availablityid = NULL;
        if (!empty($Data['availablity'])) {
            $sql = 'SELECT
					availablityid
				FROM
					availablitytranslation
				WHERE
					name = :name
				AND
					languageid = :languageid';
            $stmt = Db::getInstance()->prepare($sql);
            $stmt->bindValue('name', $Data['availablity']);
            $stmt->bindValue('languageid', Helper::getLanguageId());
            $stmt->execute();
            $availablityid = $stmt->fetchColumn();
            // Add
            if (empty($availablityid)) {
                $availablityid = App::getModel('availablity')->addAvailablity(array('name' => array(Helper::getLanguageId() => $Data['availablity']), 'description' => array(Helper::getLanguageId() => '')));
            }
        }
        if ($rs) {
            $idproduct = $rs['idproduct'];
            $sql = 'UPDATE product SET
	    				ean				=	:ean,
	    				delivelercode	=	:ean,
	    				barcode			=	:ean,
						buyprice		=	:buyprice,
						sellprice		=	:sellprice,
						weight			=	:weight,
						enable			= 	:enable,
						availablityid		=	:availablityid,
						stock			= 	:stock,
						promotion		= 	:promotion,
						discountprice		= 	:discountprice
					WHERE idproduct = :id
			';
            $stmt = Db::getInstance()->prepare($sql);
            $stmt->bindValue('ean', $Data['ean']);
            $stmt->bindValue('buyprice', $Data['buyprice']);
            $stmt->bindValue('sellprice', $Data['sellprice']);
            $stmt->bindValue('stock', $Data['stock']);
            $stmt->bindValue('weight', $Data['weight']);
            $stmt->bindValue('availablityid', $availablityid);
            $stmt->bindValue('enable', $Data['enable']);
            $stmt->bindValue('promotion', isset($Data['promotion']) ? $Data['promotion'] : 0);
            $stmt->bindValue('discountprice', isset($Data['discountprice']) ? $Data['discountprice'] : 0);
            $stmt->bindValue('id', $idproduct);
            $stmt->execute();
            $sql = 'UPDATE producttranslation SET
						name = :name,
						description = :description,
						shortdescription = :shortdescription,
						seo = :seo,
						keyword_title = :keyword_title,
						keyword = :keyword,
						keyword_description = :keyword_description
					WHERE productid = :productid AND languageid = :languageid
			';
            $stmt = Db::getInstance()->prepare($sql);
            $stmt->bindValue('productid', $idproduct);
            $stmt->bindValue('name', $Data['name']);
            $stmt->bindValue('description', $Data['description']);
            $stmt->bindValue('shortdescription', $Data['shortdescription']);
            $stmt->bindValue('keyword_title', $Data['keyword_title']);
            $stmt->bindValue('keyword', $Data['keyword']);
            $stmt->bindValue('keyword_description', $Data['keyword_description']);
            $stmt->bindValue('seo', str_replace('/', '', strtolower(Core::clearSeoUTF($Data['name']))));
            $stmt->bindValue('languageid', Helper::getLanguageId());
            try {
                $stmt->execute();
            } catch (Exception $e) {
                throw new CoreException(_('ERR_PRODUCT_UPDATE'), 112, $e->getMessage());
            }
            Db::getInstance()->commit();
        } else {
            $vatValues = array_flip(App::getModel('vat')->getVATValuesAll());
            $sql = 'INSERT INTO product SET
	    				ean				=	:ean,
	    				delivelercode	=	:ean,
	    				barcode			=	:ean,
						buyprice		=	:buyprice,
						sellprice		=	:sellprice,
						buycurrencyid   =	:buycurrencyid,
						sellcurrencyid  =	:sellcurrencyid,
						weight			=	:weight,
						availablityid		=	:availablityid,
						vatid			=	:vatid,
						producerid		=	(SELECT idproducer FROM producer WHERE migrationid = :producerid),
						enable			= 	:enable,
						stock			= 	:stock,
						promotion		= 	:promotion,
						discountprice		= 	:discountprice,
						migrationid		=   :migrationid
			';
            $stmt = Db::getInstance()->prepare($sql);
            $stmt->bindValue('ean', $Data['ean']);
            $stmt->bindValue('buyprice', $Data['buyprice']);
            $stmt->bindValue('sellprice', $Data['sellprice']);
            $stmt->bindValue('buycurrencyid', Session::getActiveShopCurrencyId());
            $stmt->bindValue('sellcurrencyid', Session::getActiveShopCurrencyId());
            $stmt->bindValue('stock', $Data['stock']);
            $stmt->bindValue('weight', $Data['weight']);
            $stmt->bindValue('availablityid', $availablityid);
            if (isset($vatValues[number_format($Data['vatvalue'], 2)])) {
                $stmt->bindValue('vatid', $vatValues[number_format($Data['vatvalue'], 2)]);
            } else {
                $stmt->bindValue('vatid', 2);
            }
            $stmt->bindValue('migrationid', $Data['id']);
            $stmt->bindValue('producerid', $Data['producerid']);
            $stmt->bindValue('enable', $Data['enable']);
            $stmt->bindValue('stock', $Data['stock']);
            $stmt->bindValue('promotion', isset($Data['promotion']) ? $Data['promotion'] : 0);
            $stmt->bindValue('discountprice', isset($Data['discountprice']) ? $Data['discountprice'] : 0);
            $stmt->execute();
            $idproduct = Db::getInstance()->lastInsertId();
            $sql = 'INSERT INTO producttranslation SET
						productid = :productid,
						name = :name,
						description = :description,
						shortdescription = :shortdescription,
						seo = :seo,
						keyword_title = :keyword_title,
						keyword = :keyword,
						keyword_description = :keyword_description,
						languageid = :languageid
			';
            $stmt = Db::getInstance()->prepare($sql);
            $stmt->bindValue('productid', $idproduct);
            $stmt->bindValue('name', $Data['name']);
            $stmt->bindValue('description', $Data['description']);
            $stmt->bindValue('shortdescription', $Data['shortdescription']);
            $stmt->bindValue('keyword_title', $Data['keyword_title']);
            $stmt->bindValue('keyword', $Data['keyword']);
            $stmt->bindValue('keyword_description', $Data['keyword_description']);
            $stmt->bindValue('seo', str_replace('/', '', strtolower(Core::clearSeoUTF($Data['name']))));
            $stmt->bindValue('languageid', Helper::getLanguageId());
            try {
                $stmt->execute();
            } catch (Exception $e) {
                throw new CoreException(_('ERR_PRODUCT_UPDATE'), 112, $e->getMessage());
            }
            foreach ($Data['categories'] as $category) {
                $sql = 'INSERT INTO productcategory (productid, categoryid)
						SELECT :productid, idcategory FROM category WHERE migrationid = :categoryid';
                $stmt = Db::getInstance()->prepare($sql);
                $stmt->bindValue('productid', $idproduct);
                $stmt->bindValue('categoryid', $category);
                try {
                    $stmt->execute();
                } catch (Exception $e) {
                    throw new CoreException(_('ERR_PRODUCT_CATEGORY_UPDATE'), 112, $e->getMessage());
                }
            }
            if (isset($Data['recursivecategories'])) {
                $sql = 'SELECT categoryid FROM productcategory WHERE productid = :productid';
                $stmt = Db::getInstance()->prepare($sql);
                $stmt->bindValue('productid', $idproduct);
                try {
                    $stmt->execute();
                } catch (Exception $e) {
                    throw new CoreException($e->getMessage());
                }
                while ($rs = $stmt->fetch()) {
                    $sql2 = 'SELECT ancestorcategoryid FROM categorypath WHERE categoryid = :categoryid';
                    $stmt2 = Db::getInstance()->prepare($sql2);
                    $stmt2->bindValue('categoryid', $rs['categoryid']);
                    try {
                        $stmt2->execute();
                    } catch (Exception $e) {
                        throw new CoreException($e->getMessage());
                    }
                    while ($rs2 = $stmt2->fetch()) {
                        $sql3 = 'INSERT INTO productcategory SET productid = :productid, categoryid = :categoryid
								 ON DUPLICATE KEY UPDATE productid = :productid';
                        $stmt3 = Db::getInstance()->prepare($sql3);
                        $stmt3->bindValue('productid', $idproduct);
                        $stmt3->bindValue('categoryid', $rs2['ancestorcategoryid']);
                        try {
                            $stmt3->execute();
                        } catch (Exception $e) {
                            throw new CoreException($e->getMessage());
                        }
                    }
                }
            }
            Db::getInstance()->commit();
            $mainphoto = 1;
            foreach ($Data['photos'] as $name => $url) {
                $sql = 'SELECT idfile FROM file WHERE name = :name';
                $stmt = Db::getInstance()->prepare($sql);
                $stmt->bindValue('name', Core::clearUTF($name));
                $stmt->execute();
                $rs = $stmt->fetch();
                if ($rs) {
                    $photoid = $rs['idfile'];
                } else {
                    $photoid = App::getModel('gallery')->getRemoteImage($url, $name);
                }
                $sql = 'INSERT INTO productphoto (productid, mainphoto, photoid)
						VALUES (:productid, :mainphoto, :photoid)';
                $stmt = Db::getInstance()->prepare($sql);
                $stmt->bindValue('productid', $idproduct);
                $stmt->bindValue('mainphoto', $mainphoto);
                $stmt->bindValue('photoid', $photoid);
                try {
                    $stmt->execute();
                } catch (Exception $e) {
                    throw new CoreException(_('ERR_PRODUCT_PHOTO_UPDATE'), 112, $e->getMessage());
                }
                $mainphoto = 0;
            }
            if (empty($Data['attributes'])) {
                return;
            }
            $productModel = App::getModel('product');
            $attributes = array();
            $attributesData = array();
            foreach ($Data['attributes'] as $attributeName => $attributeValue) {
                foreach ($attributeValue as $value) {
                    $sql = "\n\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\tAPV.idattributeproductvalue,\n\t\t\t\t\t\t\tAPV.attributeproductid\n\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\tattributeproductvalue APV\n\t\t\t\t\t\tINNER JOIN\n\t\t\t\t\t\t\tattributeproduct AP ON AP.idattributeproduct = APV.attributeproductid\n\t\t\t\t\t\tINNER JOIN\n\t\t\t\t\t\t\tattributegroup AG ON AG.attributeproductid = AP.idattributeproduct\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tAP.name = :attribute\n\t\t\t\t\t\tAND\n\t\t\t\t\t\t\tAG.attributegroupnameid = :groupid\n\t\t\t\t\t\tAND\n\t\t\t\t\t\t\tAPV.name = :attributevaluename\n\t\t\t\t\t";
                    $stmt = Db::getInstance()->prepare($sql);
                    $stmt->bindValue('attribute', $attributeName);
                    $stmt->bindValue('groupid', Session::getActiveMigrationGroupId());
                    $stmt->bindValue('attributevaluename', $value['name']);
                    try {
                        $stmt->execute();
                    } catch (Exception $e) {
                        throw new CoreException($e->getMessage());
                    }
                    $attributeData = $stmt->fetch();
                    $attributes[$attributeData['attributeproductid']][] = $attributeData['idattributeproductvalue'];
                    $attributesData[$attributeData['idattributeproductvalue']] = array('modifier' => $value['price'], 'attributes' => array($attributeData['attributeproductid'] => $attributeData['idattributeproductvalue']), 'photo' => isset($value['photo']) ? $value['photo'] : NULL);
                }
            }
            $i = 0;
            $allAttributes = array();
            if (!empty($attributes)) {
                $c = $productModel->doCartesianProduct($attributes);
                if (!is_array($c[0])) {
                    $c = array_chunk($c, 1);
                }
                foreach ($c as $attribute) {
                    $sum = 0;
                    $attrs = array();
                    foreach ($attribute as $attr) {
                        $sum += $attributesData[$attr]['modifier'];
                        $attrs += $attributesData[$attr]['attributes'];
                    }
                    $photoid = NULL;
                    foreach ($attribute as $photo) {
                        if ($attributesData[$photo]['photo']) {
                            $filename = key($attributesData[$photo]['photo']);
                            $fileurl = current($attributesData[$photo]['photo']);
                            if (empty($filename) || empty($fileurl)) {
                                continue;
                            }
                            $sql = 'SELECT idfile FROM file WHERE name = :name';
                            $stmt = Db::getInstance()->prepare($sql);
                            $stmt->bindValue('name', Core::clearUTF($filename));
                            try {
                                $stmt->execute();
                            } catch (Exception $e) {
                                throw new CoreException($e->getMessage());
                            }
                            $rs = $stmt->fetch();
                            if ($rs) {
                                $photoid = $rs['idfile'];
                            } else {
                                $photoid = App::getModel('gallery')->getRemoteImage($fileurl, $filename);
                            }
                            break;
                        }
                    }
                    $allAttributes['new-' . $i] = array('suffix' => 2, 'stock' => 0, 'symbol' => '', 'modifier' => $sum, 'status' => 1, 'deletable' => 0, 'weight' => 0, 'attributes' => $attrs, 'availablity' => $availablityid, 'photo' => $photoid);
                    ++$i;
                }
                $attributes = array('set' => Session::getActiveMigrationGroupId()) + $allAttributes;
                $productModel->addAttributesProducts($attributes, $idproduct);
            }
        }
    }