diff --git a/database/factories/Document.php b/database/factories/Document.php index 37a48a5ff..fd2dc24d5 100644 --- a/database/factories/Document.php +++ b/database/factories/Document.php @@ -57,28 +57,30 @@ class Document extends AbstractFactory */ public function invoice(): Factory { - $contacts = Contact::customer()->enabled()->get(); + return $this->state(function (array $attributes): array { + $contacts = Contact::customer()->enabled()->get(); - if ($contacts->count()) { - $contact = $contacts->random(1)->first(); - } else { - $contact = Contact::factory()->customer()->enabled()->create(); - } + if ($contacts->count()) { + $contact = $contacts->random(1)->first(); + } else { + $contact = Contact::factory()->customer()->enabled()->create(); + } - $statuses = ['draft', 'sent', 'viewed', 'partial', 'paid', 'cancelled']; + $statuses = ['draft', 'sent', 'viewed', 'partial', 'paid', 'cancelled']; - return $this->state([ - 'type' => Model::INVOICE_TYPE, - 'document_number' => $this->getNextDocumentNumber(Model::INVOICE_TYPE), - 'category_id' => $this->company->categories()->income()->get()->random(1)->pluck('id')->first(), - 'contact_id' => $contact->id, - 'contact_name' => $contact->name, - 'contact_email' => $contact->email, - 'contact_tax_number' => $contact->tax_number, - 'contact_phone' => $contact->phone, - 'contact_address' => $contact->address, - 'status' => $this->faker->randomElement($statuses), - ]); + return [ + 'type' => Model::INVOICE_TYPE, + 'document_number' => $this->getDocumentNumber(Model::INVOICE_TYPE), + 'category_id' => $this->company->categories()->income()->get()->random(1)->pluck('id')->first(), + 'contact_id' => $contact->id, + 'contact_name' => $contact->name, + 'contact_email' => $contact->email, + 'contact_tax_number' => $contact->tax_number, + 'contact_phone' => $contact->phone, + 'contact_address' => $contact->address, + 'status' => $this->faker->randomElement($statuses), + ]; + }); } /** @@ -86,28 +88,30 @@ class Document extends AbstractFactory */ public function bill(): Factory { - $contacts = Contact::vendor()->enabled()->get(); + return $this->state(function (array $attributes): array { + $contacts = Contact::vendor()->enabled()->get(); - if ($contacts->count()) { - $contact = $contacts->random(1)->first(); - } else { - $contact = Contact::factory()->vendor()->enabled()->create(); - } + if ($contacts->count()) { + $contact = $contacts->random(1)->first(); + } else { + $contact = Contact::factory()->vendor()->enabled()->create(); + } - $statuses = ['draft', 'received', 'partial', 'paid', 'cancelled']; + $statuses = ['draft', 'received', 'partial', 'paid', 'cancelled']; - return $this->state([ - 'type' => Model::BILL_TYPE, - 'document_number' => $this->getNextDocumentNumber(Model::BILL_TYPE), - 'category_id' => $this->company->categories()->expense()->get()->random(1)->pluck('id')->first(), - 'contact_id' => $contact->id, - 'contact_name' => $contact->name, - 'contact_email' => $contact->email, - 'contact_tax_number' => $contact->tax_number, - 'contact_phone' => $contact->phone, - 'contact_address' => $contact->address, - 'status' => $this->faker->randomElement($statuses), - ]); + return [ + 'type' => Model::BILL_TYPE, + 'document_number' => $this->getDocumentNumber(Model::BILL_TYPE), + 'category_id' => $this->company->categories()->expense()->get()->random(1)->pluck('id')->first(), + 'contact_id' => $contact->id, + 'contact_name' => $contact->name, + 'contact_email' => $contact->email, + 'contact_tax_number' => $contact->tax_number, + 'contact_phone' => $contact->phone, + 'contact_address' => $contact->address, + 'status' => $this->faker->randomElement($statuses), + ]; + }); } /** @@ -205,7 +209,7 @@ class Document extends AbstractFactory return $this->state([ 'type' => $type, - 'document_number' => $this->getNextDocumentNumber($type), + 'document_number' => $this->getDocumentNumber($type), 'recurring_started_at' => $this->getRawAttribute('issued_at'), 'recurring_frequency' => 'daily', 'recurring_interval' => '1', @@ -257,6 +261,19 @@ class Document extends AbstractFactory ]); } + /** + * Get document number + * + */ + public function getDocumentNumber($type) + { + $document_number = $this->getNextDocumentNumber($type); + + $this->increaseNextDocumentNumber($type); + + return $document_number; + } + /** * Configure the model factory. * diff --git a/database/factories/Transaction.php b/database/factories/Transaction.php index d5ecffb05..725fe8be9 100644 --- a/database/factories/Transaction.php +++ b/database/factories/Transaction.php @@ -40,7 +40,7 @@ class Transaction extends Factory return [ 'company_id' => $this->company->id, 'type' => $this->type, - 'number' => $this->getNextTransactionNumber(), + 'number' => $this->getNumber(), 'account_id' => setting('default.account'), 'paid_at' => $this->faker->dateTimeBetween(now()->startOfYear(), now()->endOfYear())->format('Y-m-d H:i:s'), 'amount' => $this->faker->randomFloat(2, 1, 1000), @@ -89,7 +89,7 @@ class Transaction extends Factory { return $this->state([ 'type' => $this->getRawAttribute('type') . '-recurring', - 'number' => $this->getNextTransactionNumber('-recurring'), + 'number' => $this->getNumber('-recurring'), 'recurring_started_at' => $this->getRawAttribute('paid_at'), 'recurring_frequency' => 'daily', 'recurring_custom_frequency' => 'daily', @@ -101,4 +101,17 @@ class Transaction extends Factory 'real_type' => $this->getRawAttribute('type'), ]); } + + /** + * Get transaction number + * + */ + public function getNumber($suffix = '') + { + $number = $this->getNextTransactionNumber($suffix); + + $this->increaseNextTransactionNumber($suffix); + + return $number; + } } diff --git a/resources/views/components/transactions/template/default.blade.php b/resources/views/components/transactions/template/default.blade.php index a53ced5cd..a44c6be89 100644 --- a/resources/views/components/transactions/template/default.blade.php +++ b/resources/views/components/transactions/template/default.blade.php @@ -229,7 +229,7 @@