255], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'name' => 'Nom', 'reference' => 'Référence', 'date' => 'Date', 'comment' => 'Commentaire', 'id_user' => 'Utilisateur', 'address' => 'Adresse', 'id_producer' => 'Producteur', 'status' => 'Statut', ]; } /* * Relations */ public function getUser() { return $this->hasOne(User::className(), ['id' => 'id_user']); } public function getProducer() { return $this->hasOne(Producer::className(), ['id' => 'id_producer']); } public function relationOrders($fieldIdDocument) { $defaultOptionsSearch = Order::defaultOptionsSearch(); return $this->hasMany(Order::className(), [$fieldIdDocument => 'id']) ->with($defaultOptionsSearch['with']) ->joinWith($defaultOptionsSearch['join_with']); } /* * Méthodes */ public function getAmount($type = Order::AMOUNT_TOTAL, $format = false) { return $this->_getAmountGeneric($type, false, $format) ; } public function getAmountWithTax($type = Order::AMOUNT_TOTAL, $format = false) { return $this->_getAmountGeneric($type, true, $format) ; } protected function _getAmountGeneric($type = Order::AMOUNT_TOTAL, $withTax = true, $format = false) { $amount = 0; $ordersArray = $this->orders; foreach ($ordersArray as $order) { $order->init(); if($withTax) { $amount += $order->getAmountWithTax($type); } else { $amount += $order->getAmount($type); } } if ($format) { return Price::format($amount); } else { return $amount; } } public function getPointSale() { if(isset($this->orders) && isset($this->orders[0])) { return $this->orders[0]->pointSale ; } else { return '' ; } } public function getDistribution() { if(isset($this->orders) && isset($this->orders[0])) { return $this->orders[0]->distribution; } else { return '' ; } } public function getClass() { return str_replace('common\models\\','',get_class($this)) ; } public function getControllerUrlPath() { $class = $this->getClass() ; $path = strtolower($class) ; if($path == 'deliverynote') { $path = 'delivery_note' ; } return $path ; } public function isValidClass($typeDocument) { return in_array($typeDocument, ['Invoice', 'DeliveryNote', 'Quotation']) ; } public function generateReference() { $class = $this->getClass() ; $classLower = strtolower($class) ; if($classLower == 'deliverynote') { $classLower = 'delivery_note' ; } $prefix = Producer::getConfig('document_'.$classLower.'_prefix') ; $oneDocumentExist = $class::searchOne([],['orderby'=> 'reference DESC']) ; if($oneDocumentExist) { $reference = $oneDocumentExist->reference ; $pattern = '#([A-Z]+)?([0-9]+)#'; preg_match($pattern, $reference, $matches, PREG_OFFSET_CAPTURE); $sizeNumReference = strlen($matches[2][0]) ; $numReference = ((int) $matches[2][0]) + 1 ; $numReference = str_pad($numReference, $sizeNumReference, '0', STR_PAD_LEFT); return $prefix.$numReference ; } else { $firstReference = Producer::getConfig('document_'.$classLower.'_first_reference') ; if(strlen($firstReference) > 0) { return $firstReference ; } else { return $prefix.'00001' ; } } } public function changeStatus($status) { if($status == Document::STATUS_VALID) { $this->status = $status ; $this->reference = $this->generateReference() ; } } public function getStatusWording() { return ($this->status == self::STATUS_DRAFT) ? 'Brouillon' : 'Validé' ; } public function getStatusCssClass() { return ($this->status == self::STATUS_DRAFT) ? 'default' : 'success' ; } public function getHtmlLabel() { $label = $this->getStatusWording(); $classLabel = $this->getStatusCssClass() ; return ''.$label.'' ; } public function isStatus($status) { return $this->status == $status ; } public function isStatusDraft() { return $this->isStatus(self::STATUS_DRAFT) ; } public function isStatusValid() { return $this->isStatus(self::STATUS_VALID) ; } }