EqualizationRemains::deleteAll(['shift_id' => $shiftTransfer->id]);
$plus = ShiftRemains::find()
- ->andWhere(['shift_transfer_id' => $shiftTransfer->id])
+ ->where(['shift_transfer_id' => $shiftTransfer->id])
->andWhere(['!=', 'fact_and_1c_diff', 0])
->andWhere(['>', 'fact_and_1c_diff', 0])
->orderBy('retail_price')
->all();
- $minus = ArrayHelper::map(ShiftRemains::find()
- ->andWhere(['shift_transfer_id' => $shiftTransfer->id])
- ->andWhere(['!=', 'fact_and_1c_diff', 0])
- ->andWhere(['<', 'fact_and_1c_diff', 0])
- ->select(['product_guid', 'fact_and_1c_diff', 'retail_price', 'self_cost'])
- ->asArray()
- ->all(), 'product_guid', function ($item) {
- return ['fact_and_1c_diff' => $item['fact_and_1c_diff'], 'retail_price' => $item['retail_price'], 'self_cost' => $item['self_cost']];
- });
+ $minus = ArrayHelper::map(
+ ShiftRemains::find()
+ ->where(['shift_transfer_id' => $shiftTransfer->id])
+ ->andWhere(['!=', 'fact_and_1c_diff', 0])
+ ->andWhere(['<', 'fact_and_1c_diff', 0])
+ ->select(['product_guid', 'fact_and_1c_diff', 'retail_price', 'self_cost'])
+ ->asArray()
+ ->all(),
+ 'product_guid',
+ fn($item) => [
+ 'fact_and_1c_diff' => $item['fact_and_1c_diff'],
+ 'retail_price' => $item['retail_price'],
+ 'self_cost' => $item['self_cost']
+ ]
+ );
foreach ($plus as $p) {
$replacement = Product1cReplacement::find()
$quanity = $p->fact_and_1c_diff;
- foreach ($minus as $key => $item) {
+ $intersect = array_intersect_key($minus, array_flip($replacement));
- $count = $count ?? $item['fact_and_1c_diff'];
+ if (empty($intersect)) {
+ continue;
+ }
- if (self::find()
- ->select(['shift_id', 'product_replacement_id'])
- ->andWhere(['shift_id' => $shiftTransfer->id])
- ->andWhere(['product_replacement_id' => $key])
+ foreach ($intersect as $key => $item) {
+ $eqRemains = self::find()
+ ->select(['SUM(product_count)'])
+ ->where(['shift_id' => $shiftTransfer->id, 'product_id' => $key])
->groupBy(['shift_id', 'product_replacement_id'])
- ->having(['SUM(product_replacement_count)' => abs($item['fact_and_1c_diff'])])
- ->exists()) {
+ ->scalar();
+
+ $diff = $eqRemains
+ ? min($quanity, abs($item['fact_and_1c_diff']) - $eqRemains)
+ : min($quanity, abs($item['fact_and_1c_diff']));
+
+ if (self::find()
+ ->select(['SUM(product_count)'])
+ ->where(['shift_id' => $shiftTransfer->id])
+ ->andWhere(['product_id' => $key])
+ ->groupBy(['shift_id', 'product_id'])
+ ->having(['SUM(product_count)' => abs($item['fact_and_1c_diff'])])
+ ->scalar() == abs($item['fact_and_1c_diff'])) {
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'])
+ 'product_id' => $key,
+ 'product_count' => $diff,
+ 'product_price' => $item['retail_price'],
+ 'product_self_cost' => $item['self_cost'],
+ 'product_replacement_id' => $p->product_guid,
+ 'product_replacement_count' => $diff,
+ 'product_replacement_price' => $p->retail_price,
+ 'product_replacement_self_cost' => $p->self_cost,
+ 'balance' => $diff * ($p->retail_price - $item['retail_price']),
+ 'balance_self_cost' => $diff * ($p->self_cost - $item['self_cost'])
]);
if ($model->validate()) {
$model->save();
+ $quanity -= $diff;
- $quanity = $quanity - min($quanity, abs($item['fact_and_1c_diff']));
- $count += min($quanity, abs($item['fact_and_1c_diff']));
-//
- if ($count == 0) {
- unset($minus[$key]);
- unset($count);
+ if ($quanity <= 0) {
break;
}
- } elseif ($model->getErrors()) {
+ } else {
var_dump($model->errors);
die();
}