|
- <?php
-
- namespace domain\Document\DeliveryNote;
-
- use domain\Document\Document\Document;
- use domain\Document\Invoice\Invoice;
- use domain\Order\Order\Order;
- use domain\_\AbstractRepository;
-
- class DeliveryNoteRepository extends AbstractRepository
- {
- protected DeliveryNoteRepositoryQuery $query;
-
- public function loadDependencies(): void
- {
- $this->loadQuery(DeliveryNoteRepositoryQuery::class);
- }
-
- public function getDefaultOptionsSearch(): array
- {
- return [
- self::WITH => [],
- self::JOIN_WITH => ['user AS user_delivery_note', 'producer'],
- self::ORDER_BY => 'date ASC',
- self::ATTRIBUTE_ID_PRODUCER => 'delivery_note.id_producer'
- ];
- }
-
- public function findOneDeliveryNoteById(int $id): ?DeliveryNote
- {
- return $this->createDefaultQuery()
- ->filterById($id)
- ->findOne();
- }
-
- public function getOneDeliveryNoteExistingFromOrders(array $ordersArray): ?DeliveryNote
- {
- $i = 0;
- $deliveryNote = null;
- do {
- $order = $ordersArray[$i];
- if ($order->distribution->id_producer == $this->getProducerContextId() && $order->id_delivery_note > 0) {
- $deliveryNote = $this->findOneDeliveryNoteById($order->id_delivery_note);
- }
- $i++;
- } while ($deliveryNote == null && isset($ordersArray[$i]));
-
- return $deliveryNote;
- }
-
- public function findOneDeliveryNoteByOrder(Order $order): ?DeliveryNote
- {
- $deliveryNoteExist = null;
- if ($order->id_delivery_note) {
- $deliveryNoteExist = $this->findOneDeliveryNoteById($order->id_delivery_note);
- }
-
- return $deliveryNoteExist;
- }
-
- public function findDeliveryNotesByInvoice(Invoice $invoice): array
- {
- return DeliveryNote::searchAll([
- 'id_user' => $invoice->id_user,
- 'status' => Document::STATUS_VALID,
- 'order.id_invoice' => $invoice->id,
- 'ignore_when_invoicing' => null
- ], [
- 'orderby' => 'distribution.date ASC',
- 'join_with' => ['user AS user_delivery_note', 'orders', 'producer']
- ]);
- }
- }
|