|
2 | 2 |
|
3 | 3 | namespace frontend\models\forms;
|
4 | 4 |
|
| 5 | +use console\jobs\CreateReportJob; |
5 | 6 | use frontend\models\Customer;
|
6 | 7 | use Yii;
|
| 8 | +use yii\base\InvalidCallException; |
7 | 9 | use yii\base\Model;
|
8 | 10 |
|
9 | 11 | /**
|
|
17 | 19 | */
|
18 | 20 | class ReportForm extends Model
|
19 | 21 | {
|
| 22 | + public const SCENARIO_BY_DATE = 'date'; |
| 23 | + public const SCENARIO_BY_ORDER_NR = 'order_nr'; |
20 | 24 |
|
21 | 25 | public $start_date;
|
22 | 26 | public $end_date;
|
23 | 27 | public $customer;
|
24 | 28 | public $items = true;
|
| 29 | + public $order_nrs; |
| 30 | + |
25 | 31 |
|
26 | 32 | public function rules()
|
27 | 33 | {
|
28 | 34 | return [
|
29 |
| - [['start_date', 'end_date', 'customer'], 'required'], |
30 |
| - [['items'], 'boolean'], |
| 35 | + [['start_date', 'end_date', 'customer'], 'default', 'value' => null], |
| 36 | + [['start_date', 'end_date', 'customer'], 'required', 'on' => self::SCENARIO_BY_DATE], |
| 37 | + |
| 38 | + ['start_date', 'date', 'on' => self::SCENARIO_BY_DATE, |
| 39 | + 'timestampAttribute' => 'start_date', |
| 40 | + // Always set for beginning of day and end of day for query |
| 41 | + 'timestampAttributeFormat' => 'php:Y-m-d 00:00:00' |
| 42 | + ], |
| 43 | + ['end_date', 'date', 'on' => self::SCENARIO_BY_DATE, |
| 44 | + 'timestampAttribute' => 'end_date', |
| 45 | + // Always set for beginning of day and end of day for query |
| 46 | + 'timestampAttributeFormat' => 'php:Y-m-d 23:59:59' |
| 47 | + ], |
| 48 | + |
| 49 | + ['end_date', 'compare', 'operator' => '>=', 'compareAttribute' => 'start_date', 'on' => self::SCENARIO_BY_DATE, 'enableClientValidation' => false], |
| 50 | + |
31 | 51 | [
|
32 | 52 | 'customer',
|
33 | 53 | 'in',
|
34 |
| - 'range' => array_keys( |
35 |
| - Yii::$app->user->identity->isAdmin |
36 |
| - ? Customer::getList() |
37 |
| - : Yii::$app->user->identity->getCustomerList() |
38 |
| - ), |
| 54 | + 'range' => array_keys($this->getCustomerList()), |
| 55 | + 'on' => self::SCENARIO_BY_DATE |
39 | 56 | ],
|
| 57 | + |
| 58 | + [['order_nrs'], 'required', 'on' => self::SCENARIO_BY_ORDER_NR], |
| 59 | + |
| 60 | + [['items'], 'boolean'], |
40 | 61 | ];
|
41 | 62 | }
|
42 | 63 |
|
| 64 | + public function attributeLabels() |
| 65 | + { |
| 66 | + return [ |
| 67 | + 'order_nrs' => 'Order Numbers', |
| 68 | + ]; |
| 69 | + } |
43 | 70 |
|
| 71 | + public function scenarios() |
| 72 | + { |
| 73 | + return [ |
| 74 | + self::SCENARIO_BY_DATE => ['start_date', 'end_date', 'customer', 'items'], |
| 75 | + self::SCENARIO_BY_ORDER_NR => ['order_nrs', 'items'], |
| 76 | + ]; |
| 77 | + } |
| 78 | + |
| 79 | + public function getOrderNrs() |
| 80 | + { |
| 81 | + return preg_split('~[;,\s]+~', $this->order_nrs, -1, PREG_SPLIT_NO_EMPTY); |
| 82 | + } |
| 83 | + |
| 84 | + private $_customerList; |
| 85 | + /** |
| 86 | + * @return array |
| 87 | + */ |
| 88 | + public function getCustomerList(): array |
| 89 | + { |
| 90 | + if ($this->_customerList !== null) { |
| 91 | + return $this->_customerList; |
| 92 | + } |
| 93 | + return $this->_customerList = Yii::$app->user->identity->isAdmin |
| 94 | + ? Customer::getList() |
| 95 | + : Yii::$app->user->identity->getCustomerList(); |
| 96 | + } |
| 97 | + |
| 98 | + public function pushReportQueueJob() |
| 99 | + { |
| 100 | + switch ($this->scenario) { |
| 101 | + case self::SCENARIO_BY_DATE: |
| 102 | + $job = new CreateReportJob([ |
| 103 | + 'customer' => $this->customer, |
| 104 | + 'start_date' => $this->start_date, |
| 105 | + 'end_date' => $this->end_date, |
| 106 | + 'items' => $this->items, |
| 107 | + ]); |
| 108 | + break; |
| 109 | + case self::SCENARIO_BY_ORDER_NR: |
| 110 | + $job = new CreateReportJob([ |
| 111 | + 'order_nrs' => $this->getOrderNrs(), |
| 112 | + 'items' => $this->items, |
| 113 | + ]); |
| 114 | + break; |
| 115 | + default: |
| 116 | + throw new InvalidCallException('Unknown model scenario: ' . $this->scenario); |
| 117 | + } |
| 118 | + |
| 119 | + $job->user_id = Yii::$app->user->id; |
| 120 | + $job->user_email = Yii::$app->user->identity->email; |
| 121 | + |
| 122 | + Yii::$app->queue->push($job); |
| 123 | + } |
44 | 124 | }
|
0 commit comments