diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index a60b6e714ea1..9f2193c8f74a 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -279,11 +279,8 @@ class CompanySettings extends BaseSettings public $email_from_name = ''; public $auto_archive_invoice_cancelled = false; - - public $purchase_order_number_counter = 1; //TODO - public static $casts = [ - 'purchase_order_number_pattern' => 'purchase_order_number_pattern', + 'purchase_order_number_pattern' => 'string', 'purchase_order_number_counter' => 'int', 'page_numbering_alignment' => 'string', 'page_numbering' => 'bool', diff --git a/app/Services/PurchaseOrder/ApplyNumber.php b/app/Services/PurchaseOrder/ApplyNumber.php index c3fd4d5df078..9f47baec4a9c 100644 --- a/app/Services/PurchaseOrder/ApplyNumber.php +++ b/app/Services/PurchaseOrder/ApplyNumber.php @@ -1,11 +1,18 @@ client = $client; + $this->vendor = $vendor; $this->purchase_order = $purchase_order; } @@ -43,7 +50,7 @@ class ApplyNumber extends AbstractService $x=1; do{ try{ - $this->purchase_order->number = $this->getNextPurchaseOrderNumber($this->client, $this->purchase_order); + $this->purchase_order->number = $this->getNextPurchaseOrderNumber($this->purchase_order); $this->purchase_order->saveQuietly(); $this->completed = false; } diff --git a/app/Services/PurchaseOrder/CreateInvitations.php b/app/Services/PurchaseOrder/CreateInvitations.php index 4ae8f85323e6..6365bc912262 100644 --- a/app/Services/PurchaseOrder/CreateInvitations.php +++ b/app/Services/PurchaseOrder/CreateInvitations.php @@ -1,10 +1,19 @@ purchase_order = $purchase_order; } + private function createBlankContact() { - $new_contact = PurchaseOrderInvitationFactory::create($this->purchase_order->company_id, $this->purchase_order->user_id); - $new_contact->client_id = $this->purchase_order->client_id; + $new_contact = VendorContactFactory::create($this->purchase_order->company_id, $this->purchase_order->user_id); + $new_contact->vendor_id = $this->purchase_order->vendor_id; $new_contact->contact_key = Str::random(40); $new_contact->is_primary = true; $new_contact->save(); } public function run() { - $contacts = $this->purchase_order->vendor->contacts; + $contacts = $this->purchase_order->vendor->contacts()->where('send_email', true)->get(); if($contacts->count() == 0){ $this->createBlankContact(); @@ -41,14 +51,14 @@ class CreateInvitations extends AbstractService } $contacts->each(function ($contact) { - $invitation = PurchaseOrderInvitation::whereCompanyId($this->purchase_order->company_id) - ->whereClientContactId($contact->id) - ->whereCreditId($this->purchase_order->id) + $invitation = PurchaseOrderInvitation::where('company_id', $this->purchase_order->company_id) + ->where('vendor_contact_id', $contact->id) + ->where('purchase_order_id', $this->purchase_order->id) ->withTrashed() ->first(); if (! $invitation) { - $ii = PurchaseOrderInvitation::create($this->purchase_order->company_id, $this->purchase_order->user_id); + $ii = PurchaseOrderInvitationFactory::create($this->purchase_order->company_id, $this->purchase_order->user_id); $ii->key = $this->createDbHash($this->purchase_order->company->db); $ii->purchase_order_id = $this->purchase_order->id; $ii->vendor_contact_id = $contact->id; @@ -78,7 +88,7 @@ class CreateInvitations extends AbstractService } } - $ii = PurchaseOrderInvitation::create($this->purchase_order->company_id, $this->purchase_order->user_id); + $ii = PurchaseOrderInvitationFactory::create($this->purchase_order->company_id, $this->purchase_order->user_id); $ii->key = $this->createDbHash($this->purchase_order->company->db); $ii->purchase_order_id = $this->purchase_order->id; $ii->vendor_contact_id = $contact->id; diff --git a/app/Services/PurchaseOrder/MarkSent.php b/app/Services/PurchaseOrder/MarkSent.php index 2938f956557e..c3b1df47b0d7 100644 --- a/app/Services/PurchaseOrder/MarkSent.php +++ b/app/Services/PurchaseOrder/MarkSent.php @@ -1,5 +1,13 @@ purchase_order = $purchase_order; } - /** - * Saves the purchase order. - * @return \App\Models\PurchaseOrder object - */ - public function save(): ?PurchaseOrder - { - $this->purchase_order->saveQuietly(); - - return $this->purchase_order; - } - public function createInvitations() { @@ -86,4 +75,15 @@ class PurchaseOrderService return $this; } + /** + * Saves the purchase order. + * @return \App\Models\PurchaseOrder object + */ + public function save(): ?PurchaseOrder + { + $this->purchase_order->saveQuietly(); + + return $this->purchase_order; + } + } diff --git a/app/Utils/Traits/GeneratesCounter.php b/app/Utils/Traits/GeneratesCounter.php index baaab3c5244d..939a90849480 100644 --- a/app/Utils/Traits/GeneratesCounter.php +++ b/app/Utils/Traits/GeneratesCounter.php @@ -196,20 +196,6 @@ trait GeneratesCounter return $this->replaceUserVars($credit, $entity_number); - } - /** - * Gets the next purchase order number. - * - * @param PurchaseOrder $purchase_order The purchase order - * - * @return string The next purchase order number. - */ - public function getNextPurchaseOrderNumber(Client $client, ?PurchaseOrder $purchase_order) :string - { - $entity_number = $this->getNextEntityNumber(PurchaseOrder::class, $client); - - return $this->replaceUserVars($purchase_order, $entity_number); - } /** @@ -376,7 +362,7 @@ trait GeneratesCounter $purchase_order_number = $this->checkEntityNumber(PurchaseOrder::class, $purchase_order, $counter, $purchase_order->company->settings->counter_padding, $purchase_order->company->settings->purchase_order_number_pattern); - $this->incrementCounter($purchase_order->company, 'purchase_order_number_pattern'); + $this->incrementCounter($purchase_order->company, 'purchase_order_number_counter'); $entity_number = $purchase_order_number; diff --git a/database/migrations/2022_06_01_224339_create_purchase_order_invitations_table.php b/database/migrations/2022_06_01_224339_create_purchase_order_invitations_table.php index 984e6c6cd634..859464145dfa 100644 --- a/database/migrations/2022_06_01_224339_create_purchase_order_invitations_table.php +++ b/database/migrations/2022_06_01_224339_create_purchase_order_invitations_table.php @@ -39,6 +39,11 @@ class CreatePurchaseOrderInvitationsTable extends Migration $table->softDeletes('deleted_at', 6); }); + + Schema::table('purchase_orders', function (Blueprint $table) { + $table->unsignedInteger('client_id')->nullable()->change(); + }); + } /** diff --git a/tests/Feature/PurchaseOrderTest.php b/tests/Feature/PurchaseOrderTest.php index 63799022b7aa..b41141db897f 100644 --- a/tests/Feature/PurchaseOrderTest.php +++ b/tests/Feature/PurchaseOrderTest.php @@ -83,7 +83,7 @@ class PurchaseOrderTest extends TestCase 'custom_value3' => 0, 'custom_value4' => 0, 'status' => 1, - 'client_id' => $this->encodePrimaryKey($this->client->id), + 'vendor_id' => $this->encodePrimaryKey($this->vendor->id), ]; $response = $this->withHeaders([ @@ -117,7 +117,7 @@ class PurchaseOrderTest extends TestCase 'custom_value3' => 0, 'custom_value4' => 0, 'status' => 1, - 'client_id' => $this->encodePrimaryKey($this->client->id), + 'vendor_id' => $this->encodePrimaryKey($this->vendor->id), ]; $response = $this->withHeaders([ diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index 2bb170b9388b..f8767315832c 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -450,22 +450,12 @@ trait MockAccountData $this->quote->save(); - - - - - - - $this->purchase_order = PurchaseOrderFactory::create($this->company->id, $user_id); - $this->purchase_order->client_id = $this->client->id; - + $this->purchase_order->vendor_id = $this->vendor->id; $this->purchase_order->amount = 10; $this->purchase_order->balance = 10; - // $this->credit->due_date = now()->addDays(200); - $this->purchase_order->tax_name1 = ''; $this->purchase_order->tax_name2 = ''; $this->purchase_order->tax_name3 = '';