diff --git a/app/Classes/Jobs/GenerateGroupTransactionsPurchaseOrder.php b/app/Classes/Jobs/GenerateGroupTransactionsPurchaseOrder.php new file mode 100644 index 00000000..970b577b --- /dev/null +++ b/app/Classes/Jobs/GenerateGroupTransactionsPurchaseOrder.php @@ -0,0 +1,22 @@ +make(GeneratesGroupTransactionsPurchaseOrder::class))->execute(); + } +} diff --git a/app/Classes/Modules/Transactions/ControllersLogic/CreateBulkPurchaseOrderDocumentLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/CreateBulkPurchaseOrderDocumentLogic.php index 9478e865..297d275b 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/CreateBulkPurchaseOrderDocumentLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/CreateBulkPurchaseOrderDocumentLogic.php @@ -3,25 +3,15 @@ namespace App\Classes\Modules\Transactions\ControllersLogic; +use App\Classes\Jobs\GenerateGroupTransactionsPurchaseOrder; use Illuminate\Http\Request; use Illuminate\Http\JsonResponse; -use Meneses\LaravelMpdf\Facades\LaravelMpdf; -use App\Classes\ValueObjects\Constants\DocumentType; -use App\Classes\Exceptions\MalformedRequestException; -use App\Classes\ValueObjects\Constants\ApprovalStatus; -use App\Classes\ValueObjects\Constants\TransactionType; use App\Classes\General\Abstracts\AbstractControllerLogic; -use App\Classes\Modules\Companies\Services\FetchesCompany; -use App\Classes\Modules\Transactions\Services\ListsGroups; -use App\Classes\Modules\Transactions\Services\FetchesGroup; -use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject; -use App\Classes\Modules\Documents\Services\CreatesDocument; -use App\Classes\Modules\Documents\Services\CreatesFiles; -use App\Classes\Modules\Transactions\Processors\CreateInvoiceDocumentProcessor; class CreateBulkPurchaseOrderDocumentLogic extends AbstractControllerLogic { + /** * @return array */ @@ -33,71 +23,25 @@ class CreateBulkPurchaseOrderDocumentLogic extends AbstractControllerLogic ]; } - /** @var ListsGroups */ - private $listsGroups; - - /** @var FetchesGroup */ - private $fetchesGroup; - - /** @var FetchesCompany */ - private $fetchesCompany; - - /** @var CreatesDocument */ - private $createsDocument; - - /** @var CreatesFiles */ - private $createsFile; + /** @var GenerateGroupTransactionsPurchaseOrder */ + private $generateGroupTransactionsPurchaseOrder; /** - * CreateBulkPurchaseOrderTransactionLogic constructor. - * @param ListsGroups $listsGroups - * @param FetchesGroup $fetchesGroup - * @param FetchesCompany $fetchesCompany - * @param CreatesDocument $createsDocument - * @param CreatesFiles $createsFile + * CreateBulkPurchaseOrderDocumentLogic constructor. + * @param GenerateGroupTransactionsPurchaseOrder $generateGroupTransactionsPurchaseOrder */ - public function __construct(ListsGroups $listsGroups, FetchesGroup $fetchesGroup, FetchesCompany $fetchesCompany, CreatesDocument $createsDocument, CreatesFiles $createsFile) + public function __construct(GenerateGroupTransactionsPurchaseOrder $generateGroupTransactionsPurchaseOrder) { - $this->listsGroups = $listsGroups; - $this->fetchesGroup = $fetchesGroup; - $this->fetchesCompany = $fetchesCompany; - $this->createsDocument = $createsDocument; - $this->createsFile = $createsFile; + $this->generateGroupTransactionsPurchaseOrder = $generateGroupTransactionsPurchaseOrder; } /** * @param Request $request * @return JsonResponse - * @throws \App\Classes\Exceptions\MalformedRequestException */ public function logic(Request $request): JsonResponse { - $group = $this->fetchesGroup->execute(['id' => $request->route('id')]); - - foreach ($group->transactions as $transaction) { - if ($transaction->owner()->owner()->transactions()->where('type', TransactionType::PURCHASE_ORDER)->where('status', '!=', ApprovalStatus::APPROVED)->exists()) { - throw new MalformedRequestException('You can\'t generate bulk purchased order if there in uncomplete transactions'); - } - } - - $supplier = $this->fetchesCompany->execute(['id' => $group->receiver]); - - $document_type = DocumentType::BULK_PURCHASE_ORDER; - - $lowercaseDocumentType = strtolower($document_type); - - $order_pdf = LaravelMpdf::loadView('pages.pdfs.' . $lowercaseDocumentType, ['group' => $group, 'supplier' => $supplier]); - $document_object = new DocumentObject( - $document_type, - [chunk_split('data:application/pdf;base64,' . base64_encode($order_pdf->output()))], - '', - ApprovalStatus::COMPLETED, - $lowercaseDocumentType . 's' - ); - - /** @var Document $document */ - $document = $this->createsDocument->execute($group, $document_object); - $this->createsFile->execute($document, $document_object); + $this->generateGroupTransactionsPurchaseOrder::dispatch(); return $this->response([]); } diff --git a/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php index 0aa0fb7f..52018b01 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php @@ -122,7 +122,7 @@ class CreateSupplierTransactionLogic extends AbstractControllerLogic ); /** @var Document $document */ - $document = $this->createsDocument->execute($supplier, $object); + $document = $this->createsDocument->execute($group, $object); $this->createsFile->execute($document, $object); diff --git a/app/Classes/Modules/Transactions/Processors/GeneratesGroupTransactionsPurchaseOrder.php b/app/Classes/Modules/Transactions/Processors/GeneratesGroupTransactionsPurchaseOrder.php new file mode 100644 index 00000000..6e6b35be --- /dev/null +++ b/app/Classes/Modules/Transactions/Processors/GeneratesGroupTransactionsPurchaseOrder.php @@ -0,0 +1,91 @@ +listsGroups = $listsGroups; + $this->fetchesCompany = $fetchesCompany; + $this->createsDocument = $createsDocument; + $this->createsFile = $createsFile; + } + + public function execute(){ + $groups = Group::where('status', '!=', ApprovalStatus::COMPLETED)->whereDoesntHave('transactions', function ($query){ + $query->whereHasMorph('owner', [Transaction::class], function($query){ + return $query->whereHas('booking', function($query){ + return $query->whereDoesntHave('transactions', function($query){ + return $query->where('type', TransactionType::PURCHASE_ORDER)->where('status', '=', ApprovalStatus::APPROVED); + }); + }); + }); + })->get(); + + + foreach ($groups as $group) { +// if ($transaction->owner()->owner()->transactions()->where('type', TransactionType::PURCHASE_ORDER)->where('status', '!=', ApprovalStatus::APPROVED)->exists()) { +// throw new MalformedRequestException('You can\'t generate bulk purchased order if there in uncomplete transactions'); +// } + $supplier = $this->fetchesCompany->execute(['id' => $group->receiver]); + + $document_type = DocumentType::BULK_PURCHASE_ORDER; + + $lowercaseDocumentType = strtolower($document_type); + + $order_pdf = LaravelMpdf::loadView('pages.pdfs.bulk_purchase_order', ['group' => $group, 'supplier' => $supplier]); + $document_object = new DocumentObject( + $document_type, + [chunk_split('data:application/pdf;base64,' . base64_encode($order_pdf->output()))], + '', + ApprovalStatus::COMPLETED, + $lowercaseDocumentType . 's' + ); + + /** @var Document $document */ + $document = $this->createsDocument->execute($group, $document_object); + $this->createsFile->execute($document, $document_object); + + $group->status = ApprovalStatus::COMPLETED; + $group->save(); + + } + } +} \ No newline at end of file diff --git a/app/Http/Resources/BookingResource.php b/app/Http/Resources/BookingResource.php index f1fbe662..0eeb9670 100644 --- a/app/Http/Resources/BookingResource.php +++ b/app/Http/Resources/BookingResource.php @@ -31,43 +31,43 @@ class BookingResource extends JsonResource 'service' => new ServiceTypeResource($this->service), 'marking' => $this->marking, 'amount' => $this->fix_amount, -// 'floating_amount' => floatval((App()->make(CalculatesBookingFloatingAmount::class))->execute($this->resource, $this->fix_currency_id)), -// 'paid_amount' => floatval((App()->make(CalculatesBookingPayableAmount::class))->execute($this->resource, $this->fix_currency_id)) - floatval((App()->make(CalculatesBookingRefundAmount::class))->execute($this->resource, $this->fix_currency_id)), -// 'outstanding_amount' => floatval((App()->make(CalculatesBookingOutstanding::class))->execute($this->resource)) - floatval((App()->make(CalculatesBookingRefundAmount::class))->execute($this->resource, $this->fix_currency_id)), -// 'fixed_currency' => new CurrencyResource($this->fixedCurrency), -// 'convertible_currency' => new CurrencyResource($this->convertibleCurrency), -// 'conversion_currency' => new CurrencyResource($this->conversionCurrency), -// 'documents' => [ -// 'purchase_order' => new DocumentResource($this->documents()->where('document_type', DocumentType::PURCHASE_ORDER)->first()), -// 'delivery_order' => new DocumentResource($this->documents()->where('document_type', DocumentType::DELIVER_ORDER)->first()), -// 'invoice' => new DocumentResource($this->documents()->where('document_type', DocumentType::INVOICE)->first()), -// 'supplier_delivery_order' => new DocumentResource($this->documents()->where('document_type', DocumentType::SUPPLIER_DELIVER_ORDER)->first()), -// 'proforma_invoice' => new DocumentResource($this->documents()->where('document_type', DocumentType::PROFORMA_INVOICE)->whereNotIn('status', [ApprovalStatus::REJECTED, ApprovalStatus::EXPIRED])->orderByDesc('id')->first()), -// ], + 'floating_amount' => floatval((App()->make(CalculatesBookingFloatingAmount::class))->execute($this->resource, $this->fix_currency_id)), + 'paid_amount' => floatval((App()->make(CalculatesBookingPayableAmount::class))->execute($this->resource, $this->fix_currency_id)) - floatval((App()->make(CalculatesBookingRefundAmount::class))->execute($this->resource, $this->fix_currency_id)), + 'outstanding_amount' => floatval((App()->make(CalculatesBookingOutstanding::class))->execute($this->resource)) - floatval((App()->make(CalculatesBookingRefundAmount::class))->execute($this->resource, $this->fix_currency_id)), + 'fixed_currency' => new CurrencyResource($this->fixedCurrency), + 'convertible_currency' => new CurrencyResource($this->convertibleCurrency), + 'conversion_currency' => new CurrencyResource($this->conversionCurrency), + 'documents' => [ + 'purchase_order' => new DocumentResource($this->documents()->where('document_type', DocumentType::PURCHASE_ORDER)->first()), + 'delivery_order' => new DocumentResource($this->documents()->where('document_type', DocumentType::DELIVER_ORDER)->first()), + 'invoice' => new DocumentResource($this->documents()->where('document_type', DocumentType::INVOICE)->first()), + 'supplier_delivery_order' => new DocumentResource($this->documents()->where('document_type', DocumentType::SUPPLIER_DELIVER_ORDER)->first()), + 'proforma_invoice' => new DocumentResource($this->documents()->where('document_type', DocumentType::PROFORMA_INVOICE)->whereNotIn('status', [ApprovalStatus::REJECTED, ApprovalStatus::EXPIRED])->orderByDesc('id')->first()), + ], 'status' => $this->status, 'created_at' => Carbon::parse($this->created_at)->format('d-m-Y'), 'created_at_with_time' => Carbon::parse($this->created_at)->format('d-m-Y h:i:s A'), -// $this->mergeWhen($this->relationLoaded('transactions'), [ -// 'purchase_order' => new TransactionResource($this->transactions()->where('type', TransactionType::PURCHASE_ORDER)->first()), -// 'payment_attempts' => TransactionResource::collection( -// $this->transactions() -// ->payments()->where('status', ApprovalStatus::PENDING_SUBMISSION) -// ->whereDate('expires_on', '>=', Carbon::now()) -// ->get() -// ), -// 'expired_payment_attempts' => TransactionResource::collection($this->transactions()->payments()->where('status', ApprovalStatus::PENDING_SUBMISSION)->whereDate('expires_on', '<', Carbon::now())->get()), -// 'payment_history' => TransactionResource::collection($this->transactions()->where(function($query){ -// $query->where(function($query){ -// $query->payments()->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::COMPLETED, ApprovalStatus::REJECTED]); -// })->orWhere(function($query){ -// $query->where(function($query){ -// $query->where('type', TransactionType::REFUND)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::REJECTED, ApprovalStatus::COMPLETED]); -// })->orWhere(function($query){ -// $query->where('type', TransactionType::CREDIT_NOTE)->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); -// }); -// }); -// })->latest()->get()) -// ]) + $this->mergeWhen($this->relationLoaded('transactions'), [ + 'purchase_order' => new TransactionResource($this->transactions()->where('type', TransactionType::PURCHASE_ORDER)->first()), + 'payment_attempts' => TransactionResource::collection( + $this->transactions() + ->payments()->where('status', ApprovalStatus::PENDING_SUBMISSION) + ->whereDate('expires_on', '>=', Carbon::now()) + ->get() + ), + 'expired_payment_attempts' => TransactionResource::collection($this->transactions()->payments()->where('status', ApprovalStatus::PENDING_SUBMISSION)->whereDate('expires_on', '<', Carbon::now())->get()), + 'payment_history' => TransactionResource::collection($this->transactions()->where(function($query){ + $query->where(function($query){ + $query->payments()->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::COMPLETED, ApprovalStatus::REJECTED]); + })->orWhere(function($query){ + $query->where(function($query){ + $query->where('type', TransactionType::REFUND)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::REJECTED, ApprovalStatus::COMPLETED]); + })->orWhere(function($query){ + $query->where('type', TransactionType::CREDIT_NOTE)->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + }); + }); + })->latest()->get()) + ]) ]; } } diff --git a/app/Http/Resources/CompanyResource.php b/app/Http/Resources/CompanyResource.php index acf48f8c..fceab5bc 100644 --- a/app/Http/Resources/CompanyResource.php +++ b/app/Http/Resources/CompanyResource.php @@ -40,33 +40,33 @@ class CompanyResource extends JsonResource 'type' => (int) $this->type, 'business_type' => (int) $this->business_type, 'status' => (int) $this->status, -// 'contact' => new ContactResource ($this->when($this->has('contacts'), $this->contacts->first())), -// 'address' => new AddressResource($this->when($this->has('addresses'), $this->addresses->where('billing', true)->first())), -// 'employee' => new UserResource(Auth::user()->type === RoleTypes::USER ? $this->employees()->where('email', '=', Auth::user()->email)->first() : $this->employees()->orderBy('id', 'DESC')->first()), -// 'identification' => new DocumentResource($this->documents->whereIn('document_type', DocumentType::IDENTIFICATION_DOCUMENTS)->first()), -// 'bookings' => $this->whenLoaded('bookings', $this->bookings()->orderBy('id', 'DESC')->get(), []), -// 'confirmed_bookings' => $this->bookings()->whereHas('transactions', function ($query){ -// $query->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); -// })->count(), -// 'total_payments' => (float) $totalPayments, -// 'average_spending_per_day' => (float) $totalPayments / ($this->created_at->diff(Carbon::now())->days === 0 ? 1 : $this->created_at->diff(Carbon::now())->days), -// 'average_spending_per_booking' => (float) $totalPayments > 0 ? $totalPayments / $this->bookings()->whereHas('transactions', function ($query){ -// $query->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); -// })->count() : $totalPayments, -// 'last_payment' => $lastPayment ? $lastPayment->created_at->diffForHumans() : 'No Payments', -// 'personal_banks' => BankResource::collection($this->banks->where('type', BankAccountType::PERSONAL)), -// 'recipient_banks' => [ -// 'accounts' => BankResource::collection($this->banks->where('type', BankAccountType::EXTERNAL)), -// 'default' => new BankResource($this->banks->where('type', BankAccountType::EXTERNAL)->where('default', true)->first()) -// ], -// 'segments' => SegmentResource::collection($this->segments), -// 'services' => (new FetchesCompanyServices())->getServices($this->servicesConfigurations()), -// 'wallet' => new WalletResource($this->wallets()->first()), -// 'created_at' => $this->created_at->format('d-m-Y'), -// $this->mergeWhen($this->business_type === BusinessType::CURRENCY_VENDOR, [ -// 'currencies' => $segment ? CurrencyResource::collection(Currency::whereIn('id', $segment->detail->currencies)->get()) : [], -// 'service_charge' => $serviceCharge -// ]) + 'contact' => new ContactResource ($this->when($this->has('contacts'), $this->contacts->first())), + 'address' => new AddressResource($this->when($this->has('addresses'), $this->addresses->where('billing', true)->first())), + 'employee' => new UserResource(Auth::user()->type === RoleTypes::USER ? $this->employees()->where('email', '=', Auth::user()->email)->first() : $this->employees()->orderBy('id', 'DESC')->first()), + 'identification' => new DocumentResource($this->documents->whereIn('document_type', DocumentType::IDENTIFICATION_DOCUMENTS)->first()), + 'bookings' => $this->whenLoaded('bookings', $this->bookings()->orderBy('id', 'DESC')->get(), []), + 'confirmed_bookings' => $this->bookings()->whereHas('transactions', function ($query){ + $query->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + })->count(), + 'total_payments' => (float) $totalPayments, + 'average_spending_per_day' => (float) $totalPayments / ($this->created_at->diff(Carbon::now())->days === 0 ? 1 : $this->created_at->diff(Carbon::now())->days), + 'average_spending_per_booking' => (float) $totalPayments > 0 ? $totalPayments / $this->bookings()->whereHas('transactions', function ($query){ + $query->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + })->count() : $totalPayments, + 'last_payment' => $lastPayment ? $lastPayment->created_at->diffForHumans() : 'No Payments', + 'personal_banks' => BankResource::collection($this->banks->where('type', BankAccountType::PERSONAL)), + 'recipient_banks' => [ + 'accounts' => BankResource::collection($this->banks->where('type', BankAccountType::EXTERNAL)), + 'default' => new BankResource($this->banks->where('type', BankAccountType::EXTERNAL)->where('default', true)->first()) + ], + 'segments' => SegmentResource::collection($this->segments), + 'services' => (new FetchesCompanyServices())->getServices($this->servicesConfigurations()), + 'wallet' => new WalletResource($this->wallets()->first()), + 'created_at' => $this->created_at->format('d-m-Y'), + $this->mergeWhen($this->business_type === BusinessType::CURRENCY_VENDOR, [ + 'currencies' => $segment ? CurrencyResource::collection(Currency::whereIn('id', $segment->detail->currencies)->get()) : [], + 'service_charge' => $serviceCharge + ]) ]; } diff --git a/app/Http/Resources/GroupResource.php b/app/Http/Resources/GroupResource.php index baed798f..204d5de8 100644 --- a/app/Http/Resources/GroupResource.php +++ b/app/Http/Resources/GroupResource.php @@ -3,7 +3,10 @@ namespace App\Http\Resources; use App\Classes\ValueObjects\Constants\ApprovalStatus; +use App\Classes\ValueObjects\Constants\DocumentType; use App\Classes\ValueObjects\Constants\TransactionType; +use App\Models\Booking; +use App\Models\Transaction; use Carbon\Carbon; use Illuminate\Database\Eloquent\Collection; use Illuminate\Http\Resources\Json\JsonResource; @@ -18,25 +21,30 @@ class GroupResource extends JsonResource */ public function toArray($request) { - $transactions = $this->transactions()->get(); - $complete_transactions = new Collection([]); - foreach($transactions as $transaction){ - $booking = $transaction->owner()->first()->owner()->first(); - $purchaseOrders = $booking->transactions()->where('type', TransactionType::PURCHASE_ORDER)->where('status', '=', ApprovalStatus::APPROVED)->get(); - $complete_transactions = $complete_transactions->merge($purchaseOrders); - } - return [ 'id' => $this->id, 'original_amount' => (float) $this->original_amount, 'original_currency' => new CurrencyResource($this->original_currency), + 'issuer_name' => $this->issuerCompany->name, + 'issuer_id' => $this->issuerCompany->id, 'amount' => (float) $this->amount, + 'service_charge' => (float) $this->amount, 'currency' => new CurrencyResource($this->currency), 'created_at' => Carbon::parse($this->created_at)->format('d-m-Y h:i:s A'), 'currency_rate' => (float) $this->currency_rate, - 'transactions' => TransactionResource::collection($transactions), - 'complete_transactions' => TransactionResource::collection($complete_transactions), + 'transactions' => $this->transactions()->count(), + 'complete_transactions' => $this->transactions()->whereHasMorph('owner', [Transaction::class], function($query){ + return $query->whereHas('booking', function($query){ + return $query->whereHas('transactions', function($query){ + return $query->where('type', TransactionType::PURCHASE_ORDER)->where('status', '=', ApprovalStatus::APPROVED); + }); + }); + })->count(), + 'documents' => [ + 'currency_order' => new DocumentResource($this->documents()->where('document_type', DocumentType::CURRENCY_VENDOR_ORDER)->first()), + 'purchase_order' => new DocumentResource($this->documents()->where('document_type', DocumentType::BULK_PURCHASE_ORDER)->first()) + ] ]; } } diff --git a/app/Http/Resources/TransactionResource.php b/app/Http/Resources/TransactionResource.php index 900fe64e..e35dcc36 100644 --- a/app/Http/Resources/TransactionResource.php +++ b/app/Http/Resources/TransactionResource.php @@ -18,17 +18,17 @@ class TransactionResource extends JsonResource public function toArray($request) { -// $booking = in_array((int)$this->type, [TransactionType::BILL, TransactionType::REFUND])? $this->owner->owner : $this->owner; -// $days = $this->created_at->endOfDay()->addWeekdays($booking->service_id === 3 ? 3 : 1); + $booking = in_array((int)$this->type, [TransactionType::BILL, TransactionType::REFUND])? $this->owner->owner : $this->owner; + $days = $this->created_at->endOfDay()->addWeekdays($booking->service_id === 3 ? 3 : 1); return [ 'id' => $this->id, -// 'booking' => new BookingResource($booking), + 'booking' => new BookingResource($booking), 'type' => (int) $this->type, 'bill_no' => $this->bill_no, 'payment_reference' => $this->payment_reference, 'payment_method' => (float) $this->payment_method, -// 'recipient_bank_account' => new BankResource($booking->bank), + 'recipient_bank_account' => new BankResource($booking->bank), 'issuer_name' => $this->issuerCompany->name, 'issuer_id' => $this->issuerCompany->id, 'amount' => (double) $this->amount, @@ -40,15 +40,15 @@ class TransactionResource extends JsonResource 'currency_rate' => (double) $this->currency_rate, 'status' => (int) $this->status, 'details' => TransactionDetailResource::collection($this->transactionDetails), -// 'documents' => new DocumentResource($this->documents()->first()), -// 'transaction_bill' => new TransactionResource($this->when((int) $this->type === TransactionType::PAYMENT, $this->transactions()->bills()->first())), -// 'transaction_refunds' => TransactionResource::collection($this->when((int) $this->type === TransactionType::PAYMENT, $this->transactions()->refunds()->get())), -// 'expires_on' => Carbon::parse($this->expires_on)->format('d-m-Y h:i:s A'), -// 'updated_at' => Carbon::parse($this->updated_at)->format('d-m-Y h:i:s A'), -// 'interval' => [ -// 'value' => $days->gt(Carbon::now()) ? '+' : '-', -// 'duration' => $days->diff(Carbon::now())->format('%d'), -// ] + 'documents' => new DocumentResource($this->documents()->first()), + 'transaction_bill' => new TransactionResource($this->when((int) $this->type === TransactionType::PAYMENT, $this->transactions()->bills()->first())), + 'transaction_refunds' => TransactionResource::collection($this->when((int) $this->type === TransactionType::PAYMENT, $this->transactions()->refunds()->get())), + 'expires_on' => Carbon::parse($this->expires_on)->format('d-m-Y h:i:s A'), + 'updated_at' => Carbon::parse($this->updated_at)->format('d-m-Y h:i:s A'), + 'interval' => [ + 'value' => $days->gt(Carbon::now()) ? '+' : '-', + 'duration' => $days->diff(Carbon::now())->format('%d'), + ] ]; } } diff --git a/app/Models/Group.php b/app/Models/Group.php index bd5d5a38..ae2207a9 100644 --- a/app/Models/Group.php +++ b/app/Models/Group.php @@ -6,11 +6,15 @@ use Illuminate\Database\Eloquent\Model; use App\Classes\General\Interfaces\Documentable; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphMany; +use Staudenmeir\EloquentHasManyDeep\HasManyDeep; use Staudenmeir\EloquentHasManyDeep\HasRelationships; class Group extends Model implements Documentable { use HasRelationships; + use \Staudenmeir\EloquentHasManyDeep\HasTableAlias; + + public $timestamps = false; public function transactions() { @@ -33,6 +37,23 @@ class Group extends Model implements Documentable return $this->BelongsTo(Currency::class, 'currency_id', 'id'); } + + /** + * @return HasManyDeep + */ + public function transferFees(): HasManyDeep + { + return $this->HasManyDeep(Transaction::class, [GroupTransaction::class, Transaction::class.' as alias'], ['group_id', ['owner_type', 'owner_id'], ['owner_type', 'owner_id']], ['id', null, null]); + } + + /** + * @return BelongsTo + */ + public function issuerCompany(): BelongsTo + { + return $this->BelongsTo( Company::class, 'issuer', 'id'); + } + /** * @return BelongsTo */ diff --git a/database/migrations/2022_03_20_170628_create_groups_table.php b/database/migrations/2022_03_20_170628_create_groups_table.php index 596a6ed5..fa7f8f58 100644 --- a/database/migrations/2022_03_20_170628_create_groups_table.php +++ b/database/migrations/2022_03_20_170628_create_groups_table.php @@ -1,5 +1,6 @@ decimal('currency_rate', 14, 5)->default(0.00); $table->decimal('tax', 14, 5)->default(0.00); $table->decimal('service_charge', 14, 5)->default(0.00); + $table->integer('status')->default(ApprovalStatus::PENDING_VERIFICATION); $table->timestamps(); }); } diff --git a/database/seeds/RecoverGroupTransactionTableSeeder.php b/database/seeds/RecoverGroupTransactionTableSeeder.php index fbd587b0..38832d66 100644 --- a/database/seeds/RecoverGroupTransactionTableSeeder.php +++ b/database/seeds/RecoverGroupTransactionTableSeeder.php @@ -1,14 +1,39 @@ createsDocument = $createsDocument; + $this->createsFile = $createsFile; + } + /** * Run the database seeds. * @@ -17,24 +42,22 @@ class RecoverGroupTransactionTableSeeder extends Seeder { DB::beginTransaction(); - $document = Document::where('document_type', 'CURRENCY_VENDOR_ORDER')->get(); $transaction_group = Transaction:: - select('issuer', 'currency_rate', 'type', DB::raw('count(DISTINCT id) as total'), DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d') as new_date")) + select('issuer', 'currency_rate', 'type', DB::raw('count(DISTINCT id) as total'), DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i') as new_date")) ->where('type', 3) ->groupBy( 'issuer', 'currency_rate', 'new_date' ) - ->get(); -// %H:%i + ->orderBy('id')->get(); - foreach ($transaction_group as $key => $row) { - $transaction = Transaction:: + foreach ($transaction_group as $group) { + $transactions = Transaction:: where('type', 3) - ->where('issuer', $row->issuer) - ->where('currency_rate', $row->currency_rate) - ->where(DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d')"), $row->new_date) + ->where('issuer', $group->issuer) + ->where('currency_rate', $group->currency_rate) + ->where(DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i')"), $group->new_date) ->get(); $group = new Group(); @@ -51,18 +74,18 @@ class RecoverGroupTransactionTableSeeder extends Seeder $tax = 0; $service_charge = 0; - foreach ($transaction as $key_2 => $row_2) { - $group->transactions()->sync($row_2->id, false); - $issuer = $row_2->issuer; - $date = $row_2->created_at; - $receiver = $row_2->receiver; - $amount += $row_2->amount; - $original_amount += $row_2->original_amount; - $currency_id = $row_2->currency_id; - $original_currency_id = $row_2->original_currency_id; - $currency_rate = $row_2->currency_rate; - $tax += $row_2->tax; - $service_charge += $row_2->service_charge; + foreach ($transactions as $transaction) { + $group->transactions()->sync($transaction->id, false); + $issuer = $transaction->issuer; + $date = $transaction->created_at; + $receiver = $transaction->receiver; + $amount += $transaction->amount; + $original_amount += $transaction->original_amount; + $currency_id = $transaction->currency_id; + $original_currency_id = $transaction->original_currency_id; + $currency_rate = $transaction->currency_rate; + $tax += $transaction->tax; + $service_charge += $transaction->service_charge; } $group->issuer = $issuer; @@ -75,8 +98,23 @@ class RecoverGroupTransactionTableSeeder extends Seeder $group->tax = $tax; $group->service_charge = $service_charge; $group->created_at = $date; + $group->updated_at = $date; $group->update(); + + $pdf = LaravelMpdf::loadView('pages.pdfs.currency_vendor_order', ['transactions' => $group->transactions, 'transferFeeTransactions' => $group->transferFees, 'supplier' => $group->issuerCompany]); + + $object = new DocumentObject( + DocumentType::CURRENCY_VENDOR_ORDER, + [chunk_split('data:application/pdf;base64,'.base64_encode($pdf->output()))], + '', + ApprovalStatus::COMPLETED, + 'currency_vendor_order' + ); + + /** @var Document $document */ + $document = $this->createsDocument->execute($group, $object); + $this->createsFile->execute($document, $object); } DB::commit(); diff --git a/resources/assets/vue/components/bookings/elements/TransactionGroupComponent.vue b/resources/assets/vue/components/bookings/elements/TransactionGroupComponent.vue index 51562c23..66c638e1 100644 --- a/resources/assets/vue/components/bookings/elements/TransactionGroupComponent.vue +++ b/resources/assets/vue/components/bookings/elements/TransactionGroupComponent.vue @@ -17,28 +17,58 @@ {{item.currency_rate}} +
{{item.complete_transactions}}/{{item.transactions}}
+ + +