use yii\data\ActiveDataProvider;
use yii\data\ArrayDataProvider;
use yii\db\Expression;
+use yii_app\records\MatrixErpProperty;
use yii_app\records\Products1c;
use yii_app\records\Products1cNomenclature;
use yii_app\records\ProductsClass;
$isValid = Yii::$app->request->get('is_valid', Yii::$app->request->post('is_valid'));
$productId = Yii::$app->request->get('product_id', Yii::$app->request->post('product_id'));
+ $model = new MatrixErpProperty();
+ $labels = $model->attributeLabels();
+
+ $fields = [
+ 'description' => 'string',
+ 'image_id' => 'numeric',
+ 'url_link_video' => 'string',
+ 'date' => 'date',
+ 'display_name' => 'string',
+ 'external_image_url' => 'string',
+ 'product_url' => 'string',
+ 'flowwow_category' => 'string',
+ 'flowwow_subcategory' => 'string',
+ 'yandex_category' => 'string',
+ 'length' => 'numeric',
+ 'width' => 'numeric',
+ 'height' => 'numeric',
+ 'weight' => 'numeric',
+ ];
+
+ $conditions = [];
+ $texts = [];
+
+ foreach ($fields as $field => $type) {
+ $label = $labels[$field] ?? $field;
+
+ if ($type === 'string') {
+ $conditions[] = "mep.$field IS NULL OR TRIM(mep.$field) = ''";
+ $texts[] = "CASE WHEN mep.$field IS NULL OR TRIM(mep.$field) = '' THEN 'В карточке Матричного букета не заполнено: {$label}' END";
+ } else {
+ $conditions[] = "mep.$field IS NULL";
+ $texts[] = "CASE WHEN mep.$field IS NULL THEN 'В карточке Матричного букета не заполнено: {$label}' END";
+ }
+ }
+
+ $hasMissingSql = implode(" OR ", $conditions);
+ $missingTextSql = "TRIM(BOTH E'\n' FROM concat_ws(E'\n', " . implode(", ", $texts) . "))";
+
$subQuery = Products1c::find()
->alias('p1n')
->leftJoin('products_class pc', 'p1n.id = pc.category_id')
$innerQuery = Products1c::find()
->alias('p1n')
->leftJoin('prices p', 'p.product_id = p1n.id')
- ->leftJoin('matrix_erp_property_dynamic mep', 'mep.product_id = p1n.id AND mep.active = 1')
+ ->leftJoin('matrix_erp_property_dynamic mepd', 'mepd.product_id = p1n.id AND mepd.active = 1')
->leftJoin('marketplace_priority mp', 'mp.guid::text = p1n.id::text')
+ ->leftJoin('matrix_erp_property mep', 'mep.guid::text = p1n.id::text')
->leftJoin(['mp_check' => new Expression("
LATERAL (
SELECT
) FILTER (WHERE COALESCE(b.quantity, 0) < (j.value::numeric * COALESCE(mp.reminder_koef, 1))) AS insuff_reason
FROM jsonb_each(
CASE
- WHEN mep.value = '' THEN '{}'
- ELSE replace(trim(both '\"' from mep.value), '\"\"', '\"')
+ WHEN mepd.value = '' THEN '{}'
+ ELSE replace(trim(both '\"' from mepd.value), '\"\"', '\"')
END::jsonb
) AS j(key, value)
LEFT JOIN balances b ON b.product_id::text = j.key AND b.store_id = :storeId
LEFT JOIN products_1c prod ON prod.id::text = j.key
)
")], 'true')
+ ->leftJoin(['mep_check' => new Expression("
+ LATERAL (
+ SELECT
+ ($hasMissingSql) AS has_missing,
+ $missingTextSql AS missing_text
+ )
+")], 'true')
->select([
'p1n.id',
'p1n.name',
'p1n.articule',
new \yii\db\Expression("
- CASE
- WHEN mep.id IS NULL OR p.price IS NULL OR mp_check.has_insufficient_quantity THEN
- 'Коэффициент: ' || COALESCE(mp.reminder_koef::text, '-') || E'\n' ||
- concat_ws(
- E'\n',
- CASE WHEN mep.id IS NULL THEN 'Не имеет состава букета' ELSE NULL END,
- CASE WHEN p.price IS NULL THEN 'Нет имеет цены' ELSE NULL END,
- CASE WHEN mp_check.has_insufficient_quantity THEN mp_check.insuff_reason ELSE NULL END
- )
- ELSE
- ''
- END AS reason
- "),
+ CASE
+ WHEN mepd.id IS NULL OR p.price IS NULL OR mp_check.has_insufficient_quantity OR mep_check.has_missing THEN
+ 'Коэффициент: ' || COALESCE(mp.reminder_koef::text, '-') || E'\n' ||
+ concat_ws(
+ E'\n',
+ CASE WHEN mepd.id IS NULL THEN 'Не имеет состава букета' END,
+ CASE WHEN p.price IS NULL THEN 'Нет цены' END,
+ CASE WHEN mp_check.has_insufficient_quantity THEN mp_check.insuff_reason END,
+ NULLIF(mep_check.missing_text, '')
+ )
+ ELSE
+ ''
+ END AS reason
+"),
new \yii\db\Expression("
CASE
- WHEN mep.id IS NULL OR p.price IS NULL OR mp_check.has_insufficient_quantity THEN FALSE
+ WHEN mepd.id IS NULL OR p.price IS NULL OR mp_check.has_insufficient_quantity OR mep_check.has_missing THEN FALSE
ELSE TRUE
END AS is_valid
"),