renamed income/expense
This commit is contained in:
195
app/Jobs/Purchase/CreateBill.php
Normal file
195
app/Jobs/Purchase/CreateBill.php
Normal file
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Purchase;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Purchase\BillCreated;
|
||||
use App\Events\Purchase\BillCreating;
|
||||
use App\Models\Purchase\Bill;
|
||||
use App\Models\Purchase\BillTotal;
|
||||
use App\Traits\Currencies;
|
||||
use App\Traits\DateTime;
|
||||
|
||||
class CreateBill extends Job
|
||||
{
|
||||
use Currencies, DateTime;
|
||||
|
||||
protected $request;
|
||||
|
||||
protected $bill;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($request)
|
||||
{
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Bill
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if (empty($this->request['amount'])) {
|
||||
$this->request['amount'] = 0;
|
||||
}
|
||||
|
||||
event(new BillCreating($this->request));
|
||||
|
||||
$this->bill = Bill::create($this->request->all());
|
||||
|
||||
// Upload attachment
|
||||
if ($this->request->file('attachment')) {
|
||||
$media = $this->getMedia($this->request->file('attachment'), 'bills');
|
||||
|
||||
$this->bill->attachMedia($media, 'attachment');
|
||||
}
|
||||
|
||||
$this->createItemsAndTotals();
|
||||
|
||||
$this->bill->update($this->request->input());
|
||||
|
||||
$this->bill->createRecurring();
|
||||
|
||||
event(new BillCreated($this->bill));
|
||||
|
||||
return $this->bill;
|
||||
}
|
||||
|
||||
protected function createItemsAndTotals()
|
||||
{
|
||||
// Create items
|
||||
list($sub_total, $taxes) = $this->createItems();
|
||||
|
||||
$sort_order = 1;
|
||||
|
||||
// Add sub total
|
||||
BillTotal::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'code' => 'sub_total',
|
||||
'name' => 'bills.sub_total',
|
||||
'amount' => $sub_total,
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
|
||||
$this->request['amount'] += $sub_total;
|
||||
|
||||
$sort_order++;
|
||||
|
||||
// Add discount
|
||||
if (!empty($this->request['discount'])) {
|
||||
$discount_total = $sub_total * ($this->request['discount'] / 100);
|
||||
|
||||
BillTotal::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'code' => 'discount',
|
||||
'name' => 'bills.discount',
|
||||
'amount' => $discount_total,
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
|
||||
$this->request['amount'] -= $discount_total;
|
||||
|
||||
$sort_order++;
|
||||
}
|
||||
|
||||
// Add taxes
|
||||
if (!empty($taxes)) {
|
||||
foreach ($taxes as $tax) {
|
||||
BillTotal::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'code' => 'tax',
|
||||
'name' => $tax['name'],
|
||||
'amount' => $tax['amount'],
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
|
||||
$this->request['amount'] += $tax['amount'];
|
||||
|
||||
$sort_order++;
|
||||
}
|
||||
}
|
||||
|
||||
// Add extra totals, i.e. shipping fee
|
||||
if (!empty($this->request['totals'])) {
|
||||
foreach ($this->request['totals'] as $total) {
|
||||
$total['company_id'] = $this->bill->company_id;
|
||||
$total['bill_id'] = $this->bill->id;
|
||||
$total['sort_order'] = $sort_order;
|
||||
|
||||
if (empty($total['code'])) {
|
||||
$total['code'] = 'extra';
|
||||
}
|
||||
|
||||
BillTotal::create($total);
|
||||
|
||||
if (empty($total['operator']) || ($total['operator'] == 'addition')) {
|
||||
$this->request['amount'] += $total['amount'];
|
||||
} else {
|
||||
// subtraction
|
||||
$this->request['amount'] -= $total['amount'];
|
||||
}
|
||||
|
||||
$sort_order++;
|
||||
}
|
||||
}
|
||||
|
||||
// Add total
|
||||
BillTotal::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'code' => 'total',
|
||||
'name' => 'bills.total',
|
||||
'amount' => $this->request['amount'],
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function createItems()
|
||||
{
|
||||
$sub_total = 0;
|
||||
|
||||
$taxes = [];
|
||||
|
||||
if (empty($this->request['items'])) {
|
||||
return [$sub_total, $taxes];
|
||||
}
|
||||
|
||||
foreach ((array) $this->request['items'] as $item) {
|
||||
if (empty($item['discount'])) {
|
||||
$item['discount'] = !empty($this->request['discount']) ? !empty($this->request['discount']) : 0;
|
||||
}
|
||||
|
||||
$bill_item = $this->dispatch(new CreateBillItem($item, $this->bill));
|
||||
|
||||
// Calculate totals
|
||||
$sub_total += $bill_item->total;
|
||||
|
||||
if (!$bill_item->item_taxes) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set taxes
|
||||
foreach ((array) $bill_item->item_taxes as $item_tax) {
|
||||
if (array_key_exists($item_tax['tax_id'], $taxes)) {
|
||||
$taxes[$item_tax['tax_id']]['amount'] += $item_tax['amount'];
|
||||
} else {
|
||||
$taxes[$item_tax['tax_id']] = [
|
||||
'name' => $item_tax['name'],
|
||||
'amount' => $item_tax['amount']
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [$sub_total, $taxes];
|
||||
}
|
||||
}
|
49
app/Jobs/Purchase/CreateBillHistory.php
Normal file
49
app/Jobs/Purchase/CreateBillHistory.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Purchase;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Purchase\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;
|
||||
}
|
||||
}
|
162
app/Jobs/Purchase/CreateBillItem.php
Normal file
162
app/Jobs/Purchase/CreateBillItem.php
Normal file
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Purchase;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Purchase\BillItem;
|
||||
use App\Models\Purchase\BillItemTax;
|
||||
use App\Models\Setting\Tax;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CreateBillItem extends Job
|
||||
{
|
||||
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 BillItem
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$item_id = !empty($this->request['item_id']) ? $this->request['item_id'] : 0;
|
||||
$item_amount = (double) $this->request['price'] * (double) $this->request['quantity'];
|
||||
|
||||
$item_discounted_amount = $item_amount;
|
||||
|
||||
// Apply discount to amount
|
||||
if (!empty($this->request['discount'])) {
|
||||
$item_discounted_amount = $item_amount - ($item_amount * ($this->request['discount'] / 100));
|
||||
}
|
||||
|
||||
$tax_amount = 0;
|
||||
$item_taxes = [];
|
||||
$item_tax_total = 0;
|
||||
|
||||
if (!empty($this->request['tax_id'])) {
|
||||
$inclusives = $compounds = [];
|
||||
|
||||
foreach ((array) $this->request['tax_id'] as $tax_id) {
|
||||
$tax = Tax::find($tax_id);
|
||||
|
||||
switch ($tax->type) {
|
||||
case 'inclusive':
|
||||
$inclusives[] = $tax;
|
||||
|
||||
break;
|
||||
case 'compound':
|
||||
$compounds[] = $tax;
|
||||
|
||||
break;
|
||||
case 'fixed':
|
||||
$tax_amount = $tax->rate * (double) $this->request['quantity'];
|
||||
|
||||
$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;
|
||||
default:
|
||||
$tax_amount = ($item_discounted_amount / 100) * $tax->rate;
|
||||
|
||||
$item_taxes[] = [
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'tax_id' => $tax_id,
|
||||
'name' => $tax->name,
|
||||
'amount' => $tax_amount,
|
||||
];
|
||||
|
||||
$item_tax_total += $tax_amount;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($inclusives) {
|
||||
$item_amount = $item_discounted_amount + $item_tax_total;
|
||||
|
||||
$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);
|
||||
|
||||
$item_taxes[] = [
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'tax_id' => $inclusive->id,
|
||||
'name' => $inclusive->name,
|
||||
'amount' => $tax_amount,
|
||||
];
|
||||
}
|
||||
|
||||
$item_amount = ($item_amount - $item_tax_total) / (1 - $this->request['discount'] / 100);
|
||||
}
|
||||
|
||||
if ($compounds) {
|
||||
foreach ($compounds as $compound) {
|
||||
$tax_amount = (($item_discounted_amount + $item_tax_total) / 100) * $compound->rate;
|
||||
|
||||
$item_tax_total += $tax_amount;
|
||||
|
||||
$item_taxes[] = [
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'tax_id' => $compound->id,
|
||||
'name' => $compound->name,
|
||||
'amount' => $tax_amount,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$bill_item = BillItem::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'item_id' => $item_id,
|
||||
'name' => Str::limit($this->request['name'], 180, ''),
|
||||
'quantity' => (double) $this->request['quantity'],
|
||||
'price' => (double) $this->request['price'],
|
||||
'tax' => $item_tax_total,
|
||||
'total' => $item_amount,
|
||||
]);
|
||||
|
||||
$bill_item->item_taxes = false;
|
||||
$bill_item->inclusives = false;
|
||||
$bill_item->compounds = false;
|
||||
|
||||
if (!empty($item_taxes)) {
|
||||
$bill_item->item_taxes = $item_taxes;
|
||||
$bill_item->inclusives = $inclusives;
|
||||
$bill_item->compounds = $compounds;
|
||||
|
||||
foreach ($item_taxes as $item_tax) {
|
||||
$item_tax['bill_item_id'] = $bill_item->id;
|
||||
|
||||
BillItemTax::create($item_tax);
|
||||
}
|
||||
}
|
||||
|
||||
return $bill_item;
|
||||
}
|
||||
}
|
37
app/Jobs/Purchase/DeleteBill.php
Normal file
37
app/Jobs/Purchase/DeleteBill.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Purchase;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Purchase\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/Purchase/DuplicateBill.php
Normal file
43
app/Jobs/Purchase/DuplicateBill.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Purchase;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Purchase\Bill;
|
||||
use App\Models\Purchase\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;
|
||||
}
|
||||
}
|
209
app/Jobs/Purchase/UpdateBill.php
Normal file
209
app/Jobs/Purchase/UpdateBill.php
Normal file
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Purchase;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Purchase\BillUpdated;
|
||||
use App\Events\Purchase\BillUpdating;
|
||||
use App\Models\Purchase\Bill;
|
||||
use App\Models\Purchase\BillTotal;
|
||||
use App\Traits\Currencies;
|
||||
use App\Traits\DateTime;
|
||||
use App\Traits\Relationships;
|
||||
|
||||
class UpdateBill extends Job
|
||||
{
|
||||
use Currencies, DateTime, Relationships;
|
||||
|
||||
protected $bill;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($bill, $request)
|
||||
{
|
||||
$this->bill = $bill;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Bill
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if (empty($this->request['amount'])) {
|
||||
$this->request['amount'] = 0;
|
||||
}
|
||||
|
||||
event(new BillUpdating($this->bill, $this->request));
|
||||
|
||||
// Upload attachment
|
||||
if ($this->request->file('attachment')) {
|
||||
$media = $this->getMedia($this->request->file('attachment'), 'bills');
|
||||
|
||||
$this->bill->attachMedia($media, 'attachment');
|
||||
}
|
||||
|
||||
$this->createItemsAndTotals();
|
||||
|
||||
$bill_paid = $this->bill->paid;
|
||||
|
||||
unset($this->bill->reconciled);
|
||||
|
||||
if (($bill_paid) && $this->request['amount'] > $bill_paid) {
|
||||
$this->request['bill_status_code'] = 'partial';
|
||||
}
|
||||
|
||||
$this->bill->update($this->request->input());
|
||||
|
||||
$this->bill->updateRecurring();
|
||||
|
||||
event(new BillUpdated($this->bill));
|
||||
|
||||
return $this->bill;
|
||||
}
|
||||
|
||||
protected function createItemsAndTotals()
|
||||
{
|
||||
// Create items
|
||||
list($sub_total, $taxes) = $this->createItems();
|
||||
|
||||
// Delete current totals
|
||||
$this->deleteRelationships($this->bill, 'totals');
|
||||
|
||||
$sort_order = 1;
|
||||
|
||||
// Add sub total
|
||||
BillTotal::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'code' => 'sub_total',
|
||||
'name' => 'bills.sub_total',
|
||||
'amount' => $sub_total,
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
|
||||
$this->request['amount'] += $sub_total;
|
||||
|
||||
$sort_order++;
|
||||
|
||||
// Add discount
|
||||
if (!empty($this->request['discount'])) {
|
||||
$discount_total = $sub_total * ($this->request['discount'] / 100);
|
||||
|
||||
BillTotal::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'code' => 'discount',
|
||||
'name' => 'bills.discount',
|
||||
'amount' => $discount_total,
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
|
||||
$this->request['amount'] -= $discount_total;
|
||||
|
||||
$sort_order++;
|
||||
}
|
||||
|
||||
// Add taxes
|
||||
if (!empty($taxes)) {
|
||||
foreach ($taxes as $tax) {
|
||||
BillTotal::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'code' => 'tax',
|
||||
'name' => $tax['name'],
|
||||
'amount' => $tax['amount'],
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
|
||||
$this->request['amount'] += $tax['amount'];
|
||||
|
||||
$sort_order++;
|
||||
}
|
||||
}
|
||||
|
||||
// Add extra totals, i.e. shipping fee
|
||||
if (!empty($this->request['totals'])) {
|
||||
foreach ($this->request['totals'] as $total) {
|
||||
$total['company_id'] = $this->bill->company_id;
|
||||
$total['bill_id'] = $this->bill->id;
|
||||
$total['sort_order'] = $sort_order;
|
||||
|
||||
if (empty($total['code'])) {
|
||||
$total['code'] = 'extra';
|
||||
}
|
||||
|
||||
BillTotal::create($total);
|
||||
|
||||
if (empty($total['operator']) || ($total['operator'] == 'addition')) {
|
||||
$this->request['amount'] += $total['amount'];
|
||||
} else {
|
||||
// subtraction
|
||||
$this->request['amount'] -= $total['amount'];
|
||||
}
|
||||
|
||||
$sort_order++;
|
||||
}
|
||||
}
|
||||
|
||||
// Add total
|
||||
BillTotal::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'code' => 'total',
|
||||
'name' => 'bills.total',
|
||||
'amount' => $this->request['amount'],
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function createItems()
|
||||
{
|
||||
$sub_total = 0;
|
||||
|
||||
$taxes = [];
|
||||
|
||||
if (empty($this->request['items'])) {
|
||||
return [$sub_total, $taxes];
|
||||
}
|
||||
|
||||
// Delete current items
|
||||
$this->deleteRelationships($this->bill, ['items', 'item_taxes']);
|
||||
|
||||
foreach ((array) $this->request['items'] as $item) {
|
||||
if (empty($item['discount'])) {
|
||||
$item['discount'] = !empty($this->request['discount']) ? !empty($this->request['discount']) : 0;
|
||||
}
|
||||
|
||||
$bill_item = $this->dispatch(new CreateBillItem($item, $this->bill));
|
||||
|
||||
// Calculate totals
|
||||
$sub_total += $bill_item->total;
|
||||
|
||||
if (!$bill_item->item_taxes) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set taxes
|
||||
foreach ((array) $bill_item->item_taxes as $item_tax) {
|
||||
if (array_key_exists($item_tax['tax_id'], $taxes)) {
|
||||
$taxes[$item_tax['tax_id']]['amount'] += $item_tax['amount'];
|
||||
} else {
|
||||
$taxes[$item_tax['tax_id']] = [
|
||||
'name' => $item_tax['name'],
|
||||
'amount' => $item_tax['amount']
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [$sub_total, $taxes];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user