v2 first commit
This commit is contained in:
@@ -2,18 +2,16 @@
|
||||
|
||||
namespace App\Jobs\Expense;
|
||||
|
||||
use App\Events\BillCreated;
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Expense\BillCreated;
|
||||
use App\Models\Expense\Bill;
|
||||
use App\Models\Expense\BillHistory;
|
||||
use App\Models\Expense\BillTotal;
|
||||
use App\Traits\Currencies;
|
||||
use App\Traits\DateTime;
|
||||
use App\Traits\Uploads;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
|
||||
class CreateBill
|
||||
class CreateBill extends Job
|
||||
{
|
||||
use Currencies, DateTime, Dispatchable, Uploads;
|
||||
use Currencies, DateTime;
|
||||
|
||||
protected $request;
|
||||
|
||||
@@ -24,7 +22,7 @@ class CreateBill
|
||||
*/
|
||||
public function __construct($request)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,7 +32,7 @@ class CreateBill
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$bill = Bill::create($this->request->input());
|
||||
$bill = Bill::create($this->request->all());
|
||||
|
||||
// Upload attachment
|
||||
if ($this->request->file('attachment')) {
|
||||
@@ -50,9 +48,9 @@ class CreateBill
|
||||
$discount_total = 0;
|
||||
$discount = $this->request['discount'];
|
||||
|
||||
if ($this->request['item']) {
|
||||
foreach ($this->request['item'] as $item) {
|
||||
$bill_item = dispatch(new CreateBillItem($item, $bill, $discount));
|
||||
if ($this->request['items']) {
|
||||
foreach ($this->request['items'] as $item) {
|
||||
$bill_item = $this->dispatch(new CreateBillItem($item, $bill, $discount));
|
||||
|
||||
// Calculate totals
|
||||
$tax_total += $bill_item->tax;
|
||||
@@ -92,15 +90,6 @@ class CreateBill
|
||||
// Add bill totals
|
||||
$this->addTotals($bill, $this->request, $taxes, $sub_total, $discount_total, $tax_total);
|
||||
|
||||
// Add bill history
|
||||
BillHistory::create([
|
||||
'company_id' => session('company_id'),
|
||||
'bill_id' => $bill->id,
|
||||
'status_code' => 'draft',
|
||||
'notify' => 0,
|
||||
'description' => trans('messages.success.added', ['type' => $bill->bill_number]),
|
||||
]);
|
||||
|
||||
// Recurring
|
||||
$bill->createRecurring();
|
||||
|
||||
|
||||
49
app/Jobs/Expense/CreateBillHistory.php
Normal file
49
app/Jobs/Expense/CreateBillHistory.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Expense;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Expense\BillHistory;
|
||||
|
||||
class CreateBillHistory extends Job
|
||||
{
|
||||
protected $bill;
|
||||
|
||||
protected $notify;
|
||||
|
||||
protected $description;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $bill
|
||||
* @param $notify
|
||||
* @param $description
|
||||
*/
|
||||
public function __construct($bill, $notify = 0, $description = null)
|
||||
{
|
||||
$this->bill = $bill;
|
||||
$this->notify = $notify;
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return BillHistory
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$description = $this->description ?: trans_choice('general.payments', 1);
|
||||
|
||||
$bill_history = BillHistory::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'status_code' => $this->bill->bill_status_code,
|
||||
'notify' => $this->notify,
|
||||
'description' => $description,
|
||||
]);
|
||||
|
||||
return $bill_history;
|
||||
}
|
||||
}
|
||||
@@ -2,18 +2,14 @@
|
||||
|
||||
namespace App\Jobs\Expense;
|
||||
|
||||
use App\Models\Common\Item;
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Expense\BillItem;
|
||||
use App\Models\Expense\BillItemTax;
|
||||
use App\Models\Setting\Tax;
|
||||
use App\Notifications\Common\Item as ItemNotification;
|
||||
use App\Notifications\Common\ItemReminder as ItemReminderNotification;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CreateBillItem
|
||||
class CreateBillItem extends Job
|
||||
{
|
||||
use Dispatchable;
|
||||
|
||||
protected $data;
|
||||
|
||||
protected $bill;
|
||||
@@ -41,8 +37,6 @@ class CreateBillItem
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$item_sku = '';
|
||||
|
||||
$item_id = !empty($this->data['item_id']) ? $this->data['item_id'] : 0;
|
||||
$item_amount = (double) $this->data['price'] * (double) $this->data['quantity'];
|
||||
|
||||
@@ -53,19 +47,6 @@ class CreateBillItem
|
||||
$item_discount_amount = $item_amount - ($item_amount * ($this->discount / 100));
|
||||
}
|
||||
|
||||
if (!empty($item_id)) {
|
||||
$item_object = Item::find($item_id);
|
||||
|
||||
$this->data['name'] = $item_object->name;
|
||||
$item_sku = $item_object->sku;
|
||||
|
||||
// Increase stock (item bought)
|
||||
$item_object->quantity += (double) $this->data['quantity'];
|
||||
$item_object->save();
|
||||
} elseif (!empty($this->data['sku'])) {
|
||||
$item_sku = $this->data['sku'];
|
||||
}
|
||||
|
||||
$tax_amount = 0;
|
||||
$item_taxes = [];
|
||||
$item_tax_total = 0;
|
||||
@@ -83,6 +64,20 @@ class CreateBillItem
|
||||
case 'compound':
|
||||
$compounds[] = $tax;
|
||||
break;
|
||||
case 'fixed':
|
||||
$fixed_taxes[] = $tax;
|
||||
$tax_amount = $tax->rate;
|
||||
|
||||
$item_taxes[] = [
|
||||
'company_id' => $this->invoice->company_id,
|
||||
'invoice_id' => $this->invoice->id,
|
||||
'tax_id' => $tax_id,
|
||||
'name' => $tax->name,
|
||||
'amount' => $tax_amount,
|
||||
];
|
||||
|
||||
$item_tax_total += $tax_amount;
|
||||
break;
|
||||
case 'normal':
|
||||
default:
|
||||
$taxes[] = $tax;
|
||||
@@ -105,7 +100,7 @@ class CreateBillItem
|
||||
if ($inclusives) {
|
||||
$item_amount = $item_discount_amount + $item_tax_total;
|
||||
|
||||
$item_base_rate = $item_amount / (1 + collect($inclusives)->sum('rate')/100);
|
||||
$item_base_rate = $item_amount / (1 + collect($inclusives)->sum('rate') / 100);
|
||||
|
||||
foreach ($inclusives as $inclusive) {
|
||||
$item_tax_total += $tax_amount = $item_base_rate * ($inclusive->rate / 100);
|
||||
@@ -143,8 +138,7 @@ class CreateBillItem
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'item_id' => $item_id,
|
||||
'name' => str_limit($this->data['name'], 180, ''),
|
||||
'sku' => $item_sku,
|
||||
'name' => Str::limit($this->data['name'], 180, ''),
|
||||
'quantity' => (double) $this->data['quantity'],
|
||||
'price' => (double) $this->data['price'],
|
||||
'tax' => $item_tax_total,
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Expense;
|
||||
|
||||
use App\Models\Expense\BillHistory;
|
||||
use App\Models\Expense\BillPayment;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
|
||||
class CreateBillPayment
|
||||
{
|
||||
use Dispatchable;
|
||||
|
||||
protected $request;
|
||||
|
||||
protected $bill;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
* @param $bill
|
||||
*/
|
||||
public function __construct($request, $bill)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->bill = $bill;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return BillPayment
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$bill_payment = BillPayment::create($this->request->input());
|
||||
|
||||
$desc_amount = money((float) $bill_payment->amount, (string) $bill_payment->currency_code, true)->format();
|
||||
|
||||
$history_data = [
|
||||
'company_id' => $bill_payment->company_id,
|
||||
'bill_id' => $bill_payment->bill_id,
|
||||
'status_code' => $this->bill->bill_status_code,
|
||||
'notify' => '0',
|
||||
'description' => $desc_amount . ' ' . trans_choice('general.payments', 1),
|
||||
];
|
||||
|
||||
BillHistory::create($history_data);
|
||||
|
||||
return $bill_payment;
|
||||
}
|
||||
}
|
||||
37
app/Jobs/Expense/DeleteBill.php
Normal file
37
app/Jobs/Expense/DeleteBill.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Expense;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Expense\Bill;
|
||||
|
||||
class DeleteBill extends Job
|
||||
{
|
||||
protected $bill;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $bill
|
||||
*/
|
||||
public function __construct($bill)
|
||||
{
|
||||
$this->bill = $bill;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Bill
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->deleteRelationships($this->bill, [
|
||||
'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals'
|
||||
]);
|
||||
|
||||
$this->bill->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
43
app/Jobs/Expense/DuplicateBill.php
Normal file
43
app/Jobs/Expense/DuplicateBill.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Expense;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Expense\Bill;
|
||||
use App\Models\Expense\BillHistory;
|
||||
|
||||
class DuplicateBill extends Job
|
||||
{
|
||||
protected $bill;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $bill
|
||||
*/
|
||||
public function __construct($bill)
|
||||
{
|
||||
$this->bill = $bill;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Bill
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$clone = $this->bill->duplicate();
|
||||
|
||||
// Add bill history
|
||||
BillHistory::create([
|
||||
'company_id' => session('company_id'),
|
||||
'bill_id' => $clone->id,
|
||||
'status_code' => 'draft',
|
||||
'notify' => 0,
|
||||
'description' => trans('messages.success.added', ['type' => $clone->bill_number]),
|
||||
]);
|
||||
|
||||
return $clone;
|
||||
}
|
||||
}
|
||||
@@ -2,19 +2,17 @@
|
||||
|
||||
namespace App\Jobs\Expense;
|
||||
|
||||
use App\Events\BillUpdated;
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Common\Item;
|
||||
use App\Models\Expense\Bill;
|
||||
use App\Models\Expense\BillTotal;
|
||||
use App\Traits\Currencies;
|
||||
use App\Traits\DateTime;
|
||||
use App\Traits\Uploads;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
|
||||
class UpdateBill
|
||||
class UpdateBill extends Job
|
||||
{
|
||||
use Currencies, DateTime, Dispatchable, Uploads;
|
||||
use Currencies, DateTime;
|
||||
|
||||
protected $bill;
|
||||
|
||||
@@ -28,7 +26,7 @@ class UpdateBill
|
||||
public function __construct($bill, $request)
|
||||
{
|
||||
$this->bill = $bill;
|
||||
$this->request = $request;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,27 +50,11 @@ class UpdateBill
|
||||
$discount_total = 0;
|
||||
$discount = $this->request['discount'];
|
||||
|
||||
if ($this->request['item']) {
|
||||
$items = $this->bill->items;
|
||||
if ($this->request['items']) {
|
||||
$this->deleteRelationships($this->bill, ['items', 'item_taxes']);
|
||||
|
||||
if ($items) {
|
||||
foreach ($items as $item) {
|
||||
if (empty($item->item_id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$item_object = Item::find($item->item_id);
|
||||
|
||||
// Decrease stock
|
||||
$item_object->quantity -= (double) $item->quantity;
|
||||
$item_object->save();
|
||||
}
|
||||
}
|
||||
|
||||
$this->deleteRelationships($this->bill, 'items');
|
||||
|
||||
foreach ($this->request['item'] as $item) {
|
||||
$bill_item = dispatch(new CreateBillItem($item, $this->bill, $discount));
|
||||
foreach ($this->request['items'] as $item) {
|
||||
$bill_item = dispatch_now(new CreateBillItem($item, $this->bill, $discount));
|
||||
|
||||
// Calculate totals
|
||||
$tax_total += $bill_item->tax;
|
||||
@@ -127,7 +109,7 @@ class UpdateBill
|
||||
$this->bill->updateRecurring();
|
||||
|
||||
// Fire the event to make it extensible
|
||||
event(new BillUpdated($this->bill));
|
||||
event(new \App\Events\Expense\BillUpdated($this->bill));
|
||||
|
||||
return $this->bill;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user