ultimatepos/Modules/AssetExchange/Services/AssetExchangeErpBridgeService.php

158 lines
5.5 KiB
PHP

<?php
namespace Modules\AssetExchange\Services;
use App\Business;
use App\Transaction;
use App\User;
use App\Utils\ProductUtil;
use App\Utils\TransactionUtil;
use Illuminate\Support\Facades\Schema;
use Modules\AssetExchange\Models\Deal;
use Modules\AssetExchange\Models\Listing;
class AssetExchangeErpBridgeService
{
public function __construct(
protected ProductUtil $productUtil,
protected TransactionUtil $transactionUtil
) {
}
public function createPurchaseOrder(
Business $business,
Listing $listing,
Deal $deal,
?int $locationId = null,
?User $user = null
): ?Transaction {
if (! Schema::hasTable('transactions')) {
throw new \RuntimeException('جدول تراکنش‌های ERP موجود نیست.');
}
$amount = (int) ($deal->agreed_price ?: $listing->asking_price);
$contactId = $listing->seller_contact_id;
if (! $contactId) {
throw new \RuntimeException('عرضه‌کننده برای ایجاد سفارش خرید مشخص نشده است.');
}
$refCount = $this->productUtil->setAndGetReferenceCount('purchase_order', $business->id);
$refNo = $this->productUtil->generateReferenceNumber('purchase_order', $refCount);
$transaction = Transaction::create([
'business_id' => $business->id,
'location_id' => $locationId,
'type' => 'purchase_order',
'status' => 'draft',
'contact_id' => $contactId,
'transaction_date' => now()->toDateTimeString(),
'total_before_tax' => $amount,
'tax_amount' => 0,
'discount_type' => 'fixed',
'discount_amount' => 0,
'final_total' => $amount,
'created_by' => $user?->id ?? auth()->id(),
'ref_no' => $refNo,
'additional_notes' => 'سفارش خرید AssetExchange — عرضه '.$listing->listing_code,
]);
$this->transactionUtil->activityLog($transaction, 'added');
$deal->update(['transaction_purchase_id' => $transaction->id]);
return $transaction->fresh();
}
public function createQuotation(
Business $business,
Listing $listing,
Deal $deal,
?int $locationId = null,
?User $user = null
): ?Transaction {
if (! Schema::hasTable('transactions')) {
throw new \RuntimeException('جدول تراکنش‌های ERP موجود نیست.');
}
$amount = (int) ($deal->agreed_price ?: $listing->asking_price);
$contactId = $deal->buyer_contact_id;
if (! $contactId) {
throw new \RuntimeException('خریدار برای ایجاد پیش‌فاکتور مشخص نشده است.');
}
$refCount = $this->productUtil->setAndGetReferenceCount('sell', $business->id);
$refNo = $this->productUtil->generateReferenceNumber('sell', $refCount);
$transaction = Transaction::create([
'business_id' => $business->id,
'location_id' => $locationId,
'type' => 'sell',
'status' => 'draft',
'sub_status' => 'quotation',
'contact_id' => $contactId,
'transaction_date' => now()->toDateTimeString(),
'total_before_tax' => $amount,
'tax_amount' => 0,
'discount_type' => 'fixed',
'discount_amount' => 0,
'final_total' => $amount,
'created_by' => $user?->id ?? auth()->id(),
'ref_no' => $refNo,
'additional_notes' => 'پیش‌فاکتور AssetExchange — معامله '.$deal->deal_code,
]);
$this->transactionUtil->activityLog($transaction, 'added');
$deal->update(['renovation_quote_id' => $transaction->id]);
return $transaction->fresh();
}
public function createSellInvoice(
Business $business,
Listing $listing,
Deal $deal,
?int $locationId = null,
?User $user = null
): ?Transaction {
if (! Schema::hasTable('transactions')) {
throw new \RuntimeException('جدول تراکنش‌های ERP موجود نیست.');
}
$amount = (int) ($deal->agreed_price ?: $listing->asking_price);
$contactId = $deal->buyer_contact_id;
if (! $contactId) {
throw new \RuntimeException('خریدار برای ایجاد فاکتور فروش مشخص نشده است.');
}
$refCount = $this->productUtil->setAndGetReferenceCount('sell', $business->id);
$refNo = $this->productUtil->generateReferenceNumber('sell', $refCount);
$transaction = Transaction::create([
'business_id' => $business->id,
'location_id' => $locationId,
'type' => 'sell',
'status' => 'draft',
'contact_id' => $contactId,
'transaction_date' => now()->toDateTimeString(),
'total_before_tax' => $amount,
'tax_amount' => 0,
'discount_type' => 'fixed',
'discount_amount' => 0,
'final_total' => $amount,
'created_by' => $user?->id ?? auth()->id(),
'ref_no' => $refNo,
'additional_notes' => 'فاکتور فروش AssetExchange — معامله '.$deal->deal_code,
]);
$this->transactionUtil->activityLog($transaction, 'added');
$deal->update(['transaction_sell_id' => $transaction->id]);
return $transaction->fresh();
}
}