getProduct()->getCode(); } public static function getCurrencyCode(AddPrice $addPrice) { return $addPrice->getCurrency()->getShortcut(); } public static function getTaxRate(AddPrice $addPrice) { return $addPrice->getOptVat(); } public static function setProductCode(AddPrice $addPrice, $value) { $id = self::getProductIdByCode($value); $addPrice->setProductId($id); } public static function setCurrencyCode(AddPrice $addPrice, $value) { $id = self::getCurrencyIdByCode($value); $addPrice->setCurrencyId($id); } public static function setTaxRate(AddPrice $addPrice, $value) { $id = self::getTaxIdByRate($value); $addPrice->setTaxId($id); $addPrice->setOptVat($value); } public static function setPriceNetto(AddPrice $addPrice, $value, $logger, $data) { $addPrice->setPriceNetto($value); if (!isset($data['price_brutto'])) { $addPrice->setPriceBrutto(null); } } public static function setOldPriceNetto(AddPrice $addPrice, $value, $logger, $data) { $addPrice->setOldPriceNetto($value); if (!isset($data['old_price_brutto'])) { $addPrice->setOldPriceBrutto(null); } } public static function setPriceBrutto(AddPrice $addPrice, $value, $logger, $data) { $addPrice->setPriceBrutto($value); if (!isset($data['price_netto'])) { $addPrice->setPriceNetto(null); } } public static function setOldPriceBrutto(AddPrice $addPrice, $value, $logger, $data) { $addPrice->setOldPriceBrutto($value); if (!isset($data['old_price_netto'])) { $addPrice->setOldPriceNetto(null); } } public static function importValidateProductCode($code, $primaryKey, $data, stPropelImporter $importer) { if (!self::getProductIdByCode($code)) { $i18n = sfContext::getInstance()->getI18n(); $importer->getLogger()->add(null, $i18n->__('Product o kodzie "%%code%%" nie istnieje', array('%%code%%' => $code)), stImportExportLog::$NOTICE); return false; } return true; } public static function importValidateCurrencyCode($code, $primaryKey, $data, stPropelImporter $importer) { if (!self::getCurrencyIdByCode($code)) { $i18n = sfContext::getInstance()->getI18n(); $importer->getLogger()->add(null, $i18n->__('Waluta "%%code%%" nie istnieje', array('%%code%%' => $code)), stImportExportLog::$NOTICE); return false; } return true; } public static function importValidateTaxRate($taxRate, $primaryKey, $data, stPropelImporter $importer) { if (!self::getTaxIdByRate($taxRate)) { $i18n = sfContext::getInstance()->getI18n(); $importer->getLogger()->add(null, $i18n->__('Stawka VAT "%%tax_rate%%" nie istnieje', array('%%tax_rate%%' => $taxRate)), stImportExportLog::$NOTICE); return false; } return true; } public static function ImportValidatePriceNetto($value, $primaryKey, $data, stPropelImporter $importer) { return self::validateNettoBruttoField('price_netto', 'price_brutto', $data, $importer); } public static function ImportValidatePriceBrutto($value, $primaryKey, $data, stPropelImporter $importer) { return self::validateBruttoNettoField('price_brutto', 'price_netto', $data, $importer); } public static function ImportValidateOldPriceNetto($value, $primaryKey, $data, stPropelImporter $importer) { return self::validateNettoBruttoField('old_price_netto', 'old_price_brutto', $data, $importer); } public static function ImportValidateOldPriceBrutto($value, $primaryKey, $data, stPropelImporter $importer) { return self::validateBruttoNettoField('old_price_brutto', 'old_price_netto', $data, $importer); } public static function ImportValidateWholesaleANetto($value, $primaryKey, $data, stPropelImporter $importer) { return self::validateNettoBruttoField('wholesale_a_netto', 'wholesale_a_brutto', $data, $importer); } public static function ImportValidateWholesaleABrutto($value, $primaryKey, $data, stPropelImporter $importer) { return self::validateBruttoNettoField('wholesale_a_brutto', 'wholesale_a_netto', $data, $importer); } public static function ImportValidateWholesaleBNetto($value, $primaryKey, $data, stPropelImporter $importer) { return self::validateNettoBruttoField('wholesale_b_netto', 'wholesale_b_brutto', $data, $importer); } public static function ImportValidateWholesaleBBrutto($value, $primaryKey, $data, stPropelImporter $importer) { return self::validateBruttoNettoField('wholesale_b_brutto', 'wholesale_b_netto', $data, $importer); } public static function ImportValidateWholesaleCNetto($value, $primaryKey, $data, stPropelImporter $importer) { return self::validateNettoBruttoField('wholesale_c_netto', 'wholesale_c_brutto', $data, $importer); } public static function ImportValidateWholesaleCBrutto($value, $primaryKey, $data, stPropelImporter $importer) { return self::validateBruttoNettoField('wholesale_c_brutto', 'wholesale_c_netto', $data, $importer); } protected static function validatePriceFormat($context, $value) { $nv = new sfNumberValidator(); $nv->initialize($context, array( 'min' => 0, 'type' => 'any' )); return empty($value) || $nv->execute($value, $error) == true; } protected static function validateNettoBruttoField($nettoFieldName, $bruttoFieldName, array $data, stPropelImporter $importer) { $netto = $data[$nettoFieldName]; list($label) = explode('::', $importer->getUserName($nettoFieldName)); $context = sfContext::getInstance(); if (!self::validatePriceFormat($context, $netto)) { $message = $context->getI18n()->__('%%label%% "%%price%%" posiada nieprawidłowy format (poprawny format: 10, 10.00).', array('%%price%%' => $netto, '%%label%%' => $label)); $importer->getLogger()->add(null, $message, 2); return false; } $brutto = isset($data[$bruttoFieldName]) ? $data[$bruttoFieldName] : null; if (!empty($netto) && $brutto && !(stPrice::round($netto) == stPrice::extract($brutto, $data['tax_rate']) || stPrice::round($brutto) == stPrice::calculate($netto, $data['tax_rate']))) { $importer->getLogger()->add(null, $context->getI18n()->__('%%label%% nie pokrywa się z ceną brutto (jeżeli chcesz zmienić cenę netto usuń cenę brutto).', array('%%label%%' => $label)), 2); return false; } return true; } protected static function validateBruttoNettoField($bruttoFieldName, $nettoFieldName, array $data, stPropelImporter $importer) { $brutto = $data[$bruttoFieldName]; list($label) = explode('::', $importer->getUserName($bruttoFieldName)); $context = sfContext::getInstance(); if (!self::validatePriceFormat($context, $brutto)) { $message = $context->getI18n()->__('%%label%% "%%price%%" posiada nieprawidłowy format (poprawny format: 10, 10.00).', array('%%price%%' => $brutto, '%%label%%' => $label)); $importer->getLogger()->add(null, $message, 2); return false; } $netto = isset($data[$nettoFieldName]) ? $data[$nettoFieldName] : null; if (!empty($brutto) && $netto && !(stPrice::round($netto) == stPrice::extract($brutto, $data['tax_rate']) || stPrice::round($brutto) == stPrice::calculate($netto, $data['tax_rate']))) { $importer->getLogger()->add(null, $context->getI18n()->__('%%label%% nie pokrywa się z ceną netto (jeżeli chcesz zmienić cenę brutto usuń cenę netto).', array('%%label%%' => $label)), 2); return false; } return true; } protected static function getProductIdByCode($code) { if (!isset(self::$productMap[$code]) && !array_key_exists($code, self::$productMap)) { $c = new Criteria(); $c->addSelectColumn(ProductPeer::ID); $c->add(ProductPeer::CODE, $code); $rs = ProductPeer::doSelectRS($c); self::$productMap[$code] = $rs && $rs->next() ? $rs->getInt(1) : null; } return self::$productMap[$code]; } protected static function getCurrencyIdByCode($code) { if (!isset(self::$currencyMap[$code]) && !array_key_exists($code, self::$currencyMap)) { $currency = CurrencyPeer::retrieveByIso($code); self::$currencyMap[$code] = $currency ? $currency->getId() : null; } return self::$currencyMap[$code]; } protected static function getTaxIdByRate($rate) { if (!isset(self::$taxMap[$rate]) && !array_key_exists($rate, self::$taxMap)) { $tax = TaxPeer::retrieveByTax($rate); self::$taxMap[$rate] = $tax ? $tax->getId() : null; } return self::$taxMap[$rate]; } }