''],
[['id_user', 'id_point_sale', 'id_production'], 'integer'],
[['auto_payment'], 'boolean'],
[['date', 'date_update', 'comment', 'point_sale_comment'], '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 .= '