diff --git a/app/Utils/TemplateEngine.php b/app/Utils/TemplateEngine.php index 4a2e2ca0a5..9f13195f80 100644 --- a/app/Utils/TemplateEngine.php +++ b/app/Utils/TemplateEngine.php @@ -16,8 +16,12 @@ use App\Models\Client; use App\Models\ClientContact; use App\Models\Invoice; use App\Models\InvoiceInvitation; +use App\Models\PurchaseOrder; +use App\Models\PurchaseOrderInvitation; use App\Models\Quote; use App\Models\QuoteInvitation; +use App\Models\Vendor; +use App\Models\VendorContact; use App\Services\PdfMaker\Designs\Utilities\DesignHelpers; use App\Utils\Ninja; use App\Utils\Traits\MakesHash; @@ -26,6 +30,7 @@ use App\Utils\Traits\MakesTemplateData; use DB; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Lang; +use Illuminate\Support\Str; use League\CommonMark\CommonMarkConverter; use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles; @@ -88,7 +93,7 @@ class TemplateEngine private function setEntity() { if (strlen($this->entity) > 1 && strlen($this->entity_id) > 1) { - $class = 'App\Models\\'.ucfirst($this->entity); + $class = 'App\Models\\'.ucfirst(Str::camel($this->entity)); $this->entity_obj = $class::withTrashed()->where('id', $this->decodePrimaryKey($this->entity_id))->company()->first(); } else { $this->mockEntity(); @@ -99,7 +104,11 @@ class TemplateEngine private function setSettingsObject() { - if ($this->entity_obj) { + if($this->entity == 'purchase_order'){ + $this->settings_entity = auth()->user()->company(); + $this->settings = $this->settings_entity->settings; + } + elseif ($this->entity_obj) { $this->settings_entity = $this->entity_obj->client; $this->settings = $this->settings_entity->getMergedSettings(); } else { @@ -143,7 +152,10 @@ class TemplateEngine $this->raw_body = $this->body; $this->raw_subject = $this->subject; - if ($this->entity_obj) { + if($this->entity == 'purchase_order'){ + $this->fakerValues(); + } + elseif ($this->entity_obj) { $this->entityValues($this->entity_obj->client->primary_contact()->first()); } else { $this->fakerValues(); @@ -198,7 +210,17 @@ class TemplateEngine $data['footer'] = ''; $data['logo'] = auth()->user()->company()->present()->logo(); - $data = array_merge($data, Helpers::sharedEmailVariables($this->entity_obj->client)); + if($this->entity_obj->client) + $data = array_merge($data, Helpers::sharedEmailVariables($this->entity_obj->client)); + else{ + + $data['signature'] = $this->settings->email_signature; + $data['settings'] = $this->settings; + $data['whitelabel'] = $this->entity_obj ? $this->entity_obj->company->account->isPaid() : true; + $data['company'] = $this->entity_obj ? $this->entity_obj->company : ''; + $data['settings'] = $this->settings; + } + if ($email_style == 'custom') { $wrapper = $this->settings_entity->getSetting('email_style_custom'); @@ -243,6 +265,8 @@ class TemplateEngine { DB::connection(config('database.default'))->beginTransaction(); + $vendor = false; + $client = Client::factory()->create([ 'user_id' => auth()->user()->id, 'company_id' => auth()->user()->company()->id, @@ -289,12 +313,60 @@ class TemplateEngine ]); } - $this->entity_obj->setRelation('invitations', $invitation); - $this->entity_obj->setRelation('client', $client); - $this->entity_obj->setRelation('company', auth()->user()->company()); - $this->entity_obj->load('client'); - $client->setRelation('company', auth()->user()->company()); - $client->load('company'); + + + if($this->entity == 'purchase_order') + { + + $vendor = Vendor::factory()->create([ + 'user_id' => auth()->user()->id, + 'company_id' => auth()->user()->company()->id, + ]); + + $contact = VendorContact::factory()->create([ + 'user_id' => auth()->user()->id, + 'company_id' => auth()->user()->company()->id, + 'vendor_id' => $vendor->id, + 'is_primary' => 1, + 'send_email' => true, + ]); + + + $this->entity_obj = PurchaseOrder::factory()->create([ + 'user_id' => auth()->user()->id, + 'company_id' => auth()->user()->company()->id, + 'vendor_id' => $vendor->id, + ]); + + $invitation = PurchaseOrderInvitation::factory()->create([ + 'user_id' => auth()->user()->id, + 'company_id' => auth()->user()->company()->id, + 'purchase_order_id' => $this->entity_obj->id, + 'vendor_contact_id' => $contact->id, + ]); + + } + + if($vendor) + { + + $this->entity_obj->setRelation('invitations', $invitation); + $this->entity_obj->setRelation('vendor', $vendor); + $this->entity_obj->setRelation('company', auth()->user()->company()); + $this->entity_obj->load('vendor'); + $vendor->setRelation('company', auth()->user()->company()); + $vendor->load('company'); + + } + else + { + $this->entity_obj->setRelation('invitations', $invitation); + $this->entity_obj->setRelation('client', $client); + $this->entity_obj->setRelation('company', auth()->user()->company()); + $this->entity_obj->load('client'); + $client->setRelation('company', auth()->user()->company()); + $client->load('company'); + } } private function tearDown() diff --git a/app/Utils/Traits/MakesTemplateData.php b/app/Utils/Traits/MakesTemplateData.php index 2b17cbea61..fa6b932821 100644 --- a/app/Utils/Traits/MakesTemplateData.php +++ b/app/Utils/Traits/MakesTemplateData.php @@ -200,6 +200,36 @@ trait MakesTemplateData $data['$task.tax_name3'] = ['value' => 'CA Sales Tax', 'label' => ctrans('texts.tax')]; $data['$task.line_total'] = ['value' => '$100.00', 'label' => ctrans('texts.line_total')]; + $data['$vendor_name'] = &$data['$client_name']; + $data['$vendor.name'] = &$data['$client_name']; + $data['$vendor'] = &$data['$client_name']; + + $data['$vendor.address1'] = &$data['$address1']; + $data['$vendor.address2'] = &$data['$address2']; + $data['$vendor_address'] = ['value' => '5 Kalamazoo Way\n Jimbuckeroo\n USA 90210', 'label' => ctrans('texts.address')]; + $data['$vendor.address'] = &$data['$vendor_address']; + $data['$vendor.postal_code'] = ['value' => '90210', 'label' => ctrans('texts.postal_code')]; + $data['$vendor.public_notes'] = $data['$invoice.public_notes']; + $data['$vendor.city'] = &$data['$company.city']; + $data['$vendor.state'] = &$data['$company.state']; + $data['$vendor.id_number'] = &$data['$id_number']; + $data['$vendor.vat_number'] = &$data['$vat_number']; + $data['$vendor.website'] = &$data['$website']; + $data['$vendor.phone'] = &$data['$phone']; + $data['$vendor.city_state_postal'] = &$data['$city_state_postal']; + $data['$vendor.postal_city_state'] = &$data['$postal_city_state']; + $data['$vendor.country'] = &$data['$country']; + $data['$vendor.email'] = &$data['$email']; + + $data['$vendor.billing_address1'] = &$data['$vendor.address1']; + $data['$vendor.billing_address2'] = &$data['$vendor.address2']; + $data['$vendor.billing_city'] = &$data['$vendor.city']; + $data['$vendor.billing_state'] = &$data['$vendor.state']; + $data['$vendor.billing_postal_code'] = &$data['$vendor.postal_code']; + $data['$vendor.billing_country'] = &$data['$vendor.country']; + + + //$data['$paid_to_date'] = ; // $data['$your_invoice'] = ; // $data['$quote'] = ; diff --git a/database/factories/PurchaseOrderFactory.php b/database/factories/PurchaseOrderFactory.php new file mode 100644 index 0000000000..df91de22f5 --- /dev/null +++ b/database/factories/PurchaseOrderFactory.php @@ -0,0 +1,51 @@ + Invoice::STATUS_SENT, + 'number' => $this->faker->ean13(), + 'discount' => $this->faker->numberBetween(1, 10), + 'is_amount_discount' => (bool) random_int(0, 1), + 'tax_name1' => 'GST', + 'tax_rate1' => 10, + 'tax_name2' => 'VAT', + 'tax_rate2' => 17.5, + 'is_deleted' => false, + 'po_number' => $this->faker->text(10), + 'date' => $this->faker->date(), + 'due_date' => $this->faker->date(), + 'line_items' => InvoiceItemFactory::generate(5), + 'terms' => $this->faker->text(500), + ]; + } +}