namespace app\controllers;
+include_once('../inc/base_new.php');
+include_once('../inc/amo_inc.php');
+
use Yii;
use yii\web\Controller;
+use yii_app\records\CreateChecks;
+use yii_app\records\OrdersAmo;
use yii_app\records\OrdersAmoSearch;
class OrdersController extends Controller
public function actionDelivery()
{
+ if (!empty($_REQUEST["umoup"])) {
+
+ $date1 = mktime(0, 0, 0, date("n"), date("d", time() - 86400), date("Y"));
+ $date2 = mktime(0, 0, 0, date("n"), date("d"), date("Y"));
+ //include_once "/var/www/www-root/data/www/amo.bazacvetov24.ru/amo/amo_inc.php";
+ $dt = "updated_at";
+
+ $statuses = [38932165, 142];
+ $unix0 = $unix0 ?? '';
+ $delivery__date = '&filter[' . $dt . '][from]=' . $unix0 . '&filter[' . $dt . '][to]=' . $date1 . '';
+ $delivery__date = '&filter[custom_fields_values][647935][from]=' . $date1 . '&filter[custom_fields_values][647935][to]=' . $date2 . '';
+
+ $k = 0;
+ foreach ($statuses as $status) {
+
+ $deal0 = amo_rest($ACCESS_TOKEN, '/api/v4/leads?order[updated_at]=desc&limit=250&filter[statuses][0][pipeline_id]=4021495&filter[statuses][0][status_id]=' . $status . '' . $delivery__date . '', 'GET', []);
+
+ foreach ($deal0["_embedded"]["leads"] as $lid_id3 => $arr0) {
+ foreach ($arr0["custom_fields_values"] as $fid => $arr20) {
+ if ($arr20["field_id"] == 655357) $lid_id5 = $arr20["values"][0]["value"];
+ if ($arr20["field_id"] == 647929) $store = $arr20["values"][0]["value"]; // store
+ if ($arr20["field_id"] == 647927) $delivery = $arr20["values"][0]["value"]; // delivery
+
+
+ }
+
+ $k++;
+ echo " <br>$k) Сделка " . $arr0["id"] . " lid_id=$lid_id5 " . $arr0["name"] . " price= " . $arr0["name"] . " status_id=" . $arr0["status_id"] . " ";
+
+ $dat = OrdersAmo::find()->select(['status_id', 'price', 'delivery_date'])
+ ->where([
+ 'amo_id' => $arr0["id"]
+ ])
+ ->asArray()
+ ->one();
+
+ if (empty($dat)) echo "<div> " . $dat["delivery_date"] . " " . $dat["status_id"] . " Заказа нет в системе " . $dat["id"] . " " . $dat["price"] . " </div>";
+
+
+ if ($arr0["price"] != $dat["price"]) {
+ echo "Сумма заказа не равна ! " . $arr0["price"] . "!=" . $dat["price"] . "";
+ OrdersAmo::updateAll(['price' => $arr0['price']], ['amo_id' => $arr0['id'], 'id' => $lid_id5]);
+
+ }
+
+ if ($dat["status_id"] != $arr0["status_id"]) {
+ if ($arr0["status_id"] == 142) {
+ OrdersAmo::updateAll(['status_id' => $arr0['status_id'], 'price' => $arr0['price']], ['amo_id' => $arr0['id'], 'id' => $lid_id5]);
+
+ }
+
+ }
+
+
+ if (!empty($arr0["id"])) {
+ OrdersAmo::updateAll(['store' => $store, $delivery => $delivery], ['amo_id' => $arr0['id']]);
+
+ }
+ }
+ }
+ }
+
$searchModel = new OrdersAmoSearch();
$dataProvider = $searchModel->searchDelivery(Yii::$app->request->queryParams);
- return $this->render('delivery',[
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider
+ return $this->render('delivery', [
+ 'searchModel' => $searchModel,
+ 'dataProvider' => $dataProvider
]);
}
+
+ public function actionAjax_return_check()
+ {
+ $salesCheck = Yii::$app->request->post('sales_check');
+ $type = "Возврат";
+ $sales_check = htmlentities($salesCheck);
+ $this->VozvratCheck($sales_check);
+
+ }
+
+ public function VozvratCheck($id)
+ {
+
+ $order_id = CreateChecks::find()->select(['order_id'])->where(['check_id' => $id])->asArray()->one()['order_id'];
+ $date = CreateChecks::find()->select([
+ 'date' => 'UNIX_TIMESTAMP(date)'
+ ])
+ ->where([
+ 'AND',
+ ['order_id' => $order_id],
+ ['type' => 'Возврат']
+ ])
+ ->orderBy([
+ 'date' => SORT_DESC
+ ])
+ ->asArray()
+ ->one();
+
+ $error = "";
+
+ if (!empty($date['date']) && $date['date'] > (time() - 60)) {
+ $error = "Возврат не создан так как прошло менее 60 секунд после создания чека на возврат";
+ error_mess($error);
+ return;
+ }
+
+ $data = CreateChecks::findOne(['check_id' => $id, 'type' => 'Продажа']);
+
+ if (!empty($data)) {
+ $createCheck = new CreateChecks([
+ 'date' => date('Y-m-d H:i:s'),
+ 'status' => 0,
+ 'type' => 'Возврат',
+ 'kkm_id' => $data->kkm_id,
+ 'seller_id' => $data->seller_id,
+ 'store_id' => $data->store_id,
+ 'order_id' => $data->order_id,
+ 'check_id' => create_guid(),
+ 'guid' => '-',
+ 'name' => '-',
+ 'held' => 1,
+ 'comments' => '-',
+ 'sales_check' => $data->check_id,
+ 'items' => $data->items,
+ 'payments' => $data->payments
+ ]);
+
+ if ($createCheck->save()) {
+ mess("Чек на возврат создан");
+ } else {
+ mess("Возникла ошибка оздния чека");
+ var_dump($createCheck->errors);
+ }
+ }
+
+ }
}
\ No newline at end of file
--- /dev/null
+<?php
+header('Access-Control-Allow-Origin: *');
+define('SECRET_FILE', '/var/www/www-root/data/www/amo.bazacvetov24.ru/amo/token_amo__1234u5u6uvhvhfdjhrrtghhr2022.json');
+define('SUBDOMAIN', 'bazacvetov24');
+$arr_token = json_decode(file_get_contents(SECRET_FILE), true);
+$ACCESS_TOKEN = $arr_token['access_token'];
+function amo_rest($access_token, $end_point, $method = 'GET', $params) {
+
+ $root_link = 'https://' . SUBDOMAIN . '.amocrm.ru';
+
+ $headers = [
+
+ 'Authorization: Bearer ' . $access_token,
+
+ 'Content-Type: application/json',
+
+ ];
+
+ $curl = curl_init();
+
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
+
+ curl_setopt($curl, CURLOPT_USERAGENT, 'amoCRM-oAuth-client/1.0');
+
+ curl_setopt($curl, CURLOPT_URL, $root_link . $end_point);
+
+ curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
+
+ curl_setopt($curl, CURLOPT_HEADER, false);
+
+ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
+
+ curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
+
+ if ($method != 'GET') {
+
+ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
+
+ curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
+
+ }
+
+ $response = curl_exec($curl);
+
+ curl_close($curl);
+
+ return json_decode($response, true);
+}
\ No newline at end of file
* @property CityStore $storeObj
* @property Admin $courierObj
* @property Admin $floristObj
+ * @property OrdersStatus $status
*/
class OrdersAmo extends \yii\db\ActiveRecord
{
{
return $this->hasOne(Admin::class, ['id' => 'courier_id']);
}
+
+ public function getStatus()
+ {
+ return $this->hasOne(OrdersStatus::class, ['status_id' => 'status_id']);
+ }
}
{
$query = OrdersAmo::find();
$query->with('sales');
+ $query->with('storeObj');
+ $query->with('courierObj');
+ $query->with('floristObj');
+ $query->with('status');
// add conditions that should always apply here
--- /dev/null
+<?php
+
+namespace yii_app\records;
+
+use Yii;
+
+/**
+ * This is the model class for table "orders_status".
+ *
+ * @property int $id
+ * @property int $client_id
+ * @property string $name
+ * @property string $name_public
+ * @property int $status_id
+ * @property int $pipeline_id
+ * @property int $account_id
+ * @property string $tip
+ * @property string $color
+ * @property string $type
+ * @property int $posit
+ */
+class OrdersStatus extends \yii\db\ActiveRecord
+{
+ /**
+ * {@inheritdoc}
+ */
+ public static function tableName()
+ {
+ return 'orders_status';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function rules()
+ {
+ return [
+ [['client_id', 'name', 'name_public', 'status_id', 'pipeline_id', 'account_id', 'tip', 'color', 'type', 'posit'], 'required'],
+ [['client_id', 'status_id', 'pipeline_id', 'account_id', 'posit'], 'integer'],
+ [['name', 'name_public'], 'string', 'max' => 255],
+ [['tip', 'type'], 'string', 'max' => 20],
+ [['color'], 'string', 'max' => 25],
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function attributeLabels()
+ {
+ return [
+ 'id' => 'ID',
+ 'client_id' => 'Client ID',
+ 'name' => 'Name',
+ 'name_public' => 'Name Public',
+ 'status_id' => 'Status ID',
+ 'pipeline_id' => 'Pipeline ID',
+ 'account_id' => 'Account ID',
+ 'tip' => 'Tip',
+ 'color' => 'Color',
+ 'type' => 'Type',
+ 'posit' => 'Posit',
+ ];
+ }
+}
* @var $searchModel OrdersAmoSearch
* @var $dataProvider ActiveDataProvider
*/
+
+$this->registerJs(<<<JS
+ function ajax_return_check(sales_check) {
+ jQuery('#modal-7').modal('show', {backdrop: 'static'}); $('#modal-7 .modal-title').text('');
+
+ $.ajax({
+ url: '/orders/ajax_return_check/',
+ method: 'post',
+ dataType: 'html',
+ data: { sales_check: ''+sales_check+''},
+ success: function(data){
+ $('#modal-7 .modal-body').html(data);
+ $('#check__'+sales_check+'').remove();
+ }});
+ }
+JS, $this::POS_HEAD);
?>
<div class="p-8">
+ <h1>Заказы из amo и чеки в магазинах <a href="/content/Amo142/?status_id=142&date1='<?= date("Y-m-d", time())?>'&date2='<?= date("Y-m-d", time())?>'" target="new" class="btn btn-info">
+ импортировать заказы из amo с датой доставки <?= date("d.m.Y", time()) ?> </a></h1>
+ <a href="?umoup=1" class="btn btn-info">синхронизировать заказы с amo</a>
<?= GridView::widget([
'filterModel' => $searchModel,
'dataProvider' => $dataProvider,
$div = '<div class="w-100 text-center">';
$div .= Html::a(
- $model->delivery_date,
+ 'Заказ в AMO',
'https://bazacvetov24.amocrm.ru/leads/detail/' . $model->amo_id,
[
'target' => 'new' . $model->amo_id
$div .= '</div>';
+ $div .= '<div>Дата доставки: <span class="" >' . $model->delivery_date . '</span></div>';
+
$div .= '<div>Магазин: <span class="" >' . ($model->storeObj->name ?? '-') . '</span></div>';
$div .= '<div> Доставка: <span class="">' . $model->delivery . '</span></div>';
}
],
[
- 'label' => 'Статусы',
+ 'label' => 'Статус',
'format' => 'raw',
'value' => function (OrdersAmo $model) {
- if ($model->status_id === 142) {
- $div = '<span class="badge bg-success">Успешно</span>';
- } else if ($model->status_id === 143) {
- $div = '<span class="badge bg-danger">Отказ</span>';
+ if ($model->status) {
+ return '<h3><span class="badge" style="background-color: ' . $model->status->color .'">' . $model->status->name .'</span></h3>';
} else {
- $div = '<span class="badge bg-info">' . $model->status_id .'</span>';
+ return '<h3><span class="badge bg-danger">' . $model->status_id .'</span></h3>';
}
- return $div;
}
],
[
'label' => 'Информация по чекам',
'format' => 'raw',
'value' => function (OrdersAmo $model) {
- $table = '<table class="table text-center"><thead><td>GUID</td><td>Операция</td></thead><tbody>';
+ $table = '<table class="table text-center"><thead><td>GUID</td><td>Операция</td><td>Дата</td><td>Кто провел чек</td><td></td></thead><tbody>';
if (!$model->sales) {
- $tr = '<tr class="table-danger"><td>-</td><td>-</td></tr>';
+ $tr = '<tr class="table-danger"><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>';
$table .= $tr;
}
foreach ($model->sales as $sale) {
if ($sale->operation === 'Продажа') {
- $tr = '<tr class="table-success"><td>' . $sale->id . '</td><td>' . $sale->operation . '</td></tr>';
+ $tr = '<tr class="table-success"><td>' . $sale->id . '</td><td>' . $sale->operation . '</td><td>' . $sale->date . '</td><td>' . $sale->sellerById->name . '</td><td><button class="btn btn-danger" onclick="ajax_return_check(\'' . $sale->id . '\')">Сделать возврат</button></td></tr>';
} else if ($sale->operation === 'Возврат') {
- $tr = '<tr class="table-warning"><td>' . $sale->id . '</td><td>' . $sale->operation . '</td></tr>';
+ $tr = '<tr class="table-warning"><td>' . $sale->id . '</td><td>' . $sale->operation . ': ' . ($sale->sales_check != '' ? $sale->sales_check : '-') . '</td><td>' . $sale->date . '</td><td>' . $sale->sellerById->name . '</td><td></td></tr>';
} else {
- $tr = '<tr class="table-info"><td>' . $sale->id . '</td><td>' . $sale->operation . '</td></tr>';
+ $tr = '<tr class="table-info"><td>' . $sale->id . '</td><td>' . $sale->operation . '</td><td>' . $sale->date . '</td><td>' . $sale->sellerById->name . '</td><td></td></tr>';
}
+
+
$table .= $tr;
}
]
]) ?>
</div>
+<!-- MODAL -->
+<div class="modal fade" id="modal-7">
+ <div class="modal-dialog modal-dialog-centered text-center" role="document">
+ <div class="modal-content modal-content-demo">
+ <div class="modal-header">
+ <h6 class="modal-title"></h6><button aria-label="Close" class="btn-close" data-bs-dismiss="modal" type="button"><span aria-hidden="true">×</span></button>
+ </div>
+ <div class="modal-body">
+
+ </div>
+ <div class="modal-footer">
+
+ <button class="btn btn-secondary" data-bs-dismiss="modal" type="button">закрыть</button>
+ </div>
+ </div>
+ </div>
+</div>
\ No newline at end of file