'Generate Bulk Purchase Order', 'message' => 'You have successfully generated bulk purchase order' ]; } /** @var ListsGroups */ private $listsGroups; /** @var FetchesCompany */ private $fetchesCompany; /** @var CreateInvoiceDocumentProcessor */ private $createInvoiceDocumentProcessor; /** * CreateBulkPurchaseOrderTransactionLogic constructor. * @param ListsGroups $listsGroups * @param FetchesCompany $fetchesCompany * @param CreateInvoiceDocumentProcessor $createInvoiceDocumentProcessor */ public function __construct(ListsGroups $listsGroups, FetchesCompany $fetchesCompany, CreateInvoiceDocumentProcessor $createInvoiceDocumentProcessor) { $this->listsGroups = $listsGroups; $this->fetchesCompany = $fetchesCompany; $this->createInvoiceDocumentProcessor = $createInvoiceDocumentProcessor; } /** * @param Request $request * @return JsonResponse * @throws \App\Classes\Exceptions\MalformedRequestException */ public function logic(Request $request) : JsonResponse { $groups = $this->listsGroups->execute(['issuer_id' => [$request->issuer_id], 'date_start' => $request->start_date, 'date_end' => $request->end_date]); foreach($groups as $group){ 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'); } } } foreach($groups as $group){ foreach($group->transactions as $transaction){ $completed_transactions = $transaction->owner()->owner()->transactions()->where('type', TransactionType::PURCHASE_ORDER)->where('status', '=', ApprovalStatus::APPROVED)->get(); $purchaseOrder = $transaction->owner()->transactions() ->where('type', TransactionType::PURCHASE_ORDER) ->complete() ->first(); dd($purchaseOrder); foreach($completed_transactions as $transaction){ $supplier = $this->fetchesCompany->execute(['id' => $transaction->receiver]); // purchase order $this->invoiceDocumentProcessor->execute($transaction, $purchaseOrder, $supplier, DocumentType::PURCHASE_ORDER); } } } return $this->response([]); } }