]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
ERP-85 Реализовать обмен данных с 1С Бухгалтерия
authormarina <m.zozirova@gmail.com>
Mon, 26 Aug 2024 12:06:13 +0000 (15:06 +0300)
committermarina <m.zozirova@gmail.com>
Mon, 26 Aug 2024 12:06:13 +0000 (15:06 +0300)
erp24/api2/controllers/DataBuhController.php
erp24/records/Motivation.php
erp24/records/MotivationCostsItem.php
erp24/scripts/tasks/task_32_create_request_for_1c_buh.php [new file with mode: 0644]
erp24/services/MotivationServiceBuh.php

index 0a3ca131716698e238d5706955c58ef1f44c07bd..8268de9917e900180e2a8b8e5c42f9761930a285 100644 (file)
@@ -113,5 +113,4 @@ class DataBuhController extends BaseController
 
         return $this->asJson(['request_id' => $requestId, 'response' => true]);
     }
-
 }
\ No newline at end of file
index e843bd00fd99583eac94f3048066263252bcfed0..e2649b21c48302543ced211426c165ae6b9b8adb 100644 (file)
@@ -64,4 +64,26 @@ class Motivation extends \yii\db\ActiveRecord
             'created_at' => 'Created At',
         ];
     }
+
+    public static function getWeekRangeForDate($date = null)
+    {
+        $date = $date ?? date('Y-m-d');
+
+        $day = date('d', $date);
+        $month = date('m', $date);
+        $year = date('m', $date);
+
+        $weekNumber = intdiv($day - 1, 7) + 1;
+
+        $startDay = ($weekNumber - 1) * 7 + 1;
+        $endDay = min($weekNumber * 7, date('t'));
+
+        $startDate = \DateTime::createFromFormat('Y-m-d', "$year-$month-$startDay");
+        $endDate = \DateTime::createFromFormat('Y-m-d', "$year-$month-$endDay");
+
+        return [
+            'start_time' => $startDate->format('Y-m-d'),
+            'end_time' => $endDate->format('Y-m-d'),
+        ];
+    }
 }
index 1f9de0bee65e93b06f5d8d14dfbb8cdfe54c111d..9ee01e75bf59fb6fc1d1eef203ce6c7aa75cf4c1 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace yii_app\records;
 
+use yii\behaviors\BlameableBehavior;
 use yii\db\ActiveRecord;
 use yii\behaviors\TimestampBehavior;
 use yii\db\Expression;
@@ -29,13 +30,19 @@ class MotivationCostsItem extends ActiveRecord
     const ITEM_DEFECT_DUE_TO_EQUIPMENT_FAILURE = 'Брак из-за поломки оборудования';
     const ITEM_REGRADING = 'Пересорт';
 
-    public static function writeOffsToMotivationItemMap($itemType) {
+    public static function writeOffsToMotivationItemMap($itemType)
+    {
         switch ($itemType) {
-            case WriteOffsErp::WRITE_OFFS_TYPE_BRAK: return self::ITEM_WRITE_OFF_OF_ILLIQUID_GOODS_SPOILAGE_EXPIRATION_OF_SHELF_LIFE;
-            case WriteOffsErp::WRITE_OFFS_TYPE_DELIVERY_BRAK: return self::ITEM_DEFECTIVE_DELIVERY;
-            case WriteOffsErp::WRITE_OFFS_TYPE_DUE_TO_EQUIPMENT_FAILURE_BRAK: return self::ITEM_DEFECT_DUE_TO_EQUIPMENT_FAILURE;
-            case WriteOffsErp::WRITE_OFFS_TYPE_RESORTING_DOES_NOT_COUNT_TOWARDS_COST: return self::ITEM_REGRADING;
-            default: return '';
+            case WriteOffsErp::WRITE_OFFS_TYPE_BRAK:
+                return self::ITEM_WRITE_OFF_OF_ILLIQUID_GOODS_SPOILAGE_EXPIRATION_OF_SHELF_LIFE;
+            case WriteOffsErp::WRITE_OFFS_TYPE_DELIVERY_BRAK:
+                return self::ITEM_DEFECTIVE_DELIVERY;
+            case WriteOffsErp::WRITE_OFFS_TYPE_DUE_TO_EQUIPMENT_FAILURE_BRAK:
+                return self::ITEM_DEFECT_DUE_TO_EQUIPMENT_FAILURE;
+            case WriteOffsErp::WRITE_OFFS_TYPE_RESORTING_DOES_NOT_COUNT_TOWARDS_COST:
+                return self::ITEM_REGRADING;
+            default:
+                return '';
         }
     }
 
@@ -80,7 +87,7 @@ class MotivationCostsItem extends ActiveRecord
     }
 
 
-     /**
+    /**
      * {@inheritdoc}
      */
     public function behaviors()
@@ -90,6 +97,11 @@ class MotivationCostsItem extends ActiveRecord
                 'class' => TimestampBehavior::class,
                 'value' => new Expression('NOW()'),
             ],
+            [
+                'class' => BlameableBehavior::class,
+                'createdByAttribute' => 'created_by',
+                'updatedByAttribute' => 'updated_by',
+            ],
         ];
     }
 
diff --git a/erp24/scripts/tasks/task_32_create_request_for_1c_buh.php b/erp24/scripts/tasks/task_32_create_request_for_1c_buh.php
new file mode 100644 (file)
index 0000000..a64edb8
--- /dev/null
@@ -0,0 +1,137 @@
+<?php
+
+/**
+ * @var $time integer
+ */
+
+use yii\db\Exception;
+use yii\helpers\ArrayHelper;
+
+use yii\helpers\Json;
+use yii_app\records\ApiCronBuh;
+use yii_app\records\Firms;
+use yii_app\records\Motivation;
+use yii_app\records\Users;
+use yii_app\records\UsersBonus;
+use yii_app\api3\modules\v1\models\orders\OrdersAmo;
+use yii_app\records\Sales;
+use yii_app\records\SchedulerTaskLog;
+use yii_app\records\NotifiableUser;
+
+ini_set('max_execution_time', (string)(60 * 60 * 1)); // 1 час
+ini_set('display_errors', 'on');
+ini_set('display_errors', 1);
+ini_set('display_startup_errors', 1);
+error_reporting(E_ALL);
+
+echo "time2_" . $time . "_time2 ";
+$taskNum = 32;
+$dateTask = date('Y-m-d H:i:s');
+$dateTaskStart = null;
+$dateTaskStop = null;
+$log = '';
+$error = '';
+$infoError = '';
+$infoText = '';
+$description = '';
+$result = 0;
+
+$enable = true;
+$start = false;
+$force = false;
+
+$minuetTimeInTask = date('i', $time);
+$fullTimeInTask = date('Y-m-d H:i:s', $time);
+
+try {
+    if (
+        (
+            (
+                date('H:i', $time) == "06:00" ||
+                date('H:i', $time) == "18:00"
+            )
+            || $force
+        )
+        && $enable
+    ) {
+
+        $dateTaskStart = date('Y-m-d H:i:s');
+        $info = ' ================ test Task ' . $taskNum . ' start ================';
+        echo $info;
+        $log .= $info;
+        $log .= $time;
+
+        $schedulerTaskLog = new SchedulerTaskLog();
+        $schedulerTaskLog->setTaskNum($taskNum)
+            ->setName('Task ' . $taskNum)
+            ->setDate($dateTask)
+            ->setDateStart($dateTaskStart)
+        ;
+        $validate = $schedulerTaskLog->validate();
+        if ($validate) {
+            $schedulerTaskLog->save();
+        }
+
+        $weekRange = Motivation::getWeekRangeForDate();
+
+        foreach (Firms::getInn() as $key => $firm ) {
+            $model = new ApiCronBuh();
+            $model->date = date('Y-m-d H:i:s');
+            $model->request_id = strval(strtotime($model->date). '_' .$key);
+            $model->json_post = Json::encode([
+                'request_id' => $model->request_id,
+                'cost_items' => [
+                    'start_time' => date('Y-m-d 00:00:00', strtotime($weekRange['start_time'])),
+                    'end_time' => date('Y-m-d 23:59:59', strtotime($weekRange['end_time'])),
+                ]
+            ]);
+            $model->inn = $key;
+
+            try {
+                $model->save();
+            } catch (Exception $e) {
+                throw new Exception($e);
+            }
+        }
+
+
+
+
+        $info = ' ================ test Task ' . $taskNum . ' stop ================';
+        echo $info;
+        $log .= $info;
+        $log .= ' date >= ' . strtotime("-1 week", time());
+        $dateTaskStop = date('Y-m-d H:i:s');
+    } else {
+        $info = '   Task ' . $taskNum . ' skip   ';
+        echo $info;
+        $log .= $info;
+    }
+} catch (Exception $e) {
+    $error = 'Exception: ' . $e->getMessage() . ' ' . $e->getFile() . ' >>> ' . $e->getLine();
+}
+
+if (empty($schedulerTaskLog)) {
+    $schedulerTaskLog = new SchedulerTaskLog();
+    $schedulerTaskLog->setTaskNum($taskNum)
+        ->setName('Task ' . $taskNum)
+        ->setDate($dateTask)
+        ->setDateStart($dateTaskStart)
+        ->setDateStop($dateTaskStop)
+        ->setDescription($description)
+        ->setError($error)
+        ->setInfo($infoText)
+        ->setLog($log)
+    ;
+} else {
+    $schedulerTaskLog->setDateStop($dateTaskStop)
+        ->setDescription($description)
+        ->setError($error)
+        ->setInfo($infoText)
+        ->setLog($log)
+    ;
+}
+$validate = $schedulerTaskLog->validate();
+if ($validate) {
+    $schedulerTaskLog->save();
+}
\ No newline at end of file
index 637d0802b8a8a5143adcf1b5d4feb00aef364f21..b10a534995f3ddc557dd9e97e34d3b401ab46286 100644 (file)
@@ -28,14 +28,18 @@ class MotivationServiceBuh
             $inn = preg_replace('/^.*_/', '', $data['request_id']);
             $week = null;
 
-            if (self::validateWeek($start, $end)) {
+            $validate = self::validateWeek($start, $end);
+
+            if ($validate !== 'month') {
                 $week = self::getWeek($start) == self::getWeek($end) ? self::getWeek($start) : null;
+                if (!$validate && !$week) {
+                    LogService::apiErrorLog(json_encode(["error_id" => 45, "error" => 'Указан некорректный период'], JSON_UNESCAPED_UNICODE));
+                    return;
+                }
             }
 
-            if (!$week) {
-                LogService::apiErrorLog(json_encode(["error_id" => 45, "error" => 'Указан некорректный период'], JSON_UNESCAPED_UNICODE));
-                return;
-            }
+            $alias = $validate === 'month' ? 'month' : 'week' . $week;
+            $motivationValueGroup = MotivationValueGroup::findOne(['alias' =>   $alias]);
 
             if (!empty($data['cost_items'])) {
                 foreach ($data['cost_items'] as $stores) {
@@ -74,13 +78,11 @@ class MotivationServiceBuh
                                     $motivationBuhValue = MotivationBuhValue::findOne([
                                         'motivation_buh_id' => $motivationBuh->id,
                                         'store_id' => $store['id'],
-                                        'motivation_group_id' => $week,
+                                        'motivation_group_id' => $motivationValueGroup->id,
                                         'value_id' => $value->id,
                                         'value_type' => MotivationCostsItem::DATA_TYPE_FLOAT,
                                     ]);
 
-                                    $motivationValueGroup = MotivationValueGroup::findOne(['alias' => 'week' . $week]);
-
                                     if ($motivationBuhValue) {
                                         $motivationBuhValue->setAttribute('value_float', $item['summ']);
                                     } else {
@@ -130,6 +132,13 @@ class MotivationServiceBuh
         )
             return null;
 
+        if (
+            date('d', strtotime($startTime)) == 1
+            &&
+            (date('d', strtotime($endTime)) == date('t', strtotime($endTime)))
+        )
+            return 'month';
+
         if (
             (!in_array(intval(date('j', strtotime($startTime))), [1, 8, 15, 22, 29]))
             ||
@@ -143,7 +152,6 @@ class MotivationServiceBuh
         if ((date('d', strtotime($endTime)) != date('t', strtotime($endTime))) && date('d', strtotime($startTime)) == 29)
             return null;
 
-
         return true;
     }