restore composer.json, add mysqli extension

This commit is contained in:
2026-04-15 17:02:52 +05:00
commit 77cf56a348
4317 changed files with 1397107 additions and 0 deletions

View 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";
}
}

View 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
View 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();
}
}
}

View 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
View 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;
}
}