src/Diplix/KMGBundle/Entity/Accounting/Billing.php line 21

Open in your IDE?
  1. <?php
  2. namespace Diplix\KMGBundle\Entity\Accounting;
  3. use Diplix\KMGBundle\Entity\BasicEntity;
  4. use Diplix\KMGBundle\Entity\Customer;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. /**
  8.  * @ORM\Table(name="acc_billing", indexes={@ORM\Index(name="be_deleted",columns={"be_deleted"})})
  9.  * @ORM\Entity()
  10.  *
  11.  *
  12.  * @ORM\InheritanceType("SINGLE_TABLE")
  13.  * @ORM\DiscriminatorColumn(name="type", type="string")
  14.  * @ORM\DiscriminatorMap({"K" = "BillingForCustomer", "M" = "BillingForMember"})
  15.  *
  16.  */
  17. abstract class Billing extends BasicEntity
  18. {
  19.     const TYPE_MEMBER 'M';
  20.     const TYPE_CUSTOMER 'K';
  21.     /**
  22.      * @ORM\Column(type="integer",name="id")
  23.      * @ORM\Id()
  24.      * @ORM\GeneratedValue(strategy="AUTO")
  25.      */
  26.     protected $id;
  27.     /**
  28.      * @ORM\Column(name="bill_from", type="datetime",nullable=false)
  29.      */
  30.     protected $from;
  31.     /**
  32.      * @ORM\Column(name="bill_until",type="datetime",nullable=false)
  33.      */
  34.     protected $until;
  35.     /**
  36.      * @ORM\ManyToOne(targetEntity="Diplix\KMGBundle\Entity\Accounting\CoopMember")
  37.      * @ORM\JoinColumn(nullable=true)
  38.      */
  39.     protected $member;
  40.     /**
  41.      * @ORM\ManyToOne(targetEntity="Diplix\KMGBundle\Entity\Customer")
  42.      * @ORM\JoinColumn(nullable=true)
  43.      */
  44.     protected $customer;
  45.     /**
  46.      * @ORM\Column(type="json")
  47.      * @var array ( item => amount )
  48.      */
  49.     protected $extras = [];
  50.     /**
  51.      * @ORM\Column(type="decimal", nullable=false, precision=5, scale=2,options={"unsigned":true})
  52.      */
  53.     protected $deductionPercent 0.0;
  54.     /**
  55.      * @ORM\Column(type="decimal", nullable=false, precision=7, scale=2,options={"unsigned":false})
  56.      */
  57.     protected $totalNet 0.0;
  58.     /////////////////////////////////////
  59.     public static function createFor($type)
  60.     {
  61.         if ($type===self::TYPE_CUSTOMER)
  62.             return new BillingForCustomer();
  63.         if ($type===self::TYPE_MEMBER)
  64.             return new BillingForMember();
  65.         throw new \RuntimeException('Unknown type');
  66.     }
  67.     public function recalculateTotal()
  68.     {
  69.         $total 0;
  70.         foreach ($this->getJobList() as $j)
  71.         {
  72.             if ($this->getType()=== self::TYPE_CUSTOMER)
  73.             {
  74.                 $total += $j->getTotalCustomerNet();
  75.             }
  76.             else
  77.             if ($this->getType()=== self::TYPE_MEMBER)
  78.             {
  79.                 $total += $j->getTotalMemberNet();
  80.             }
  81.             else
  82.             {
  83.                 throw new \RuntimeException('unknown type');
  84.             }
  85.         }
  86.         $total = (- ($this->getDeductionPercent()/100)) *   $total;
  87.         if ($this->extras!==null)
  88.         {
  89.             foreach ($this->extras as $k=>$e)
  90.             {
  91.                 $total += $e;
  92.             }
  93.         }
  94.         $this->setTotalNet($total);
  95.         return $total;
  96.     }
  97.     /**
  98.      * @return mixed
  99.      */
  100.     public function getId()
  101.     {
  102.         return $this->id;
  103.     }
  104.     /**
  105.      * @return mixed
  106.      */
  107.     public function getFrom()
  108.     {
  109.         return $this->from;
  110.     }
  111.     /**
  112.      * @param mixed $from
  113.      */
  114.     public function setFrom($from)
  115.     {
  116.         $this->from $from;
  117.     }
  118.     /**
  119.      * @return mixed
  120.      */
  121.     public function getUntil()
  122.     {
  123.         return $this->until;
  124.     }
  125.     /**
  126.      * @param mixed $until
  127.      */
  128.     public function setUntil($until)
  129.     {
  130.         $this->until $until;
  131.     }
  132.     /**
  133.      * @return ArrayCollection|Job[]
  134.      */
  135.     abstract public function getJobList();
  136.     /**
  137.      * @param ArrayCollection $jobList
  138.      */
  139.      abstract public function setJobList($jobList);
  140.     /**
  141.      * @return string
  142.      */
  143.     abstract public function getType();
  144.     /**
  145.      * @return CoopMember|null
  146.      */
  147.     public function getMember()
  148.     {
  149.         return $this->member;
  150.     }
  151.     /**
  152.      * @param mixed $member
  153.      */
  154.     public function setMember($member)
  155.     {
  156.         if (!($this instanceof BillingForMember)) throw new \RuntimeException('Cannot assign a member to a Billing which is not of type MEMBER');
  157.         $this->member $member;
  158.     }
  159.     /**
  160.      * @return Customer|null
  161.      */
  162.     public function getCustomer()
  163.     {
  164.         return $this->customer;
  165.     }
  166.     /**
  167.      * @param mixed $customer
  168.      */
  169.     public function setCustomer($customer)
  170.     {
  171.         if (!($this instanceof BillingForCustomer)) throw new \RuntimeException('Cannot assign a customer to a Billing which is not of type customer');
  172.         $this->customer $customer;
  173.     }
  174.     /**
  175.      * @return float
  176.      */
  177.     public function getTotalNet()
  178.     {
  179.         return $this->totalNet;
  180.     }
  181.     /**
  182.      * @param float $totalNet
  183.      */
  184.     public function setTotalNet($totalNet)
  185.     {
  186.         $this->totalNet $totalNet;
  187.     }
  188.     /**
  189.      * @return float
  190.      */
  191.     public function getDeductionPercent()
  192.     {
  193.         return $this->deductionPercent;
  194.     }
  195.     /**
  196.      * @param float $deductionPercent
  197.      */
  198.     public function setDeductionPercent($deductionPercent)
  199.     {
  200.         $this->deductionPercent $deductionPercent;
  201.     }
  202.     /**
  203.      * @return array
  204.      */
  205.     public function getExtras()
  206.     {
  207.         if ($this->extras === null) return [];
  208.         return $this->extras;
  209.     }
  210.     public function addExtra($text,$amount,$overwrite=false)
  211.     {
  212.         if ((array_key_exists($text,$this->extras)) && (!$overwrite) )
  213.         {
  214.             throw new \RuntimeException('Duplicate key for extra: '.$text);
  215.         }
  216.         $this->extras[$text] = $amount;
  217.     }
  218.     public function setExtras($val)
  219.     {
  220.         if ($val===null$val = [];
  221.         $this->extras $val;
  222.     }
  223. }