|
- <?php
-
-
-
- namespace common\models;
-
- use Yii;
- use common\components\ActiveRecordCommon ;
- use yii\db\ActiveRecord;
- use common\models\User;
- use common\models\Order;
- use common\models\Producer;
- use yii\helpers\Html;
-
-
- class CreditHistory extends ActiveRecordCommon
- {
-
- const TYPE_INITIAL_CREDIT = 'initial-credit';
- const TYPE_CREDIT = 'credit';
- const TYPE_PAYMENT = 'payment';
- const TYPE_REFUND = 'refund';
- const TYPE_DEBIT = 'debit';
-
-
-
- public static function tableName()
- {
- return 'credit_history';
- }
-
-
-
- public function rules()
- {
- return [
- [['amount'], 'required'],
- [['id_user', 'id_user_action', 'id_order', 'id_producer'], 'integer'],
- [['date'], 'safe'],
- [['amount'], 'double'],
- [['type', 'mean_payment', 'comment'], 'string', 'max' => 255],
- ];
- }
-
-
-
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'id_user' => 'Utilisateur',
- 'id_user_action' => 'Utilisateur',
- 'id_order' => 'Commande',
- 'date' => 'Date',
- 'amount' => 'Montant',
- 'type' => 'Type',
- 'id_producer' => 'Producteur',
- 'mean_payment' => 'Moyen de paiement',
- 'comment' => 'Commentaire',
- ];
- }
-
-
-
-
-
-
- public function getUser()
- {
- return $this->hasOne(User::className(), ['id' => 'id_user']);
- }
-
- public function getUserAction()
- {
- return $this->hasOne(User::className(), ['id' => 'id_user_action']);
- }
-
- public function getOrder()
- {
- return $this->hasOne(Order::className(), ['id' => 'id_order']);
- }
-
-
-
- public static function defaultOptionsSearch() {
- return [
- 'with' => [],
- 'join_with' => [],
- 'orderby' => self::tableName().'.date ASc',
- 'attribute_id_producer' => self::tableName().'.id_producer'
- ] ;
- }
-
-
-
- public function getStrType()
- {
- switch ($this->type) {
- case self::TYPE_PAYMENT:
- return 'Paiement';
- break;
- case self::TYPE_REFUND:
- return 'Remboursement';
- break;
- case self::TYPE_DEBIT:
- return 'Débit';
- break;
- }
- }
-
-
-
- public function save($runValidation = true, $attributeNames = NULL)
- {
-
- $this->comment .= $this->getStrComment() ;
-
- parent::save($runValidation, $attributeNames);
-
-
- $userProducer = UserProducer::searchOne([
- 'id_user' => $this->id_user,
- 'id_producer' => $this->id_producer
- ]) ;
-
- $creditLimitReminder = Producer::getConfig('credit_limit_reminder') ;
- $oldCredit = $userProducer->credit ;
-
- if ($userProducer) {
- if ($this->isTypeCredit()) {
- $userProducer->credit += $this->amount;
- } elseif ($this->isTypeDebit()) {
- $userProducer->credit -= $this->amount;
- }
- $userProducer->save();
-
-
- if($this->id_order && $this->id_order > 0) {
- $order = Order::searchOne(['id' => (int) $this->id_order]) ;
- if($order) {
- $paymentStatus = $order->getPaymentStatus() ;
- if($paymentStatus == Order::PAYMENT_PAID
- || $paymentStatus == Order::PAYMENT_SURPLUS) {
- $order->mean_payment = MeanPayment::CREDIT ;
- $order->save() ;
- }
- }
- }
-
-
-
- $newCredit = $userProducer->credit ;
- if(!is_null($creditLimitReminder) &&
- $oldCredit > $creditLimitReminder && $newCredit <= $creditLimitReminder) {
- $user = User::findOne($this->id_user) ;
- $producer = Producer::findOne($this->id_producer) ;
- Yii::$app->mailer->compose(
- [
- 'html' => 'creditLimitReminder-html',
- 'text' => 'creditLimitReminder-text'
- ],
- [
- 'user' => $user,
- 'producer' => $producer,
- 'credit' => $newCredit
- ]
- )
- ->setTo($user->email)
- ->setFrom(['contact@opendistrib.net' => 'distrib'])
- ->setSubject('[distrib] Seuil limite de crédit dépassé')
- ->send();
- }
- }
- }
-
-
-
- public function isTypeDebit()
- {
- return in_array($this->type, [
- self::TYPE_DEBIT,
- self::TYPE_PAYMENT,
- ]) ;
- }
-
-
-
- public function isTypeCredit()
- {
- return in_array($this->type, [
- self::TYPE_CREDIT,
- self::TYPE_INITIAL_CREDIT,
- self::TYPE_REFUND
- ]) ;
- }
-
-
-
- public function getAmount($format = false)
- {
- if($format) {
- return number_format($this->amount,2) .' €' ;
- }
- else {
- return $this->amount ;
- }
- }
-
-
-
- public function getStrWording()
- {
- $str = '' ;
-
- if($this->type == self::TYPE_INITIAL_CREDIT) {
- $str = 'Crédit initial' ;
- }
- elseif($this->type == self::TYPE_CREDIT) {
- $str = 'Crédit' ;
- }
- elseif($this->type == self::TYPE_PAYMENT) {
- $str = 'Paiement' ;
- }
- elseif($this->type == self::TYPE_REFUND) {
- $str = 'Remboursement' ;
- }
- elseif($this->type == self::TYPE_DEBIT) {
- $str = 'Débit' ;
- }
-
- if($this->type == self::TYPE_PAYMENT || $this->type == self::TYPE_REFUND) {
- if(isset($this->order)) {
- $str .= '<br />Commande : '.date('d/m/Y',strtotime($this->order->distribution->date)) ;
- }
- else {
- $str .= '<br />Commande supprimée' ;
- }
- }
-
- return $str ;
- }
-
-
-
-
- public function getStrComment()
- {
- $str = '' ;
- if(strlen($this->comment)) {
- $str .= '<br />' ;
- }
- $str .= $this->getStrWording() ;
- if(isset($this->order)) {
- $str .= '<br />Montant de la commande : '.$this->order->getAmountWithTax(Order::AMOUNT_TOTAL, true) ;
- }
- if(isset($this->user)) {
- $str .= '<br />Client : '.Html::encode($this->user->name. ' '.$this->user->lastname) ;
- }
- if(isset($this->userAction)) {
- $str .= '<br />Action : '.Html::encode($this->userAction->name. ' '.$this->userAction->lastname) ;
- }
-
- return $str ;
- }
-
-
-
- public function getDate($format = false)
- {
- if($format)
- return date('d/m/Y à H:i:s',strtotime($this->date)) ;
- else
- return $this->date ;
- }
-
-
-
- public function getStrMeanPayment()
- {
- return MeanPayment::getStrBy($this->mean_payment) ;
- }
-
-
-
- public function strUserAction()
- {
- if($this->userAction) {
- return $this->userAction->name . ' ' . $this->userAction->lastname ;
- }
- else {
- return 'Système' ;
- }
- }
-
- }
|