Merge pull request #3006 from CihanSenturk/import/export-improvements

Import/export improvements
This commit is contained in:
Cüneyt Şentürk 2023-07-20 16:08:54 +03:00 committed by GitHub
commit 1f42081b98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 158 additions and 0 deletions

View File

@ -20,6 +20,7 @@ class Transactions extends Export implements WithColumnFormatting
$model->contact_email = $model->contact->email;
$model->category_name = $model->category->name;
$model->invoice_bill_number = $model->document->document_number ?? 0;
$model->parent_number = Model::isRecurring()->find($model->parent_id)?->number;
return parent::map($model);
}
@ -41,6 +42,7 @@ class Transactions extends Export implements WithColumnFormatting
'payment_method',
'reference',
'reconciled',
'parent_number',
];
}

View File

@ -26,6 +26,7 @@ class Bills extends Export implements WithColumnFormatting
$model->bill_number = $model->document_number;
$model->billed_at = $model->issued_at;
$model->contact_country = $country;
$model->parent_number = Model::billRecurring()->find($model->parent_id)?->document_number ?? null;
return parent::map($model);
}
@ -52,6 +53,7 @@ class Bills extends Export implements WithColumnFormatting
'contact_zip_code',
'contact_city',
'notes',
'parent_number',
];
}

View File

@ -26,6 +26,7 @@ class Invoices extends Export implements WithColumnFormatting
$model->invoice_number = $model->document_number;
$model->invoiced_at = $model->issued_at;
$model->contact_country = $country;
$model->parent_number = Model::invoiceRecurring()->find($model->parent_id)?->document_number;
return parent::map($model);
}
@ -53,6 +54,7 @@ class Invoices extends Export implements WithColumnFormatting
'contact_city',
'notes',
'footer',
'parent_number',
];
}

View File

@ -24,6 +24,7 @@ class Transactions extends Import
$row['category_id'] = $this->getCategoryId($row);
$row['contact_id'] = $this->getContactId($row);
$row['document_id'] = $this->getDocumentId($row);
$row['parent_id'] = $this->getParentId($row);
return $row;
}

View File

@ -35,6 +35,7 @@ class Bills extends Import
$row['currency_code'] = $this->getCurrencyCode($row);
$row['type'] = Model::BILL_TYPE;
$row['contact_country'] = !empty($country) ? $country : null;
$row['parent_id'] = $this->getParentId($row);
return $row;
}

View File

@ -35,6 +35,7 @@ class Invoices extends Import
$row['currency_code'] = $this->getCurrencyCode($row);
$row['type'] = Model::INVOICE_TYPE;
$row['contact_country'] = !empty($country) ? $country : null;
$row['parent_id'] = $this->getParentId($row);
return $row;
}

View File

@ -165,6 +165,11 @@ class Transaction extends Model
return $this->belongsTo('App\Models\Auth\User', 'contact_id', 'id');
}
public function scopeNumber(Builder $query, string $number): Builder
{
return $query->where('number', '=', $number);
}
public function scopeType(Builder $query, $types): Builder
{
if (empty($types)) {

View File

@ -13,6 +13,10 @@ class Recurring extends Model
public const ACTIVE_STATUS = 'active';
public const END_STATUS = 'ended';
public const COMPLETE_STATUS = 'completed';
public const INVOICE_RECURRING_TYPE = 'invoice-recurring';
public const BILL_RECURRING_TYPE = 'bill-recurring';
public const INCOME_RECURRING_TYPE = 'income-recurring';
public const EXPENSE_RECURRING_TYPE = 'expense-recurring';
protected $table = 'recurring';
@ -55,6 +59,21 @@ class Recurring extends Model
return $this->morphTo()->isRecurring();
}
public function documents()
{
return $this->morphedByMany('App\Models\Document\Document', 'recurable');
}
public function invoices()
{
return $this->documents()->where('type', self::INVOICE_RECURRING_TYPE);
}
public function bills()
{
return $this->documents()->where('type', self::BILL_RECURRING_TYPE);
}
public function scopeActive(Builder $query): Builder
{
return $query->where($this->qualifyColumn('status'), '=', static::ACTIVE_STATUS);
@ -69,4 +88,49 @@ class Recurring extends Model
{
return $query->where($this->qualifyColumn('status'), '=', static::COMPLETE_STATUS);
}
public function scopeDocument(Builder $query, $type): Builder
{
return $query->where($this->qualifyColumn('recurable_type'), '=', 'App\\Models\\Document\\Document')
->whereHas('recurable', function (Builder $query) use ($type) {
$query->where('type', $type);
});
}
public function scopeInvoice(Builder $query): Builder
{
return $query->where($this->qualifyColumn('recurable_type'), '=', 'App\\Models\\Document\\Document')
->whereHas('recurable', function (Builder $query) {
$query->where('type', self::INVOICE_RECURRING_TYPE);
});
}
public function scopeBill(Builder $query): Builder
{
return $query->where($this->qualifyColumn('recurable_type'), '=', 'App\\Models\\Document\\Document')
->whereHas('recurable', function (Builder $query) {
$query->where('type', self::BILL_RECURRING_TYPE);
});
}
public function scopeTransaction(Builder $query): Builder
{
return $query->where($this->qualifyColumn('recurable_type'), '=', 'App\\Models\\Banking\\Transaction');
}
public function scopeExpenseTransaction(Builder $query): Builder
{
return $query->where($this->qualifyColumn('recurable_type'), '=', 'App\\Models\\Banking\\Transaction')
->whereHas('recurable', function (Builder $query) {
$query->where('type', self::EXPENSE_RECURRING_TYPE);
});
}
public function scopeIncomeTransaction(Builder $query): Builder
{
return $query->where($this->qualifyColumn('recurable_type'), '=', 'App\\Models\\Banking\\Transaction')
->whereHas('recurable', function (Builder $query) {
$query->where('type', self::INCOME_RECURRING_TYPE);
});
}
}

View File

@ -30,8 +30,24 @@ class DocumentHistory extends Model
return $query->where($this->qualifyColumn('type'), '=', Document::INVOICE_TYPE);
}
public function scopeInvoiceRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), '=', Document::INVOICE_RECURRING_TYPE)
->whereHas('document.recurring', function (Builder $query) {
$query->whereNull('deleted_at');
});
}
public function scopeBill(Builder $query)
{
return $query->where($this->qualifyColumn('type'), '=', Document::BILL_TYPE);
}
public function scopeBillRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), '=', Document::BILL_RECURRING_TYPE)
->whereHas('document.recurring', function (Builder $query) {
$query->whereNull('deleted_at');
});
}
}

View File

@ -100,11 +100,27 @@ class DocumentItem extends Model
return $query->where($this->qualifyColumn('type'), '=', Document::INVOICE_TYPE);
}
public function scopeInvoiceRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), '=', Document::INVOICE_RECURRING_TYPE)
->whereHas('document.recurring', function (Builder $query) {
$query->whereNull('deleted_at');
});
}
public function scopeBill(Builder $query)
{
return $query->where($this->qualifyColumn('type'), '=', Document::BILL_TYPE);
}
public function scopeBillRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), '=', Document::BILL_RECURRING_TYPE)
->whereHas('document.recurring', function (Builder $query) {
$query->whereNull('deleted_at');
});
}
public function getDiscountAttribute(): string
{
if (setting('localisation.percent_position', 'after') === 'after') {

View File

@ -41,11 +41,27 @@ class DocumentItemTax extends Model
return $query->where($this->qualifyColumn('type'), '=', Document::INVOICE_TYPE);
}
public function scopeInvoiceRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), '=', Document::INVOICE_RECURRING_TYPE)
->whereHas('document.recurring', function (Builder $query) {
$query->whereNull('deleted_at');
});
}
public function scopeBill(Builder $query)
{
return $query->where($this->qualifyColumn('type'), '=', Document::BILL_TYPE);
}
public function scopeBillRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), '=', Document::BILL_RECURRING_TYPE)
->whereHas('document.recurring', function (Builder $query) {
$query->whereNull('deleted_at');
});
}
public function onCloned($src)
{
$document_item = DocumentItem::find($this->document_item_id);

View File

@ -33,11 +33,27 @@ class DocumentTotal extends Model
return $query->where($this->qualifyColumn('type'), '=', Document::INVOICE_TYPE);
}
public function scopeInvoiceRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), '=', Document::INVOICE_RECURRING_TYPE)
->whereHas('document.recurring', function (Builder $query) {
$query->whereNull('deleted_at');
});
}
public function scopeBill(Builder $query)
{
return $query->where($this->qualifyColumn('type'), '=', Document::BILL_TYPE);
}
public function scopeBillRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), '=', Document::BILL_RECURRING_TYPE)
->whereHas('document.recurring', function (Builder $query) {
$query->whereNull('deleted_at');
});
}
public function scopeCode($query, $code)
{
return $query->where('code', '=', $code);

View File

@ -16,6 +16,7 @@ use App\Jobs\Setting\CreateCurrency;
use App\Jobs\Setting\CreateTax;
use App\Models\Auth\User;
use App\Models\Banking\Account;
use App\Models\Banking\Transaction;
use App\Models\Common\Contact;
use App\Models\Common\Item;
use App\Models\Document\Document;
@ -149,6 +150,21 @@ trait Import
return is_null($id) ? $id : (int) $id;
}
public function getParentId($row)
{
$id = isset($row['parent_id']) ? $row['parent_id'] : null;
if (empty($id) && isset($row['document_number']) && !empty($row['parent_number'])) {
$id = Document::number($row['parent_number'])->pluck('id')->first();
}
if (empty($id) && isset($row['number']) && !empty($row['parent_number'])) {
$id = Transaction::number($row['parent_number'])->pluck('id')->first();
}
return is_null($id) ? $id : (int) $id;
}
public function getItemId($row, $type = null)
{
$id = isset($row['item_id']) ? $row['item_id'] : null;