Skip to content

Commit 5489a81

Browse files
committed
1 parent 8214aef commit 5489a81

34 files changed

+542
-173
lines changed

app/Events/PurchaseReceiptCreated.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Queue\SerializesModels;
7+
use App\Models\Purchases\PurchaseReceipt;
8+
use Illuminate\Broadcasting\PrivateChannel;
9+
use Illuminate\Broadcasting\PresenceChannel;
10+
use Illuminate\Foundation\Events\Dispatchable;
11+
use Illuminate\Broadcasting\InteractsWithSockets;
12+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
13+
14+
class PurchaseReceiptCreated
15+
{
16+
use Dispatchable, InteractsWithSockets, SerializesModels;
17+
18+
public $purchaseReceipt;
19+
20+
/**
21+
* Create a new event instance.
22+
*/
23+
public function __construct(PurchaseReceipt $purchaseReceipt)
24+
{
25+
$this->purchaseReceipt = $purchaseReceipt;
26+
}
27+
28+
/**
29+
* Get the channels the event should broadcast on.
30+
*
31+
* @return array<int, \Illuminate\Broadcasting\Channel>
32+
*/
33+
public function broadcastOn(): array
34+
{
35+
return [
36+
new PrivateChannel('channel-name'),
37+
];
38+
}
39+
}

app/Http/Controllers/Purchases/PurchasesController.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,22 @@
1313
use App\Models\Purchases\Purchases;
1414
use App\Services\SelectDataService;
1515
use App\Http\Controllers\Controller;
16-
use App\Services\PurchaseCalculatorService;
1716
use Illuminate\Support\Facades\Auth;
1817
use App\Models\Products\StockLocation;
1918
use App\Models\Purchases\PurchaseLines;
19+
use App\Models\Accounting\AccountingVat;
2020
use App\Models\Purchases\PurchaseInvoice;
2121
use App\Models\Purchases\PurchaseReceipt;
2222
use App\Models\Companies\CompaniesContacts;
23+
use App\Services\PurchaseCalculatorService;
2324
use App\Models\Companies\CompaniesAddresses;
2425
use App\Models\Purchases\PurchasesQuotation;
2526
use App\Models\Products\StockLocationProducts;
2627
use App\Models\Purchases\PurchaseReceiptLines;
2728
use App\Models\Purchases\PurchaseQuotationLines;
2829
use App\Http\Requests\Purchases\StorePurchaseRequest;
2930
use App\Http\Requests\Purchases\UpdatePurchaseRequest;
31+
use App\Http\Requests\Purchases\UpdatePurchaseInvoiceRequest;
3032
use App\Http\Requests\Purchases\UpdatePurchaseReceiptRequest;
3133
use App\Http\Requests\Purchases\UpdatePurchaseQuotationRequest;
3234

@@ -214,6 +216,8 @@ public function showPurchase(Purchases $id)
214216
$ContactSelect = CompaniesContacts::select('id', 'first_name','name')->where('companies_id', $id->companies_id)->get();
215217
$PurchaseCalculatorService = new PurchaseCalculatorService($id);
216218
$totalPrice = $PurchaseCalculatorService->getTotalPrice();
219+
$subPrice = $PurchaseCalculatorService->getSubTotal();
220+
$vatPrice = $PurchaseCalculatorService->getVatTotal();
217221
list($previousUrl, $nextUrl) = $this->getNextPrevious(new Purchases(), $id->id);
218222
$CustomFields = CustomField::where('custom_fields.related_type', '=', 'purchase')
219223
->leftJoin('custom_field_values as cfv', function($join) use ($id) {
@@ -230,6 +234,8 @@ public function showPurchase(Purchases $id)
230234
'AddressSelect' => $AddressSelect,
231235
'ContactSelect' => $ContactSelect,
232236
'totalPrices' => $totalPrice,
237+
'subPrice' => $subPrice,
238+
'vatPrice' => $vatPrice,
233239
'previousUrl' => $previousUrl,
234240
'nextUrl' => $nextUrl,
235241
'CustomFields' => $CustomFields,
@@ -254,13 +260,25 @@ public function storePurchaseOrder(Request $request, $id)
254260
else{
255261
$purchaseCode = "PU-". $LastPurchase->id;
256262
}
263+
264+
$defaultAddress = CompaniesAddresses::getDefault(['companies_id' => $purchaseData->companies_id]);
265+
$defaultContact = CompaniesContacts::getDefault(['companies_id' => $purchaseData->companies_id]);
266+
$AccountingVat = AccountingVat::getDefault();
267+
$defaultAddress = ($defaultAddress->id ?? 0);
268+
$defaultContact = ($defaultContact->id ?? 0);
269+
$AccountingVat = ($AccountingVat->id ?? 0);
270+
271+
if($defaultAddress == 0 || $defaultContact == 0 || $AccountingVat == 0){
272+
return redirect()->back()->with('error', 'No default settings');
273+
}
274+
257275
// Create order
258276
$PurchaseOrderCreated = Purchases::create([
259277
'code'=> $purchaseCode,
260278
'label'=> $purchaseCode,
261279
'companies_id'=>$purchaseData->companies_id,
262-
//'companies_contacts_id'
263-
//'companies_addresses_id'
280+
'companies_contacts_id' =>$defaultContact,
281+
'companies_addresses_id' =>$defaultAddress,
264282
//'statu' => defaut 1
265283
'user_id'=>Auth::id(),
266284
//'Comment' => defaut emtpy
@@ -300,7 +318,7 @@ public function storePurchaseOrder(Request $request, $id)
300318
//'receipt_qty' =>, defaut to 0
301319
//'invoiced_qty' =>, defaut to 0
302320
'methods_units_id' => $Task->methods_units_id,
303-
//'accounting_allocation_id' => , can be null
321+
'accounting_vats_id' => $AccountingVat,
304322
//'stock_locations_id' => , can be null
305323
'statu' => 1
306324
]);
@@ -378,7 +396,6 @@ public function updatePurchase(UpdatePurchaseRequest $request)
378396
{
379397
$Purchases = Purchases::find($request->id);
380398
$Purchases->label=$request->label;
381-
$Purchases->statu=$request->statu;
382399
$Purchases->companies_id=$request->companies_id;
383400
$Purchases->companies_contacts_id=$request->companies_contacts_id;
384401
$Purchases->companies_addresses_id=$request->companies_addresses_id;
@@ -423,6 +440,21 @@ public function updatePurchaseReceipt(UpdatePurchaseReceiptRequest $request)
423440
return redirect()->route('purchase.receipts.show', ['id' => $PurchaseReceipt->id])->with('success', 'Successfully updated reciept');
424441
}
425442

443+
/**
444+
* @param \Illuminate\Http\Request $request
445+
* @return \Illuminate\Http\RedirectResponse
446+
*/
447+
public function updatePurchaseInvoice(UpdatePurchaseInvoiceRequest $request)
448+
{
449+
$PurchaseInvoice = PurchaseInvoice::find($request->id);
450+
$PurchaseInvoice->label=$request->label;
451+
$PurchaseInvoice->statu=$request->statu;
452+
$PurchaseInvoice->comment=$request->comment;
453+
$PurchaseInvoice->save();
454+
455+
return redirect()->route('purchase.invoices.show', ['id' => $PurchaseInvoice->id])->with('success', 'Successfully updated reciept');
456+
}
457+
426458
/**
427459
* @return \Illuminate\Contracts\View\View
428460
*/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Purchases;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class UpdatePurchaseInvoiceRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
//
28+
'label'=>'required',
29+
];
30+
}
31+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Models\Purchases\Purchases;
6+
use App\Events\PurchaseReceiptCreated;
7+
use App\Models\Purchases\PurchaseLines;
8+
use Illuminate\Queue\InteractsWithQueue;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
use App\Models\Purchases\PurchaseReceiptLines;
11+
12+
class UpdatePurchaseStatus
13+
{
14+
/**
15+
* Create the event listener.
16+
*/
17+
public function __construct()
18+
{
19+
//
20+
}
21+
22+
/**
23+
* Handle the event.
24+
*/
25+
public function handle(PurchaseReceiptCreated $event)
26+
{
27+
// Retrieve the reception lines associated with the reception
28+
$purchaseReceiptLines = PurchaseReceiptLines::where('purchase_receipt_id', $event->purchaseReceipt->id)->get();
29+
30+
// For each receipt line, update the associated purchase
31+
foreach ($purchaseReceiptLines as $line) {
32+
$purchase = Purchases::find($line->purchaseLines->purchases_id);
33+
34+
if ($purchase) {
35+
// Retrieve all lines associated with this purchase order
36+
$purchaseLines = PurchaseLines::where('purchases_id', $purchase->id)->get();
37+
38+
// Check if all rows have 'receipt_qty' greater than or equal to 'qty'
39+
$allReceived = $purchaseLines->every(function ($purchaseLine) {
40+
return $purchaseLine->receipt_qty >= $purchaseLine->qty;
41+
});
42+
43+
// If all lines are received, update the purchase order status to 4
44+
if ($allReceived) {
45+
$purchase->statu = 4;
46+
} else {
47+
// If this is not the case, we make sure to set the status to 3
48+
$purchase->statu = 3;
49+
}
50+
51+
$purchase->save();
52+
}
53+
}
54+
}
55+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Livewire\ArrowSteps;
4+
5+
use App\Models\Purchases\Purchases;
6+
use Livewire\Component;
7+
8+
class ArrowPurchase extends Component
9+
{
10+
public $PurchaseId;
11+
public $PurchaseStatu;
12+
13+
public function mount($PurchaseId, $PurchaseStatu)
14+
{
15+
$this->PurchaseId = $PurchaseId;
16+
$this->PurchaseStatu = $PurchaseStatu;
17+
}
18+
19+
public function render()
20+
{
21+
return view('livewire.arrow-steps.arrow-purchase');
22+
}
23+
24+
public function changeStatu($statuNumber){
25+
try{
26+
Purchases::where('id',$this->PurchaseId)->update(['statu'=>$statuNumber]);
27+
return redirect()->route('purchases.show', ['id' => $this->PurchaseId])->with('success', 'Successfully updated statu');
28+
}catch(\Exception $e){
29+
session()->flash('error',"Something goes wrong on update statu");
30+
}
31+
}
32+
}

app/Livewire/PurchasesLinesIndex.php

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class PurchasesLinesIndex extends Component
2929
public $OrderType;
3030
public $status_id;
3131
public $OrderLineslist;
32-
public $order_lines_id, $orders_id, $ordre,$product_id, $methods_units_id, $selling_price, $accounting_vats_id, $delivery_date, $statu;
32+
public $purchase_lines_id, $ordre = 1,$product_id, $methods_units_id, $selling_price, $accounting_vats_id, $delivery_date, $statu;
3333
public $code='';
3434
public $label='';
3535
public $qty= 0;
@@ -44,6 +44,15 @@ class PurchasesLinesIndex extends Component
4444
public $TechProductList = [];
4545
public $BOMProductList = [];
4646

47+
// Validation Rules
48+
protected $rules = [
49+
'ordre' =>'required|numeric|gt:0',
50+
'label'=>'required',
51+
'qty'=>'required|min:1|not_in:0',
52+
'selling_price'=>'required|numeric|gt:0',
53+
'discount'=>'required',
54+
];
55+
4756
public function sortBy($field)
4857
{
4958
if ($this->sortField === $field) {
@@ -61,7 +70,7 @@ public function updatingSearch()
6170

6271
public function mount($purchase_id, $OrderStatu)
6372
{
64-
$this->orders_id = $purchase_id;
73+
$this->purchase_id = $purchase_id;
6574
$this->order_Statu = $OrderStatu;
6675
$this->Factory = Factory::first();
6776
$this->status_id = Status::select('id')->orderBy('order')->first();
@@ -101,12 +110,18 @@ public function storeOrderLine(){
101110
'selling_price'=>'required|numeric|gt:0',
102111
'discount'=>'required',
103112
'methods_units_id'=>'required',
104-
'accounting_vats_id'=>'required',
105113
]);
114+
115+
$AccountingVat = AccountingVat::getDefault();
116+
$AccountingVat = ($AccountingVat->id ?? 0);
117+
118+
if($AccountingVat == 0){
119+
return redirect()->route('purchases.show', ['id' => $this->purchase_id])->with('error', 'No default settings');
120+
}
106121

107122
// Create Line
108123
$NewPurchaseLines = PurchaseLines::create([
109-
'purchases_id'=>$this->orders_id,
124+
'purchases_id'=>$this->purchase_id,
110125
'ordre'=>$this->ordre,
111126
'code'=>$this->code,
112127
'product_id'=>$this->product_id,
@@ -115,7 +130,7 @@ public function storeOrderLine(){
115130
'selling_price'=>$this->selling_price,
116131
'discount'=>$this->discount,
117132
'methods_units_id'=>$this->methods_units_id,
118-
'accounting_vats_id'=>$this->accounting_vats_id,
133+
'accounting_vats_id'=>$AccountingVat,
119134
]);
120135

121136
// Set Flash Message
@@ -146,4 +161,41 @@ public function resetFields(){
146161
$this->product_id = '';
147162
$this->label = '';
148163
}
164+
165+
public function editPurchaseLine($id){
166+
$Line = PurchaseLines::findOrFail($id);
167+
$this->purchase_lines_id = $id;
168+
$this->ordre = $Line->ordre;
169+
$this->code = $Line->code;
170+
$this->product_id = $Line->product_id;
171+
$this->label = $Line->label;
172+
$this->qty = $Line->qty;
173+
$this->methods_units_id = $Line->methods_units_id;
174+
$this->selling_price = $Line->selling_price;
175+
$this->discount = $Line->discount;
176+
$this->accounting_vats_id = $Line->accounting_vats_id;
177+
$this->delivery_date = $Line->delivery_date;
178+
$this->statu = $Line->statu;
179+
$this->updateLines = true;
180+
}
181+
182+
public function updatePurchaseLine(){
183+
// Validate request
184+
$this->validate();
185+
// Update line
186+
PurchaseLines::find($this->purchase_lines_id)->fill([
187+
'ordre'=>$this->ordre,
188+
'code'=>$this->code,
189+
'product_id'=>$this->product_id,
190+
'label'=>$this->label,
191+
'qty'=>$this->qty,
192+
'methods_units_id'=>$this->methods_units_id,
193+
'selling_price'=>$this->selling_price,
194+
'discount'=>$this->discount,
195+
'accounting_vats_id'=>$this->accounting_vats_id,
196+
'delivery_date'=>$this->delivery_date,
197+
'statu'=>$this->statu,
198+
])->save();
199+
session()->flash('success','Line Updated Successfully');
200+
}
149201
}

app/Livewire/PurchasesReceiptIndex.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class PurchasesReceiptIndex extends Component
2020
public $label;
2121
public $customer_reference;
2222
public $companies_id;
23-
public $companies_contacts_id;
24-
public $companies_addresses_id;
2523
public $statu;
2624
public $user_id;
2725
public $comment;

0 commit comments

Comments
 (0)