<?php
/**
* FacturesEntete.php
* Created by Stéphane Brun
* Date: 19/04/2018 at 16:39
*/
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Annotation\MaxDepth;
/**
* @ORM\Entity
* @ORM\Table(name="tfacturesentete")
* @ORM\Entity(repositoryClass="App\Repository\FacturesRepository")
*/
class Factures
{
const COUPONS_POURCENT = 5;
const HISTORY_WATCH = array(
'date' => 'Date',
'client' => 'Client',
'transport' => 'Livraison',
'commentaire' => 'Commentaire',
);
/**
* @ORM\Column(name="EFacture", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="EDate", type="date", nullable=true)
*/
protected $date;
/**
* @ORM\Column(name="EBonCommande", type="string", length=100, nullable=true)
*/
protected $bonCommande;
/**
* @ORM\Column(name="commentaire", type="string", length=255, nullable=true)
*/
protected $commentaire;
/**
* @ORM\Column(name="ECommission", type="integer", nullable=true)
*/
protected $commission;
/**
* @ORM\Column(name="ETransport", type="float", nullable=false, options={"default": 0})
*/
protected $transport = 0;
/**
* @ORM\Column(name="ETPS", type="float", nullable=true)
*/
protected $tps;
/**
* @ORM\Column(name="ETVQ", type="float", nullable=true)
*/
protected $tvq;
/**
* @ORM\Column(name="EFermee", type="boolean", nullable=true)
*/
protected $fermee = false;
/**
* @ORM\Column(name="ERabaisP", type="integer", nullable=true)
*/
protected $rabaisP = 0;
/**
* @ORM\Column(name="ERabaisM", type="float", nullable=true)
*/
protected $rabaisM = 0;
/**
* @ORM\Column(name="ERabaisCatalogue", type="float", nullable=true)
*/
protected $rabaisCatalogue;
/**
* @ORM\Column(name="EComptant", type="float", nullable=true)
*/
protected $comptant;
/**
* @ORM\Column(name="EInteract", type="float", nullable=true)
*/
protected $interact;
/**
* @ORM\Column(name="ECarteMC", type="float", nullable=true)
*/
protected $carteMC;
/**
* @ORM\Column(name="ECarteV", type="float", nullable=true)
*/
protected $carteV;
/**
* @ORM\Column(name="ECoupons", type="float", nullable=true)
*/
protected $coupons;
/**
* @ORM\Column(name="ECertificatsCadeaux", type="float", nullable=true)
*/
protected $certificatsCadeaux;
/**
* @ORM\Column(name="ECertificatsCentre", type="float", nullable=true)
*/
protected $certificatsCentre;
/**
* @ORM\Column(name="EFidelite", type="float", nullable=true)
*/
protected $fidelite;
/**
* @ORM\Column(name="ECarteAPuce", type="float", nullable=true)
*/
protected $carteAPuce;
/**
* @ORM\Column(name="EMonnaie", type="float", nullable=true)
*/
protected $monnaie = 0;
/**
* @ORM\Column(name="ESoldeARecevoir", type="float", nullable=true)
*/
protected $soldeARecevoir;
/**
* @ORM\Column(name="EAcompte", type="float", nullable=true)
*/
protected $acompte;
/**
* @ORM\Column(name="EacompteUtilise", type="float", nullable=true)
*/
protected $acompteUtilise;
/**
* @ORM\Column(name="ECommis", type="integer", nullable=true)
*/
protected $commis;
/**
* @ORM\Column(name="EAnnulee", type="boolean", nullable=true)
*/
protected $annulee = 0;
/**
* @ORM\Column(name="EAnnuleeRaison", type="string", length=100, nullable=true)
*/
protected $annuleeRaison;
/**
* @ORM\Column(name="EAnnuleeOp", type="string", length=50, nullable=true)
*/
protected $annuleeOp;
/**
* @ORM\Column(name="EannuleeQuand", type="datetime", nullable=true)
*/
protected $annuleeQuand;
/**
* @ORM\Column(name="ECaisse", type="string", length=50, nullable=true)
*/
protected $caisse;
/**
* @ORM\Column(name="ESession", type="string", length=50, nullable=true)
*/
protected $session;
/**
* @ORM\Column(name="ECertN1", type="integer", nullable=true)
*/
protected $certN1;
/**
* @ORM\Column(name="ECertN2", type="integer", nullable=true)
*/
protected $certN2;
/**
* @ORM\Column(name="ECertN3", type="integer", nullable=true)
*/
protected $certN3;
/**
* @ORM\Column(name="ECertN4", type="integer", nullable=true)
*/
protected $certN4;
/**
* @ORM\Column(name="ECertM1", type="float", nullable=true)
*/
protected $certM1;
/**
* @ORM\Column(name="ECertM2", type="float", nullable=true)
*/
protected $certM2;
/**
* @ORM\Column(name="ECertM3", type="float", nullable=true)
*/
protected $certM3;
/**
* @ORM\Column(name="ECertM4", type="float", nullable=true)
*/
protected $certM4;
/**
* @ORM\Column(name="ETS", type="datetime", nullable=true)
*/
protected $ts;
/**
* one Fournisseurs (Transporteur)
*
* @ORM\ManyToOne(targetEntity="App\Entity\Fournisseurs")
* @ORM\JoinColumn(name="ETransporteur", referencedColumnName="id", nullable=true)
*/
protected $transporteur;
/**
* Many Factures has one Company (Succursalle)
*
* @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="factures")
* @ORM\JoinColumn(name="ESuccursale", referencedColumnName="id", nullable=true)
*/
protected $succursale;
/**
* Many Factures has one Client
*
* @ORM\ManyToOne(targetEntity="App\Entity\Clients")
* @ORM\JoinColumn(name="EClient", referencedColumnName="id", nullable=true)
*/
protected $client;
/**
* Many Factures has one Provenance
*
* @ORM\ManyToOne(targetEntity="App\Entity\Provenances")
* @ORM\JoinColumn(name="EProvenance", referencedColumnName="id", nullable=true)
*/
protected $provenance;
/**
* Many Factures has one ClientsAdressesLivraison
*
* @ORM\ManyToOne(targetEntity="App\Entity\ClientsAdressesLivraison")
* @ORM\JoinColumn(name="EClientADL", referencedColumnName="id", nullable=true)
*/
protected $clientADL;
/**
* One Factures has many FacturesDetail
* , fetch="EAGER" ???
*
*
* @ORM\OneToMany(targetEntity="FacturesDetail", mappedBy="facture", cascade={"persist", "remove"}, orphanRemoval=true)
* @MaxDepth(1)
*/
protected $details;
/**
* has one Company
*
* @ORM\ManyToOne(targetEntity="App\Entity\Company")
* @ORM\JoinColumn(name="company_id", referencedColumnName="id", nullable=true)
* @MaxDepth(1)
*/
protected $company;
public function __construct()
{
$this->details = new ArrayCollection();
$this->date = new \DateTime();
}
/**
* @return mixed
*/
public function getCompany()
{
return $this->company;
}
/**
* @param Company $company
*/
public function setCompany(Company $company)
{
$this->company = $company;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getDate()
{
return $this->date;
}
/**
* @param mixed $date
*/
public function setDate($date)
{
$this->date = $date;
}
/**
* @return mixed
*/
public function getBonCommande()
{
return $this->bonCommande;
}
/**
* @param mixed $bonCommande
*/
public function setBonCommande($bonCommande)
{
$this->bonCommande = $bonCommande;
}
/**
/**
* @return mixed
*/
public function getTransport()
{
return $this->transport;
}
/**
* @param mixed $transport
*/
public function setTransport($transport)
{
$this->transport = $transport;
}
/**
* @return mixed
*/
public function getTps()
{
return $this->tps;
}
/**
* @param mixed $tps
*/
public function setTps($tps)
{
$this->tps = $tps;
}
/**
* @return mixed
*/
public function getTvq()
{
return $this->tvq;
}
/**
* @param mixed $tvq
*/
public function setTvq($tvq)
{
$this->tvq = $tvq;
}
/**
* @return mixed
*/
public function getFermee()
{
return $this->fermee;
}
/**
* @param mixed $fermee
*/
public function setFermee($fermee)
{
$this->fermee = $fermee;
}
/**
* @return mixed
*/
public function getRabaisP()
{
return $this->rabaisP;
}
/**
* @param mixed $rabaisP
*/
public function setRabaisP($rabaisP)
{
$this->rabaisP = $rabaisP;
}
/**
* @return mixed
*/
public function getRabaisM()
{
return $this->rabaisM;
}
/**
* @param mixed $rabaisM
*/
public function setRabaisM($rabaisM)
{
$this->rabaisM = $rabaisM;
}
/**
* @return mixed
*/
public function getRabaisCatalogue()
{
return $this->rabaisCatalogue;
}
/**
* @param mixed $rabaisCatalogue
*/
public function setRabaisCatalogue($rabaisCatalogue)
{
$this->rabaisCatalogue = $rabaisCatalogue;
}
/**
* @return mixed
*/
public function getComptant()
{
return $this->comptant;
}
/**
* @param mixed $comptant
*/
public function setComptant($comptant)
{
$this->comptant = $comptant;
}
/**
* @return mixed
*/
public function getInteract()
{
return $this->interact;
}
/**
* @param mixed $interact
*/
public function setInteract($interact)
{
$this->interact = $interact;
}
/**
* @return mixed
*/
public function getCarteMC()
{
return $this->carteMC;
}
/**
* @param mixed $carteMC
*/
public function setCarteMC($carteMC)
{
$this->carteMC = $carteMC;
}
/**
* @return mixed
*/
public function getCarteV()
{
return $this->carteV;
}
/**
* @param mixed $carteV
*/
public function setCarteV($carteV)
{
$this->carteV = $carteV;
}
/**
* @return mixed
*/
public function getCoupons()
{
return $this->coupons;
}
/**
* @param mixed $coupons
*/
public function setCoupons($coupons)
{
$this->coupons = $coupons;
}
/**
* @return mixed
*/
public function getCertificatsCadeaux()
{
return $this->certificatsCadeaux;
}
/**
* @param mixed $certificatsCadeaux
*/
public function setCertificatsCadeaux($certificatsCadeaux)
{
$this->certificatsCadeaux = $certificatsCadeaux;
}
/**
* @return mixed
*/
public function getCertificatsCentre()
{
return $this->certificatsCentre;
}
/**
* @param mixed $certificatsCentre
*/
public function setCertificatsCentre($certificatsCentre)
{
$this->certificatsCentre = $certificatsCentre;
}
/**
* @return mixed
*/
public function getFidelite()
{
return $this->fidelite;
}
/**
* @param mixed $fidelite
*/
public function setFidelite($fidelite)
{
$this->fidelite = $fidelite;
}
/**
* @return mixed
*/
public function getCarteAPuce()
{
return $this->carteAPuce;
}
/**
* @param mixed $carteAPuce
*/
public function setCarteAPuce($carteAPuce)
{
$this->carteAPuce = $carteAPuce;
}
/**
* @return mixed
*/
public function getMonnaie()
{
if (empty($this->monnaie)) {
return 0;
}
return $this->monnaie;
}
/**
* @param mixed $monnaie
*/
public function setMonnaie($monnaie): void
{
$this->monnaie = $monnaie;
}
/**
* @return mixed
*/
public function getSoldeARecevoir()
{
return $this->soldeARecevoir;
}
/**
* @param mixed $soldeARecevoir
*/
public function setSoldeARecevoir($soldeARecevoir)
{
$this->soldeARecevoir = $soldeARecevoir;
}
/**
* @return mixed
*/
public function getAcompte()
{
return $this->acompte;
}
/**
* @param mixed $acompte
*/
public function setAcompte($acompte)
{
$this->acompte = $acompte;
}
/**
* @return mixed
*/
public function getAcompteUtilise()
{
return $this->acompteUtilise;
}
/**
* @param mixed $acompteUtilise
*/
public function setAcompteUtilise($acompteUtilise)
{
$this->acompteUtilise = $acompteUtilise;
}
/**
* @return mixed
*/
public function getCommis()
{
return $this->commis;
}
/**
* @param mixed $commis
*/
public function setCommis($commis)
{
$this->commis = $commis;
}
/**
* @return mixed
*/
public function getAnnulee()
{
return $this->annulee;
}
/**
* @param mixed $annulee
*/
public function setAnnulee($annulee)
{
$this->annulee = $annulee;
}
/**
* @return mixed
*/
public function getAnnuleeRaison()
{
return $this->annuleeRaison;
}
/**
* @param mixed $annuleeRaison
*/
public function setAnnuleeRaison($annuleeRaison)
{
$this->annuleeRaison = $annuleeRaison;
}
/**
* @return mixed
*/
public function getAnnuleeOp()
{
return $this->annuleeOp;
}
/**
* @param mixed $annuleeOp
*/
public function setAnnuleeOp($annuleeOp)
{
$this->annuleeOp = $annuleeOp;
}
/**
* @return mixed
*/
public function getAnnuleeQuand()
{
return $this->annuleeQuand;
}
/**
* @param mixed $annuleeQuand
*/
public function setAnnuleeQuand($annuleeQuand)
{
$this->annuleeQuand = $annuleeQuand;
}
/**
* @return mixed
*/
public function getCaisse()
{
return $this->caisse;
}
/**
* @param mixed $caisse
*/
public function setCaisse($caisse)
{
$this->caisse = $caisse;
}
/**
* @return mixed
*/
public function getSession()
{
return $this->session;
}
/**
* @param mixed $session
*/
public function setSession($session)
{
$this->session = $session;
}
/**
* @return mixed
*/
public function getCertN1()
{
return $this->certN1;
}
/**
* @param mixed $certN1
*/
public function setCertN1($certN1)
{
$this->certN1 = $certN1;
}
/**
* @return mixed
*/
public function getCertN2()
{
return $this->certN2;
}
/**
* @param mixed $certN2
*/
public function setCertN2($certN2)
{
$this->certN2 = $certN2;
}
/**
* @return mixed
*/
public function getCertN3()
{
return $this->certN3;
}
/**
* @param mixed $certN3
*/
public function setCertN3($certN3)
{
$this->certN3 = $certN3;
}
/**
* @return mixed
*/
public function getCertN4()
{
return $this->certN4;
}
/**
* @param mixed $certN4
*/
public function setCertN4($certN4)
{
$this->certN4 = $certN4;
}
/**
* @return mixed
*/
public function getCertM1()
{
return $this->certM1;
}
/**
* @param mixed $certM1
*/
public function setCertM1($certM1)
{
$this->certM1 = $certM1;
}
/**
* @return mixed
*/
public function getCertM2()
{
return $this->certM2;
}
/**
* @param mixed $certM2
*/
public function setCertM2($certM2)
{
$this->certM2 = $certM2;
}
/**
* @return mixed
*/
public function getCertM3()
{
return $this->certM3;
}
/**
* @param mixed $certM3
*/
public function setCertM3($certM3)
{
$this->certM3 = $certM3;
}
/**
* @return mixed
*/
public function getCertM4()
{
return $this->certM4;
}
/**
* @param mixed $certM4
*/
public function setCertM4($certM4)
{
$this->certM4 = $certM4;
}
/**
* @return mixed
*/
public function getTs()
{
return $this->ts;
}
/**
* @param mixed $ts
*/
public function setTs($ts)
{
$this->ts = $ts;
}
/**
* @return mixed
*/
public function getClient()
{
return $this->client;
}
/**
* @param Clients $client
*/
public function setClient(Clients $client)
{
$this->client = $client;
}
/**
* @return mixed
*/
public function getClientADL()
{
return $this->clientADL;
}
/**
* @param mixed $clientADL
*/
public function setClientADL($clientADL=null)
{
$this->clientADL = $clientADL;
}
/**
* @return mixed
*/
public function getDetails()
{
return $this->details;
}
/**
* @param mixed $details
*/
public function setDetails($details)
{
$this->details = $details;
}
/**
* @param FacturesDetail $detail
* @return Factures
*/
public function addDetail(FacturesDetail $detail)
{
if (!$this->details->contains($detail)) {
$this->details->add($detail);
$detail->setFacture($this);
}
return $this;
}
/**
* @param FacturesDetail $detail
* @return $this
*/
public function removeDetail(FacturesDetail $detail)
{
if ($this->details->contains($detail)) {
$this->details->removeElement($detail);
// $detail->setFacture(null);
}
return $this;
}
/**
* @return mixed
*/
public function getSuccursale()
{
return $this->succursale;
}
/**
* @param Company $succursale
*/
public function setSuccursale(Company $succursale = null)
{
$this->succursale = $succursale;
}
/**
* @return mixed
*/
public function getTransporteur()
{
return $this->transporteur;
}
/**
* @param Fournisseurs $transporteur
*/
public function setTransporteur(Fournisseurs $transporteur = null)
{
$this->transporteur = $transporteur;
}
/**
* @return mixed
*/
public function getCommission()
{
return $this->commission;
}
/**
* @param mixed $commission
*/
public function setCommission($commission)
{
$this->commission = $commission;
}
/**
* @return mixed
*/
public function getCommentaire()
{
return $this->commentaire;
}
/**
* @param mixed $commentaire
* @return Factures
*/
public function setCommentaire($commentaire)
{
$this->commentaire = $commentaire;
return $this;
}
/**
* @return mixed
*/
public function getProvenance()
{
return $this->provenance;
}
/**
* @param Provenances $provenance
*/
public function setProvenance(Provenances $provenance = null)
{
$this->provenance = $provenance;
}
/**
* @return string
*/
public function getNumero()
{
$n = $this->getCompany()->getId().$this->getId();
return $n;
}
/**
*
* @param int $tvq
* @param int $tps
* @param bool $usedQteDelivred
* @return array
*/
public function getTotals($tvq=0, $tps=0, $usedQteDelivred = false)
{
$totals = [
"brut" => 0,
"net" => 0,
"rabaisP" => 0,
"rabaisM" => 0,
"rabaisTotal" => 0,
"acompte" => 0,
"total" => 0,
"tvq" => 0,
"tps" => 0,
"coupons" => 0,
"monaie" => 0,
"cstotal" => 0,
"cstvq" => 0,
"cstps" => 0,
];
$client = $this->getClient();
/** @var FacturesDetail $detail */
foreach ($this->getDetails() as $detail) {
$net = $detail->getNet($usedQteDelivred);
$totals["brut"] += $detail->getBrut($usedQteDelivred);
$totals["net"] += $net;
$totals["rabaisP"] += $detail->getRabaisP();
$totals["rabaisM"] += $detail->getRabaisM();
$totals["rabaisTotal"] += $detail->getRabaisTotal();
// Si la facture est encore ouverte, on recalcule le total des taxes
// if (false == $this->getFermee()) {
$dtps = 0;
if (!$client->getExemptTps() && $detail->getTps()) {
$dtps = ($net * $tps);
$totals["tps"] += (!empty($dtps)) ? $dtps : 0;
}
$dtvq = 0;
if (!$client->getExemptTvq() && $detail->getTvq()) {
$dtvq = ($net * $tvq);
$totals["tvq"] += (!empty($dtvq)) ? $dtvq : 0;
}
$totals["total"] += ($net + $dtps + $dtvq);
}
$totals["cstotal"] = $totals["total"];
$totals["cstvq"] = $totals["tvq"];
$totals["cstps"] = $totals["tps"];
if (true == $this->getFermee()) {
// Le transport est déjà inclu
$totals["tps"] = $this->getTps();
$totals["tvq"] = $this->getTvq();
$totals["total"] = ($totals["net"] + $totals["tps"] + $totals["tvq"]);
} else {
// On ajoute le transport
$totals["tps"] = $this->getTps() + ($this->getTransport() * $tps);
$totals["tvq"] = $this->getTvq() + ($this->getTransport() * $tvq);
$totals["total"] = ($totals["net"] + $totals["tps"] + $totals["tvq"]);
}
$totals["acompte"] = $this->getAcompteUtilise();
// $totals["total"] -= $this->getAcompteUtilise();
$totals["monaie"] = $this->getMonnaie();
// Ajoute le transport
$totals["total"] += $this->getTransport();
//Calcul coupons total
if (empty($totals["rabaisP"]) && empty($totals["rabaisM"]) &&
(!empty($this->getComptant()) || !empty($this->getInteract()))
) {
$totals["coupons"] = round(
((($this->getComptant()+$this->getInteract()) * self::COUPONS_POURCENT) / 100)
, 2);
if ($totals["coupons"] < 0) {
$totals["coupons"] = 0;
}
}
return $totals;
}
/**
* @return array
*/
public function getPaiementsUsed() : array
{
$paiements = [];
if ($this->getComptant()) {
$paiements['comptant'] = $this->getComptant();
}
if ($this->getInteract()) {
$paiements['interact'] = $this->getInteract();
}
if ($this->getCarteMC()) {
$paiements['carteMC'] = $this->getCarteMC();
}
if ($this->getCarteV()) {
$paiements['carteV'] = $this->getCarteV();
}
if ($this->getCoupons()) {
$paiements['coupons'] = $this->getCoupons();
}
if ($this->getCertificatsCadeaux()) {
$paiements['certifCadeaux'] = $this->getCertificatsCadeaux();
}
if ($this->getCertificatsCentre()) {
$paiements['certifCentre'] = $this->getCertificatsCentre();
}
if ($this->getAcompteUtilise()) {
$paiements['acompteUtilise'] = $this->getAcompteUtilise();
}
return $paiements;
}
public function getRabaisTotal($withEscompte = false)
{
$total = 0;
/** @var FacturesDetail $detail */
foreach ($this->getDetails() as $detail) {
$total += $detail->getRabaisTotal();
if ($withEscompte) {
$total += $detail->getEscm();
}
}
return $total;
}
/**
* get all quantity on all details
* @return array
*/
public function getTotalsQuantity() {
$livre = $commande = 0;
/** @var FacturesDetail $detail */
foreach ($this->getDetails() as $detail) {
$livre += $detail->getQuantiteeLivree();
$commande += $detail->getQuantiteeCommandee();
}
return [$livre, $commande];
}
}