From 5dc4eafde9cddcd0db588ee602c74bfed1669c51 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 15 Mar 2023 10:46:36 +1100 Subject: [PATCH] Do not subtract services as product inventory --- app/Http/Controllers/EmailController.php | 3 + app/Http/Requests/Email/SendEmailRequest.php | 1 + app/Jobs/Inventory/AdjustProductInventory.php | 146 ++++++++++++------ app/Services/Email/EmailDefaults.php | 10 +- app/Services/Email/EmailMailable.php | 5 +- tests/Feature/InvoiceEmailTest.php | 29 ++++ 6 files changed, 144 insertions(+), 50 deletions(-) diff --git a/app/Http/Controllers/EmailController.php b/app/Http/Controllers/EmailController.php index 56d76a4c79..af73ff04cc 100644 --- a/app/Http/Controllers/EmailController.php +++ b/app/Http/Controllers/EmailController.php @@ -31,6 +31,7 @@ use App\Http\Requests\Email\SendEmailRequest; use App\Jobs\PurchaseOrder\PurchaseOrderEmail; use App\Transformers\PurchaseOrderTransformer; use App\Transformers\RecurringInvoiceTransformer; +use Illuminate\Mail\Mailables\Address; class EmailController extends BaseController { @@ -135,6 +136,8 @@ class EmailController extends BaseController $mo->email_template_body = $request->input('template'); $mo->email_template_subject = str_replace("template", "subject", $request->input('template')); + if($request->has('cc_email')) + $mo->cc[] = new Address($request->cc_email); // if ($entity == 'purchaseOrder' || $entity == 'purchase_order' || $template == 'purchase_order' || $entity == 'App\Models\PurchaseOrder') { // return $this->sendPurchaseOrder($entity_obj, $data, $template); diff --git a/app/Http/Requests/Email/SendEmailRequest.php b/app/Http/Requests/Email/SendEmailRequest.php index 36cd4d2e98..c6d16d54c6 100644 --- a/app/Http/Requests/Email/SendEmailRequest.php +++ b/app/Http/Requests/Email/SendEmailRequest.php @@ -43,6 +43,7 @@ class SendEmailRequest extends Request 'template' => 'bail|required', 'entity' => 'bail|required', 'entity_id' => 'bail|required', + 'cc_email' => 'bail|sometimes|email', ]; } diff --git a/app/Jobs/Inventory/AdjustProductInventory.php b/app/Jobs/Inventory/AdjustProductInventory.php index f4772ca901..8e5c01e1dc 100644 --- a/app/Jobs/Inventory/AdjustProductInventory.php +++ b/app/Jobs/Inventory/AdjustProductInventory.php @@ -31,17 +31,8 @@ class AdjustProductInventory implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, UserNotifies; - public Company $company; - - public Invoice $invoice; - - public array $old_invoice; - - public function __construct(Company $company, Invoice $invoice, $old_invoice = []) + public function __construct(public Company $company, public Invoice $invoice, public $old_invoice = []) { - $this->company = $company; - $this->invoice = $invoice; - $this->old_invoice = $old_invoice; } /** @@ -65,33 +56,64 @@ class AdjustProductInventory implements ShouldQueue { MultiDB::setDb($this->company->db); - foreach ($this->invoice->line_items as $item) { - $p = Product::where('product_key', $item->product_key)->where('company_id', $this->company->id)->first(); + // foreach ($this->invoice->line_items as $item) { + // $p = Product::where('product_key', $item->product_key)->where('company_id', $this->company->id)->first(); + + // if (! $p) { + // continue; + // } + + // $p->in_stock_quantity += $item->quantity; + + // $p->saveQuietly(); + // } + + collect($this->invoice->line_items)->filter(function ($item){ + return $item->type_id == '1'; + })->each(function ($i){ + + $p = Product::where('product_key', $i->product_key)->where('company_id', $this->company->id)->first(); + + if ($p) { + + $p->in_stock_quantity += $i->quantity; + + $p->saveQuietly(); - if (! $p) { - continue; } - $p->in_stock_quantity += $item->quantity; + }); - $p->saveQuietly(); - } } public function handleRestoredInvoice() { MultiDB::setDb($this->company->db); - foreach ($this->invoice->line_items as $item) { - $p = Product::where('product_key', $item->product_key)->where('company_id', $this->company->id)->first(); + // foreach ($this->invoice->line_items as $item) { + // $p = Product::where('product_key', $item->product_key)->where('company_id', $this->company->id)->first(); - if (! $p) { - continue; + // if (! $p) { + // continue; + // } + + // $p->in_stock_quantity -= $item->quantity; + // $p->saveQuietly(); + // } + + collect($this->invoice->line_items)->filter(function ($item) { + return $item->type_id == '1'; + })->each(function ($i) { + $p = Product::where('product_key', $i->product_key)->where('company_id', $this->company->id)->first(); + + if ($p) { + $p->in_stock_quantity -= $i->quantity; + + $p->saveQuietly(); } + }); + - $p->in_stock_quantity -= $item->quantity; - $p->saveQuietly(); - } } public function middleware() @@ -101,38 +123,74 @@ class AdjustProductInventory implements ShouldQueue private function newInventoryAdjustment() { - $line_items = $this->invoice->line_items; + // $line_items = $this->invoice->line_items; - foreach ($line_items as $item) { - $p = Product::where('product_key', $item->product_key)->where('company_id', $this->company->id)->where('in_stock_quantity', '>', 0)->first(); + // foreach ($line_items as $item) { + // $p = Product::where('product_key', $item->product_key)->where('company_id', $this->company->id)->where('in_stock_quantity', '>', 0)->first(); + + // if (! $p) { + // continue; + // } + + // $p->in_stock_quantity -= $item->quantity; + // $p->saveQuietly(); + + // if ($this->company->stock_notification && $p->stock_notification && $p->stock_notification_threshold && $p->in_stock_quantity <= $p->stock_notification_threshold) { + // $this->notifyStockLevels($p, 'product'); + // } elseif ($this->company->stock_notification && $p->stock_notification && $this->company->inventory_notification_threshold && $p->in_stock_quantity <= $this->company->inventory_notification_threshold) { + // $this->notifyStocklevels($p, 'company'); + // } + // } + + collect($this->invoice->line_items)->filter(function ($item) { + return $item->type_id == '1'; + })->each(function ($i) { + $p = Product::where('product_key', $i->product_key)->where('company_id', $this->company->id)->first(); + + if ($p) { + $p->in_stock_quantity -= $i->quantity; + + $p->saveQuietly(); + + if ($this->company->stock_notification && $p->stock_notification && $p->stock_notification_threshold && $p->in_stock_quantity <= $p->stock_notification_threshold) { + $this->notifyStockLevels($p, 'product'); + } elseif ($this->company->stock_notification && $p->stock_notification && $this->company->inventory_notification_threshold && $p->in_stock_quantity <= $this->company->inventory_notification_threshold) { + $this->notifyStocklevels($p, 'company'); + } - if (! $p) { - continue; } + }); + - $p->in_stock_quantity -= $item->quantity; - $p->saveQuietly(); - if ($this->company->stock_notification && $p->stock_notification && $p->stock_notification_threshold && $p->in_stock_quantity <= $p->stock_notification_threshold) { - $this->notifyStockLevels($p, 'product'); - } elseif ($this->company->stock_notification && $p->stock_notification && $this->company->inventory_notification_threshold && $p->in_stock_quantity <= $this->company->inventory_notification_threshold) { - $this->notifyStocklevels($p, 'company'); - } - } } private function existingInventoryAdjustment() { - foreach ($this->old_invoice as $item) { - $p = Product::where('product_key', $item->product_key)->where('company_id', $this->company->id)->first(); + // foreach ($this->old_invoice as $item) { + // $p = Product::where('product_key', $item->product_key)->where('company_id', $this->company->id)->first(); - if (! $p) { - continue; + // if (! $p) { + // continue; + // } + + // $p->in_stock_quantity += $item->quantity; + // $p->saveQuietly(); + // } + + collect($this->invoice->line_items)->filter(function ($item) { + return $item->type_id == '1'; + })->each(function ($i) { + $p = Product::where('product_key', $i->product_key)->where('company_id', $this->company->id)->first(); + + if ($p) { + $p->in_stock_quantity += $i->quantity; + + $p->saveQuietly(); } + }); + - $p->in_stock_quantity += $item->quantity; - $p->saveQuietly(); - } } private function notifyStocklevels(Product $product, string $notification_level) diff --git a/app/Services/Email/EmailDefaults.php b/app/Services/Email/EmailDefaults.php index de0872d573..2aa356a4d8 100644 --- a/app/Services/Email/EmailDefaults.php +++ b/app/Services/Email/EmailDefaults.php @@ -69,6 +69,7 @@ class EmailDefaults $this->setLocale() ->setFrom() ->setTo() + ->setCc() ->setTemplate() ->setBody() ->setSubject() @@ -259,13 +260,14 @@ class EmailDefaults /** * Sets the CC of the email - * @todo at some point.... */ - private function buildCc() + private function setCc(): self { - return [ + return $this; + // return $this->email->email_object->cc; + // return [ - ]; + // ]; } /** diff --git a/app/Services/Email/EmailMailable.php b/app/Services/Email/EmailMailable.php index 665a0c0ac7..08c8a54dc2 100644 --- a/app/Services/Email/EmailMailable.php +++ b/app/Services/Email/EmailMailable.php @@ -39,14 +39,15 @@ class EmailMailable extends Mailable * @return \Illuminate\Mail\Mailables\Envelope */ public function envelope() - { + {nlog($this->email_object->cc); return new Envelope( subject: $this->email_object->subject, tags: [$this->email_object->company_key], replyTo: $this->email_object->reply_to, from: $this->email_object->from, to: $this->email_object->to, - bcc: $this->email_object->bcc + bcc: $this->email_object->bcc, + cc: $this->email_object->cc, ); } diff --git a/tests/Feature/InvoiceEmailTest.php b/tests/Feature/InvoiceEmailTest.php index f1387f9bf5..13fc696189 100644 --- a/tests/Feature/InvoiceEmailTest.php +++ b/tests/Feature/InvoiceEmailTest.php @@ -18,6 +18,7 @@ use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Session; use Tests\MockAccountData; use Tests\TestCase; +use Illuminate\Validation\ValidationException; /** * @test @@ -40,6 +41,34 @@ class InvoiceEmailTest extends TestCase Model::reguard(); $this->makeTestData(); + + $this->withoutExceptionHandling(); + + } + + public function test_cc_email_implementation() + { + $data = [ + 'template' => 'email_template_invoice', + 'entity' => 'invoice', + 'entity_id' => $this->invoice->hashed_id, + 'cc_email' => 'jj@gmail.com' + ]; + + $response = false; + + try { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/emails', $data); + } catch (ValidationException $e) { + $message = json_decode($e->validator->getMessageBag(), 1); + nlog($message); + } + + $response->assertStatus(200); + } public function test_initial_email_send_emails()