<commit_before><?php

namespace hipanel\modules\finance\behaviors;

use hipanel\modules\finance\logic\bill\BillQuantityFactory;
use hipanel\modules\finance\logic\bill\BillQuantityInterface;
use yii\behaviors\AttributeBehavior;
use yii\db\ActiveRecord;

class BillQuantity extends AttributeBehavior
{
    public $quantityAttribute = 'quantity';

    public $billTypeAttribute = 'type';

    public function events()
    {
        return [
            ActiveRecord::EVENT_BEFORE_INSERT => 'transformQuantity',
            ActiveRecord::EVENT_BEFORE_UPDATE => 'transformQuantity',
        ];
    }

    public function transformQuantity()
    {
        $billQty = (new BillQuantityFactory())->createByType($this->owner->{$this->billTypeAttribute}, $this->owner);

        if ($billQty and $billQty instanceof BillQuantityInterface) {
            $this->owner->{$this->quantityAttribute} = $billQty->getValue();
        }
    }
}<commit_msg>Modified BillQuantity behavior for transform Charge quantity<commit_after><?php

namespace hipanel\modules\finance\behaviors;

use hipanel\modules\finance\logic\bill\BillQuantityFactory;
use hipanel\modules\finance\models\Charge;
use yii\behaviors\AttributeBehavior;
use yii\db\ActiveRecord;

class BillQuantity extends AttributeBehavior
{
    public $quantityAttribute = 'quantity';

    /**
     * @var BillQuantityFactory
     */
    private $qtyFactory;

    public function __construct($config = [], BillQuantityFactory $qtyFactory)
    {
        parent::__construct($config);
        $this->qtyFactory = $qtyFactory;
    }

    public function events()
    {
        return [
            ActiveRecord::EVENT_BEFORE_INSERT => 'transformQuantity',
            ActiveRecord::EVENT_BEFORE_UPDATE => 'transformQuantity',
        ];
    }

    public function transformQuantity()
    {
        // For Bills
        if ($billQty = $this->transform($this->owner)) {
            $this->owner->{$this->quantityAttribute} = $billQty->getValue();
        }

        // For Charges
        if (isset($this->owner->charges) && !empty($this->owner->charges)) {
            foreach ($this->owner->charges as $k => $data) {
                $charge = new Charge($data);
                if ($chargeQty = $this->transform($charge)) {
                    $this->owner->charges[$k][$this->quantityAttribute] = $chargeQty->getValue();
                }
            }
        }
    }

    protected function transform($model)
    {
        return $this->qtyFactory->create($model);
    }
}
{'fraction_non_alphanumeric': 0.1283891547049442, 'fraction_numerical': 0.0, 'mean_word_length': 2.848159509202454, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5262102', 'n_tokens_mistral': 739, 'n_tokens_neox': 690, 'n_words': 121}