181 lines
4.9 KiB
PHP
181 lines
4.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Subclass for representing a row from the 'st_invoice' table.
|
|
*
|
|
*
|
|
*
|
|
* @package plugins.stInvoicePlugin.lib.model
|
|
*/
|
|
class Invoice extends BaseInvoice
|
|
{
|
|
protected $status = null;
|
|
protected $culture = null;
|
|
|
|
/**
|
|
* Zwraca typ faktury
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getType()
|
|
{
|
|
if ($this->getIsProforma())
|
|
{
|
|
return 'proforma';
|
|
}
|
|
|
|
if ($this->getIsConfirm())
|
|
{
|
|
return 'confirm';
|
|
}
|
|
|
|
if ($this->getIsRequest())
|
|
{
|
|
return 'request';
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function getAdminGeneratorTitle()
|
|
{
|
|
return $this->getNumber();
|
|
}
|
|
|
|
public function getCulture()
|
|
{
|
|
return $this->culture;
|
|
}
|
|
|
|
public function setCulture($culture)
|
|
{
|
|
$this->culture = $culture;
|
|
}
|
|
|
|
public function getOptCustomerCompany()
|
|
{
|
|
return trim(parent::getOptCustomerCompany());
|
|
}
|
|
|
|
public function hydrate(ResultSet $rs, $startcol = 1)
|
|
{
|
|
$result = parent::hydrate($rs, $startcol);
|
|
stTax::setIsCustomerEuTaxEnabled(empty($this->getOptCustomerCompany()));
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function getTotalAmount($with_discount = true)
|
|
{
|
|
$total_amount = $this->getOptTotalAmmountBrutto();
|
|
|
|
if ($with_discount && $this->getIsProforma() && $this->hasDiscount())
|
|
{
|
|
return $total_amount - $this->getOrderDiscount();
|
|
}
|
|
|
|
return $total_amount;
|
|
}
|
|
|
|
public function getTotalDiscountAmount()
|
|
{
|
|
return $this->getTotalAmount(false) - $this->getTotalAmount();
|
|
}
|
|
|
|
public function hasDiscount()
|
|
{
|
|
return $this->getOrderDiscount() > 0;
|
|
}
|
|
|
|
public function getStatus()
|
|
{
|
|
if (null === $this->status)
|
|
{
|
|
$c = new Criteria();
|
|
$c->setLimit(1);
|
|
$statuses = $this->getInvoiceStatuss($c);
|
|
$this->status = !empty($statuses) ? $statuses[0] : null;
|
|
}
|
|
|
|
return $this->status;
|
|
}
|
|
|
|
public function getInvoiceProductsJoinProduct($criteria = null, $con = null)
|
|
{
|
|
$criteria = null === $criteria ? new Criteria() : clone $criteria;
|
|
|
|
$criteria->addAscendingOrderByColumn(InvoiceProductPeer::ID);
|
|
|
|
return parent::getInvoiceProductsJoinProduct($criteria, $con);
|
|
}
|
|
|
|
/**
|
|
* Zwraca podsumowanie poszczególnych stawek VAT
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getTaxRatesSummary()
|
|
{
|
|
$taxRatesSummary = array();
|
|
|
|
/**
|
|
* @var InvoiceProduct $product
|
|
*/
|
|
foreach ($this->getInvoiceProductsJoinProduct() as $product)
|
|
{
|
|
if (!isset($taxRatesSummary[$product->getVat()]))
|
|
{
|
|
$tax = stTax::getById($product->getVatId());
|
|
$taxRatesSummary[$product->getVat()] = array(
|
|
'total_brutto' => $product->getOptTotalPriceBrutto(),
|
|
'is_default' => $tax && $tax->getDefaultTaxRate() == $product->getVat(),
|
|
);
|
|
}
|
|
else
|
|
{
|
|
$taxRatesSummary[$product->getVat()]['total_brutto'] += $product->getOptTotalPriceBrutto();
|
|
}
|
|
}
|
|
|
|
foreach ($taxRatesSummary as $rate => $taxRateSummary)
|
|
{
|
|
$totalBrutto = $taxRateSummary['total_brutto'];
|
|
$totalNetto = stPrice::extract($totalBrutto, $rate);
|
|
$taxRatesSummary[$rate]['total_netto'] = $totalNetto;
|
|
$taxRatesSummary[$rate]['total_ammount_vat'] = $totalBrutto - $totalNetto;
|
|
}
|
|
|
|
return $taxRatesSummary;
|
|
}
|
|
|
|
public function save($con = null)
|
|
{
|
|
if ($this->isNew())
|
|
{
|
|
$invoiceDataDefault = stConfig::getInstance('stInvoiceBackend');
|
|
$invoiceDataDefault->setCulture(stLanguage::getOptLanguage());
|
|
$currentDate = date('Y-M-d');
|
|
|
|
$this->setDateCreateCopy(empty($invoiceDataDefault->get('date_create_copy')) ? $currentDate : $invoiceDataDefault->get('date_create_copy'));
|
|
$this->setDateSelle(empty($invoiceDataDefault->get('date_selle')) ? $currentDate : $invoiceDataDefault->get('date_selle'));
|
|
}
|
|
|
|
if ($this->aInvoiceUserCustomer && $this->aInvoiceUserCustomer->isColumnModified(InvoiceUserCustomerPeer::COMPANY))
|
|
{
|
|
$this->setOptCustomerCompany($this->aInvoiceUserCustomer->getCompany());
|
|
}
|
|
|
|
if ($this->aInvoiceUserCustomer && $this->aInvoiceUserCustomer->isColumnModified(InvoiceUserCustomerPeer::FULL_NAME))
|
|
{
|
|
$this->setOptCustomerFullName($this->aInvoiceUserCustomer->getFullName());
|
|
}
|
|
|
|
if ($this->aInvoiceUserCustomer && $this->aInvoiceUserCustomer->isColumnModified(InvoiceUserCustomerPeer::VAT_NUMBER))
|
|
{
|
|
$this->setOptCustomerVatNumber($this->aInvoiceUserCustomer->getVatNumber());
|
|
}
|
|
|
|
return parent::save($con);
|
|
}
|
|
}
|