2013-12-29 12:28:44 +01:00
|
|
|
<?php namespace ninja\repositories;
|
|
|
|
|
|
|
|
use TaxRate;
|
2014-01-14 12:52:56 +01:00
|
|
|
use Utils;
|
2013-12-29 12:28:44 +01:00
|
|
|
|
|
|
|
class TaxRateRepository
|
|
|
|
{
|
2015-01-11 13:30:08 +01:00
|
|
|
public function save($taxRates)
|
|
|
|
{
|
|
|
|
$taxRateIds = [];
|
|
|
|
|
|
|
|
foreach ($taxRates as $record) {
|
|
|
|
if (!isset($record->rate) || (isset($record->is_deleted) && $record->is_deleted)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-01-25 07:48:40 +01:00
|
|
|
if (!isset($record->name) || !trim($record->name)) {
|
2015-01-11 13:30:08 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($record->public_id) {
|
|
|
|
$taxRate = TaxRate::scope($record->public_id)->firstOrFail();
|
|
|
|
} else {
|
|
|
|
$taxRate = TaxRate::createNew();
|
|
|
|
}
|
|
|
|
|
|
|
|
$taxRate->rate = Utils::parseFloat($record->rate);
|
|
|
|
$taxRate->name = trim($record->name);
|
|
|
|
$taxRate->save();
|
|
|
|
|
|
|
|
$taxRateIds[] = $taxRate->public_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$taxRates = TaxRate::scope()->get();
|
|
|
|
|
|
|
|
foreach ($taxRates as $taxRate) {
|
|
|
|
if (!in_array($taxRate->public_id, $taxRateIds)) {
|
|
|
|
$taxRate->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|