Skip to content

Commit

Permalink
Merge pull request #285 from oc-shopaholic/develop
Browse files Browse the repository at this point in the history
Release version 1.27.1
  • Loading branch information
kharanenka authored May 15, 2020
2 parents 5006103 + 599d4f3 commit 82a3941
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 30 deletions.
29 changes: 28 additions & 1 deletion classes/import/ImportOfferModelFromCSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected function prepareImportData()
$this->setProductField();
$this->setQuantityField();
$this->setMeasureField();
$this->setMeasureOfUnitField();

$this->initPreviewImage();
$this->initImageList();
Expand Down Expand Up @@ -100,7 +101,7 @@ protected function setQuantityField()
/**
* Set measure filed value
*/
protected function setMeasureField()
protected function setMeasureOfUnitField()
{
$sMeasure = array_get($this->arImportData, 'measure_of_unit');
array_forget($this->arImportData, 'measure_of_unit');
Expand All @@ -122,4 +123,30 @@ protected function setMeasureField()

$this->arImportData['measure_of_unit_id'] = $obMeasure->id;
}

/**
* Set measure filed value
*/
protected function setMeasureField()
{
$sMeasure = array_get($this->arImportData, 'measure_id');
array_forget($this->arImportData, 'measure_id');
if ($sMeasure === null) {
return;
}

if (empty($sMeasure)) {
$this->arImportData['measure_id'] = null;
return;
}

$obMeasure = Measure::getByName($sMeasure)->first();
if (empty($obMeasure)) {
$obMeasure = Measure::create([
'name' => $sMeasure,
]);
}

$this->arImportData['measure_id'] = $obMeasure->id;
}
}
1 change: 1 addition & 0 deletions classes/import/ImportOfferModelFromXML.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function getFields() : array
'height' => Lang::get('lovata.toolbox::lang.field.height'),
'length' => Lang::get('lovata.toolbox::lang.field.length'),
'width' => Lang::get('lovata.toolbox::lang.field.width'),
'measure_id' => Lang::get('lovata.shopaholic::lang.field.measure'),
'quantity_in_unit' => Lang::get('lovata.shopaholic::lang.field.quantity_in_unit'),
'measure_of_unit' => Lang::get('lovata.shopaholic::lang.field.measure_of_unit'),
'preview_text' => Lang::get('lovata.toolbox::lang.field.preview_text'),
Expand Down
4 changes: 3 additions & 1 deletion controllers/offers/config_import_export.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import:
label: 'lovata.toolbox::lang.field.length'
width:
label: 'lovata.toolbox::lang.field.width'
measure_id:
label: 'lovata.shopaholic::lang.field.measure'
quantity_in_unit:
label: 'lovata.shopaholic::lang.field.quantity_in_unit'
measure_of_unit:
Expand All @@ -45,4 +47,4 @@ import:
deactivate:
label: 'lovata.toolbox::lang.field.import_deactivate'
comment: 'lovata.toolbox::lang.field.import_deactivate_description'
type: checkbox
type: checkbox
85 changes: 58 additions & 27 deletions models/Offer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Kharanenka\Scope\ExternalIDField;
use Kharanenka\Scope\NameField;

use Lovata\Toolbox\Classes\Helper\PriceHelper;
use Lovata\Toolbox\Traits\Helpers\TraitCached;
use Lovata\Toolbox\Traits\Helpers\PriceHelperTrait;

Expand Down Expand Up @@ -285,20 +286,12 @@ public function afterSave()
{
$this->savePriceValue(null, $this->fSavedPrice, $this->fSavedOldPrice);
$this->savePriceListValue();
}

/**
* Set quantity attribute value
* @param int $iQuantity
*/
public function setQuantityAttribute($iQuantity)
{
$iQuantity = (int) $iQuantity;
if (empty($iQuantity) || $iQuantity < 0) {
$iQuantity = 0;
}

$this->attributes['quantity'] = $iQuantity;
//Clear relations with old prices and saved values
$this->reloadRelations('main_price');
$this->reloadRelations('price_link');
$this->fSavedPrice = null;
$this->fSavedOldPrice = null;
}

/**
Expand Down Expand Up @@ -391,14 +384,19 @@ public function getActiveCurrency()
*/
protected function getPriceValueAttribute()
{
$obPriceModel = $this->getPriceObject($this->getActivePriceType());
$this->setActivePriceType(null);
if ($this->fSavedPrice !== null) {
$fPrice = $this->fSavedPrice;
} else {
$obPriceModel = $this->getPriceObject($this->getActivePriceType());
$this->setActivePriceType(null);

if (empty($obPriceModel)) {
return 0;
if (empty($obPriceModel)) {
return 0;
}

$fPrice = $obPriceModel->price_value;
}

$fPrice = $obPriceModel->price_value;
$fPrice = CurrencyHelper::instance()->convert($fPrice, $this->getActiveCurrency());

return $fPrice;
Expand All @@ -410,14 +408,19 @@ protected function getPriceValueAttribute()
*/
protected function getOldPriceValueAttribute()
{
$obPriceModel = $this->getPriceObject($this->getActivePriceType());
$this->setActivePriceType(null);
if ($this->fSavedOldPrice !== null) {
$fPrice = $this->fSavedOldPrice;
} else {
$obPriceModel = $this->getPriceObject($this->getActivePriceType());
$this->setActivePriceType(null);

if (empty($obPriceModel)) {
return 0;
if (empty($obPriceModel)) {
return 0;
}

$fPrice = $obPriceModel->old_price_value;
}

$fPrice = $obPriceModel->old_price_value;
$fPrice = CurrencyHelper::instance()->convert($fPrice, $this->getActiveCurrency());
$this->setActiveCurrency(null);

Expand Down Expand Up @@ -468,7 +471,7 @@ protected function getPriceListAttribute()
*/
protected function setPriceAttribute($sValue)
{
$this->fSavedPrice = $sValue;
$this->fSavedPrice = PriceHelper::toFloat($sValue);
}

/**
Expand All @@ -478,7 +481,7 @@ protected function setPriceAttribute($sValue)
*/
protected function setOldPriceAttribute($sValue)
{
$this->fSavedOldPrice = $sValue;
$this->fSavedOldPrice = PriceHelper::toFloat($sValue);
}

/**
Expand All @@ -493,8 +496,8 @@ protected function setPriceListAttribute($arPriceList)
}

if (isset($arPriceList[0])) {
$this->fSavedPrice = array_get($arPriceList[0], 'price');
$this->fSavedOldPrice = array_get($arPriceList[0], 'old_price');
$this->fSavedPrice = PriceHelper::toFloat(array_get($arPriceList[0], 'price'));
$this->fSavedOldPrice = PriceHelper::toFloat(array_get($arPriceList[0], 'old_price'));
unset($arPriceList[0]);
}

Expand All @@ -512,6 +515,34 @@ protected function getTaxPercentAttribute()
return $obOfferItem->tax_percent;
}

/**
* Set quantity attribute value
* @param int $iQuantity
*/
protected function setQuantityAttribute($iQuantity)
{
$iQuantity = (int) $iQuantity;
if (empty($iQuantity) || $iQuantity < 0) {
$iQuantity = 0;
}

$this->attributes['quantity'] = $iQuantity;
}

/**
* Set quantity_in_unit attribute value
* @param int $iQuantity
*/
protected function setQuantityInUnitAttribute($sQuantity)
{
$fQuantity = (float) PriceHelper::toFloat($sQuantity);
if (empty($fQuantity) || $fQuantity < 0) {
$fQuantity = 0;
}

$this->attributes['quantity_in_unit'] = $fQuantity;
}

/**
* Create or update main price object
* @param int|null $iPriceTypeID
Expand Down
4 changes: 4 additions & 0 deletions models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use October\Rain\Database\Traits\SoftDelete;
use October\Rain\Database\Traits\Validation;
use October\Rain\Database\Traits\Purgeable;
use October\Rain\Database\Traits\Nullable;

use Lovata\Toolbox\Traits\Helpers\TraitCached;
use Lovata\Shopaholic\Classes\Import\ImportProductModelFromCSV;
Expand Down Expand Up @@ -134,6 +135,8 @@ class Product extends ImportModel
use CodeField;
use ExternalIDField;
use TraitCached;
use Nullable;


public $table = 'lovata_shopaholic_products';

Expand Down Expand Up @@ -181,6 +184,7 @@ class Product extends ImportModel

public $appends = [];
public $purgeable = [];
public $nullable = [];
public $fillable = [
'active',
'name',
Expand Down
4 changes: 3 additions & 1 deletion updates/version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,6 @@
- 'Added relation to Offer model with Measure model.'
- update_table_offers_add_measure_field.php
1.27.0:
- 'Added parent category field to backend in Category model. Thanks for contribution Dinwid.'
- 'Added parent category field to backend in Category model. Thanks for contribution Dinwid.'
1.27.1:
- 'Fixed logic of getting prices in Offer model.'

0 comments on commit 82a3941

Please sign in to comment.