restore composer.json, add mysqli extension
This commit is contained in:
17
app/Services/API/CalculaionService.php
Normal file
17
app/Services/API/CalculaionService.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\API;
|
||||
|
||||
|
||||
class CalculationService
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
echo "Empty func";
|
||||
}
|
||||
|
||||
public function get_file(int $kv, int $f): string
|
||||
{
|
||||
return "salom";
|
||||
}
|
||||
}
|
||||
217
app/Services/API/ContractService.php
Executable file
217
app/Services/API/ContractService.php
Executable file
@@ -0,0 +1,217 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\API;
|
||||
|
||||
use App\Models\Company;
|
||||
use App\Models\ContractTemplate;
|
||||
use App\Models\Currency;
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use PhpOffice\PhpWord\TemplateProcessor;
|
||||
|
||||
class ContractService
|
||||
{
|
||||
protected $lang;
|
||||
protected $type;
|
||||
protected $order;
|
||||
protected $product_total_price = 0;
|
||||
protected $product_total_tax = 0;
|
||||
protected $product_total_price_with_tax = 0;
|
||||
protected $total_product_count = 0;
|
||||
protected $currency = 0;
|
||||
protected $price_master_total = 0;
|
||||
protected $price_delivery_total = 0;
|
||||
|
||||
public function __construct($order, $lang = 'uz', $type = 'physical')
|
||||
{
|
||||
$this->order = $order;
|
||||
$this->lang = $lang;
|
||||
$this->type = $type;
|
||||
$this->currency = Currency::getCurrency()->dollar;
|
||||
}
|
||||
|
||||
|
||||
public function generate()
|
||||
{
|
||||
$tableData = $this->generateTableData();
|
||||
|
||||
$prepateData = $this->prepateData();
|
||||
$product = $this->order->products()->get()->first()->product()->get()->first();
|
||||
$category = $product->categories()->get()->first();
|
||||
$template = $this->findTemplate($product);
|
||||
$templatePath = $template->full_path();
|
||||
|
||||
// if (!file_exists($templatePath)) {
|
||||
// throw new \Exception("Template file does not exist at path: " . $templatePath);
|
||||
// }
|
||||
$templateProcessor = new TemplateProcessor($templatePath);
|
||||
$templateProcessor->setValue('contract_number', $this->order->id);
|
||||
|
||||
foreach ($prepateData as $key => $value) {
|
||||
try {
|
||||
$templateProcessor->setValue($key, $value);
|
||||
} catch (Exception $e) {
|
||||
Log::error($e);
|
||||
}
|
||||
}
|
||||
|
||||
$calc = tempnam(sys_get_temp_dir(), "calc_time_" . time() . "jpg");
|
||||
if ($product->calc)
|
||||
file_put_contents($calc, Storage::get($product->calc));
|
||||
// Generate table data
|
||||
$templateProcessor->cloneRowAndSetValues('product_index', $tableData);
|
||||
|
||||
// Set the total price
|
||||
$templateProcessor->setValue('product_total_price', number_format(ceiling($this->product_total_price * $this->currency, 100), 0, '.', ' '));
|
||||
$templateProcessor->setValue('total_product_count', $this->total_product_count);
|
||||
$templateProcessor->setValue('product_tax_itself_total', number_format(ceiling($this->product_total_tax * $this->currency, 100), 0, '.', ' '));
|
||||
$templateProcessor->setValue('product_price_with_tax_total', number_format($this->product_total_price_with_tax, 0, '.', ' '));
|
||||
|
||||
$templateProcessor->setValue('price_delivery_total', number_format($this->price_delivery_total, 0, ".", " "));
|
||||
$templateProcessor->setValue('price_master_total', number_format($this->price_master_total, 0, ".", " "));
|
||||
if ($product->calc)
|
||||
$templateProcessor->setImageValue("calc", ["path" => $calc, "width" => 700, "height" => 700]);
|
||||
else
|
||||
$templateProcessor->setValue("calc", "");
|
||||
// Set seller info
|
||||
$templateProcessor->setValues($this->prepareSellerData());
|
||||
// Create the directory if it does not exist
|
||||
if (!file_exists(storage_path('app/public/contracts'))) {
|
||||
mkdir(storage_path('app/public/contracts'), 0777, true);
|
||||
}
|
||||
// Define the file name and save path
|
||||
$fileName = 'contracts/contract_' . time() . '.docx';
|
||||
$filePath = storage_path("app/public/{$fileName}");
|
||||
// Save the new document
|
||||
$templateProcessor->saveAs($filePath);
|
||||
$this->storeImageToS3($fileName);
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
private function storeImageToS3($oldPath): void
|
||||
{
|
||||
// first store temp file and resize it, then upload to s3
|
||||
// 1 - store temp file
|
||||
$path = storage_path('app/public/' . $oldPath);
|
||||
|
||||
// Store the image on S3 or keep local
|
||||
if (in_array(env('FILESYSTEM_DISK'), ['s3', 'minio'])) {
|
||||
Storage::disk('s3')->put($oldPath, file_get_contents($path));
|
||||
}
|
||||
|
||||
// 3 - delete resized file
|
||||
if (in_array(env('FILESYSTEM_DISK'), ['s3', 'minio']) && is_file($path)) {
|
||||
unlink($path);
|
||||
}
|
||||
}
|
||||
|
||||
private function findTemplate($product = null)
|
||||
{
|
||||
$is_getgreen = $product->categories()->where(['company' => "getgreen"])->exists();
|
||||
$template = ContractTemplate::where('lang', $this->lang)->where('company', 'getgreen');
|
||||
if ($this->type == 'physical') {
|
||||
$template = $template->where('type', 'physical')->latest()->first();
|
||||
} elseif ($this->type == 'legal') {
|
||||
$template = $template->where('type', 'legal')->latest()->first();
|
||||
}
|
||||
|
||||
if (empty($template)) {
|
||||
throw new \Exception("Template not found");
|
||||
}
|
||||
|
||||
return $template;
|
||||
}
|
||||
|
||||
private function prepareSellerData()
|
||||
{
|
||||
$seller = Company::latest()->first();
|
||||
|
||||
if (!$seller) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
'seller_address' => $seller->address[$this->lang],
|
||||
'seller_inn' => $seller->inn,
|
||||
'seller_payment_account' => $seller->payment_account,
|
||||
'seller_bank_name' => $seller->bank_name[$this->lang],
|
||||
'seller_mfo' => $seller->mfo,
|
||||
'seller_oked' => $seller->oked,
|
||||
'seller_phone' => '+' . $seller->phone,
|
||||
'seller_director_full_name' => $seller->director_full_name[$this->lang],
|
||||
];
|
||||
}
|
||||
|
||||
private function prepateData()
|
||||
{
|
||||
// for legal user
|
||||
$date = Carbon::now();
|
||||
$date->locale($this->lang);
|
||||
|
||||
$data = [
|
||||
'date_day_in_num' => $date->day,
|
||||
'date_month' => $date->translatedFormat('F'),
|
||||
'date_year_in_num' => $date->year,
|
||||
];
|
||||
|
||||
if ($this->type == 'legal') {
|
||||
$data['client_company_name'] = $this->order->legalInfo->company_name;
|
||||
$data['client_inn'] = $this->order->legalInfo->inn;
|
||||
$data['client_bank_name'] = $this->order->legalInfo->bank_name;
|
||||
$data['client_payment_account'] = $this->order->legalInfo->payment_account;
|
||||
$data['client_mfo'] = $this->order->legalInfo->mfo;
|
||||
$data['client_phone'] = '+' . $this->order->legalInfo->phone;
|
||||
$data['client_address'] = $this->order->legalInfo->address;
|
||||
$data['client_full_name'] = $this->order->legalInfo->director_full_name;
|
||||
return $data;
|
||||
}
|
||||
|
||||
// For physical user
|
||||
$data['client_phone'] = '+' . $this->order->phone;
|
||||
$data['client_full_name'] = $this->order->full_name;
|
||||
$data['client_fish'] = $this->order->full_name;
|
||||
$data['client_address'] = $this->order->address()->get()->first()?->address;
|
||||
$data['clien_series'] = $this->order->series;
|
||||
$data['client_jshir'] = $this->order->jshir;
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function generateTableData()
|
||||
{
|
||||
$order = $this->order;
|
||||
|
||||
$values = [];
|
||||
foreach ($order->products as $product) {
|
||||
$this->total_product_count += $product->count;
|
||||
|
||||
$tax = $product->final_price / 112 * 12;
|
||||
$price_without_tax = $product->final_price - $tax;
|
||||
|
||||
$this->product_total_price += $price_without_tax;
|
||||
$this->product_total_tax += $tax;
|
||||
|
||||
$this->price_delivery_total += $order->price_delivery;
|
||||
$this->price_master_total += $order->price_master;
|
||||
|
||||
$total = ceiling($product->final_price * $this->currency, 100) + $order->price_delivery + $order->price_master;
|
||||
$this->product_total_price_with_tax += $total;
|
||||
|
||||
|
||||
$values[] = [
|
||||
'measurement' => $product->product->measurement?->name[$this->lang],
|
||||
'product_index' => count($values) + 1,
|
||||
'product_name' => $product->product->name[$this->lang],
|
||||
'product_price' => number_format(ceiling($price_without_tax * $this->currency, 100), 0, '.', ' '),
|
||||
'product_count' => $product->count,
|
||||
'product_tax_itself' => number_format(ceiling($tax * $this->currency, 100), 0, '.', ' '),
|
||||
'product_price_with_tax' => number_format($total, 0, '.', ' '),
|
||||
"price_master" => number_format($order->price_master, 0, ".", " "),
|
||||
"price_delivery" => number_format($order->price_delivery, 0, ".", " ")
|
||||
];
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
286
app/Services/API/OrderService.php
Executable file
286
app/Services/API/OrderService.php
Executable file
@@ -0,0 +1,286 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\API;
|
||||
|
||||
use App\Models\Address;
|
||||
use App\Models\Billing;
|
||||
use App\Models\Cart;
|
||||
use App\Models\City;
|
||||
use App\Models\Currency;
|
||||
use App\Models\DeliveryPrice;
|
||||
use App\Models\Order;
|
||||
use App\Models\OrderContract;
|
||||
use App\Models\Power;
|
||||
use App\Models\Product;
|
||||
use App\Models\Setting;
|
||||
use App\Models\UserLegalInfo;
|
||||
use App\Models\OrderProducts;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class OrderService
|
||||
{
|
||||
protected $data;
|
||||
public $order;
|
||||
protected $user;
|
||||
protected $address_id = null;
|
||||
protected $phone = null;
|
||||
protected $full_name = null;
|
||||
protected $legal_id = null;
|
||||
public $billing;
|
||||
protected $total;
|
||||
protected $currency;
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
$this->currency = Currency::orderBy('id', 'desc')->first()->dollar;
|
||||
$this->data = $data;
|
||||
$this->user = request()->user();
|
||||
}
|
||||
|
||||
private function calc()
|
||||
{
|
||||
$products_cart = collect($this->data['products']);
|
||||
|
||||
$product_ids = $products_cart->pluck('id');
|
||||
$products = Product::whereNull('child_id')->whereIn('id', $product_ids)->get();
|
||||
|
||||
$price_delivery = 0;
|
||||
$price_master = 0;
|
||||
|
||||
foreach ($products_cart as $cart) {
|
||||
$product = Product::query()->whereNull('child_id')->where('id', $cart['id'])->first();
|
||||
if (!empty($product)) {
|
||||
if ($product->count < $cart['count']) {
|
||||
|
||||
throw new \Exception($product->getName() . ' - ' . trans('app.errors.count'), 403);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//delivery price
|
||||
if ($this->data['delivery_type'] == 'delivery') {
|
||||
$delivery_products = $products->map(function ($product) use ($products_cart) {
|
||||
$product->power_delivery = $product->power * $products_cart->filter(function ($cart) use ($product) {
|
||||
return $cart['id'] == $product->id;
|
||||
})->value('count');
|
||||
return $product;
|
||||
});
|
||||
|
||||
$power = Power::where('power', '>=', $delivery_products->sum('power_delivery'))->first();
|
||||
|
||||
if (empty($power)) {
|
||||
$power_id = 0;
|
||||
} else {
|
||||
$power_id = $power->id;
|
||||
}
|
||||
|
||||
$city = City::where('id', $this->data['address']['city_id'])->first();
|
||||
|
||||
$price_delivery = DeliveryPrice::where('power_id', $power_id)->where('region_id', $city->region_id)->first();
|
||||
$price_delivery = $price_delivery->price ?? 0;
|
||||
}
|
||||
|
||||
//delivery price end
|
||||
//master price
|
||||
$power = 0;
|
||||
if ($this->data['type'] == 'ready_solutions') {
|
||||
$settings = Setting::query()->first();
|
||||
|
||||
$power_products = $products->map(function ($product) use ($products_cart) {
|
||||
$product->pp = $product->power;
|
||||
$product->power_master = $product->power * $products_cart->filter(function ($cart) use ($product) {
|
||||
return $cart['id'] == $product->id;
|
||||
})->value('count');
|
||||
return $product;
|
||||
});
|
||||
|
||||
$power = $power_products->sum('power_master') / 1000;
|
||||
$price_master = $power * $settings->master_price;
|
||||
}
|
||||
//master price end
|
||||
$price = $products->filter(function ($product) use ($products_cart) {
|
||||
foreach ($products_cart as $p_cart) {
|
||||
if ($p_cart['id'] == $product->id) {
|
||||
return $product->count >= $p_cart['count'];
|
||||
}
|
||||
}
|
||||
})->map(function ($product) use ($products_cart) {
|
||||
foreach ($products_cart as $p_cart) {
|
||||
if ($p_cart['id'] == $product->id) {
|
||||
if (!empty($product->price_discount)) {
|
||||
$product->price_total = ceiling($product->price_discount * $this->currency, 100) * $p_cart['count'];
|
||||
} else {
|
||||
$product->price_total = ceiling($product->price * $this->currency, 100) * $p_cart['count'];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $product;
|
||||
})->sum('price_total');
|
||||
|
||||
if ($this->data['with_installation']) {
|
||||
$price_master = $price_master;
|
||||
} else {
|
||||
$price_master = 0;
|
||||
}
|
||||
|
||||
$this->total = $price + $price_delivery + $price_master;
|
||||
|
||||
return [
|
||||
'power' => $power,
|
||||
'price_delivery' => $price_delivery,
|
||||
'price_master' => $price_master,
|
||||
|
||||
'price_products' => $price,
|
||||
'price_total' => $this->total
|
||||
];
|
||||
}
|
||||
|
||||
public function createOrder()
|
||||
{
|
||||
if ($this->data['delivery_type'] == 'delivery')
|
||||
$this->storeAddress();
|
||||
if ($this->data['client_type'] == 'legal') {
|
||||
$this->storeLegalClientInformation();
|
||||
}
|
||||
$this->storeOrder();
|
||||
$this->storeProducts();
|
||||
if (!in_array($this->data['payment_type'], ['cash', 'bank'])) {
|
||||
$this->storeBilling();
|
||||
} else {
|
||||
// generate contract
|
||||
$lang = request()->header('Accept-Language') ?? 'ru';
|
||||
$contract = new ContractService($this->order, $lang, $this->data['client_type']);
|
||||
$contractPath = $contract->generate();
|
||||
// store OrderContract
|
||||
$this->storeOrderContract($contractPath);
|
||||
|
||||
}
|
||||
// orders contract
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function storeOrderContract($contractPath)
|
||||
{
|
||||
OrderContract::create([
|
||||
'order_id' => $this->order->id,
|
||||
'path' => $contractPath,
|
||||
'type' => 'contract' //TODO: for now
|
||||
]);
|
||||
}
|
||||
|
||||
private function storeBilling()
|
||||
{
|
||||
$this->billing = Billing::create([
|
||||
'order_id' => $this->order->id,
|
||||
'amount' => $this->total,
|
||||
'payment_system' => $this->data['payment_type']
|
||||
]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function storeAddress()
|
||||
{
|
||||
$address = Address::create([
|
||||
'user_id' => $this->user->id,
|
||||
'city_id' => $this->data['address']['city_id'],
|
||||
'address' => isset($this->data['address']['address']) ? $this->data['address']['address'] : null,
|
||||
'home' => isset($this->data['address']['home']) ? $this->data['address']['home'] : null,
|
||||
'landmark' => isset($this->data['address']['landmark']) ? $this->data['address']['landmark'] : null,
|
||||
]);
|
||||
|
||||
$this->address_id = $address->id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function storeLegalClientInformation()
|
||||
{
|
||||
$legal_user = UserLegalInfo::create([
|
||||
'user_id' => $this->user->id,
|
||||
'director_full_name' => $this->data['client_information']['director_full_name'],
|
||||
'company_name' => $this->data['client_information']['company_name'],
|
||||
'inn' => $this->data['client_information']['inn'],
|
||||
'bank_name' => $this->data['client_information']['bank_name'],
|
||||
'mfo' => $this->data['client_information']['mfo'],
|
||||
'oked' => $this->data['client_information']['oked'],
|
||||
'payment_account' => $this->data['client_information']['payment_account'],
|
||||
'address' => $this->data['client_information']['address'],
|
||||
'email' => isset($this->data['client_information']['email']) ?? null,
|
||||
'phone' => $this->data['client_information']['phone'],
|
||||
]);
|
||||
|
||||
$this->legal_id = $legal_user->id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function storeOrder()
|
||||
{
|
||||
if (!empty($this->data['client_information']) && !empty($this->data['client_information']['phone'])) {
|
||||
$this->phone = $this->data['client_information']['phone'];
|
||||
}
|
||||
|
||||
if (!empty($this->data['client_information']) && !empty($this->data['client_information']['full_name'])) {
|
||||
$this->full_name = $this->data['client_information']['full_name'];
|
||||
}
|
||||
|
||||
$currency = Currency::orderBy('id', 'desc')->first();
|
||||
|
||||
$calc = $this->calc();
|
||||
|
||||
if ($this->data['payment_type'] == 'cash') {
|
||||
$payment_status = 'cash';
|
||||
} else {
|
||||
$payment_status = 'processing';
|
||||
}
|
||||
|
||||
$this->order = Order::create([
|
||||
'phone' => $this->phone,
|
||||
'full_name' => $this->full_name,
|
||||
'legal_id' => $this->legal_id,
|
||||
'user_id' => $this->user->id,
|
||||
"jshir" => $this->data["client_information"]['jshir'] ?? null,
|
||||
"series" => $this->data["client_information"]['series'] ?? null,
|
||||
'type' => $this->data['type'],
|
||||
'delivery_type' => $this->data['delivery_type'],
|
||||
'client_type' => $this->data['client_type'],
|
||||
'address_id' => $this->address_id,
|
||||
'with_installation' => isset($this->data['with_installation']) ? $this->data['with_installation'] : false,
|
||||
'payment_type' => $this->data['payment_type'],
|
||||
'with_didox' => isset($this->data['with_didox']) ? $this->data['with_didox'] : false,
|
||||
'branch_id' => isset($this->data['branch_id']) ? $this->data['branch_id'] : null,
|
||||
'currency' => $currency->id,
|
||||
'payment_status' => $payment_status,
|
||||
'price_products' => $calc['price_products'],
|
||||
'price_delivery' => $calc['price_delivery'],
|
||||
'price_master' => $calc['price_master'],
|
||||
'price_total' => $calc['price_total']
|
||||
]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function storeProducts()
|
||||
{
|
||||
foreach ($this->data['products'] as $row) {
|
||||
$product = Product::whereNull('child_id')->where('id', $row['id'])->first();
|
||||
if (empty($product)) {
|
||||
throw new \Exception(trans('admin.Product not found'), 404);
|
||||
}
|
||||
|
||||
$product->count = $product->count - (int) $row['count'];
|
||||
$product->save();
|
||||
Log::info('Product count updated', ['product_id' => $product->id, 'count' => $product->count]);
|
||||
|
||||
OrderProducts::create([
|
||||
'order_id' => $this->order->id,
|
||||
'product_id' => $row['id'],
|
||||
'count' => $row['count'],
|
||||
'price' => $product->price,
|
||||
'discount' => $product->price_discount,
|
||||
]);
|
||||
|
||||
Cart::where('user_id', $this->user->id)->where('product_id', $row['id'])->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
23
app/Services/API/ProductService.php
Normal file
23
app/Services/API/ProductService.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\API;
|
||||
|
||||
class ProductService
|
||||
{
|
||||
public $product;
|
||||
public $instalition_categories = [
|
||||
173,
|
||||
152,
|
||||
183
|
||||
];
|
||||
|
||||
public function __construct($product)
|
||||
{
|
||||
$this->product = $product;
|
||||
}
|
||||
public function isInstall()
|
||||
{
|
||||
// O'rnatib berish xizmati bor yoki yo'qligini aniqash uchun
|
||||
return $this->product->categories()->whereIn('id', $this->instalition_categories)->count() >= 1;
|
||||
}
|
||||
}
|
||||
25
app/Services/API/SmsService.php
Executable file
25
app/Services/API/SmsService.php
Executable file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\API;
|
||||
|
||||
use App\Api\Sms;
|
||||
|
||||
class SmsService
|
||||
{
|
||||
public static function send($phone)
|
||||
{
|
||||
if ($phone == 998999400049) {
|
||||
$verify_code = 55555;
|
||||
} else {
|
||||
$verify_code = rand(10000, 99999);
|
||||
}
|
||||
|
||||
$lang = request()->header('Accept-Language') ?? 'ru';
|
||||
$message = $lang == 'uz' ?
|
||||
"Quyoshli marketplace sayti va mobil ilovasiga ro'yxatdan o'tishingingiz uchun tasdiqlash kodi: $verify_code" :
|
||||
'Код подтверждения для сброса пароля сайта и мобильного приложения Quyoshli Marketplace: ' . $verify_code;
|
||||
$res = (new Sms())->send($phone, $message);
|
||||
|
||||
return $verify_code;
|
||||
}
|
||||
}
|
||||
129
app/Services/BotService.php
Normal file
129
app/Services/BotService.php
Normal file
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Env;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class BotService
|
||||
{
|
||||
public string|int $chatId;
|
||||
public string $botToken;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->chatId = Env::get("BOT_CHAT_ID");
|
||||
$this->botToken = Env::get("BOT_TOKEN");
|
||||
}
|
||||
|
||||
|
||||
function sendSupportMessage(string $message)
|
||||
{
|
||||
Http::get("https://api.telegram.org/bot{$this->botToken}/sendMessage?chat_id={$this->chatId}&text={$message}");
|
||||
}
|
||||
|
||||
function sendMessage(array $data)
|
||||
{
|
||||
$order_id = $data['order_id'] ?? null;
|
||||
$client_type = $data['client_type'] ?? null;
|
||||
$delivery_type = $data['delivery_type'] ?? null;
|
||||
$group_id = $data['group_id'] ?? null;
|
||||
$message = $data['message'] ?? null;
|
||||
$order_url = $data['order_url'] ?? null;
|
||||
$client = $data['client'] ?? null;
|
||||
$phone = $data['phone'] ?? null;
|
||||
$products = $data['products'] ?? null;
|
||||
$address = $data['address'] ?? null;
|
||||
$file = $data['file'] ?? null;
|
||||
$payment_type = $data['payment_type'] ?? null;
|
||||
$time = now()->timezone('Asia/Tashkent')->format('Y-m-d H:i:s');
|
||||
|
||||
//! Habar matni:
|
||||
//! To'liq ma'lumot: URL
|
||||
//! Buyurtmachi: Palonchiyev Pistonchi (Yuridik shaxs)
|
||||
//! Telefon raqam: +998 99 XXX XX XX
|
||||
//! Buyurtma raqami: XXXXXX
|
||||
//! Buyurtma mahsulotlar va ularning narxlari:
|
||||
//! Mahsulot 1 - 15 000 000 so'm
|
||||
//! Mahsulot 2 - 30 000 000 so'm
|
||||
|
||||
// validation for file if exists
|
||||
if ($file) {
|
||||
if (!file_exists($file)) {
|
||||
return response()->json(['error' => 'File not found'], 404);
|
||||
}
|
||||
}
|
||||
|
||||
//! Yetkazib berishi turi:
|
||||
//! Manzil: Palonchi viloyat, Palonchi tuman , adress va uy raqami
|
||||
|
||||
$sendMessage = "🆔 Buyurtma raqami: {$order_id}\n";
|
||||
|
||||
if ($message) {
|
||||
$sendMessage .= "📝 Habar matni: {$message}\n";
|
||||
}
|
||||
|
||||
if ($order_url) {
|
||||
$sendMessage .= "🔗 To'liq ma'lumot: {$order_url}\n";
|
||||
}
|
||||
|
||||
if ($client && $client_type) {
|
||||
$sendMessage .= "👤 Buyurtmachi: {$client} ($client_type)\n";
|
||||
}
|
||||
|
||||
if ($phone) {
|
||||
$sendMessage .= "📞 Telefon raqam: {$phone}\n";
|
||||
}
|
||||
if ($products) {
|
||||
// check if products is array
|
||||
$summa = 0;
|
||||
$sendMessage .= "💵 Buyurtma mahsulotlar va ularning narxlari: \n";
|
||||
foreach ($products as $product) {
|
||||
if (isset($product['name']) && isset($product['price']) && isset($product['count'])) {
|
||||
$product_price = number_format($product['price'], 0, '.', ' ');
|
||||
$sendMessage .= "{$product['name']} - {$product_price} so'm - {$product['count']} ta\n";
|
||||
}
|
||||
$summa += $product['price'] * $product['count'];
|
||||
}
|
||||
$summa = number_format($summa, 0, '.', ' ');
|
||||
$sendMessage .= "💰 Summa: {$summa} so'm\n";
|
||||
}
|
||||
|
||||
if ($delivery_type) {
|
||||
$sendMessage .= "🚚 Yetkazib berishi turi: {$delivery_type}\n";
|
||||
}
|
||||
|
||||
if ($client_type) {
|
||||
$sendMessage .= "💼 Тип клиента: {$client_type}\n";
|
||||
}
|
||||
|
||||
if ($address) {
|
||||
$sendMessage .= "🌐 Manzil: {$address}\n";
|
||||
}
|
||||
|
||||
if ($payment_type) {
|
||||
$sendMessage .= "💳 To'lov turi: {$payment_type}\n";
|
||||
}
|
||||
|
||||
$sendMessage .= "📆 Buyurtma sanasi: {$time}";
|
||||
|
||||
if ($group_id) {
|
||||
$this->chatId = $group_id;
|
||||
}
|
||||
$url = "https://api.telegram.org/bot{$this->botToken}/" . ($file ? 'sendDocument' : 'sendMessage');
|
||||
|
||||
if ($file) {
|
||||
$filename = 'file.' . pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
|
||||
|
||||
Http::attach('document', $file, $filename)->post($url, [
|
||||
'chat_id' => $this->chatId,
|
||||
'caption' => $sendMessage,
|
||||
]);
|
||||
} else {
|
||||
Http::post($url, [
|
||||
'chat_id' => $this->chatId,
|
||||
'text' => $sendMessage,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
67
app/Services/Dashboard/Notification/NotificationService.php
Executable file
67
app/Services/Dashboard/Notification/NotificationService.php
Executable file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Dashboard\Notification;
|
||||
|
||||
class NotificationService
|
||||
{
|
||||
// Function to send Firebase Notification
|
||||
function sendNotificationToTopic($topic, $title, $body)
|
||||
{
|
||||
// Firebase server key (replace with your key)
|
||||
$serverKey = env('FIREBASE_TOKEN');
|
||||
|
||||
// The Firebase endpoint for sending messages
|
||||
$url = 'https://fcm.googleapis.com/fcm/send';
|
||||
|
||||
// Notification data
|
||||
$notification = [
|
||||
'title' => $title,
|
||||
'body' => $body,
|
||||
'sound' => 'default',
|
||||
];
|
||||
|
||||
// Payload data (custom data for your app)
|
||||
$data = [
|
||||
'custom_key' => 'custom_value'
|
||||
];
|
||||
|
||||
// The message payload (sending to a topic)
|
||||
$payload = [
|
||||
'to' => '/topics/' . $topic, // Send to topic "all"
|
||||
'notification' => $notification,
|
||||
'data' => $data,
|
||||
'priority' => 'high', // Set priority for the notification
|
||||
];
|
||||
|
||||
// Set headers for the POST request
|
||||
$headers = [
|
||||
'Authorization: key=' . $serverKey,
|
||||
'Content-Type: application/json'
|
||||
];
|
||||
|
||||
// Initialize curl
|
||||
$ch = curl_init();
|
||||
|
||||
// Set curl options
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // For HTTPS
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
|
||||
|
||||
// Execute curl and capture response
|
||||
$response = curl_exec($ch);
|
||||
|
||||
// Check for curl errors
|
||||
if ($response === FALSE) {
|
||||
die('Curl failed: ' . curl_error($ch));
|
||||
}
|
||||
|
||||
// Close curl
|
||||
curl_close($ch);
|
||||
|
||||
// Return Firebase response
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
80
app/Services/Dashboard/Order/OrderFilterService.php
Executable file
80
app/Services/Dashboard/Order/OrderFilterService.php
Executable file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Dashboard\Order;
|
||||
|
||||
use App\Models\Order;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class OrderFilterService
|
||||
{
|
||||
protected $request, $id, $client_phone, $status, $payment_status, $payment_type, $client_type, $delivery_type, $from, $to, $with_didox, $with_installation;
|
||||
|
||||
public function __construct(Request $request)
|
||||
{
|
||||
$this->id = empty($request->get('id')) ? null : $request->get('id');
|
||||
$this->client_phone = empty($request->get('client_phone')) ? null : $request->get('client_phone');
|
||||
$this->status = empty($request->get('order_status')) ? null : $request->get('order_status');
|
||||
$this->payment_status = empty($request->get('payment_status')) ? null : $request->get('payment_status');
|
||||
$this->payment_type = empty($request->get('payment_type')) ? null : $request->get('payment_type');
|
||||
$this->client_type = empty($request->get('client_type')) ? null : $request->get('client_type');
|
||||
$this->delivery_type = empty($request->get('delivery_type')) ? null : $request->get('delivery_type');
|
||||
$this->with_didox = $request->get('with_didox');
|
||||
$this->with_installation = $request->get('with_installation');
|
||||
$this->from = empty($request->get('from')) ? Carbon::parse('2000-01-01')->format('Y-m-d') : Carbon::parse($request->get('from'))->format('Y-m-d');
|
||||
$this->to = empty($request->get('to')) ? Carbon::parse('2040-01-01')->format('Y-m-d') : Carbon::parse($request->get('to'))->format('Y-m-d');
|
||||
}
|
||||
|
||||
public function filter()
|
||||
{
|
||||
$orders = Order::query()
|
||||
->when($this->id, function ($query) {
|
||||
return $query->where('id', $this->id);
|
||||
})
|
||||
->when($this->client_phone, function ($query) {
|
||||
return $query->where('phone', 'like', '%' . $this->formatPhone($this->client_phone) . '%');
|
||||
})
|
||||
->when($this->status, function ($query) {
|
||||
return $query->where('status', $this->status);
|
||||
})
|
||||
->when($this->payment_status, function ($query) {
|
||||
return $query->where('payment_status', $this->payment_status);
|
||||
})
|
||||
->when($this->payment_type, function ($query) {
|
||||
return $query->where('payment_type', $this->payment_type);
|
||||
})
|
||||
->when($this->client_type, function ($query) {
|
||||
return $query->where('client_type', $this->client_type);
|
||||
})
|
||||
->when($this->delivery_type, function ($query) {
|
||||
return $query->where('delivery_type', $this->delivery_type);
|
||||
})
|
||||
->when($this->with_didox, function ($query) {
|
||||
if (in_array('unchecked', $this->with_didox)) {
|
||||
$query->orWhere('with_didox', 0);
|
||||
}
|
||||
if (in_array('checked', $this->with_didox)) {
|
||||
$query->orWhere('with_didox', 1);
|
||||
}
|
||||
return $query;
|
||||
})
|
||||
->when($this->with_installation, function ($query) {
|
||||
if (in_array('unchecked', $this->with_installation)) {
|
||||
$query->orWhere('with_installation', 0);
|
||||
}
|
||||
if (in_array('checked', $this->with_installation)) {
|
||||
$query->orWhere('with_installation', 1);
|
||||
}
|
||||
return $query;
|
||||
})
|
||||
->whereBetween('created_at', [$this->from, $this->to])
|
||||
->paginate(20);
|
||||
|
||||
return $orders;
|
||||
}
|
||||
|
||||
private function formatPhone($phone)
|
||||
{
|
||||
return preg_replace('/[^0-9]/', '', $phone);
|
||||
}
|
||||
}
|
||||
21
app/Services/Dashboard/Region/RegionService.php
Executable file
21
app/Services/Dashboard/Region/RegionService.php
Executable file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Dashboard\Region;
|
||||
|
||||
use App\Models\Region;
|
||||
|
||||
class RegionService
|
||||
{
|
||||
public static function deliveryPrice(Region $region, $data)
|
||||
{
|
||||
$toDelete = $region->deliveryPrice()->whereNotIn('power_id', array_column($data, 'power_id'))->get();
|
||||
$toDelete->each->delete();
|
||||
|
||||
foreach ($data as $item) {
|
||||
$region->deliveryPrice()->updateOrCreate(
|
||||
['power_id' => $item['power_id']],
|
||||
['price' => $item['price']]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
102
app/Services/Dashboard/Stat/StatService.php
Executable file
102
app/Services/Dashboard/Stat/StatService.php
Executable file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Dashboard\Stat;
|
||||
|
||||
use App\Models\Order;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class StatService
|
||||
{
|
||||
private int $numberOfDays = 0;
|
||||
private $from;
|
||||
private $to;
|
||||
|
||||
public function getLabels($from, $to)
|
||||
{
|
||||
$this->from = $from;
|
||||
$this->to = $to;
|
||||
|
||||
// get the number of days between the two dates
|
||||
$this->numberOfDays = Carbon::parse($from)->diffInDays(Carbon::parse($to));
|
||||
|
||||
// Start date
|
||||
$startDate = Carbon::parse($from);
|
||||
|
||||
|
||||
// End date
|
||||
$endDate = Carbon::parse($to);
|
||||
|
||||
// Calculate the start date as 30 days ago
|
||||
// $startDate = Carbon::today()->subDays($numberOfDays - 1); // 29 because today is included
|
||||
|
||||
// Initialize an array to hold the days
|
||||
$days = [];
|
||||
|
||||
// Loop through each day from the start date to today
|
||||
for ($date = $startDate; $date->lte($endDate); $date->addDay()) {
|
||||
// Format each day like '21 August', '22 August', etc.
|
||||
$days[] = $date->day . ' ' . $date->translatedFormat('F');
|
||||
}
|
||||
|
||||
return $days;
|
||||
}
|
||||
|
||||
public function getStatics($type)
|
||||
{
|
||||
if ($type === 'orders') {
|
||||
return $this->getOrderStatics();
|
||||
} elseif ($type === 'users') {
|
||||
return $this->getUserStatics();
|
||||
} elseif ($type === 'sales') {
|
||||
return $this->getSalesStatics();
|
||||
}
|
||||
}
|
||||
|
||||
private function getUserStatics(): array
|
||||
{
|
||||
// Step 1: Generate the last 10 days as an array
|
||||
$days = collect();
|
||||
for ($i = $this->numberOfDays - 1; $i >= 0; $i--) {
|
||||
$days->put(Carbon::parse($this->to)->subDays($i)->format('Y-m-d'), 0);
|
||||
}
|
||||
|
||||
// Step 2: Get the user count for each day from the database
|
||||
$userRegistrations = User::where('created_at', '>=', Carbon::parse($this->to)->subDays($this->numberOfDays))
|
||||
->selectRaw('DATE(created_at) as date, COUNT(*) as count')
|
||||
->groupBy('date')
|
||||
->orderBy('date', 'asc')
|
||||
->pluck('count', 'date');
|
||||
|
||||
// Step 3: Merge the user counts with the list of last 10 days, filling missing days with null
|
||||
$usersPerDay = $days->merge($userRegistrations)->toArray();
|
||||
|
||||
return array_values($usersPerDay);
|
||||
}
|
||||
|
||||
public function getOrderStatics(): array
|
||||
{
|
||||
// Step 1: Generate the last 10 days as an array
|
||||
$days = collect();
|
||||
for ($i = $this->numberOfDays - 1; $i >= 0; $i--) {
|
||||
$days->put(Carbon::parse($this->to)->subDays($i)->format('Y-m-d'), 0);
|
||||
}
|
||||
|
||||
// Step 2: Get the user count for each day from the database
|
||||
$orders = Order::where('created_at', '>=', Carbon::parse($this->to)->subDays($this->numberOfDays))
|
||||
->selectRaw('DATE(created_at) as date, COUNT(*) as count')
|
||||
->groupBy('date')
|
||||
->orderBy('date', 'asc')
|
||||
->pluck('count', 'date');
|
||||
|
||||
// Step 3: Merge the order counts with the list of last 10 days, filling missing days with null
|
||||
$ordersPerDay = $days->merge($orders)->toArray();
|
||||
|
||||
return array_values($ordersPerDay);
|
||||
}
|
||||
|
||||
public function getSalesStatics(): array
|
||||
{
|
||||
return [20];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user