''],
[['id_user', 'id_point_sale', 'id_distribution','id_subscription'], '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_distribution' => 'Date de distribution',
'id_subscription' => 'Abonnement',
];
}
/*
* 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 getDistribution()
{
return $this->hasOne(Distribution::className(), ['id' => 'id_distribution'])
->with('producer');
}
public function getPointSale()
{
return $this->hasOne(PointSale::className(), ['id' => 'id_point_sale'])
->with('userPointSale');
}
public function getCreditHistory()
{
return $this->hasMany(CreditHistory::className(), ['id_order' => 'id']);
}
public function getSubscription()
{
return $this->hasOne(Subscription::className(), ['id' => 'id_subscription'])
->with('productSubscription');
}
/**
* Retourne les options de base nécessaires à la fonction de recherche.
*
* @return array
*/
public static function defaultOptionsSearch() {
return [
'with' => ['productOrder','productOrder.product','creditHistory','creditHistory.userAction' , 'pointSale'],
'join_with' => ['distribution', 'user', 'user.userProducer'],
'orderby' => 'order.date ASC',
'attribute_id_producer' => 'distribution.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 $productOrder) {
$this->amount += $productOrder->price * $productOrder->quantity ;
if ($productOrder->unit == 'piece') {
if(isset($productOrder->product)) {
$this->weight += ($productOrder->quantity * $productOrder->product->weight) / 1000 ;
}
}
else {
$this->weight += $productOrder->quantity ;
}
}
}
}
/**
* 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;
}
}
}
}
public function delete() {
// remboursement si l'utilisateur a payé pour cette commande
$amountPaid = $this->getAmount(Order::AMOUNT_PAID);
if ($amountPaid > 0.01) {
$this->saveCreditHistory(
CreditHistory::TYPE_REFUND,
$amountPaid,
Producer::getId(),
$this->id_user,
User::getCurrentId()
);
}
// delete
if(Producer::getConfig('option_behavior_cancel_order') == Producer::BEHAVIOR_DELETE_ORDER_DELETE ||
(Producer::getConfig('option_behavior_cancel_order') == Producer::BEHAVIOR_DELETE_ORDER_STATUS && strlen($this->date_delete)) ) {
ProductOrder::deleteAll(['id_order' => $this->id]);
return parent::delete() ;
}
// status 'delete'
elseif(Producer::getConfig('option_behavior_cancel_order') == Producer::BEHAVIOR_DELETE_ORDER_STATUS) {
$this->date_delete = date('Y-m-d H:i:s');
return $this->save() ;
}
}
/**
* 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_SURPLUS :
$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::AMOUNT_TOTAL, true),
'paid_amount' => $order->getAmount(self::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 CreditHistory.
*
* @param string $type
* @param float $montant
* @param integer $idProducer
* @param integer $idUser
* @param integer $idUserAction
*/
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();
}
/**
* Ajuste le crédit pour que la commande soit payée.
*
* @return boolean
*/
public function processCredit()
{
if($this->id_user) {
$paymentStatus = $this->getPaymentStatus() ;
if($paymentStatus == self::PAYMENT_PAID) {
return true;
}
elseif($paymentStatus == self::PAYMENT_SURPLUS) {
$type = CreditHistory::TYPE_REFUND ;
$amount = $this->getAmount(self::AMOUNT_SURPLUS) ;
}
elseif($paymentStatus == self::PAYMENT_UNPAID) {
$type = CreditHistory::TYPE_PAYMENT ;
$amount = $this->getAmount(self::AMOUNT_REMAINING) ;
}
$this->saveCreditHistory(
$type,
$amount,
Producer::getId(),
$this->id_user,
User::getCurrentId()
);
}
}
/**
* 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::AMOUNT_PAID) < 0.01 &&
$this->getAmount() - $this->getAmount(self::AMOUNT_PAID) > -0.01)
{
return self::PAYMENT_PAID ;
}
// à rembourser
elseif ($this->getAmount() - $this->getAmount(self::AMOUNT_PAID) <= -0.01) {
return self::PAYMENT_SURPLUS ;
}
// reste à payer
elseif ($this->getAmount() - $this->getAmount(self::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 .= Html::encode($p->product->name) .' ('. $p->quantity .' '.Product::strUnit($p->unit, 'wording_short', true).')';
if (++$i != $count) {
$html .= '
';
}
}
}
return $html;
}
/**
* Retourne le résumé du point de vente lié à la commande au format HTML.
*
* @return string
*/
public function getPointSaleSummary()
{
$html = '';
if (isset($this->pointSale)) {
$html .= '' .
Html::encode($this->pointSale->name) .
'' .
'
'
. Html::encode($this->pointSale->locality)
. '';
if (strlen($this->comment_point_sale)) {
$html .= '