|
|
|
|
|
|
|
|
namespace common\models; |
|
|
namespace common\models; |
|
|
|
|
|
|
|
|
use common\helpers\GlobalParam; |
|
|
use common\helpers\GlobalParam; |
|
|
|
|
|
use kartik\mpdf\Pdf; |
|
|
|
|
|
|
|
|
class Document extends ActiveRecordCommon |
|
|
class Document extends ActiveRecordCommon |
|
|
{ |
|
|
{ |
|
|
const STATUS_DRAFT = 'draft'; |
|
|
|
|
|
const STATUS_VALID = 'valid'; |
|
|
|
|
|
|
|
|
|
|
|
const TAX_CALCULATION_METHOD_SUM_OF_ROUNDINGS = 'sum-roundings'; |
|
|
|
|
|
const TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM = 'rounding-sum'; |
|
|
|
|
|
const TAX_CALCULATION_METHOD_DEFAULT = self::TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM; |
|
|
|
|
|
|
|
|
|
|
|
public static $taxCalculationMethodArray = [ |
|
|
|
|
|
self::TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM => 'Arrondi de la somme des lignes', |
|
|
|
|
|
self::TAX_CALCULATION_METHOD_SUM_OF_ROUNDINGS => 'Somme des arrondis de chaque ligne' |
|
|
|
|
|
|
|
|
const STATUS_DRAFT = 'draft'; |
|
|
|
|
|
const STATUS_VALID = 'valid'; |
|
|
|
|
|
|
|
|
|
|
|
const TAX_CALCULATION_METHOD_SUM_OF_ROUNDINGS = 'sum-roundings'; |
|
|
|
|
|
const TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM = 'rounding-sum'; |
|
|
|
|
|
const TAX_CALCULATION_METHOD_DEFAULT = self::TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM; |
|
|
|
|
|
|
|
|
|
|
|
public static $taxCalculationMethodArray = [ |
|
|
|
|
|
self::TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM => 'Arrondi de la somme des lignes', |
|
|
|
|
|
self::TAX_CALCULATION_METHOD_SUM_OF_ROUNDINGS => 'Somme des arrondis de chaque ligne' |
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @inheritdoc |
|
|
|
|
|
*/ |
|
|
|
|
|
public function rules() |
|
|
|
|
|
{ |
|
|
|
|
|
return [ |
|
|
|
|
|
[['name', 'id_user'], 'required'], |
|
|
|
|
|
[['date'], 'safe'], |
|
|
|
|
|
[['comment', 'address', 'tax_calculation_method'], 'string'], |
|
|
|
|
|
[['id_user', 'id_producer'], 'integer'], |
|
|
|
|
|
[['name', 'reference', 'status'], 'string', 'max' => 255], |
|
|
|
|
|
[['deliveryNotes'], 'safe'] |
|
|
]; |
|
|
]; |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @inheritdoc |
|
|
|
|
|
*/ |
|
|
|
|
|
public function rules() |
|
|
|
|
|
{ |
|
|
|
|
|
return [ |
|
|
|
|
|
[['name', 'id_user'], 'required'], |
|
|
|
|
|
[['date'], 'safe'], |
|
|
|
|
|
[['comment', 'address', 'tax_calculation_method'], 'string'], |
|
|
|
|
|
[['id_user', 'id_producer'], 'integer'], |
|
|
|
|
|
[['name', 'reference', 'status'], 'string', 'max' => 255], |
|
|
|
|
|
[['deliveryNotes'], 'safe'] |
|
|
|
|
|
]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @inheritdoc |
|
|
|
|
|
*/ |
|
|
|
|
|
public function attributeLabels() |
|
|
|
|
|
{ |
|
|
|
|
|
return [ |
|
|
|
|
|
'id' => 'ID', |
|
|
|
|
|
'name' => 'Nom', |
|
|
|
|
|
'reference' => 'Référence', |
|
|
|
|
|
'date' => 'Date', |
|
|
|
|
|
'comment' => 'Commentaire', |
|
|
|
|
|
'id_user' => 'Utilisateur', |
|
|
|
|
|
'address' => 'Adresse', |
|
|
|
|
|
'id_producer' => 'Producteur', |
|
|
|
|
|
'status' => 'Statut', |
|
|
|
|
|
'tax_calculation_method' => 'Méthode de calcul de la TVA' |
|
|
|
|
|
]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
* Relations |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
public function getUser() |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->hasOne(User::className(), ['id' => 'id_user']); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function getProducer() |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->hasOne(Producer::className(), ['id' => 'id_producer']); |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @inheritdoc |
|
|
|
|
|
*/ |
|
|
|
|
|
public function attributeLabels() |
|
|
|
|
|
{ |
|
|
|
|
|
return [ |
|
|
|
|
|
'id' => 'ID', |
|
|
|
|
|
'name' => 'Nom', |
|
|
|
|
|
'reference' => 'Référence', |
|
|
|
|
|
'date' => 'Date', |
|
|
|
|
|
'comment' => 'Commentaire', |
|
|
|
|
|
'id_user' => 'Utilisateur', |
|
|
|
|
|
'address' => 'Adresse', |
|
|
|
|
|
'id_producer' => 'Producteur', |
|
|
|
|
|
'status' => 'Statut', |
|
|
|
|
|
'tax_calculation_method' => 'Méthode de calcul de la TVA' |
|
|
|
|
|
]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
* Relations |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
public function getUser() |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->hasOne(User::className(), ['id' => 'id_user']); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function getProducer() |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->hasOne(Producer::className(), ['id' => 'id_producer']); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function relationOrders($fieldIdDocument) |
|
|
|
|
|
{ |
|
|
|
|
|
$defaultOptionsSearch = Order::defaultOptionsSearch(); |
|
|
|
|
|
|
|
|
|
|
|
return $this->hasMany(Order::className(), [$fieldIdDocument => 'id']) |
|
|
|
|
|
->with($defaultOptionsSearch['with']) |
|
|
|
|
|
->joinWith($defaultOptionsSearch['join_with']) |
|
|
|
|
|
->orderBy('distribution.date ASC'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
* Méthodes |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
public function getAmount($type = Order::AMOUNT_TOTAL, $format = false) |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->_getAmountGeneric($type, false, $format); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function getAmountWithTax($type = Order::AMOUNT_TOTAL, $format = false) |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->_getAmountGeneric($type, true, $format); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected function _getAmountGeneric($type = Order::AMOUNT_TOTAL, $withTax = true, $format = false) |
|
|
|
|
|
{ |
|
|
|
|
|
$amount = 0; |
|
|
|
|
|
$totalVat = 0; |
|
|
|
|
|
$ordersArray = $this->orders; |
|
|
|
|
|
|
|
|
|
|
|
foreach ($ordersArray as $order) { |
|
|
|
|
|
$order->init($this->tax_calculation_method); |
|
|
|
|
|
$amount += $order->getAmount($type); |
|
|
|
|
|
$totalVat += $order->getTotalVat($type); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function relationOrders($fieldIdDocument) |
|
|
|
|
|
{ |
|
|
|
|
|
$defaultOptionsSearch = Order::defaultOptionsSearch(); |
|
|
|
|
|
|
|
|
|
|
|
return $this->hasMany(Order::className(), [$fieldIdDocument => 'id']) |
|
|
|
|
|
->with($defaultOptionsSearch['with']) |
|
|
|
|
|
->joinWith($defaultOptionsSearch['join_with']) |
|
|
|
|
|
->orderBy('distribution.date ASC'); |
|
|
|
|
|
|
|
|
if($this->isTaxCalculationMethodRoundingOfTheSum()) { |
|
|
|
|
|
$totalVat = Price::round($totalVat); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
* Méthodes |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
public function getAmount($type = Order::AMOUNT_TOTAL, $format = false) |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->_getAmountGeneric($type, false, $format); |
|
|
|
|
|
|
|
|
if($withTax) { |
|
|
|
|
|
$amount += $totalVat; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getAmountWithTax($type = Order::AMOUNT_TOTAL, $format = false) |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->_getAmountGeneric($type, true, $format); |
|
|
|
|
|
|
|
|
if ($format) { |
|
|
|
|
|
return Price::format($amount); |
|
|
|
|
|
} else { |
|
|
|
|
|
return $amount; |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
protected function _getAmountGeneric($type = Order::AMOUNT_TOTAL, $withTax = true, $format = false) |
|
|
|
|
|
{ |
|
|
|
|
|
$amount = 0; |
|
|
|
|
|
$ordersArray = $this->orders; |
|
|
|
|
|
|
|
|
public function getTotalVatArray($typeTotal) |
|
|
|
|
|
{ |
|
|
|
|
|
$totalVatArray = []; |
|
|
|
|
|
|
|
|
foreach ($ordersArray as $order) { |
|
|
|
|
|
$order->init($this->tax_calculation_method); |
|
|
|
|
|
|
|
|
$ordersArray = $this->orders; |
|
|
|
|
|
|
|
|
if ($withTax) { |
|
|
|
|
|
$amount += $order->getAmountWithTax($type); |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
$amount += $order->getAmount($type); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ($format) { |
|
|
|
|
|
return Price::format($amount); |
|
|
|
|
|
} else { |
|
|
|
|
|
return $amount; |
|
|
|
|
|
|
|
|
foreach($ordersArray as $order) { |
|
|
|
|
|
$order->init($this->tax_calculation_method); |
|
|
|
|
|
$fieldNameVat = $order->getFieldNameAmount($typeTotal, 'vat'); |
|
|
|
|
|
foreach($order->$fieldNameVat as $idTaxRate => $vat) { |
|
|
|
|
|
if(!isset($totalVatArray[$idTaxRate])) { |
|
|
|
|
|
$totalVatArray[$idTaxRate] = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
$totalVatArray[$idTaxRate] += $vat; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getPointSale() |
|
|
|
|
|
{ |
|
|
|
|
|
if (isset($this->orders) && isset($this->orders[0])) { |
|
|
|
|
|
return $this->orders[0]->pointSale; |
|
|
|
|
|
} else { |
|
|
|
|
|
return ''; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return $totalVatArray; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public function getDistribution() |
|
|
|
|
|
{ |
|
|
|
|
|
if (isset($this->orders) && isset($this->orders[0])) { |
|
|
|
|
|
return $this->orders[0]->distribution; |
|
|
|
|
|
} else { |
|
|
|
|
|
return ''; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public function getPointSale() |
|
|
|
|
|
{ |
|
|
|
|
|
if (isset($this->orders) && isset($this->orders[0])) { |
|
|
|
|
|
return $this->orders[0]->pointSale; |
|
|
|
|
|
} else { |
|
|
|
|
|
return ''; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getClass() |
|
|
|
|
|
{ |
|
|
|
|
|
return str_replace('common\models\\', '', get_class($this)); |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function getDistribution() |
|
|
|
|
|
{ |
|
|
|
|
|
if (isset($this->orders) && isset($this->orders[0])) { |
|
|
|
|
|
return $this->orders[0]->distribution; |
|
|
|
|
|
} else { |
|
|
|
|
|
return ''; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getType() |
|
|
|
|
|
{ |
|
|
|
|
|
$class = $this->getClass(); |
|
|
|
|
|
|
|
|
|
|
|
if ($class == 'Invoice') { |
|
|
|
|
|
$documentType = 'Facture'; |
|
|
|
|
|
} elseif ($class == 'DeliveryNote') { |
|
|
|
|
|
$documentType = 'Bon de livraison'; |
|
|
|
|
|
} elseif ($class == 'Quotation') { |
|
|
|
|
|
$documentType = 'Devis'; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (isset($documentType)) { |
|
|
|
|
|
return $documentType; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return ''; |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function getClass() |
|
|
|
|
|
{ |
|
|
|
|
|
return str_replace('common\models\\', '', get_class($this)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function getType() |
|
|
|
|
|
{ |
|
|
|
|
|
$class = $this->getClass(); |
|
|
|
|
|
|
|
|
|
|
|
if ($class == 'Invoice') { |
|
|
|
|
|
$documentType = 'Facture'; |
|
|
|
|
|
} elseif ($class == 'DeliveryNote') { |
|
|
|
|
|
$documentType = 'Bon de livraison'; |
|
|
|
|
|
} elseif ($class == 'Quotation') { |
|
|
|
|
|
$documentType = 'Devis'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function isValidClass($typeDocument) |
|
|
|
|
|
{ |
|
|
|
|
|
return in_array($typeDocument, ['Invoice', 'DeliveryNote', 'Quotation']); |
|
|
|
|
|
|
|
|
if (isset($documentType)) { |
|
|
|
|
|
return $documentType; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function generateReference() |
|
|
|
|
|
{ |
|
|
|
|
|
$class = $this->getClass(); |
|
|
|
|
|
$classLower = strtolower($class); |
|
|
|
|
|
if ($classLower == 'deliverynote') { |
|
|
|
|
|
$classLower = 'delivery_note'; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return ''; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
$prefix = Producer::getConfig('document_' . $classLower . '_prefix'); |
|
|
|
|
|
$oneDocumentExist = $class::searchOne(['status' => Document::STATUS_VALID] , ['orderby' => 'reference DESC']); |
|
|
|
|
|
|
|
|
|
|
|
if ($oneDocumentExist) { |
|
|
|
|
|
$reference = $oneDocumentExist->reference; |
|
|
|
|
|
$pattern = '#([A-Z]+)?([0-9]+)#'; |
|
|
|
|
|
preg_match($pattern, $reference, $matches, PREG_OFFSET_CAPTURE); |
|
|
|
|
|
$sizeNumReference = strlen($matches[2][0]); |
|
|
|
|
|
$numReference = ((int)$matches[2][0]) + 1; |
|
|
|
|
|
$numReference = str_pad($numReference, $sizeNumReference, '0', STR_PAD_LEFT); |
|
|
|
|
|
|
|
|
|
|
|
return $prefix . $numReference; |
|
|
|
|
|
} else { |
|
|
|
|
|
$firstReference = Producer::getConfig('document_' . $classLower . '_first_reference'); |
|
|
|
|
|
|
|
|
|
|
|
if (strlen($firstReference) > 0) { |
|
|
|
|
|
return $firstReference; |
|
|
|
|
|
} else { |
|
|
|
|
|
return $prefix . '00001'; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function generatePdf($destination) |
|
|
|
|
|
{ |
|
|
|
|
|
$producer = GlobalParam::getCurrentProducer(); |
|
|
|
|
|
$content = Yii::$app->controller->renderPartial('/document/download', [ |
|
|
|
|
|
'producer' => $producer, |
|
|
|
|
|
'document' => $this |
|
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
$contentFooter = '<div id="footer">'; |
|
|
|
|
|
$contentFooter .= '<div class="infos-bottom">' . Html::encode($producer->document_infos_bottom) . '</div>'; |
|
|
|
|
|
if ($this->isStatusValid() || $this->isStatusDraft()) { |
|
|
|
|
|
$contentFooter .= '<div class="reference-document">'; |
|
|
|
|
|
if ($this->isStatusValid()) { |
|
|
|
|
|
$contentFooter .= $this->getType() . ' N°' . $this->reference; |
|
|
|
|
|
} |
|
|
|
|
|
if ($this->isStatusDraft()) { |
|
|
|
|
|
$contentFooter .= $this->getType() . ' non validé'; |
|
|
|
|
|
if($this->getType() == 'Facture') { |
|
|
|
|
|
$contentFooter .= 'e' ; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
$contentFooter .= '</div>'; |
|
|
|
|
|
} |
|
|
|
|
|
$contentFooter .= '</div>'; |
|
|
|
|
|
|
|
|
public function isValidClass($typeDocument) |
|
|
|
|
|
{ |
|
|
|
|
|
return in_array($typeDocument, ['Invoice', 'DeliveryNote', 'Quotation']); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
$marginBottom = 10 ; |
|
|
|
|
|
if(strlen(Producer::getConfig('document_infos_bottom')) > 0) { |
|
|
|
|
|
$marginBottom = 40 ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$pdf = new Pdf([ |
|
|
|
|
|
'mode' => Pdf::MODE_UTF8, |
|
|
|
|
|
'format' => Pdf::FORMAT_A4, |
|
|
|
|
|
'orientation' => Pdf::ORIENT_PORTRAIT, |
|
|
|
|
|
'destination' => $destination, |
|
|
|
|
|
'content' => $content, |
|
|
|
|
|
'filename' => $this->getFilename(), |
|
|
|
|
|
'cssFile' => Yii::getAlias('@webroot/css/document/download.css'), |
|
|
|
|
|
'marginBottom' => $marginBottom, |
|
|
|
|
|
'methods' => [ |
|
|
|
|
|
'SetHTMLFooter' => $contentFooter |
|
|
|
|
|
] |
|
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
return $pdf->render(); |
|
|
|
|
|
|
|
|
public function generateReference() |
|
|
|
|
|
{ |
|
|
|
|
|
$class = $this->getClass(); |
|
|
|
|
|
$classLower = strtolower($class); |
|
|
|
|
|
if ($classLower == 'deliverynote') { |
|
|
|
|
|
$classLower = 'delivery_note'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function send() |
|
|
|
|
|
{ |
|
|
|
|
|
if(isset($this->user) && strlen($this->user->email) > 0) { |
|
|
|
|
|
$producer = GlobalParam::getCurrentProducer(); |
|
|
|
|
|
|
|
|
|
|
|
$subjectEmail = $this->getType() ; |
|
|
|
|
|
if($this->isStatusValid()) { |
|
|
|
|
|
$subjectEmail .= ' N°'.$this->reference ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$email = Yii::$app->mailer->compose( |
|
|
|
|
|
[ |
|
|
|
|
|
'html' => 'sendDocument-html', |
|
|
|
|
|
'text' => 'sendDocument-text' |
|
|
|
|
|
], [ |
|
|
|
|
|
'document' => $this, |
|
|
|
|
|
]) |
|
|
|
|
|
->setTo($this->user->email) |
|
|
|
|
|
->setFrom([$producer->getEmailOpendistrib() => $producer->name]) |
|
|
|
|
|
->setSubject('['.$producer->name.'] '.$subjectEmail) ; |
|
|
|
|
|
|
|
|
|
|
|
$this->generatePdf(Pdf::DEST_FILE) ; |
|
|
|
|
|
$email->attach($this->getFilename()); |
|
|
|
|
|
|
|
|
|
|
|
return $email->send() ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return false ; |
|
|
|
|
|
|
|
|
$prefix = Producer::getConfig('document_' . $classLower . '_prefix'); |
|
|
|
|
|
$oneDocumentExist = $class::searchOne(['status' => Document::STATUS_VALID], ['orderby' => 'reference DESC']); |
|
|
|
|
|
|
|
|
|
|
|
if ($oneDocumentExist) { |
|
|
|
|
|
$reference = $oneDocumentExist->reference; |
|
|
|
|
|
$pattern = '#([A-Z]+)?([0-9]+)#'; |
|
|
|
|
|
preg_match($pattern, $reference, $matches, PREG_OFFSET_CAPTURE); |
|
|
|
|
|
$sizeNumReference = strlen($matches[2][0]); |
|
|
|
|
|
$numReference = ((int)$matches[2][0]) + 1; |
|
|
|
|
|
$numReference = str_pad($numReference, $sizeNumReference, '0', STR_PAD_LEFT); |
|
|
|
|
|
|
|
|
|
|
|
return $prefix . $numReference; |
|
|
|
|
|
} else { |
|
|
|
|
|
$firstReference = Producer::getConfig('document_' . $classLower . '_first_reference'); |
|
|
|
|
|
|
|
|
|
|
|
if (strlen($firstReference) > 0) { |
|
|
|
|
|
return $firstReference; |
|
|
|
|
|
} else { |
|
|
|
|
|
return $prefix . '00001'; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function changeStatus($status) |
|
|
|
|
|
{ |
|
|
|
|
|
if ($status == Document::STATUS_VALID) { |
|
|
|
|
|
$this->status = $status; |
|
|
|
|
|
$this->reference = $this->generateReference(); |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function generatePdf($destination) |
|
|
|
|
|
{ |
|
|
|
|
|
$producer = GlobalParam::getCurrentProducer(); |
|
|
|
|
|
$content = Yii::$app->controller->renderPartial('/document/download', [ |
|
|
|
|
|
'producer' => $producer, |
|
|
|
|
|
'document' => $this |
|
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
$contentFooter = '<div id="footer">'; |
|
|
|
|
|
$contentFooter .= '<div class="infos-bottom">' . Html::encode($producer->document_infos_bottom) . '</div>'; |
|
|
|
|
|
if ($this->isStatusValid() || $this->isStatusDraft()) { |
|
|
|
|
|
$contentFooter .= '<div class="reference-document">'; |
|
|
|
|
|
if ($this->isStatusValid()) { |
|
|
|
|
|
$contentFooter .= $this->getType() . ' N°' . $this->reference; |
|
|
|
|
|
} |
|
|
|
|
|
if ($this->isStatusDraft()) { |
|
|
|
|
|
$contentFooter .= $this->getType() . ' non validé'; |
|
|
|
|
|
if ($this->getType() == 'Facture') { |
|
|
|
|
|
$contentFooter .= 'e'; |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
$contentFooter .= '</div>'; |
|
|
} |
|
|
} |
|
|
|
|
|
$contentFooter .= '</div>'; |
|
|
|
|
|
|
|
|
public function getStatusWording() |
|
|
|
|
|
{ |
|
|
|
|
|
return ($this->status == self::STATUS_DRAFT) ? 'Brouillon' : 'Validé'; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function getStatusCssClass() |
|
|
|
|
|
{ |
|
|
|
|
|
return ($this->status == self::STATUS_DRAFT) ? 'default' : 'success'; |
|
|
|
|
|
|
|
|
$marginBottom = 10; |
|
|
|
|
|
if (strlen(Producer::getConfig('document_infos_bottom')) > 0) { |
|
|
|
|
|
$marginBottom = 40; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getHtmlLabel() |
|
|
|
|
|
{ |
|
|
|
|
|
$label = $this->getStatusWording(); |
|
|
|
|
|
$classLabel = $this->getStatusCssClass(); |
|
|
|
|
|
return '<span class="label label-' . $classLabel . '">' . $label . '</span>'; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
$pdf = new Pdf([ |
|
|
|
|
|
'mode' => Pdf::MODE_UTF8, |
|
|
|
|
|
'format' => Pdf::FORMAT_A4, |
|
|
|
|
|
'orientation' => Pdf::ORIENT_PORTRAIT, |
|
|
|
|
|
'destination' => $destination, |
|
|
|
|
|
'content' => $content, |
|
|
|
|
|
'filename' => $this->getFilename(), |
|
|
|
|
|
'cssFile' => Yii::getAlias('@webroot/css/document/download.css'), |
|
|
|
|
|
'marginBottom' => $marginBottom, |
|
|
|
|
|
'methods' => [ |
|
|
|
|
|
'SetHTMLFooter' => $contentFooter |
|
|
|
|
|
] |
|
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
return $pdf->render(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function send() |
|
|
|
|
|
{ |
|
|
|
|
|
if (isset($this->user) && strlen($this->user->email) > 0) { |
|
|
|
|
|
$producer = GlobalParam::getCurrentProducer(); |
|
|
|
|
|
|
|
|
|
|
|
$subjectEmail = $this->getType(); |
|
|
|
|
|
if ($this->isStatusValid()) { |
|
|
|
|
|
$subjectEmail .= ' N°' . $this->reference; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public function isStatus($status) |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->status == $status; |
|
|
|
|
|
|
|
|
$email = Yii::$app->mailer->compose( |
|
|
|
|
|
[ |
|
|
|
|
|
'html' => 'sendDocument-html', |
|
|
|
|
|
'text' => 'sendDocument-text' |
|
|
|
|
|
], [ |
|
|
|
|
|
'document' => $this, |
|
|
|
|
|
]) |
|
|
|
|
|
->setTo($this->user->email) |
|
|
|
|
|
->setFrom([$producer->getEmailOpendistrib() => $producer->name]) |
|
|
|
|
|
->setSubject('[' . $producer->name . '] ' . $subjectEmail); |
|
|
|
|
|
|
|
|
|
|
|
$this->generatePdf(Pdf::DEST_FILE); |
|
|
|
|
|
$email->attach($this->getFilename()); |
|
|
|
|
|
|
|
|
|
|
|
return $email->send(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function isStatusDraft() |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->isStatus(self::STATUS_DRAFT); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public function isStatusValid() |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->isStatus(self::STATUS_VALID); |
|
|
|
|
|
|
|
|
public function changeStatus($status) |
|
|
|
|
|
{ |
|
|
|
|
|
if ($status == Document::STATUS_VALID) { |
|
|
|
|
|
$this->status = $status; |
|
|
|
|
|
$this->reference = $this->generateReference(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getProductsOrders() |
|
|
|
|
|
{ |
|
|
|
|
|
$productsOrdersArray = []; |
|
|
|
|
|
$ordersArray = $this->orders ; |
|
|
|
|
|
if ($ordersArray && count($ordersArray)) { |
|
|
|
|
|
foreach ($ordersArray as $order) { |
|
|
|
|
|
foreach ($order->productOrder as $productOrder) { |
|
|
|
|
|
$indexProductOrder = $productOrder->product->order ; |
|
|
|
|
|
$newProductOrder = clone $productOrder ; |
|
|
|
|
|
|
|
|
|
|
|
if (!isset($productsOrdersArray[$indexProductOrder])) { |
|
|
|
|
|
$productsOrdersArray[$indexProductOrder] = [$newProductOrder]; |
|
|
|
|
|
} else { |
|
|
|
|
|
$productOrderMatch = false; |
|
|
|
|
|
foreach ($productsOrdersArray[$indexProductOrder] as &$theProductOrder) { |
|
|
|
|
|
if ($theProductOrder->unit == $productOrder->unit |
|
|
|
|
|
&& ( (!$this->isInvoicePrice() && $theProductOrder->price == $productOrder->price) |
|
|
|
|
|
|| ($this->isInvoicePrice() && $theProductOrder->invoice_price == $productOrder->invoice_price) |
|
|
|
|
|
)) { |
|
|
|
|
|
|
|
|
|
|
|
$theProductOrder->quantity += $productOrder->quantity; |
|
|
|
|
|
$productOrderMatch = true; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (!$productOrderMatch) { |
|
|
|
|
|
$productsOrdersArray[$indexProductOrder][] = $newProductOrder; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function getStatusWording() |
|
|
|
|
|
{ |
|
|
|
|
|
return ($this->status == self::STATUS_DRAFT) ? 'Brouillon' : 'Validé'; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function getStatusCssClass() |
|
|
|
|
|
{ |
|
|
|
|
|
return ($this->status == self::STATUS_DRAFT) ? 'default' : 'success'; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function getHtmlLabel() |
|
|
|
|
|
{ |
|
|
|
|
|
$label = $this->getStatusWording(); |
|
|
|
|
|
$classLabel = $this->getStatusCssClass(); |
|
|
|
|
|
return '<span class="label label-' . $classLabel . '">' . $label . '</span>'; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function isStatus($status) |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->status == $status; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function isStatusDraft() |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->isStatus(self::STATUS_DRAFT); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function isStatusValid() |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->isStatus(self::STATUS_VALID); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function getProductsOrders() |
|
|
|
|
|
{ |
|
|
|
|
|
$productsOrdersArray = []; |
|
|
|
|
|
$ordersArray = $this->orders; |
|
|
|
|
|
if ($ordersArray && count($ordersArray)) { |
|
|
|
|
|
foreach ($ordersArray as $order) { |
|
|
|
|
|
foreach ($order->productOrder as $productOrder) { |
|
|
|
|
|
$indexProductOrder = $productOrder->product->order; |
|
|
|
|
|
$newProductOrder = clone $productOrder; |
|
|
|
|
|
|
|
|
|
|
|
if (!isset($productsOrdersArray[$indexProductOrder])) { |
|
|
|
|
|
$productsOrdersArray[$indexProductOrder] = [$newProductOrder]; |
|
|
|
|
|
} else { |
|
|
|
|
|
$productOrderMatch = false; |
|
|
|
|
|
foreach ($productsOrdersArray[$indexProductOrder] as &$theProductOrder) { |
|
|
|
|
|
if ($theProductOrder->unit == $productOrder->unit |
|
|
|
|
|
&& ((!$this->isInvoicePrice() && $theProductOrder->price == $productOrder->price) |
|
|
|
|
|
|| ($this->isInvoicePrice() && $theProductOrder->invoice_price == $productOrder->invoice_price) |
|
|
|
|
|
)) { |
|
|
|
|
|
|
|
|
|
|
|
$theProductOrder->quantity += $productOrder->quantity; |
|
|
|
|
|
$productOrderMatch = true; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (!$productOrderMatch) { |
|
|
|
|
|
$productsOrdersArray[$indexProductOrder][] = $newProductOrder; |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// tri des orderProduct par product.order |
|
|
|
|
|
ksort($productsOrdersArray); |
|
|
|
|
|
|
|
|
|
|
|
return $productsOrdersArray; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function isDisplayOrders() |
|
|
|
|
|
{ |
|
|
|
|
|
$displayOrders = ($this->getClass() == 'Invoice') ? |
|
|
|
|
|
Producer::getConfig('document_display_orders_invoice') : |
|
|
|
|
|
Producer::getConfig('document_display_orders_delivery_note') ; |
|
|
|
|
|
|
|
|
|
|
|
return $displayOrders ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function getFilename() |
|
|
|
|
|
{ |
|
|
|
|
|
return Yii::getAlias('@app/web/pdf/'.$this->getType().'-' . $this->reference. '.pdf') ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function isInvoicePrice() |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->getClass() == 'Invoice' || $this->getClass() == 'DeliveryNote' ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function isTaxCalculationMethodSumOfRoundings() |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->tax_calculation_method == self::TAX_CALCULATION_METHOD_SUM_OF_ROUNDINGS; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function isTaxCalculationMethodRoundingOfTheSum() |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->tax_calculation_method == self::TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function initTaxCalculationMethod() |
|
|
|
|
|
{ |
|
|
|
|
|
$producerTaxCalculationMethod = Producer::getConfig('option_tax_calculation_method'); |
|
|
|
|
|
|
|
|
|
|
|
if($producerTaxCalculationMethod) { |
|
|
|
|
|
$this->tax_calculation_method = $producerTaxCalculationMethod; |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
$this->tax_calculation_method = self::TAX_CALCULATION_METHOD_DEFAULT; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// tri des orderProduct par product.order |
|
|
|
|
|
ksort($productsOrdersArray); |
|
|
|
|
|
|
|
|
|
|
|
return $productsOrdersArray; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function isDisplayOrders() |
|
|
|
|
|
{ |
|
|
|
|
|
$displayOrders = ($this->getClass() == 'Invoice') ? |
|
|
|
|
|
Producer::getConfig('document_display_orders_invoice') : |
|
|
|
|
|
Producer::getConfig('document_display_orders_delivery_note'); |
|
|
|
|
|
|
|
|
|
|
|
return $displayOrders; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function getFilename() |
|
|
|
|
|
{ |
|
|
|
|
|
return Yii::getAlias('@app/web/pdf/' . $this->getType() . '-' . $this->reference . '.pdf'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function isInvoicePrice() |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->getClass() == 'Invoice' || $this->getClass() == 'DeliveryNote'; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function isTaxCalculationMethodSumOfRoundings() |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->tax_calculation_method == self::TAX_CALCULATION_METHOD_SUM_OF_ROUNDINGS; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function isTaxCalculationMethodRoundingOfTheSum() |
|
|
|
|
|
{ |
|
|
|
|
|
return $this->tax_calculation_method == self::TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function initTaxCalculationMethod() |
|
|
|
|
|
{ |
|
|
|
|
|
$producerTaxCalculationMethod = Producer::getConfig('option_tax_calculation_method'); |
|
|
|
|
|
|
|
|
|
|
|
if ($producerTaxCalculationMethod) { |
|
|
|
|
|
$this->tax_calculation_method = $producerTaxCalculationMethod; |
|
|
|
|
|
} else { |
|
|
|
|
|
$this->tax_calculation_method = self::TAX_CALCULATION_METHOD_DEFAULT; |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |