use yii\behaviors\TimestampBehavior;
use yii\data\ActiveDataProvider;
use yii\db\Expression;
+use yii\helpers\ArrayHelper;
/**
* This is the model class for table "equalization_remains".
public static function setData($shiftTransfer)
{
+ EqualizationRemains::deleteAll(['shift_id' => $shiftTransfer->id]);
+
$plus = ShiftRemains::find()
->andWhere(['shift_transfer_id' => $shiftTransfer->id])
->andWhere(['!=', 'fact_and_1c_diff', 0])
- ->andWhere(['>', 'fact_and_1c_diff', 0]);
+ ->andWhere(['>', 'fact_and_1c_diff', 0])
+ ->all();
- $minus = ShiftRemains::find()
+ $minus = ArrayHelper::map(ShiftRemains::find()
->andWhere(['shift_transfer_id' => $shiftTransfer->id])
->andWhere(['!=', 'fact_and_1c_diff', 0])
->andWhere(['<', 'fact_and_1c_diff', 0])
- ->all();
-
- foreach ($minus as $m) {
- $replacement = Product1cReplacement::find()
- ->andWhere(['guid' => $m->product_guid])
- ->select('guid_replacement')
- ->scalar();
- if (!$plus->andWhere(['product_guid' => $replacement])->exists()) {
- continue;
- }
+ ->select(['product_guid_id', 'fact_and_1c_diff', 'retail_price', 'self_cost'])
+ ->asArray()
+ ->all(), 'product_guid_id', function ($item) {
+ return ['fact_and_1c_diff' => $item['fact_and_1c_diff'], 'retail_price' => $item['retail_price'], 'self_cost' => $item['self_cost']];
+ });
- $p = $plus->andWhere(['product_guid' => $replacement])->one();
- $model = new EqualizationRemains();
- $model->setAttributes([
- 'shift_id' => $shiftTransfer->id,
- 'product_id' => $m->product_guid,
- 'product_count' => min ($m->fact_and_1c_diff,$p->fact_and_1c_diff),
- 'product_price' => $m->retail_price,
- 'product_self_cost' => $m->self_cost,
- 'product_replacement_id' => $p->product_guid,
- 'product_replacement_count' => min ($m->fact_and_1c_diff,$p->fact_and_1c_diff),
- 'product_replacement_price' => $p->retail_price,
- 'product_replacement_self_cost' => $p->self_cost,
- 'balance' => $m->fact_and_1c_diff * ($p->retail_price - $m->retail_price),
- 'balance_self_cost' => $m->fact_and_1c_diff * ($p->self_cost - $m->self_cost)
- ]);
- if ($model->validate()) {
- $model->save();
- } elseif ($model->getErrors()) {
- var_dump($model->errors);
- die();
- }
- }
- $plus = ShiftRemains::find()
- ->andWhere(['shift_transfer_id' => $shiftTransfer->id])
- ->andWhere(['!=', 'fact_and_1c_diff', 0])
- ->andWhere(['>', 'fact_and_1c_diff', 0])
- ->all();
foreach ($plus as $p) {
- if (EqualizationRemains::find()
- ->andWhere(['shift_id' => $shiftTransfer->id])
- ->andWhere(['product_replacement_id' => $p->product_guid])
- ->exists()) {
- continue;
- }
-
$replacement = Product1cReplacement::find()
->andWhere(['guid' => $p->product_guid])
->select('guid_replacement')
- ->scalar();
+ ->column();
- $model = new EqualizationRemains();
- $model->setAttributes([
- 'shift_id' => $shiftTransfer->id,
- 'product_id' => $m->product_guid,
- 'product_count' => min ($m->fact_and_1c_diff,$p->fact_and_1c_diff),
- 'product_price' => $m->retail_price,
- 'product_self_cost' => $m->self_cost,
- 'product_replacement_id' => $p->product_guid,
- 'product_replacement_count' => min ($m->fact_and_1c_diff,$p->fact_and_1c_diff),
- 'product_replacement_price' => $p->retail_price,
- 'product_replacement_self_cost' => $p->self_cost,
- 'balance' => $m->fact_and_1c_diff * ($p->retail_price - $m->retail_price),
- 'balance_self_cost' => $m->fact_and_1c_diff * ($p->self_cost - $m->self_cost)
- ]);
- if ($model->validate()) {
- $model->save();
- } elseif ($model->getErrors()) {
- var_dump($model->errors);
- die();
+ $quanity = $p->fact_and_1c_diff;
+
+ $intersection = array_intersect_key($minus, array_flip($replacement));
+
+ foreach ($intersection as $key => $item) {
+ if ($quanity <= 0) {
+ break;
+ }
+
+ $model = new EqualizationRemains();
+ $model->setAttributes([
+ 'shift_id' => $shiftTransfer->id,
+ 'product_id' => $p->product_guid,
+ 'product_count' => min($quanity, abs($item['fact_and_1c_diff'])),
+ 'product_price' => $p->retail_price,
+ 'product_self_cost' => $p->self_cost,
+ 'product_replacement_id' => $key,
+ 'product_replacement_count' => min($quanity, abs($item['fact_and_1c_diff'])),
+ 'product_replacement_price' => $item['retail_price'],
+ 'product_replacement_self_cost' => $item['self_cost'],
+ 'balance' => min($quanity, abs($item['fact_and_1c_diff'])) * ($p->retail_price - $item['retail_price']),
+ 'balance_self_cost' => min($quanity, abs($item['fact_and_1c_diff'])) * ($p->self_cost - $item['self_cost'])
+ ]);
+ if ($model->validate()) {
+ $model->save();
+ $quanity -= min($quanity, abs($item['fact_and_1c_diff']));
+ } elseif ($model->getErrors()) {
+ var_dump($model->errors);
+ die();
+ }
}
}
-
}
/**