|
- <?php
-
-
-
- namespace domain\Document\DeliveryNote;
-
- use common\components\ActiveDataProvider;
- use common\helpers\GlobalParam;
-
- class DeliveryNoteSearch extends DeliveryNote
- {
- public $with_invoice;
- public $id_point_sale;
- public $date_distribution;
-
- public function rules()
- {
- return [
- [[], 'safe'],
- [['is_sent', 'with_invoice'], 'boolean'],
- [['comment', 'address', 'status', 'date_distribution'], 'string'],
- [['id_user', 'id_point_sale'], 'integer'],
- [['name', 'reference'], 'string', 'max' => 255],
- ];
- }
-
- public function search($params)
- {
- $deliveryNoteModule = DeliveryNoteModule::getInstance();
- $deliveryNoteRepository = $deliveryNoteModule->getRepository();
- $optionsSearch = $deliveryNoteRepository->getDefaultOptionsSearch();
-
- if(isset($params['DeliveryNoteSearch']['id_point_sale'])) {
- $optionsSearch['join_with'][] = 'orders';
- }
-
- $query = DeliveryNote::find()
- ->with($optionsSearch['with'])
- ->joinWith($optionsSearch['join_with'])
- ->where(['delivery_note.id_producer' => GlobalParam::getCurrentProducerId()])
- ->orderBy('delivery_note.status ASC, delivery_note.reference DESC')
- ->groupBy('delivery_note.id');
-
- $dataProvider = new ActiveDataProvider([
- 'query' => $query,
- 'sort' => ['attributes' => ['name', 'reference', 'date']],
- 'pagination' => [
- 'pageSize' => 20,
- ],
- ]);
-
- $this->load($params);
- if (!$this->validate()) {
- return $dataProvider;
- }
-
- $query->andFilterWhere(['like', 'delivery_note.name', $this->name]);
- $query->andFilterWhere(['like', 'delivery_note.reference', $this->reference]);
- $query->andFilterWhere(['like', 'delivery_note.status', $this->status]);
-
- if ($this->id_point_sale) {
- $query->andWhere(['order.id_point_sale' => $this->id_point_sale]);
- }
-
- if(!is_null($this->is_sent) && is_numeric($this->is_sent)) {
- if($this->is_sent) {
- $query->andWhere(['delivery_note.is_sent' => 1]);
- }
- else {
- $query->andWhere('delivery_note.is_sent IS NULL OR delivery_note.is_sent = 0');
- }
- }
-
- if ($this->date_distribution && strlen($this->date_distribution)) {
- $query->andFilterWhere(['like', 'distribution.date', date('Y-m-d', strtotime(str_replace('/', '-', $this->date_distribution)))]);
- }
-
-
- $withInvoice = $this->with_invoice;
- if(is_numeric($withInvoice)) {
- $dataProvider->filterByCallback(function($model) use ($withInvoice) {
- $deliveryNoteModule = DeliveryNoteModule::getInstance();
- $filterModel = false;
- if(is_numeric($withInvoice)) {
- $invoice = $deliveryNoteModule->getSolver()->getInvoice($model);
- if(($withInvoice && !$invoice) || (!$withInvoice && $invoice)) {
- $filterModel = true;
- }
- }
- return $filterModel;
- });
- }
-
- return $dataProvider;
- }
- }
|