1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Do not subtract services as product inventory

This commit is contained in:
David Bomba 2023-03-15 10:46:36 +11:00
parent 1b483c7e38
commit 5dc4eafde9
6 changed files with 144 additions and 50 deletions

View File

@ -31,6 +31,7 @@ use App\Http\Requests\Email\SendEmailRequest;
use App\Jobs\PurchaseOrder\PurchaseOrderEmail; use App\Jobs\PurchaseOrder\PurchaseOrderEmail;
use App\Transformers\PurchaseOrderTransformer; use App\Transformers\PurchaseOrderTransformer;
use App\Transformers\RecurringInvoiceTransformer; use App\Transformers\RecurringInvoiceTransformer;
use Illuminate\Mail\Mailables\Address;
class EmailController extends BaseController class EmailController extends BaseController
{ {
@ -135,6 +136,8 @@ class EmailController extends BaseController
$mo->email_template_body = $request->input('template'); $mo->email_template_body = $request->input('template');
$mo->email_template_subject = str_replace("template", "subject", $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') { // if ($entity == 'purchaseOrder' || $entity == 'purchase_order' || $template == 'purchase_order' || $entity == 'App\Models\PurchaseOrder') {
// return $this->sendPurchaseOrder($entity_obj, $data, $template); // return $this->sendPurchaseOrder($entity_obj, $data, $template);

View File

@ -43,6 +43,7 @@ class SendEmailRequest extends Request
'template' => 'bail|required', 'template' => 'bail|required',
'entity' => 'bail|required', 'entity' => 'bail|required',
'entity_id' => 'bail|required', 'entity_id' => 'bail|required',
'cc_email' => 'bail|sometimes|email',
]; ];
} }

View File

@ -31,17 +31,8 @@ class AdjustProductInventory implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, UserNotifies; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, UserNotifies;
public Company $company; public function __construct(public Company $company, public Invoice $invoice, public $old_invoice = [])
public Invoice $invoice;
public array $old_invoice;
public function __construct(Company $company, Invoice $invoice, $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); MultiDB::setDb($this->company->db);
foreach ($this->invoice->line_items as $item) { // foreach ($this->invoice->line_items as $item) {
$p = Product::where('product_key', $item->product_key)->where('company_id', $this->company->id)->first(); // $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() public function handleRestoredInvoice()
{ {
MultiDB::setDb($this->company->db); MultiDB::setDb($this->company->db);
foreach ($this->invoice->line_items as $item) { // foreach ($this->invoice->line_items as $item) {
$p = Product::where('product_key', $item->product_key)->where('company_id', $this->company->id)->first(); // $p = Product::where('product_key', $item->product_key)->where('company_id', $this->company->id)->first();
if (! $p) { // if (! $p) {
continue; // 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() public function middleware()
@ -101,38 +123,74 @@ class AdjustProductInventory implements ShouldQueue
private function newInventoryAdjustment() private function newInventoryAdjustment()
{ {
$line_items = $this->invoice->line_items; // $line_items = $this->invoice->line_items;
foreach ($line_items as $item) { // 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(); // $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() private function existingInventoryAdjustment()
{ {
foreach ($this->old_invoice as $item) { // foreach ($this->old_invoice as $item) {
$p = Product::where('product_key', $item->product_key)->where('company_id', $this->company->id)->first(); // $p = Product::where('product_key', $item->product_key)->where('company_id', $this->company->id)->first();
if (! $p) { // if (! $p) {
continue; // 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) private function notifyStocklevels(Product $product, string $notification_level)

View File

@ -69,6 +69,7 @@ class EmailDefaults
$this->setLocale() $this->setLocale()
->setFrom() ->setFrom()
->setTo() ->setTo()
->setCc()
->setTemplate() ->setTemplate()
->setBody() ->setBody()
->setSubject() ->setSubject()
@ -259,13 +260,14 @@ class EmailDefaults
/** /**
* Sets the CC of the email * 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 [
]; // ];
} }
/** /**

View File

@ -39,14 +39,15 @@ class EmailMailable extends Mailable
* @return \Illuminate\Mail\Mailables\Envelope * @return \Illuminate\Mail\Mailables\Envelope
*/ */
public function envelope() public function envelope()
{ {nlog($this->email_object->cc);
return new Envelope( return new Envelope(
subject: $this->email_object->subject, subject: $this->email_object->subject,
tags: [$this->email_object->company_key], tags: [$this->email_object->company_key],
replyTo: $this->email_object->reply_to, replyTo: $this->email_object->reply_to,
from: $this->email_object->from, from: $this->email_object->from,
to: $this->email_object->to, to: $this->email_object->to,
bcc: $this->email_object->bcc bcc: $this->email_object->bcc,
cc: $this->email_object->cc,
); );
} }

View File

@ -18,6 +18,7 @@ use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Session;
use Tests\MockAccountData; use Tests\MockAccountData;
use Tests\TestCase; use Tests\TestCase;
use Illuminate\Validation\ValidationException;
/** /**
* @test * @test
@ -40,6 +41,34 @@ class InvoiceEmailTest extends TestCase
Model::reguard(); Model::reguard();
$this->makeTestData(); $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() public function test_initial_email_send_emails()