if ($creditForm->load(\Yii::$app->request->post()) && $creditForm->validate()) { | if ($creditForm->load(\Yii::$app->request->post()) && $creditForm->validate()) { | ||||
$dateTransaction = null; | |||||
if($creditForm->date_transaction) { | |||||
$dateTransaction = date('Y-m-d', strtotime(str_replace('/', '-', $creditForm->date_transaction))); | |||||
} | |||||
$paymentModule->getManager() | $paymentModule->getManager() | ||||
->creditOrDebitUser( | ->creditOrDebitUser( | ||||
$creditForm->type, | $creditForm->type, | ||||
$creditForm->amount, | $creditForm->amount, | ||||
$creditForm->mean_payment, | $creditForm->mean_payment, | ||||
$this->getUserCurrent(), | $this->getUserCurrent(), | ||||
$creditForm->comment | |||||
$creditForm->comment, | |||||
$dateTransaction | |||||
); | ); | ||||
if ($creditForm->send_mail) { | if ($creditForm->send_mail) { |
*/ | */ | ||||
class CreditForm extends Model | class CreditForm extends Model | ||||
{ | { | ||||
public $id_user ; | public $id_user ; | ||||
public $id_user_action ; | public $id_user_action ; | ||||
public $id_producer ; | public $id_producer ; | ||||
public $mean_payment ; | public $mean_payment ; | ||||
public $comment ; | public $comment ; | ||||
public $send_mail = 1; | public $send_mail = 1; | ||||
public $date_transaction; | |||||
/** | /** | ||||
* @inheritdoc | * @inheritdoc | ||||
return [ | return [ | ||||
[['amount'], 'required'], | [['amount'], 'required'], | ||||
[['id_user', 'id_user_action', 'id_producer'], 'integer'], | [['id_user', 'id_user_action', 'id_producer'], 'integer'], | ||||
[['date','send_mail'], 'safe'], | |||||
[['date', 'send_mail', 'date_transaction'], 'safe'], | |||||
[['amount'], 'double'], | [['amount'], 'double'], | ||||
[['type', 'mean_payment'], 'string', 'max' => 255], | [['type', 'mean_payment'], 'string', 'max' => 255], | ||||
[['comment'], 'string', 'max' => 2048], | [['comment'], 'string', 'max' => 2048], | ||||
'mean_payment' => 'Moyen de paiement', | 'mean_payment' => 'Moyen de paiement', | ||||
'comment' => 'Commentaire', | 'comment' => 'Commentaire', | ||||
'send_mail' => 'Prévenir l\'utilisateur', | 'send_mail' => 'Prévenir l\'utilisateur', | ||||
'date_transaction' => 'Date de transaction' | |||||
]; | ]; | ||||
} | } | ||||
/** | |||||
* Enregistre un modèle CreditHistorique. | |||||
*/ | |||||
public function save() | |||||
{ | |||||
$userModule = UserModule::getInstance(); | |||||
$userProducerModule = UserProducerModule::getInstance(); | |||||
$paymentManager = PaymentModule::getInstance(); | |||||
if ($this->validate()) { | |||||
$user = $userModule->findOneUserById($this->id_user); | |||||
$paymentManager->creditOrDebitUser($this->type, $user, $this->amount, $this->mean_payment, $userModule->getCurrent(), $this->comment); | |||||
// on prévient l'utilisateur que son compte vient d'être crédité | |||||
if($this->send_mail) { | |||||
$producer = GlobalParam::getCurrentProducer() ; | |||||
$userProducer = $userProducerModule->findOneUserProducer($user); | |||||
\Yii::$app->mailerService->sendFromProducer( | |||||
'Mouvement de crédit', | |||||
'creditUser', | |||||
[ | |||||
'user' => $user, | |||||
'producer' => $producer, | |||||
'userProducer' => $userProducer, | |||||
'creditForm' => $this | |||||
], | |||||
$user->email, | |||||
$producer | |||||
); | |||||
} | |||||
} | |||||
} | |||||
} | } |
MeanPayment::OTHER => MeanPayment::getStrBy(MeanPayment::OTHER), | MeanPayment::OTHER => MeanPayment::getStrBy(MeanPayment::OTHER), | ||||
]) ?> | ]) ?> | ||||
<?= $form->field($creditForm, 'comment')->textarea() ?> | <?= $form->field($creditForm, 'comment')->textarea() ?> | ||||
<?= $form->field($creditForm, 'date_transaction')->textInput([ | |||||
'class' => 'datepicker form-control' | |||||
]) ?> | |||||
<?= $form->field($creditForm, 'send_mail')->checkbox() ?> | <?= $form->field($creditForm, 'send_mail')->checkbox() ?> | ||||
<div class="form-group"> | <div class="form-group"> | ||||
'label' => 'Commentaire', | 'label' => 'Commentaire', | ||||
'format' => 'raw', | 'format' => 'raw', | ||||
'value' => function ($model) { | 'value' => function ($model) { | ||||
return nl2br($model->comment); | |||||
if($model->comment) { | |||||
return nl2br($model->comment); | |||||
} | |||||
return ''; | |||||
} | |||||
], | |||||
[ | |||||
'label' => 'Transaction', | |||||
'format' => 'raw', | |||||
'value' => function ($model) { | |||||
if($model->date_transaction) { | |||||
return date('d/m/Y', strtotime($model->date_transaction)); | |||||
} | |||||
return ''; | |||||
} | } | ||||
], | ], | ||||
], | ], |
$this->userSolver->getCurrent(), | $this->userSolver->getCurrent(), | ||||
MeanPayment::CREDIT, | MeanPayment::CREDIT, | ||||
null, | null, | ||||
null, | |||||
$order | $order | ||||
); | ); | ||||
} | } |
return $this; | return $this; | ||||
} | } | ||||
public function setDateTransaction(?string $dateTransaction): self | |||||
{ | |||||
$this->date_transaction = $dateTransaction; | |||||
return $this; | |||||
} | |||||
public function getSummary(): ?string | public function getSummary(): ?string | ||||
{ | { | ||||
return $this->summary; | return $this->summary; |
User $userAction = null, | User $userAction = null, | ||||
string $meanPayment = null, | string $meanPayment = null, | ||||
string $comment = null, | string $comment = null, | ||||
string $dateTransaction = null, | |||||
Order $order = null, | Order $order = null, | ||||
Invoice $invoice = null | Invoice $invoice = null | ||||
): Payment | ): Payment | ||||
$payment->populateProducer($producer); | $payment->populateProducer($producer); | ||||
$payment->setMeanPayment($meanPayment); | $payment->setMeanPayment($meanPayment); | ||||
$payment->setComment($comment); | $payment->setComment($comment); | ||||
$payment->setDateTransaction($dateTransaction); | |||||
if($user) { | if($user) { | ||||
$payment->populateUser($user); | $payment->populateUser($user); | ||||
User $userAction = null, | User $userAction = null, | ||||
string $meanPayment = null, | string $meanPayment = null, | ||||
string $comment = null, | string $comment = null, | ||||
string $dateTransaction = null, | |||||
Order $order = null, | Order $order = null, | ||||
Invoice $invoice = null | Invoice $invoice = null | ||||
): ?Payment | ): ?Payment | ||||
return null; | return null; | ||||
} | } | ||||
$payment = $this->instanciatePayment($type, $amount, $producer, $user, $userAction, $meanPayment, $comment, $order, $invoice); | |||||
$payment = $this->instanciatePayment($type, $amount, $producer, $user, $userAction, $meanPayment, $comment, $dateTransaction, $order, $invoice); | |||||
$payment->setSummary($this->orderSolver->getPaymentComment($payment)); | $payment->setSummary($this->orderSolver->getPaymentComment($payment)); | ||||
$this->create($payment); | $this->create($payment); | ||||
$this->producerSolver = $this->loadService(ProducerSolver::class); | $this->producerSolver = $this->loadService(ProducerSolver::class); | ||||
} | } | ||||
public function creditUser(User $user, float $amount, string $meanPayment, User $userAction, string $comment = null): void | |||||
public function creditUser(User $user, float $amount, string $meanPayment, User $userAction, string $comment = null, string $dateTransaction = null): void | |||||
{ | { | ||||
$this->paymentBuilder->createPayment( | $this->paymentBuilder->createPayment( | ||||
Payment::TYPE_CREDIT, | Payment::TYPE_CREDIT, | ||||
$user, | $user, | ||||
$userAction, | $userAction, | ||||
$meanPayment, | $meanPayment, | ||||
$comment | |||||
$comment, | |||||
$dateTransaction | |||||
); | ); | ||||
} | } | ||||
public function debitUser(User $user, float $amount, string $meanPayment, User $userAction, string $comment = null): void | |||||
public function debitUser(User $user, float $amount, string $meanPayment, User $userAction, string $comment = null, string $dateTransaction = null): void | |||||
{ | { | ||||
$this->paymentBuilder->createPayment( | $this->paymentBuilder->createPayment( | ||||
Payment::TYPE_DEBIT, | Payment::TYPE_DEBIT, | ||||
$user, | $user, | ||||
$userAction, | $userAction, | ||||
$meanPayment, | $meanPayment, | ||||
$comment | |||||
$comment, | |||||
$dateTransaction | |||||
); | ); | ||||
} | } | ||||
public function creditOrDebitUser(string $type, User $user, float $amount, string $meanPayment, User $userAction, string $comment): void | |||||
public function creditOrDebitUser(string $type, User $user, float $amount, string $meanPayment, User $userAction, string $comment, string $dateTransaction = null): void | |||||
{ | { | ||||
if($type == Payment::TYPE_CREDIT) { | if($type == Payment::TYPE_CREDIT) { | ||||
$this->creditUser($user, $amount, $meanPayment, $userAction, $comment); | |||||
$this->creditUser($user, $amount, $meanPayment, $userAction, $comment, $dateTransaction); | |||||
} | } | ||||
elseif($type == Payment::TYPE_DEBIT) { | elseif($type == Payment::TYPE_DEBIT) { | ||||
$this->debitUser($user, $amount, $meanPayment, $userAction, $comment); | |||||
$this->debitUser($user, $amount, $meanPayment, $userAction, $comment, $dateTransaction); | |||||
} | } | ||||
else { | else { | ||||
throw new ErrorException('$type a une valeur incorrect'); | throw new ErrorException('$type a une valeur incorrect'); | ||||
$userAction, | $userAction, | ||||
$meanPayment, | $meanPayment, | ||||
null, | null, | ||||
null, | |||||
$order | $order | ||||
); | ); | ||||
} | } | ||||
$userAction, | $userAction, | ||||
MeanPayment::CREDIT, | MeanPayment::CREDIT, | ||||
null, | null, | ||||
null, | |||||
$order | $order | ||||
); | ); | ||||
} | } | ||||
$userAction, | $userAction, | ||||
$meanPayment, | $meanPayment, | ||||
null, | null, | ||||
null, | |||||
$order | $order | ||||
); | ); | ||||
} | } | ||||
$userAction, | $userAction, | ||||
MeanPayment::CREDIT, | MeanPayment::CREDIT, | ||||
null, | null, | ||||
null, | |||||
$order | $order | ||||
); | ); | ||||
} | } | ||||
$userAction, | $userAction, | ||||
MeanPayment::CREDIT, | MeanPayment::CREDIT, | ||||
null, | null, | ||||
null, | |||||
$order | $order | ||||
); | ); | ||||
} | } |
} | } | ||||
if ($order && $order->distribution) { | if ($order && $order->distribution) { | ||||
$str .= '<br />Commande : ' . date('d/m/Y', strtotime($order->distribution->date)); | |||||
$str .= '<br />Commande du <a href="'.\Yii::$app->urlManager->createUrl(['distribution/index', 'date' => $order->distribution->date]).'">' . date('d/m/Y', strtotime($order->distribution->date)).'</a>'; | |||||
} else { | } else { | ||||
$str .= '<br />Commande supprimée'; | $str .= '<br />Commande supprimée'; | ||||
} | } |