From: JoySystem_v Date: Tue, 27 Aug 2024 11:15:58 +0000 (+0300) Subject: добавнена загрузка себестоимости X-Git-Tag: 1.4~24^2~4 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=f175ef13346b185732a0a7c0f4f8ba31f6c483cc;p=erp24_rep%2Fyii-erp24%2F.git добавнена загрузка себестоимости --- diff --git a/erp24/api2/controllers/DataController.php b/erp24/api2/controllers/DataController.php index 9c7d6ba0..aa85fd10 100644 --- a/erp24/api2/controllers/DataController.php +++ b/erp24/api2/controllers/DataController.php @@ -2,6 +2,7 @@ namespace app\controllers; + use Exception; use Yii; use yii\helpers\ArrayHelper; @@ -14,6 +15,7 @@ use yii_app\records\ApiCron; use yii_app\records\Assemblies; use yii_app\records\Balances; use yii_app\records\Cashes; +use yii_app\records\CityStore; use yii_app\records\CreateChecks; use yii_app\records\CreateChecks2; use yii_app\records\CreateChecksBags; @@ -30,6 +32,7 @@ use yii_app\records\Products1c; use yii_app\records\Sales; use yii_app\records\SalesItems; use yii_app\records\SalesProducts; +use yii_app\records\SelfCostProduct; //use yii_app\records\SalesProductsUpdate; use yii_app\records\SalesUpdate; use yii_app\records\Terminals; @@ -44,7 +47,8 @@ class DataController extends BaseController { const OUT_DIR = // __DIR__ . "/../json"; - "/var/www/erp24/api2/json"; + "/www/api2/json"; + //"/var/www/erp24/api2/json"; // "/var/www/www-root/data/www/api.bazacvetov24.ru/data/json"; public function actionRequest() { @@ -453,6 +457,64 @@ class DataController extends BaseController { } } } + if (!empty($result['self_cost'])) { + + + $storeMap = []; + + + $cityStoreIds = CityStore::find()->select('id')->column(); + + // Получаем соответствия export_val => entity_id из таблицы ExportImportTable + $exportImportRecords = ExportImportTable::find() + ->select(['entity_id', 'export_val']) + ->where(['entity_id' => $cityStoreIds, 'entity' => 'city_store', 'export_id' => 1]) + ->all(); + + // Создаем справочник + foreach ($exportImportRecords as $record) { + $storeMap[$record->export_val] = $record->entity_id; + } + + + $values = []; + file_put_contents(self::OUT_DIR . '/error_upload.txt', + PHP_EOL . 'self_cost', FILE_APPEND); + + foreach ($result['self_cost'] as $selfCost) { + + $storeId = $storeMap[$selfCost['store_id']] ?? null; + if (!$storeId) { + continue; + } + file_put_contents(self::OUT_DIR . '/error_upload.txt', + PHP_EOL . $storeId, FILE_APPEND); + + // Удаляем старые данные + SelfCostProduct::deleteAll(['date' => $selfCost['date'], 'store_id' => $storeId]); + + foreach ($selfCost['items'] as $item) { + $values[] = [ + 'date' => $selfCost['date'], + 'store_id' => $storeId, + 'product_guid' => $item['product_id'], + 'price' => $item['price'], + 'updated_at' => date('Y-m-d H:i:s') + ]; + } + } + + if (!empty($values)) { + + file_put_contents(self::OUT_DIR . '/error_upload.txt', + PHP_EOL . 'values', FILE_APPEND); + file_put_contents(self::OUT_DIR . '/error_upload.txt', + PHP_EOL . count($values), FILE_APPEND); + $this->setSelfCostUpdate($values); + + } + } + if (!empty($result['sellers'])) { Products1c::deleteAll(['tip' => 'admin']); @@ -1593,25 +1655,44 @@ class DataController extends BaseController { // $this->insertDataSales($values, 'sales_products_update', $columns); // } - private function insertDataSales($values, $tableName, $columns) { - $chunkValues = array_chunk($values, 1000); - foreach ($chunkValues as $key => $rows) { + private function setSelfCostUpdate($values) { + $columns = [ - try { - $transaction = \Yii::$app->db->beginTransaction(); - Yii::$app->db->createCommand()->batchInsert($tableName, $columns, $rows)->execute(); - $transaction->commit(); - } catch (Exception $e) { - $errorString = substr($e->getMessage(), 0, 2000); - file_put_contents(self::OUT_DIR . '/error_upload.txt', - PHP_EOL . $errorString, FILE_APPEND); - $transaction->rollBack(); - InfoLogService::setInfoLog(__FILE__, __LINE__, $tableName , 'error_id_25'); - InfoLogService::setInfoLog(__FILE__, __LINE__, $errorString , 'error_id_25_22'); - LogService::apiErrorLog(json_encode(["error_id" => 25, "error" => $errorString], JSON_UNESCAPED_UNICODE)); + 'date', + 'store_id', + 'product_guid', + 'price', + 'updated_at' + ]; + + file_put_contents(self::OUT_DIR . '/error_upload.txt', + PHP_EOL . 'self', FILE_APPEND); + + $this->insertDataSales($values, 'self_cost_product', $columns, 4000); + } + + + + private function insertDataSales($values, $tableName, $columns, $chunks = 1000 ) { + $chunkValues = array_chunk($values, $chunks); + + foreach ($chunkValues as $key => $rows) { + + try { + $transaction = \Yii::$app->db->beginTransaction(); + Yii::$app->db->createCommand()->batchInsert($tableName, $columns, $rows)->execute(); + $transaction->commit(); + } catch (Exception $e) { + $errorString = substr($e->getMessage(), 0, 2000); + file_put_contents(self::OUT_DIR . '/error_upload.txt', + PHP_EOL . $errorString, FILE_APPEND); + $transaction->rollBack(); + InfoLogService::setInfoLog(__FILE__, __LINE__, $tableName , 'error_id_25'); + InfoLogService::setInfoLog(__FILE__, __LINE__, $errorString , 'error_id_25_22'); + LogService::apiErrorLog(json_encode(["error_id" => 25, "error" => $errorString], JSON_UNESCAPED_UNICODE)); - } } } + } } diff --git a/erp24/records/SelfCostProduct.php b/erp24/records/SelfCostProduct.php index db86f5df..7e916e48 100644 --- a/erp24/records/SelfCostProduct.php +++ b/erp24/records/SelfCostProduct.php @@ -1,6 +1,6 @@