<?php
namespace App\Entity\Slave;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="eposm_s_product")
* @ORM\Entity(repositoryClass="App\Repository\Slave\ProductRepository")
*/
class Product
{
public function __toString(){
return $this->getModel()->getName().' --- '.$this->displayCodes();
}
public function toStringShort(){
$result = $this->getModel()->getName();
if($this->codeSupplier != null && $this->codeProducer != null){
$result.= ' ('.$this->getCodeSupplier().' - '.$this->getCodeProducer().')';
}
else{
if($this->codeSupplier != null) $result.= ' ('.$this->getCodeSupplier().')';
if($this->codeProducer != null) $result.= ' ('.$this->getCodeProducer().')';
}
return $result;
}
public function canDelete(){
if(sizeof($this->getRegeneratedProducts()) > 0) return false;
if(sizeof($this->getWithdrawActivities()) > 0) return false;
if(sizeof($this->getActualActivities()) > 0) return false;
if(sizeof($this->getInstallationActivities()) > 0) return false;
if(sizeof($this->getProducts()) > 0) return false;
return true;
}
public function canBeTransferredStatus($transfer){
switch($transfer->getType()){
case 'technician':
if($this->getStatus()->getSlug() != "available")
return 'Il prodotto non è nello stato Disponibile';
break;
case 'main':
if($this->getStatus()->getSlug() == "transfer")
return 'Il prodotto è già in trasferimento';
break;
case 'destination':
if($this->getStatus()->getSlug() == "transfer")
return 'Il prodotto è già in trasferimento';
if($transfer->getSupplier() != null && !$this->hasSupplier($transfer->getSupplier()))
return "Il prodotto non può essere utilizzato per il fornitore ".$transfer->getSupplier();
break;
default: break;
}
return "ok";
}
public function getIsAlreadyInserted($type){
if(sizeof($this->getTransfers()) > 0) return false;
if($type == "withdraw" && sizeof($this->getWithdrawActivities()) > 1) return false;
if($type != "withdraw" && sizeof($this->getWithdrawActivities()) > 0) return false;
if($type == "actual" && sizeof($this->getActualActivities()) > 1) return false;
if($type != "actual" && sizeof($this->getActualActivities()) > 0) return false;
if(sizeof($this->getInstallationActivities()) > 0) return false;
if(sizeof($this->getProducts()) > 0) return false;
return true;
}
public function getDisplayConflict(){
$string = "<strong>Modello</strong>: ".$this->getModel()->getName()."<br>";
$string.= "<strong>Matricola produttore</strong>: ".$this->getCodeProducer()."<br>";
$string.= "<strong>Matricola fornitore</strong>: ".$this->getCodeSupplier()."<br>";
return $string;
}
public function displayActive(){
if($this->active){
$color = 'color_gr';
$title = 'Attivo';
}
else{
$color = 'color_r';
$title = 'Sospeso';
}
return '<i class="icon-circle cursor_p '.$color.'" data-bs-toggle="tooltip" title="'.$title.'"></i>';
}
public function displayStatus($type){
switch($this->getStatus()->getSlug()){
case 'available': $color = 'color_gr'; break;
case 'transfer': $color = 'color_am'; break;
case 'in_use': $color = 'color_r'; break;
case 'not_available': $color = 'color_am'; break;
default: break;
}
switch($type){
case 'icon': $string = '<i class="icon-circle cursor_p '.$color.'" data-bs-toggle="tooltip" title="'.$this->getStatus()->getValue().'"></i>'; break;
case 'string': $string = $this->getStatus()->getValue(); break;
default: break;
}
return $string;
}
public function displayCondition($type){
$string = '';
switch($this->getCondition()->getSlug()){
case 'new':
case 'refurbished':
case 'repaired':
case 'free':
case 'regenerated':
$color = 'color_gr';
break;
case 'retired':
case 'historic':
$color = 'color_am';
break;
case 'broken':
case 'doa':
case 'lost':
$color = 'color_r';
break;
default: break;
}
switch($type){
case 'icon': $string = '<i class="icon-circle cursor_p '.$color.'" data-bs-toggle="tooltip" title="'.$this->getCondition()->getValue().'"></i>'; break;
case 'string': $string = $this->getCondition()->getValue(); break;
case 'string-short': $str = explode('-', $this->getCondition()->getValue()); $string = $str[0]; break;
default: break;
}
return $string;
}
public function displayCodes(){
$result = '';
if($this->codeSupplier != null)
$result.= 'Matricola fornitore: '.$this->getCodeSupplier();
if($this->codeProducer != null){
if($result != '') $result.= ' --- ';
$result.= 'Matricola produttore: '.$this->getCodeProducer();
}
return $result;
}
public function displayChildrenProducts($type){
$result = '';
if((sizeof($this->products) > 0)){
switch($type){
case 'table':
$result .= '<table class="table table_no_padding b_none m_b_none"><thead><tr><th>Prodotto</th><th>Matr. Prod.</th><th>Matr. Forn.</th></thead>';
foreach($this->products as $product){
$result .= '<tr><td>'.$product->getModel()->getName().'</td><td>'.$product->getCodeProducer().'</td><td>'.$product->getCodeSupplier().'</td></tr>';
}
$result.= '</table>';
break;
case 'string':
$first = true;
foreach($this->products as $product){
if($first) $first = false; else $result.= ' - ';
$result .= $product->getModel()->getName();
}
default: break;
}
}
return $result;
}
public function displaySuppliers(){
$result = '---';
if(sizeof($this->suppliers) > 0){
$result = '<table class="table table_no_padding m_b_none b_none">';
foreach($this->suppliers as $supplier){
$result .= '<tr><td>'.$supplier->getName().'</td></tr>';
}
$result.= '</table>';
}
return $result;
}
public function displayStringSuppliers(){
$result = '';
if(sizeof($this->suppliers) > 0){
$first = true;
foreach($this->suppliers as $supplier){
if($first) $first = false; else $result.= ', ';
$result .= $supplier->getName();
}
}
return $result;
}
public function displayLastRefs(){
$string = "";
if($this->getLastTicket() != null){
$string.="<br>";
$string.="Ticket: <u>".$this->getLastTicket()->getNumber()."</u><br>";
$string.="Operazione: ".$this->getLastOperationGroupType()."<br>";
$string.="Termid: ".$this->getLastTicket()->getTermid()->getCode();
$string.="";
}
return $string;
}
public function displayTransfersDdt(){
$first = true;
$string = '';
if(sizeof($this->getTransfers()) > 3){
$tooltip = '';
foreach($this->getTransfers() as $jt){
if($first) $first = false; else $tooltip.= ' - ';
$tooltip.= $jt->getTransfer()->getDdt();
}
$string = '<a href="javascript:void(0)" data-bs-toggle="tooltip" data-bs-title="'.$tooltip.'">'.$this->getTransfers()[0]->getTransfer()->getDdt().' - '.$this->getTransfers()[1]->getTransfer()->getDdt().' - '.$this->getTransfers()[2]->getTransfer()->getDdt().' ...</a>';
}
else{
foreach($this->getTransfers() as $jt){
if($first) $first = false; else $string.= ' - ';
$string.= $jt->getTransfer()->getDdt();
}
}
return $string;
}
public function getLastLogWithIntervention(){
$lastLog = null;
$dateLastLog = date_create_from_format('d-m-Y H:i', '01-01-2000 00:00');
foreach($this->logs as $log){
if($log->getIntervention() != null && $log->getDatetime()->format('YmdHis') > $dateLastLog->format('YmdHis')){
$dateLastLog = $log->getDatetime();
$lastLog = $log;
}
}
return $lastLog;
}
public function getLastTransfer(){
$transfer = null;
$dateLastTransfer = date_create_from_format('d-m-Y H:i', '01-01-2000 00:00');
foreach($this->transfers as $jt){
if($jt->getTransfer()->getDatetimeCreation()->format('YmdHi') > $dateLastTransfer->format('YmdHi')){
$dateLastTransfer = $jt->getTransfer()->getDatetimeCreation();
$transfer = $jt->getTransfer();
}
}
return $transfer;
}
public function getLastTransferFromClient(){
$transfer = null;
$dateLastTransfer = date_create_from_format('d-m-Y H:i', '01-01-2000 00:00');
foreach($this->transfers as $jt){
if($jt->getTransfer()->getWarehouseFrom()->getClient() != null && $jt->getTransfer()->getDatetimeCreation()->format('YmdHi') > $dateLastTransfer->format('YmdHi')){
$dateLastTransfer = $jt->getTransfer()->getDatetimeCreation();
$transfer = $jt->getTransfer();
}
}
return $transfer;
}
public function getLastOperationGroupType() {
return $this->getLastTicket()->getOperation()->getGroup()->getValue();
}
public function getLastTicket() {
$ticket = null;
$log = $this->getLastLogWithIntervention();
if($log != null)
$ticket = $log->getIntervention()->getTicket();
return $ticket;
}
public function hasSupplier($supplier){
foreach($this->getSuppliers() as $sup){
if($sup->getId() == $supplier->getId())
return true;
}
return false;
}
public function movedByDS(){
switch($this->getModel()->getId()){
case 141:
case 142:
case 143:
case 144:
case 145:
case 146:
case 147:
return true;
default:
return false;
}
return false;
}
/**
* @ORM\Column(name="id", type="bigint")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="code_producer", type="string", length=191, nullable=true)
*/
protected $codeProducer;
/**
* @ORM\Column(name="code_supplier", type="string", length=191, nullable=true)
*/
protected $codeSupplier;
/**
* @ORM\Column(name="date_creation", type="datetime", nullable=true)
*/
protected $dateCreation;
/**
* @ORM\Column(name="date_last_transfer", type="datetime", nullable=true)
*/
protected $dateLastTransfer;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
protected $active = true;
/**
* @ORM\Column(name="to_manage", type="boolean")
*/
protected $toManage = false;
/**
* @ORM\Column(name="to_manage_priority", type="integer")
*/
protected $toManagePriority = 2;
// ManyToOne
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Slave\ProductModel", inversedBy="products")
* @ORM\JoinColumn(name="model_id", referencedColumnName="id")
*/
private $model;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Slave\Warehouse", inversedBy="products")
* @ORM\JoinColumn(name="actual_warehouse_id", referencedColumnName="id")
*/
private $actualWarehouse;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Slave\ProductStatus", inversedBy="products")
* @ORM\JoinColumn(name="status_id", referencedColumnName="id")
*/
private $status;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Slave\ProductCondition", inversedBy="products")
* @ORM\JoinColumn(name="condition_id", referencedColumnName="id")
*/
private $condition;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Slave\Termid", inversedBy="products")
* @ORM\JoinColumn(name="termid_id", referencedColumnName="id")
*/
private $termid;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Slave\product", inversedBy="products")
* @ORM\JoinColumn(name="parent_product_id", referencedColumnName="id")
*/
private $parentProduct;
//
// OneToMany
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\JoinTableProductProductTransfer", mappedBy="product")
*/
private $transfers;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\ProductTransferLog", mappedBy="product")
*/
private $transferLogs;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\ProductLog", mappedBy="product")
*/
private $logs;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\ProductRegenerated", mappedBy="product")
*/
private $regeneratedProducts;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\InterventionActivity", mappedBy="productWithdraw")
*/
private $withdrawActivities;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\InterventionActivity", mappedBy="productActual")
*/
private $actualActivities;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\InterventionActivity", mappedBy="productInstallation")
*/
private $installationActivities;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Slave\Product", mappedBy="parentProduct")
*/
private $products;
//
// ManyToMany
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Slave\Supplier", inversedBy="products")
* @ORM\JoinTable(name="eposm_s_join_table_product_supplier")
*/
private $suppliers;
public function __construct()
{
$this->transfers = new ArrayCollection();
$this->transferLogs = new ArrayCollection();
$this->logs = new ArrayCollection();
$this->regenerates = new ArrayCollection();
$this->withdrawActivities = new ArrayCollection();
$this->actualActivities = new ArrayCollection();
$this->installationActivities = new ArrayCollection();
$this->products = new ArrayCollection();
$this->suppliers = new ArrayCollection();
$this->regeneratedProducts = new ArrayCollection();
}
//
public function getId(): ?string
{
return $this->id;
}
public function getCodeProducer(): ?string
{
return strtoupper($this->codeProducer);
}
public function setCodeProducer(?string $codeProducer): static
{
$this->codeProducer = strtoupper($codeProducer);
return $this;
}
public function getCodeSupplier(): ?string
{
return strtoupper($this->codeSupplier);
}
public function setCodeSupplier(?string $codeSupplier): static
{
$this->codeSupplier = strtoupper($codeSupplier);
return $this;
}
public function getDateLastTransfer(): ?\DateTimeInterface
{
return $this->dateLastTransfer;
}
public function setDateLastTransfer(?\DateTimeInterface $dateLastTransfer): static
{
$this->dateLastTransfer = $dateLastTransfer;
return $this;
}
public function isActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): static
{
$this->active = $active;
return $this;
}
public function isToManage(): ?bool
{
return $this->toManage;
}
public function setToManage(bool $toManage): static
{
$this->toManage = $toManage;
return $this;
}
public function getToManagePriority(): ?int
{
return $this->toManagePriority;
}
public function setToManagePriority(int $toManagePriority): static
{
$this->toManagePriority = $toManagePriority;
return $this;
}
public function getModel(): ?ProductModel
{
return $this->model;
}
public function setModel(?ProductModel $model): static
{
$this->model = $model;
return $this;
}
public function getActualWarehouse(): ?Warehouse
{
return $this->actualWarehouse;
}
public function setActualWarehouse(?Warehouse $actualWarehouse): static
{
$this->actualWarehouse = $actualWarehouse;
return $this;
}
public function getStatus(): ?ProductStatus
{
return $this->status;
}
public function setStatus(?ProductStatus $status): static
{
$this->status = $status;
return $this;
}
public function getCondition(): ?ProductCondition
{
return $this->condition;
}
public function setCondition(?ProductCondition $condition): static
{
$this->condition = $condition;
return $this;
}
public function getTermid(): ?Termid
{
return $this->termid;
}
public function setTermid(?Termid $termid): static
{
$this->termid = $termid;
return $this;
}
public function getParentProduct(): ?product
{
return $this->parentProduct;
}
public function setParentProduct(?product $parentProduct): static
{
$this->parentProduct = $parentProduct;
return $this;
}
/**
* @return Collection<int, JoinTableProductProductTransfer>
*/
public function getTransfers(): Collection
{
return $this->transfers;
}
public function addTransfer(JoinTableProductProductTransfer $transfer): static
{
if (!$this->transfers->contains($transfer)) {
$this->transfers->add($transfer);
$transfer->setProduct($this);
}
return $this;
}
public function removeTransfer(JoinTableProductProductTransfer $transfer): static
{
if ($this->transfers->removeElement($transfer)) {
// set the owning side to null (unless already changed)
if ($transfer->getProduct() === $this) {
$transfer->setProduct(null);
}
}
return $this;
}
/**
* @return Collection<int, ProductTransferLog>
*/
public function getTransferLogs(): Collection
{
return $this->transferLogs;
}
public function addTransferLog(ProductTransferLog $transferLog): static
{
if (!$this->transferLogs->contains($transferLog)) {
$this->transferLogs->add($transferLog);
$transferLog->setProduct($this);
}
return $this;
}
public function removeTransferLog(ProductTransferLog $transferLog): static
{
if ($this->transferLogs->removeElement($transferLog)) {
// set the owning side to null (unless already changed)
if ($transferLog->getProduct() === $this) {
$transferLog->setProduct(null);
}
}
return $this;
}
/**
* @return Collection<int, ProductLog>
*/
public function getLogs(): Collection
{
return $this->logs;
}
public function addLog(ProductLog $log): static
{
if (!$this->logs->contains($log)) {
$this->logs->add($log);
$log->setProduct($this);
}
return $this;
}
public function removeLog(ProductLog $log): static
{
if ($this->logs->removeElement($log)) {
// set the owning side to null (unless already changed)
if ($log->getProduct() === $this) {
$log->setProduct(null);
}
}
return $this;
}
/**
* @return Collection<int, InterventionActivity>
*/
public function getWithdrawActivities(): Collection
{
return $this->withdrawActivities;
}
public function addWithdrawActivity(InterventionActivity $withdrawActivity): static
{
if (!$this->withdrawActivities->contains($withdrawActivity)) {
$this->withdrawActivities->add($withdrawActivity);
$withdrawActivity->setProductWithdraw($this);
}
return $this;
}
public function removeWithdrawActivity(InterventionActivity $withdrawActivity): static
{
if ($this->withdrawActivities->removeElement($withdrawActivity)) {
// set the owning side to null (unless already changed)
if ($withdrawActivity->getProductWithdraw() === $this) {
$withdrawActivity->setProductWithdraw(null);
}
}
return $this;
}
/**
* @return Collection<int, InterventionActivity>
*/
public function getActualActivities(): Collection
{
return $this->actualActivities;
}
public function addActualActivity(InterventionActivity $actualActivity): static
{
if (!$this->actualActivities->contains($actualActivity)) {
$this->actualActivities->add($actualActivity);
$actualActivity->setProductActual($this);
}
return $this;
}
public function removeActualActivity(InterventionActivity $actualActivity): static
{
if ($this->actualActivities->removeElement($actualActivity)) {
// set the owning side to null (unless already changed)
if ($actualActivity->getProductActual() === $this) {
$actualActivity->setProductActual(null);
}
}
return $this;
}
/**
* @return Collection<int, InterventionActivity>
*/
public function getInstallationActivities(): Collection
{
return $this->installationActivities;
}
public function addInstallationActivity(InterventionActivity $installationActivity): static
{
if (!$this->installationActivities->contains($installationActivity)) {
$this->installationActivities->add($installationActivity);
$installationActivity->setProductInstallation($this);
}
return $this;
}
public function removeInstallationActivity(InterventionActivity $installationActivity): static
{
if ($this->installationActivities->removeElement($installationActivity)) {
// set the owning side to null (unless already changed)
if ($installationActivity->getProductInstallation() === $this) {
$installationActivity->setProductInstallation(null);
}
}
return $this;
}
/**
* @return Collection<int, Product>
*/
public function getProducts(): Collection
{
return $this->products;
}
public function addProduct(Product $product): static
{
if (!$this->products->contains($product)) {
$this->products->add($product);
$product->setParentProduct($this);
}
return $this;
}
public function removeProduct(Product $product): static
{
if ($this->products->removeElement($product)) {
// set the owning side to null (unless already changed)
if ($product->getParentProduct() === $this) {
$product->setParentProduct(null);
}
}
return $this;
}
/**
* @return Collection<int, Supplier>
*/
public function getSuppliers(): Collection
{
return $this->suppliers;
}
public function addSupplier(Supplier $supplier): static
{
if (!$this->suppliers->contains($supplier)) {
$this->suppliers->add($supplier);
$supplier->addProduct($this);
}
return $this;
}
public function removeSupplier(Supplier $supplier): static
{
if ($this->suppliers->removeElement($supplier)) {
$supplier->removeProduct($this);
}
return $this;
}
public function getDateCreation(): ?\DateTimeInterface
{
return $this->dateCreation;
}
public function setDateCreation(?\DateTimeInterface $dateCreation): static
{
$this->dateCreation = $dateCreation;
return $this;
}
/**
* @return Collection<int, ProductRegenerated>
*/
public function getRegeneratedProducts(): Collection
{
return $this->regeneratedProducts;
}
public function addRegeneratedProduct(ProductRegenerated $regeneratedProduct): static
{
if (!$this->regeneratedProducts->contains($regeneratedProduct)) {
$this->regeneratedProducts->add($regeneratedProduct);
$regeneratedProduct->setProduct($this);
}
return $this;
}
public function removeRegeneratedProduct(ProductRegenerated $regeneratedProduct): static
{
if ($this->regeneratedProducts->removeElement($regeneratedProduct)) {
// set the owning side to null (unless already changed)
if ($regeneratedProduct->getProduct() === $this) {
$regeneratedProduct->setProduct(null);
}
}
return $this;
}
}