''], [['id_user', 'id_point_sale', 'id_production'], 'integer'], [['auto_payment'], 'boolean'], [['date', 'date_update', 'comment', 'comment_point_sale'], 'safe'] ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'id_user' => 'Id User', 'date' => 'Date', 'date_update' => 'Date de modification', 'id_point_sale' => 'Point de vente', 'id_production' => 'Date de production', ]; } /* * Relations */ public function getUser() { return $this->hasOne(User::className(), ['id' => 'id_user']); } public function getProductOrder() { return $this->hasMany(ProductOrder::className(),['id_order' => 'id']) ->with('product'); } public function getProduction() { return $this->hasOne(Production::className(), ['id' => 'id_production']) ->with('producer'); } public function getPointSale() { return $this->hasOne(PointSale::className(), ['id' => 'id_point_sale']) ->with('pointSaleUser'); } public function getCreditHistory() { return $this->hasMany(CreditHistory::className(), ['id_order' => 'id']); } /** * Retourne les options de base nécessaires à la fonction de recherche. * * @return array */ public static function defaultOptionsSearch() { return [ 'with' => ['productOrder', 'creditHistory', 'pointSale'], 'join_with' => ['production', 'user', 'user.userProducer'], 'orderby' => 'order.date ASC', 'attribute_id_producer' => 'production.id_producer' ] ; } /** * Initialise le montant total, le montant déjà payé et le poids de la * commande. */ public function init() { $this->initAmount() ; $this->initPaidAmount() ; return $this ; } /** * Initialise le montant de la commande. * */ public function initAmount() { if (isset($this->productOrder)) { foreach ($this->productOrder as $p) { if ($p->sale_mode == Product::SALE_MODE_UNIT) { $this->amount += $p->price * $p->quantity ; if(isset($p->product)) { $this->weight += ($p->quantity * $p->product->weight) / 1000 ; } } elseif ($p->sale_mode == Product::SALE_MODE_WEIGHT) { $this->amount += $p->price * $p->quantity / 1000; } } } } /** * Initialise le montant payé de la commande et le retourne. * * @return float */ public function initPaidAmount() { $history = CreditHistory::find() ->where(['id_order' => $this->id]) ->all(); $this->paid_amount = 0 ; if(count($history)) { foreach ($history as $ch) { if ($ch->type == CreditHistory::TYPE_PAYMENT) { $this->paid_amount += $ch->amount; } elseif ($ch->type == CreditHistory::TYPE_REFUND) { $this->paid_amount -= $ch->amount; } } } } /** * Retourne le montant de la commande (total, payé, restant, ou en surplus). * * @param boolean $format * @return float */ public function getAmount($type = self::AMOUNT_TOTAL, $format = false) { switch($type) { case self::AMOUNT_TOTAL : $amount = $this->amount ; break ; case self::AMOUNT_PAID : $this->initPaidAmount() ; $amount = $this->paid_amount ; break ; case self::AMOUNT_REMAINING : $amount = $this->getAmount(self::AMOUNT_TOTAL) - $this->getAmount(self::AMOUNT_PAID) ; break ; case self::AMOUNT_EXCESS : $amount = $this->getAmount(self::AMOUNT_PAID) - $this->getAmount(self::AMOUNT_TOTAL) ; break ; } if ($format) { return number_format($amount, 2) . ' €'; } else { return $amount; } } /** * Retourne les informations relatives à la commande au format JSON. * * @return string */ public function getDataJson() { $order = Order::searchOne(['order.id' => $this->id]) ; $jsonOrder = [] ; if($order) { $jsonOrder = [ 'products' => [], 'amount' => $order->amount, 'str_amount' => $order->getAmount(self::TYPE_AMOUNT_TOTAL, true), 'paid_amount' => $order->getPaidAmount(self::TYPE_AMOUNT_PAID), 'comment' => $order->comment, ]; foreach ($order->productOrder as $productOrder) { $jsonOrder['products'][$productOrder->id_product] = $productOrder->quantity; } } return json_encode($jsonOrder); } /** * Enregistre un modèle de type CreditHistorique. * * @param string $type * @param float $montant * @param integer $id_producer * @param integer $id_user * @param integer $id_user_action */ public function saveCreditHistory($type, $amount, $idProducer, $idUser, $idUserAction) { $creditHistory = new CreditHistory; $creditHistory->id_user = $this->id_user; $creditHistory->id_order = $this->id; $creditHistory->amount = $amount; $creditHistory->type = $type; $creditHistory->id_producer = $idProducer; $creditHistory->id_user_action = $idUserAction; $creditHistory->populateRelation('order', $this) ; $creditHistory->populateRelation('user', User::find()->where(['id' => $this->id_user])->one()) ; $creditHistory->save(); } /** * Retourne le statut de paiement de la commande (payée, surplus, ou impayée). * * @return string */ public function getPaymentStatus() { // payé if ($this->getAmount() - $this->getAmount(self::TYPE_AMOUNT_PAID) < 0.01 && $this->getAmount() - $this->getAmount(self::TYPE_AMOUNT_PAID) >= 0) { return self::PAYMENT_PAID ; } // à rembourser elseif ($this->getAmount() - $this->getAmount(self::TYPE_AMOUNT_PAID) <= -0.01) { return self::PAYMENT_SURPLUS ; } // reste à payer elseif ($this->getAmount() - $this->getAmount(self::TYPE_AMOUNT_PAID) >= 0.01) { return self::PAYMENT_UNPAID ; } } /** * Retourne le résumé du panier au format HTML. * * @return string */ public function getCartSummary() { if (!isset($this->productOrder)) { $this->productOrder = productOrder::find()->where(['id_order' => $this->id])->all(); } $html = ''; $count = count($this->productOrder); $i = 0; foreach ($this->productOrder as $p) { if (isset($p->product)) { $html .= $p->quantity . ' x ' . Html::encode($p->product->name); if (++$i != $count) { $html .= '
'; } } } return $html; } /** * Retourne le résumé du point de vente lié à la commande au format HTML. * * @return string */ public function getSalePointSummary() { $html = ''; if (isset($this->salePoint)) { $html .= '' . Html::encode($this->salePoint->name) . '' . '
' . Html::encode($this->salePoint->locality) . ''; if (strlen($this->sale_point_comment)) { $html .= '
' . Html::encode($this->comment_point_sale) . '
'; } } else { $html .= 'Point de vente supprimé'; } return $html; } /** * Retourne le résumé du paiement (montant, statut). * * @return string */ public function getAmountSummary() { $html = ''; $html .= $this->getAmount(self::TYPE_AMOUNT_TOTAL, true) . '
'; if ($this->paid_amount) { if ($this->getPaymentStatus() == Order::PAYMENT_PAID) { $html .= 'Payée'; } elseif ($this->getPaymentStatus() == Order::PAYMENT_UNPAID) { $html .= 'Non payée
Reste ' . $this->getRemainingAmount(true) . ' à payer'; } elseif ($this->getPaymentStatus() == Order::PAYMENT_SURPLUS) { $html .= 'Payée'; } } else { $html .= 'À régler sur place'; } return $html; } /** * Retourne une chaine de caractère décrivant l'utilisateur lié à la commande. * * @return string */ public function getStrUser() { if (isset($this->user)) { return Html::encode($this->user->prenom . ' ' . $this->user->nom); } elseif (strlen($this->username)) { return Html::encode($this->username); } else { return 'Client introuvable'; } } /** * Retourne l'état de la commande (livrée, modifiable ou en préparation) * * @return string */ public function getState() { $orderDelay = Producer::getConfig( 'order_delay', $this->production->id_producer ); $orderDeadline = Producer::getConfig( 'order_deadline', $this->production->id_producer ); $orderDate = strtotime($this->production->date); $today = strtotime(date('Y-m-d')); $todayHour = date('G'); $nbDays = (int) (($dateOrder - $today) / (24 * 60 * 60)); if ($nb_days <= 0) { return self::STATE_DELIVERED; } elseif ($nbDays >= $orderDelay && ($nbDays != $orderDelay || ($nbDays == $orderDelay && $todayHour < $orderDeadline))) { return self::STATE_OPEN; } return self::STATE_PREPARATION ; } /** * Retourne l'origine de la commande (client, automatique ou admin) sous forme * texte ou HTML. * * @param boolean $with_label * @return string */ public function getStrOrigin($withLabel = false) { $classLabel = ''; $str = ''; if ($this->type == self::ORIGIN_USER) { $classLabel = 'success'; $str = 'Client'; } elseif ($this->type == self::ORIGIN_AUTO) { $classLabel = 'default'; $str = 'Auto'; } elseif ($this->type == self::ORIGIN_ADMIN) { $classLabel = 'warning'; $str = 'Vous'; } if ($withLabel) { return '' . $str . ''; } else { return $str; } } /** * Retourne l'historique de la commande (ajoutée, modifiée, supprimée) au * format HTML. * * @return string */ public function getStrHistory() { $arr = [ 'class' => 'create', 'glyphicon' => 'plus', 'str' => 'Ajoutée', 'date' => $this->date ] ; if(!is_null($this->date_update)) { $arr = [ 'class' => 'update', 'glyphicon' => 'pencil', 'str' => 'Modifiée', 'date' => $this->date_update ] ; } if(!is_null($this->date_delete)) { $arr = [ 'class' => 'delete', 'glyphicon' => 'remove', 'str' => 'Annulée', 'date' => $this->date_delete ] ; } $html = '
' . ' ' . $arr['str'].' le ' . date('d/m/Y à G\hi', strtotime($arr['date'])).'
' ; return $html ; } /** * Retourne une classe identifiant l'historique de la commande (ajoutée, * modifiée, supprimée). * * @return string */ public function getClassHistory() { if(!is_null($this->date_delete)) { return 'commande-delete' ; } if(!is_null($this->date_update)) { return 'commande-update' ; } return 'commande-create' ; } /** * Retourne la quantité d'un produit donné de plusieurs commandes. * * @param integer $idProduct * @param array $orders * * @return integer */ public static function getProductQuantity($idProduct, $orders) { $quantity = 0; if (isset($orders) && is_array($orders) && count($orders)) { foreach ($orders as $c) { if(is_null($c->date_delete)) { foreach ($c->productOrder as $po) { if ($po->id_product == $id_product) { $quantity += $po->quantity ; } } } } } return $quantity ; } /** * Recherche et initialise des commandes. * * @param array $params * @param array $conditions * @param string $orderby * @param integer $limit * @return array */ public static function search($params = [], $options = []) { $orders = parent::search($params, $options) ; /* * Initialisation des commandes */ if(is_array($orders)) { if(count($orders)) { foreach($orders as $order) { $order->init() ; } return $orders ; } } else { $order = $orders ; return $order->init() ; } return false ; } }