forked from morrning/hesabixCore
almost finish postalcode to address
This commit is contained in:
parent
7762613814
commit
2cb7b8945c
37
hesabixCore/migrations/Version20241201000000.php
Normal file
37
hesabixCore/migrations/Version20241201000000.php
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DoctrineMigrations;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Schema\Schema;
|
||||||
|
use Doctrine\Migrations\AbstractMigration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-generated Migration: Please modify to your needs!
|
||||||
|
*/
|
||||||
|
final class Version20241201000000 extends AbstractMigration
|
||||||
|
{
|
||||||
|
public function getDescription(): string
|
||||||
|
{
|
||||||
|
return 'Create postal_code_inquiry table';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function up(Schema $schema): void
|
||||||
|
{
|
||||||
|
$this->addSql('CREATE TABLE postal_code_inquiry (
|
||||||
|
id INT AUTO_INCREMENT NOT NULL,
|
||||||
|
postal_code VARCHAR(10) NOT NULL,
|
||||||
|
address_data JSON NOT NULL,
|
||||||
|
created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\',
|
||||||
|
updated_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\',
|
||||||
|
UNIQUE INDEX UNIQ_POSTAL_CODE (postal_code),
|
||||||
|
PRIMARY KEY(id)
|
||||||
|
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(Schema $schema): void
|
||||||
|
{
|
||||||
|
$this->addSql('DROP TABLE postal_code_inquiry');
|
||||||
|
}
|
||||||
|
}
|
|
@ -454,6 +454,7 @@ class AdminController extends AbstractController
|
||||||
$resp['inquiryZohalAPIKey'] = $registryMGR->get('system', key: 'inquiryZohalAPIKey');
|
$resp['inquiryZohalAPIKey'] = $registryMGR->get('system', key: 'inquiryZohalAPIKey');
|
||||||
$resp['enablePostalCodeToAddress'] = $registryMGR->get('system', key: 'enablePostalCodeToAddress');
|
$resp['enablePostalCodeToAddress'] = $registryMGR->get('system', key: 'enablePostalCodeToAddress');
|
||||||
$resp['inquiryPanelEnable'] = $registryMGR->get('system', key: 'inquiryPanelEnable');
|
$resp['inquiryPanelEnable'] = $registryMGR->get('system', key: 'inquiryPanelEnable');
|
||||||
|
$resp['postalCodeToAddressFee'] = $registryMGR->get('system', key: 'postalCodeToAddressFee');
|
||||||
return $this->json($resp);
|
return $this->json($resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -482,6 +483,7 @@ class AdminController extends AbstractController
|
||||||
$registryMGR->update('system', 'inquiryZohalAPIKey', $params['inquiryZohalAPIKey']);
|
$registryMGR->update('system', 'inquiryZohalAPIKey', $params['inquiryZohalAPIKey']);
|
||||||
$registryMGR->update('system', 'enablePostalCodeToAddress', $params['enablePostalCodeToAddress']);
|
$registryMGR->update('system', 'enablePostalCodeToAddress', $params['enablePostalCodeToAddress']);
|
||||||
$registryMGR->update('system', 'inquiryPanelEnable', $params['inquiryPanelEnable']);
|
$registryMGR->update('system', 'inquiryPanelEnable', $params['inquiryPanelEnable']);
|
||||||
|
$registryMGR->update('system', 'postalCodeToAddressFee', $params['postalCodeToAddressFee']);
|
||||||
$entityManager->persist($item);
|
$entityManager->persist($item);
|
||||||
$entityManager->flush();
|
$entityManager->flush();
|
||||||
return $this->json(['result' => 1]);
|
return $this->json(['result' => 1]);
|
||||||
|
|
|
@ -17,6 +17,8 @@ use App\Service\Printers;
|
||||||
use App\Entity\PrintOptions;
|
use App\Entity\PrintOptions;
|
||||||
use App\Service\Log;
|
use App\Service\Log;
|
||||||
use App\Entity\Business;
|
use App\Entity\Business;
|
||||||
|
use App\Service\registryMGR;
|
||||||
|
use App\Service\Inquiry;
|
||||||
|
|
||||||
class PlugInquiryMainController extends AbstractController
|
class PlugInquiryMainController extends AbstractController
|
||||||
{
|
{
|
||||||
|
@ -27,11 +29,105 @@ class PlugInquiryMainController extends AbstractController
|
||||||
$this->entityManager = $entityManager;
|
$this->entityManager = $entityManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Route('/api/admin/plugins/inquiry/settings/get', name: 'plugin_inquiry_settings_get', methods: ['GET'])]
|
#[Route('/api/plugins/inquiry/settings/get', name: 'plugin_inquiry_settings_get', methods: ['GET'])]
|
||||||
public function plugin_inquiry_settings_get(EntityManagerInterface $entityManager, Access $access) : JsonResponse
|
public function plugin_inquiry_settings_get(registryMGR $registryMGR): JsonResponse
|
||||||
{
|
{
|
||||||
|
|
||||||
return $this->json([]);
|
$resp['inquiryPanel'] = $registryMGR->get('system', key: 'inquiryPanel');
|
||||||
|
$resp['enablePostalCodeToAddress'] = $registryMGR->get('system', key: 'enablePostalCodeToAddress');
|
||||||
|
$resp['inquiryPanelEnable'] = $registryMGR->get('system', key: 'inquiryPanelEnable');
|
||||||
|
$resp['postalCodeToAddressFee'] = $registryMGR->get('system', key: 'postalCodeToAddressFee');
|
||||||
|
return $this->json($resp);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[Route('/api/plugins/inquiry/postalcode-to-address', name: 'plugin_inquiry_postalcode_to_address', methods: ['POST'])]
|
||||||
|
public function plugin_inquiry_postalcode_to_address(Inquiry $inquiry, Access $access, Request $request, registryMGR $registryMGR, Log $log): JsonResponse
|
||||||
|
{
|
||||||
|
$acc = $access->hasRole('join');
|
||||||
|
if (!$acc) {
|
||||||
|
return $this->json([
|
||||||
|
'success' => false,
|
||||||
|
'message' => 'شما دسترسی به این سرویس را ندارید'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// دریافت کد پستی از درخواست
|
||||||
|
$data = json_decode($request->getContent(), true);
|
||||||
|
$postalCode = $data['postal_code'] ?? null;
|
||||||
|
if (!$postalCode) {
|
||||||
|
return $this->json([
|
||||||
|
'success' => false,
|
||||||
|
'message' => 'کد پستی ارسال نشده است'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// فراخوانی سرویس استعلام کد پستی
|
||||||
|
$result = $inquiry->postalCodeToAddress($postalCode);
|
||||||
|
|
||||||
|
// بررسی نتیجه و بازگرداندن پاسخ مناسب
|
||||||
|
if (isset($result['result']) && $result['result'] == 1) {
|
||||||
|
$isFromCache = isset($result['response_body']['message']) &&
|
||||||
|
strpos($result['response_body']['message'], 'از کش') !== false;
|
||||||
|
|
||||||
|
// ثبت لاگ بر اساس منبع داده
|
||||||
|
$logMessage = $isFromCache
|
||||||
|
? "استعلام کد پستی {$postalCode} از کش (بدون کسر کارمزد)"
|
||||||
|
: "استعلام کد پستی {$postalCode} از API (کسر کارمزد: " . $registryMGR->get('system', key: 'postalCodeToAddressFee') . " ریال)";
|
||||||
|
|
||||||
|
$log->insert(
|
||||||
|
'استعلام',
|
||||||
|
$logMessage,
|
||||||
|
$acc['user'],
|
||||||
|
$acc['bid']
|
||||||
|
);
|
||||||
|
|
||||||
|
// فقط در صورت عدم وجود در کش، کارمزد کسر شود
|
||||||
|
if (!$isFromCache) {
|
||||||
|
if ($acc['bid']->getSmsCharge() < $registryMGR->get('system', key: 'postalCodeToAddressFee')) {
|
||||||
|
// ثبت لاگ عدم موجودی کافی
|
||||||
|
$log->insert(
|
||||||
|
'استعلام',
|
||||||
|
"عدم موجودی کافی برای استعلام کد پستی {$postalCode}",
|
||||||
|
$acc['user'],
|
||||||
|
$acc['bid']
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->json([
|
||||||
|
'success' => false,
|
||||||
|
'message' => 'موجودی شما برای این سرویس کافی نیست'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$business = $acc['bid'];
|
||||||
|
$business->setSmsCharge($business->getSmsCharge() - $registryMGR->get('system', key: 'postalCodeToAddressFee'));
|
||||||
|
$this->entityManager->persist($business);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->json([
|
||||||
|
'success' => true,
|
||||||
|
'data' => $result['response_body']['data']['address'] ?? null,
|
||||||
|
'message' => $result['response_body']['message'] ?? 'موفق',
|
||||||
|
'from_cache' => $isFromCache
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
// ثبت لاگ خطا
|
||||||
|
$errorMessage = $result['message'] ?? 'خطا در استعلام کد پستی';
|
||||||
|
$log->insert(
|
||||||
|
'استعلام',
|
||||||
|
"خطا در استعلام کد پستی {$postalCode}: {$errorMessage}",
|
||||||
|
$acc['user'],
|
||||||
|
$acc['bid']
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->json([
|
||||||
|
'success' => false,
|
||||||
|
'message' => $errorMessage,
|
||||||
|
'error_code' => $result['error_code'] ?? null
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
83
hesabixCore/src/Entity/PostalCodeInquiry.php
Normal file
83
hesabixCore/src/Entity/PostalCodeInquiry.php
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Entity;
|
||||||
|
|
||||||
|
use App\Repository\PostalCodeInquiryRepository;
|
||||||
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
|
||||||
|
#[ORM\Entity(repositoryClass: PostalCodeInquiryRepository::class)]
|
||||||
|
#[ORM\Table(name: 'postal_code_inquiry')]
|
||||||
|
class PostalCodeInquiry
|
||||||
|
{
|
||||||
|
#[ORM\Id]
|
||||||
|
#[ORM\GeneratedValue]
|
||||||
|
#[ORM\Column]
|
||||||
|
private ?int $id = null;
|
||||||
|
|
||||||
|
#[ORM\Column(length: 10, unique: true)]
|
||||||
|
private ?string $postalCode = null;
|
||||||
|
|
||||||
|
#[ORM\Column(type: 'json')]
|
||||||
|
private array $addressData = [];
|
||||||
|
|
||||||
|
#[ORM\Column]
|
||||||
|
private ?\DateTimeImmutable $createdAt = null;
|
||||||
|
|
||||||
|
#[ORM\Column]
|
||||||
|
private ?\DateTimeImmutable $updatedAt = null;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->createdAt = new \DateTimeImmutable();
|
||||||
|
$this->updatedAt = new \DateTimeImmutable();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getId(): ?int
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPostalCode(): ?string
|
||||||
|
{
|
||||||
|
return $this->postalCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setPostalCode(string $postalCode): static
|
||||||
|
{
|
||||||
|
$this->postalCode = $postalCode;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAddressData(): array
|
||||||
|
{
|
||||||
|
return $this->addressData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setAddressData(array $addressData): static
|
||||||
|
{
|
||||||
|
$this->addressData = $addressData;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCreatedAt(): ?\DateTimeImmutable
|
||||||
|
{
|
||||||
|
return $this->createdAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setCreatedAt(\DateTimeImmutable $createdAt): static
|
||||||
|
{
|
||||||
|
$this->createdAt = $createdAt;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getUpdatedAt(): ?\DateTimeImmutable
|
||||||
|
{
|
||||||
|
return $this->updatedAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setUpdatedAt(\DateTimeImmutable $updatedAt): static
|
||||||
|
{
|
||||||
|
$this->updatedAt = $updatedAt;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
46
hesabixCore/src/Repository/PostalCodeInquiryRepository.php
Normal file
46
hesabixCore/src/Repository/PostalCodeInquiryRepository.php
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Repository;
|
||||||
|
|
||||||
|
use App\Entity\PostalCodeInquiry;
|
||||||
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||||
|
use Doctrine\Persistence\ManagerRegistry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends ServiceEntityRepository<PostalCodeInquiry>
|
||||||
|
*
|
||||||
|
* @method PostalCodeInquiry|null find($id, $lockMode = null, $lockVersion = null)
|
||||||
|
* @method PostalCodeInquiry|null findOneBy(array $criteria, array $orderBy = null)
|
||||||
|
* @method PostalCodeInquiry[] findAll()
|
||||||
|
* @method PostalCodeInquiry[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
|
||||||
|
*/
|
||||||
|
class PostalCodeInquiryRepository extends ServiceEntityRepository
|
||||||
|
{
|
||||||
|
public function __construct(ManagerRegistry $registry)
|
||||||
|
{
|
||||||
|
parent::__construct($registry, PostalCodeInquiry::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findByPostalCode(string $postalCode): ?PostalCodeInquiry
|
||||||
|
{
|
||||||
|
return $this->findOneBy(['postalCode' => $postalCode]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function save(PostalCodeInquiry $entity, bool $flush = false): void
|
||||||
|
{
|
||||||
|
$this->getEntityManager()->persist($entity);
|
||||||
|
|
||||||
|
if ($flush) {
|
||||||
|
$this->getEntityManager()->flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function remove(PostalCodeInquiry $entity, bool $flush = false): void
|
||||||
|
{
|
||||||
|
$this->getEntityManager()->remove($entity);
|
||||||
|
|
||||||
|
if ($flush) {
|
||||||
|
$this->getEntityManager()->flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
158
hesabixCore/src/Service/Inquiry.php
Normal file
158
hesabixCore/src/Service/Inquiry.php
Normal file
|
@ -0,0 +1,158 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Service;
|
||||||
|
|
||||||
|
use App\Entity\PostalCodeInquiry;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
|
class Inquiry
|
||||||
|
{
|
||||||
|
public function __construct(private EntityManagerInterface $entityManager)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postalCodeToAddress($postalCode)
|
||||||
|
{
|
||||||
|
// ابتدا بررسی دیتابیس
|
||||||
|
$existingInquiry = $this->entityManager->getRepository(PostalCodeInquiry::class)->findByPostalCode($postalCode);
|
||||||
|
|
||||||
|
if ($existingInquiry) {
|
||||||
|
// اگر در دیتابیس موجود است، از آن استفاده کن
|
||||||
|
$addressData = $existingInquiry->getAddressData();
|
||||||
|
return [
|
||||||
|
'result' => 1,
|
||||||
|
'response_body' => [
|
||||||
|
'data' => [
|
||||||
|
'address' => $addressData
|
||||||
|
],
|
||||||
|
'message' => 'موفق (از کش)',
|
||||||
|
'error_code' => null
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// اگر در دیتابیس موجود نیست، از API استفاده کن
|
||||||
|
$registryMGR = new RegistryMGR($this->entityManager);
|
||||||
|
$inquiryPanel = $registryMGR->get('system', key: 'inquiryPanel');
|
||||||
|
if($inquiryPanel == 'zohal'){
|
||||||
|
$inquiryZohalAPIKey = $registryMGR->get('system', key: 'inquiryZohalAPIKey');
|
||||||
|
|
||||||
|
// بررسی وجود API Key
|
||||||
|
if (empty($inquiryZohalAPIKey)) {
|
||||||
|
return [
|
||||||
|
'result' => 0,
|
||||||
|
'message' => 'API Key تنظیم نشده است',
|
||||||
|
'error_code' => 'API_KEY_MISSING'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// استفاده از API جدید زحل
|
||||||
|
$url = "https://service.zohal.io/api/v0/services/inquiry/postal_code_inquiry";
|
||||||
|
|
||||||
|
// آمادهسازی دادههای JSON
|
||||||
|
$postData = json_encode([
|
||||||
|
'postal_code' => $postalCode
|
||||||
|
]);
|
||||||
|
|
||||||
|
// تنظیمات cURL
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch, CURLOPT_POST, true);
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||||
|
'Content-Type: application/json',
|
||||||
|
'Content-Length: ' . strlen($postData),
|
||||||
|
'Authorization: Bearer ' . $inquiryZohalAPIKey,
|
||||||
|
'X-API-Key: ' . $inquiryZohalAPIKey
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = curl_exec($ch);
|
||||||
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
$curlError = curl_error($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
// بررسی خطای cURL
|
||||||
|
if ($curlError) {
|
||||||
|
return [
|
||||||
|
'result' => 0,
|
||||||
|
'message' => 'خطا در ارتباط با سرور: ' . $curlError,
|
||||||
|
'error_code' => 'CURL_ERROR'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($httpCode === 200) {
|
||||||
|
$data = json_decode($response, true);
|
||||||
|
|
||||||
|
// بررسی کدهای result
|
||||||
|
if (isset($data['result'])) {
|
||||||
|
switch ($data['result']) {
|
||||||
|
case 1:
|
||||||
|
// موفق - ذخیره در دیتابیس
|
||||||
|
if (isset($data['response_body']['data']['address'])) {
|
||||||
|
$this->saveToDatabase($postalCode, $data['response_body']['data']['address']);
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
case 4:
|
||||||
|
return [
|
||||||
|
'result' => 4,
|
||||||
|
'message' => 'توکن غیر فعال شده است',
|
||||||
|
'error_code' => 'TOKEN_INACTIVE'
|
||||||
|
];
|
||||||
|
case 5:
|
||||||
|
return [
|
||||||
|
'result' => 5,
|
||||||
|
'message' => 'سرویس در دسترسی نمیباشد',
|
||||||
|
'error_code' => 'SERVICE_UNAVAILABLE'
|
||||||
|
];
|
||||||
|
case 6:
|
||||||
|
return [
|
||||||
|
'result' => 6,
|
||||||
|
'message' => 'فراخوانی وبسرویس با پارامترهای ورودی صحیح نمیباشد',
|
||||||
|
'error_code' => 'INVALID_PARAMETERS'
|
||||||
|
];
|
||||||
|
default:
|
||||||
|
return [
|
||||||
|
'result' => $data['result'],
|
||||||
|
'message' => $data['message'] ?? 'خطای نامشخص',
|
||||||
|
'error_code' => $data['error_code'] ?? 'UNKNOWN_ERROR'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
} else {
|
||||||
|
return [
|
||||||
|
'result' => 0,
|
||||||
|
'message' => 'خطا در ارتباط با سرویس استعلام کد پستی (کد خطا: ' . $httpCode . ')',
|
||||||
|
'error_code' => 'HTTP_ERROR_' . $httpCode
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'result' => 0,
|
||||||
|
'message' => 'سرویس استعلام کد پستی فعال نیست',
|
||||||
|
'error_code' => 'SERVICE_NOT_ACTIVE'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function saveToDatabase(string $postalCode, array $addressData): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$inquiry = new PostalCodeInquiry();
|
||||||
|
$inquiry->setPostalCode($postalCode);
|
||||||
|
$inquiry->setAddressData($addressData);
|
||||||
|
$inquiry->setUpdatedAt(new \DateTimeImmutable());
|
||||||
|
|
||||||
|
$this->entityManager->persist($inquiry);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
// ثبت لاگ ذخیره موفق در دیتابیس
|
||||||
|
error_log("کد پستی {$postalCode} با موفقیت در کش ذخیره شد");
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// در صورت خطا در ذخیره، فقط لاگ کن و ادامه بده
|
||||||
|
error_log('خطا در ذخیره استعلام کد پستی: ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
188
webUI/src/components/widgets/inquiry/postalcode2address.vue
Normal file
188
webUI/src/components/widgets/inquiry/postalcode2address.vue
Normal file
|
@ -0,0 +1,188 @@
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import axios from "axios";
|
||||||
|
|
||||||
|
export default {
|
||||||
|
name: "PostalCode2Address",
|
||||||
|
props: {
|
||||||
|
postalCode: {
|
||||||
|
type: String,
|
||||||
|
default: ''
|
||||||
|
}
|
||||||
|
},
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
dialog: false,
|
||||||
|
settings: {
|
||||||
|
inquiryPanelEnable: false,
|
||||||
|
enablePostalCodeToAddress: false,
|
||||||
|
postalCodeToAddressFee: 0
|
||||||
|
},
|
||||||
|
loading: false,
|
||||||
|
result: null,
|
||||||
|
error: null
|
||||||
|
};
|
||||||
|
},
|
||||||
|
computed: {
|
||||||
|
shouldShow() {
|
||||||
|
// اگر تنظیمات هنوز لود نشده، نمایش بده
|
||||||
|
if (!this.settings.inquiryPanelEnable && !this.settings.enablePostalCodeToAddress) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return (this.settings.inquiryPanelEnable === '1' || this.settings.inquiryPanelEnable === true) &&
|
||||||
|
(this.settings.enablePostalCodeToAddress === '1' || this.settings.enablePostalCodeToAddress === true);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
mounted() {
|
||||||
|
this.loadSettings();
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
async loadSettings() {
|
||||||
|
try {
|
||||||
|
console.log('در حال دریافت تنظیمات...');
|
||||||
|
this.loading = true;
|
||||||
|
const response = await axios.get('/api/plugins/inquiry/settings/get');
|
||||||
|
console.log('تنظیمات دریافت شد:', response.data);
|
||||||
|
this.settings = response.data;
|
||||||
|
console.log('shouldShow:', this.shouldShow);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('خطا در دریافت تنظیمات:', error);
|
||||||
|
} finally {
|
||||||
|
this.loading = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
openDialog() {
|
||||||
|
this.dialog = true;
|
||||||
|
},
|
||||||
|
closeDialog() {
|
||||||
|
this.dialog = false;
|
||||||
|
},
|
||||||
|
async submitPostalCode() {
|
||||||
|
this.loading = true;
|
||||||
|
this.result = null;
|
||||||
|
this.error = null;
|
||||||
|
try {
|
||||||
|
console.log('ارسال درخواست برای کد پستی:', this.postalCode);
|
||||||
|
const response = await axios.post('/api/plugins/inquiry/postalcode-to-address', {
|
||||||
|
postal_code: this.postalCode
|
||||||
|
});
|
||||||
|
console.log('پاسخ دریافتی:', response.data);
|
||||||
|
if (response.data && response.data.success) {
|
||||||
|
this.result = response.data.data;
|
||||||
|
// ارسال دادهها به کامپوننت والد
|
||||||
|
const addressDataWithPostalCode = {
|
||||||
|
...response.data.data,
|
||||||
|
postalCode: this.postalCode
|
||||||
|
};
|
||||||
|
this.$emit('address-found', addressDataWithPostalCode);
|
||||||
|
|
||||||
|
// نمایش پیام مناسب بر اساس منبع داده
|
||||||
|
const message = response.data.from_cache
|
||||||
|
? 'اطلاعات آدرس از کش دریافت شد (بدون کسر کارمزد)'
|
||||||
|
: 'اطلاعات آدرس با موفقیت دریافت شد';
|
||||||
|
|
||||||
|
// بستن دیالوگ بعد از موفقیت
|
||||||
|
this.dialog = false;
|
||||||
|
|
||||||
|
// نمایش پیام موفقیت
|
||||||
|
this.$nextTick(() => {
|
||||||
|
this.showSnackbar(message, 'success');
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.error = (response.data && response.data.message) || "خطا در دریافت اطلاعات";
|
||||||
|
|
||||||
|
// مدیریت خطاهای خاص
|
||||||
|
if (response.data && response.data.error_code === 'HTTP_ERROR_504') {
|
||||||
|
this.error = "سرویس استعلام کد پستی در دسترس نیست. لطفاً بعداً تلاش کنید.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('خطا در ارسال درخواست:', e);
|
||||||
|
this.error = "خطا در ارتباط با سرور";
|
||||||
|
} finally {
|
||||||
|
this.loading = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
showSnackbar(text, color = 'success', timeout = 3000) {
|
||||||
|
// ارسال event به کامپوننت والد برای نمایش snackbar
|
||||||
|
this.$emit('show-snackbar', { text, color, timeout });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div v-if="shouldShow">
|
||||||
|
<v-tooltip text="تبدیل کد پستی به آدرس" location="top">
|
||||||
|
<template v-slot:activator="{ props }">
|
||||||
|
<v-btn v-bind="props" size="x-small" color="primary" variant="text" @click="openDialog" class="px-1">
|
||||||
|
<v-icon>mdi-map-marker</v-icon>
|
||||||
|
</v-btn>
|
||||||
|
</template>
|
||||||
|
</v-tooltip>
|
||||||
|
|
||||||
|
<v-dialog v-model="dialog" max-width="500px">
|
||||||
|
<v-card>
|
||||||
|
<v-toolbar color="primary" dark>
|
||||||
|
<v-toolbar-title>تبدیل کد پستی به آدرس</v-toolbar-title>
|
||||||
|
<v-spacer></v-spacer>
|
||||||
|
<v-btn icon @click="closeDialog">
|
||||||
|
<v-icon>mdi-close</v-icon>
|
||||||
|
</v-btn>
|
||||||
|
</v-toolbar>
|
||||||
|
|
||||||
|
<v-card-text class="pt-4">
|
||||||
|
<div class="mb-4">
|
||||||
|
<p class="text-body-2">
|
||||||
|
با استفاده از این سرویس میتوانید کد پستی را به آدرس کامل تبدیل کنید.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<v-alert type="info" variant="tonal" class="mb-4">
|
||||||
|
<template v-slot:prepend>
|
||||||
|
<v-icon>mdi-information</v-icon>
|
||||||
|
</template>
|
||||||
|
<div>
|
||||||
|
<strong>کارمزد سرویس:</strong> {{ settings.postalCodeToAddressFee.toLocaleString() }} ریال
|
||||||
|
<br>
|
||||||
|
<small class="text-caption">
|
||||||
|
💡 اگر این کد پستی قبلاً استعلام شده باشد، کارمزدی کسر نمیشود
|
||||||
|
</small>
|
||||||
|
</div>
|
||||||
|
</v-alert>
|
||||||
|
|
||||||
|
<v-text-field
|
||||||
|
:model-value="postalCode"
|
||||||
|
label="کد پستی"
|
||||||
|
placeholder="مثال: 1234567890"
|
||||||
|
prepend-inner-icon="mdi-mailbox"
|
||||||
|
hide-details
|
||||||
|
class="mb-4"
|
||||||
|
readonly
|
||||||
|
></v-text-field>
|
||||||
|
|
||||||
|
<v-btn
|
||||||
|
color="primary"
|
||||||
|
block
|
||||||
|
:loading="loading"
|
||||||
|
:disabled="!postalCode || postalCode.length < 10"
|
||||||
|
@click="submitPostalCode"
|
||||||
|
>
|
||||||
|
تبدیل به آدرس
|
||||||
|
</v-btn>
|
||||||
|
|
||||||
|
<!-- نمایش خطا -->
|
||||||
|
<v-alert
|
||||||
|
v-if="error"
|
||||||
|
type="error"
|
||||||
|
variant="tonal"
|
||||||
|
class="mt-4"
|
||||||
|
border="start"
|
||||||
|
>
|
||||||
|
{{ error }}
|
||||||
|
</v-alert>
|
||||||
|
</v-card-text>
|
||||||
|
</v-card>
|
||||||
|
</v-dialog>
|
||||||
|
</div>
|
||||||
|
</template>
|
|
@ -817,6 +817,7 @@ const fa_lang = {
|
||||||
inquiry_zohal_api_key_placeholder: "کلید API زحل",
|
inquiry_zohal_api_key_placeholder: "کلید API زحل",
|
||||||
inquiry_zohal_api_key_label: "کلید API زحل",
|
inquiry_zohal_api_key_label: "کلید API زحل",
|
||||||
enable_postalcode_to_address: "تبدیل کد پستی به آدرس",
|
enable_postalcode_to_address: "تبدیل کد پستی به آدرس",
|
||||||
|
postalcode_to_address_fee: "کارمزد تبدیل کد پستی به آدرس",
|
||||||
inquiry_panel_enable: "فعال سازی پنل سامانه استعلامات",
|
inquiry_panel_enable: "فعال سازی پنل سامانه استعلامات",
|
||||||
inquiry_panel: "پنل سامانه استعلامات",
|
inquiry_panel: "پنل سامانه استعلامات",
|
||||||
inquiry_panel_zohal: "زحل",
|
inquiry_panel_zohal: "زحل",
|
||||||
|
|
|
@ -141,7 +141,11 @@
|
||||||
</v-col>
|
</v-col>
|
||||||
<v-col cols="12" md="6">
|
<v-col cols="12" md="6">
|
||||||
<v-text-field v-model="person.postalcode" :label="$t('pages.person.postal_code')" dense
|
<v-text-field v-model="person.postalcode" :label="$t('pages.person.postal_code')" dense
|
||||||
prepend-inner-icon="mdi-mailbox" hide-details />
|
prepend-inner-icon="mdi-mailbox" hide-details>
|
||||||
|
<template v-slot:append-inner>
|
||||||
|
<PostalCode2Address :postal-code="person.postalcode" @address-found="fillAddressFields" @show-snackbar="showSnackbarFromChild" />
|
||||||
|
</template>
|
||||||
|
</v-text-field>
|
||||||
</v-col>
|
</v-col>
|
||||||
<v-col cols="12">
|
<v-col cols="12">
|
||||||
<v-textarea v-model="person.address" :label="$t('pages.person.address')" dense
|
<v-textarea v-model="person.address" :label="$t('pages.person.address')" dense
|
||||||
|
@ -211,15 +215,29 @@
|
||||||
<v-overlay :model-value="loading" contained class="align-center justify-center">
|
<v-overlay :model-value="loading" contained class="align-center justify-center">
|
||||||
<v-progress-circular indeterminate size="64" />
|
<v-progress-circular indeterminate size="64" />
|
||||||
</v-overlay>
|
</v-overlay>
|
||||||
|
|
||||||
|
<!-- Snackbar برای نمایش پیامها -->
|
||||||
|
<v-snackbar v-model="snackbar.show" :color="snackbar.color" :timeout="snackbar.timeout">
|
||||||
|
{{ snackbar.text }}
|
||||||
|
<template v-slot:actions>
|
||||||
|
<v-btn color="white" text @click="snackbar.show = false">
|
||||||
|
بستن
|
||||||
|
</v-btn>
|
||||||
|
</template>
|
||||||
|
</v-snackbar>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import Swal from "sweetalert2";
|
import Swal from "sweetalert2";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { ref } from "vue";
|
import { ref } from "vue";
|
||||||
|
import PostalCode2Address from "@/components/widgets/inquiry/postalcode2address.vue";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "insert",
|
name: "insert",
|
||||||
|
components: {
|
||||||
|
PostalCode2Address
|
||||||
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
tabs: '0',
|
tabs: '0',
|
||||||
|
@ -250,6 +268,12 @@ export default {
|
||||||
accounts: [],
|
accounts: [],
|
||||||
prelabel: ref(null),
|
prelabel: ref(null),
|
||||||
speedAccess: false
|
speedAccess: false
|
||||||
|
},
|
||||||
|
snackbar: {
|
||||||
|
show: false,
|
||||||
|
text: '',
|
||||||
|
color: '',
|
||||||
|
timeout: 3000
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
@ -362,7 +386,7 @@ export default {
|
||||||
try {
|
try {
|
||||||
const response = await axios.post('/api/person/mod/' + this.person.code, this.person);
|
const response = await axios.post('/api/person/mod/' + this.person.code, this.person);
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
if (response.data.result === 2) {
|
if (response.data && response.data.result === 2) {
|
||||||
Swal.fire({
|
Swal.fire({
|
||||||
text: this.$t('pages.person.already_exists'),
|
text: this.$t('pages.person.already_exists'),
|
||||||
icon: 'error',
|
icon: 'error',
|
||||||
|
@ -386,7 +410,72 @@ export default {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
showSnackbar(text, color = 'success', timeout = 3000) {
|
||||||
|
this.snackbar.show = true;
|
||||||
|
this.snackbar.text = text;
|
||||||
|
this.snackbar.color = color;
|
||||||
|
this.snackbar.timeout = timeout;
|
||||||
|
},
|
||||||
|
fillAddressFields(addressData) {
|
||||||
|
// پر کردن فیلدهای آدرس
|
||||||
|
this.person.keshvar = 'ایران'; // کشور به صورت پیشفرض ایران
|
||||||
|
this.person.ostan = addressData.province || '';
|
||||||
|
this.person.shahr = addressData.town || '';
|
||||||
|
this.person.postalcode = addressData.postalCode || '';
|
||||||
|
|
||||||
|
// ساخت آدرس کامل
|
||||||
|
let fullAddress = '';
|
||||||
|
|
||||||
|
if (addressData.district) {
|
||||||
|
fullAddress += addressData.district;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (addressData.street) {
|
||||||
|
if (fullAddress) fullAddress += ' - ';
|
||||||
|
fullAddress += addressData.street;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (addressData.street2) {
|
||||||
|
if (fullAddress) fullAddress += ' - ';
|
||||||
|
fullAddress += addressData.street2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (addressData.number) {
|
||||||
|
if (fullAddress) fullAddress += ' - پلاک ';
|
||||||
|
fullAddress += addressData.number;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (addressData.floor && addressData.floor !== 'همکف') {
|
||||||
|
if (fullAddress) fullAddress += ' - طبقه ';
|
||||||
|
fullAddress += addressData.floor;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (addressData.side_floor) {
|
||||||
|
if (fullAddress) fullAddress += ' - واحد ';
|
||||||
|
fullAddress += addressData.side_floor;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (addressData.building_name) {
|
||||||
|
if (fullAddress) fullAddress += ' - ';
|
||||||
|
fullAddress += addressData.building_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (addressData.description) {
|
||||||
|
if (fullAddress) fullAddress += ' - ';
|
||||||
|
fullAddress += addressData.description;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.person.address = fullAddress;
|
||||||
|
|
||||||
|
// نمایش پیام موفقیت
|
||||||
|
this.$nextTick(() => {
|
||||||
|
this.showSnackbar('اطلاعات آدرس با موفقیت تکمیل شد', 'success');
|
||||||
|
});
|
||||||
|
},
|
||||||
|
showSnackbarFromChild(snackbarData) {
|
||||||
|
this.showSnackbar(snackbarData.text, snackbarData.color, snackbarData.timeout);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -49,6 +49,7 @@ export default defineComponent({
|
||||||
inquiryZohalAPIKey: '',
|
inquiryZohalAPIKey: '',
|
||||||
enablePostalCodeToAddress: false,
|
enablePostalCodeToAddress: false,
|
||||||
inquiryPanelEnable: false,
|
inquiryPanelEnable: false,
|
||||||
|
postalCodeToAddressFee: 0,
|
||||||
},
|
},
|
||||||
loading: true,
|
loading: true,
|
||||||
}
|
}
|
||||||
|
@ -152,7 +153,7 @@ export default defineComponent({
|
||||||
<v-text-field class="" hide-details="auto" :label="$t('pages.manager.inquiry_zohal_api_key')"
|
<v-text-field class="" hide-details="auto" :label="$t('pages.manager.inquiry_zohal_api_key')"
|
||||||
v-model="systemInfo.inquiryZohalAPIKey" type="text" prepend-inner-icon="mdi-text"></v-text-field>
|
v-model="systemInfo.inquiryZohalAPIKey" type="text" prepend-inner-icon="mdi-text"></v-text-field>
|
||||||
</v-col>
|
</v-col>
|
||||||
<v-col cols="12" sm="12" md="4">
|
<v-col cols="12" sm="12" md="12">
|
||||||
<v-switch
|
<v-switch
|
||||||
v-model="systemInfo.inquiryPanelEnable"
|
v-model="systemInfo.inquiryPanelEnable"
|
||||||
:label="$t('pages.manager.inquiry_panel_enable')"
|
:label="$t('pages.manager.inquiry_panel_enable')"
|
||||||
|
@ -169,7 +170,17 @@ export default defineComponent({
|
||||||
hide-details="auto"
|
hide-details="auto"
|
||||||
inset
|
inset
|
||||||
></v-switch>
|
></v-switch>
|
||||||
|
</v-col>
|
||||||
|
<v-col cols="12" sm="12" md="4">
|
||||||
|
<v-text-field
|
||||||
|
v-model.number="systemInfo.postalCodeToAddressFee"
|
||||||
|
:label="$t('pages.manager.postalcode_to_address_fee')"
|
||||||
|
type="number"
|
||||||
|
min="0"
|
||||||
|
prepend-inner-icon="mdi-currency-usd"
|
||||||
|
hide-details="auto"
|
||||||
|
suffix="ریال"
|
||||||
|
></v-text-field>
|
||||||
</v-col>
|
</v-col>
|
||||||
</v-row>
|
</v-row>
|
||||||
<v-row class="mb-2">
|
<v-row class="mb-2">
|
||||||
|
|
Loading…
Reference in a new issue