Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.28 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Classes\DTO\DTO;
  6. use App\Classes\IssuanceHelper;
  7. use App\Form;
  8. use App\GeneralLedger;
  9. use App\LineItem;
  10. use App\OrderItem;
  11. use App\Product;
  12. use App\ProductLine;
  13. use App\PurchaseOrder;
  14. use App\Quote;
  15. use App\Requisition;
  16. use App\Role;
  17. use App\StatusLog;
  18. use App\Supplier;
  19. use App\UnitOfMeasure;
  20. use App\User;
  21. use App\Variant;
  22. use Carbon\Carbon;
  23. use function foo\func;
  24. use Illuminate\Http\Request;
  25. use Illuminate\Support\Facades\DB;
  26. use Illuminate\Support\Facades\Storage;
  27. use Maatwebsite\Excel\Facades\Excel;
  28. use SebastianBergmann\Diff\Line;
  29.  
  30. class PurchaseOrderController extends Controller
  31. {
  32. static private $series;
  33. /**
  34. * Instantiate a new controller instance.
  35. *
  36. * @return void
  37. */
  38. function __construct()
  39. {
  40. $this->middleware('auth');
  41. }
  42.  
  43. /**
  44. * Display a listing of the resource.
  45. *
  46. * @return \Illuminate\Http\Response
  47. */
  48. public function index()
  49. {
  50. return view('purchasing.purchase-orders.index');
  51. }
  52.  
  53. /**
  54. * Display a listing of the resource.
  55. *
  56. * @return \Illuminate\Http\Response
  57. */
  58. public function showItemsReadyForPO()
  59. {
  60. return view('purchasing.purchase-orders.orderitems');
  61. }
  62.  
  63. /**
  64. * Show the form for creating a new resource.
  65. *
  66. * @return \Illuminate\Http\Response
  67. */
  68. public function create($id)
  69. {
  70. $data = PurchaseOrder::where('OrderNumber','=',$id)->first();
  71. return view('purchasing.purchase-orders.create',['data'=>$data]);
  72. }
  73.  
  74. /**
  75. * Store a newly created resource in storage.
  76. *
  77. * @param \Illuminate\Http\Request $request
  78. * @return \Illuminate\Http\Response
  79. */
  80. public function store(Request $request)
  81. {
  82. $data = array();
  83. // TODO: Create PO for each vendor.
  84.  
  85. //
  86. // self::$series = 0;
  87. //
  88. // $today = Carbon::today();
  89. // $latestPO = new PurchaseOrder();
  90. // $latestPO = $latestPO->orderBy('Series', 'desc')->first();
  91. //
  92. // if($latestPO && strpos($latestPO->OrderNumber,$today->format('my'))!==false) {
  93. // self::$series = $latestPO->Series + 1;
  94. // } else {
  95. // self::$series = 1;
  96. // }
  97. //
  98. //
  99. // foreach($request->OrderItem as $item) {
  100. //
  101. // $orderItem = new OrderItem();
  102. // $orderItem = $orderItem->where('ID','=',$item)->firstOrFail();
  103. //
  104. //
  105. // $quote = $orderItem->SelectedQuote();
  106. // $supplier = $quote->Supplier();
  107. // $requisition = $orderItem->Requisition();
  108. // $lineItem = $orderItem->LineItem();
  109. //
  110. //
  111. // $data[$supplier->ID][$requisition->OrderNumber] = array();
  112. //
  113. // array_push($data[$supplier->ID][$requisition->OrderNumber], $orderItem);
  114. //
  115. // if(isset($data[$supplier->ID][$requisition->OrderNumber]['Counter'])) {
  116. // $data[$supplier->ID][$requisition->OrderNumber]['Counter']++;
  117. // } else {
  118. // $data[$supplier->ID][$requisition->OrderNumber]['Counter'] = 1;
  119. // }
  120. //
  121. //
  122. // $lineItem->Ordered = true; // ordered
  123. // //$lineItem->save();
  124. //
  125. //
  126. //
  127. // DB::transaction(function () use ($request, $item) {
  128. // $today = Carbon::today();
  129. // $po = new PurchaseOrder();
  130. // $po->OrderNumber = sprintf("%s%s",
  131. //// $category,
  132. //// $supplier->SupplierType==1?"PH":"US",
  133. // $today->format('my'),
  134. // str_pad(self::$series,3,'0',STR_PAD_LEFT)
  135. // );
  136. // $po->ChargeNo = $item;
  137. // $po->ChargeType = 'S';
  138. // $po->Series = self::$series;
  139. // $po->Supplier = $supplier->ID;
  140. // $po->OrderDate = $today;
  141. // $po->DeliveryDate = $today->addDays($supplier->SupplierType==1?14:90);
  142. // $po->PaymentTerm = 0;
  143. // $po->Total = 0;
  144. // $po->APAccount = 0;
  145. // $po->Status = 'D'; // draft
  146. //
  147. //
  148. // $po->Total += ($quote->Amount * $lineItem->Quantity);
  149. // }
  150. //
  151. // }
  152.  
  153.  
  154.  
  155. // Step 1: Identify suppliers to create PO to.
  156. foreach($request->SelectedItems as $selectedItem) {
  157. $requisition = new Requisition();
  158. $quote = new Quote();
  159.  
  160. $entry = explode('-',$selectedItem);
  161. $requisitionField = $entry[1];
  162. $quoteField = $entry[2];
  163.  
  164. $quote = $quote->where('ID','=',$quoteField)->first();
  165. $requisition = $requisition->where('ID','=',$requisitionField)->first();
  166.  
  167. $supplier = $quote->Supplier();
  168.  
  169. $data[$supplier->ID][$requisition->OrderNumber] = array();
  170. }
  171.  
  172.  
  173. // Step 2: Assign each OrderItem for each created PO.
  174. // foreach($request->SelectedItems as $selectedItem) {
  175. for($i=0;$i<count($request->SelectedItems);$i++) {
  176. $quote = new Quote();
  177. $lineItem = new LineItem();
  178. $requisition = new Requisition();
  179.  
  180. $selectedItem = $request->SelectedItems[$i];
  181. $entry = explode('-', $selectedItem);
  182. $lineItemField = $entry[0];
  183. $requisitionField = $entry[1];
  184. $quoteField = $entry[2];
  185.  
  186. $quote = $quote->where('ID','=',$quoteField)->first();
  187. $lineItem = $lineItem->where('ID','=',$lineItemField)->first();
  188. $requisition = $requisition->where('ID','=',$requisitionField)->first();
  189.  
  190. $supplier = $quote->Supplier();
  191.  
  192. $sub = array();
  193.  
  194. $sub['Requisition'] = $requisition->ID;
  195. $sub['LineItem'] = $lineItem->ID;
  196. $sub['Quote'] = $quote->ID;
  197. $sub['OrderItem'] = (int)$request['OrderItem'][$i];
  198.  
  199. array_push($data[$supplier->ID][$requisition->OrderNumber], $sub);
  200.  
  201. $data[$supplier->ID][$requisition->OrderNumber]['Supplier'] = $supplier->ID;
  202. $data[$supplier->ID][$requisition->OrderNumber]['OrderNumber'] = $requisition->OrderNumber;
  203.  
  204.  
  205. if(isset($data[$supplier->ID][$requisition->OrderNumber]['Counter'])) {
  206. $data[$supplier->ID][$requisition->OrderNumber]['Counter']++;
  207. } else {
  208. $data[$supplier->ID][$requisition->OrderNumber]['Counter'] = 1;
  209. }
  210. }
  211.  
  212. self::$series = 0;
  213.  
  214. $today = Carbon::today();
  215. $latestPO = new PurchaseOrder();
  216. $latestPO = $latestPO->orderBy('created_at')->get()->last();
  217.  
  218. if($latestPO && strpos($latestPO->OrderNumber,$today->format('my'))!==false) {
  219. self::$series = $latestPO->Series + 1;
  220. } else {
  221. self::$series = 1;
  222. }
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229. if(count(PurchaseOrder::orderByDesc('created_at')->get())>1){
  230. $lastReceipt = PurchaseOrder::orderByDesc('created_at')->orderByDesc('OrderNumber')->first();
  231. }
  232. else {
  233. $lastReceipt = PurchaseOrder::orderByDesc('created_at')->first();
  234. }
  235.  
  236. if($lastReceipt) {
  237. $length = strlen($lastReceipt->OrderNumber);
  238.  
  239. $prefix = Carbon::today()->format('my');
  240. $currentMonth = substr($lastReceipt->OrderNumber,$length-7,4);
  241.  
  242. if($prefix==$currentMonth) {
  243. $current = substr($lastReceipt->OrderNumber, $length-3);
  244. }
  245. else {
  246. $current = 0;
  247. }
  248. }
  249. else {
  250. $current = 0;
  251. }
  252. // Step 3: Create PO
  253. foreach($data as $entry) {
  254. // entry = vendor.
  255. $current++;
  256.  
  257. DB::transaction(function () use ($request, $entry, $current) {
  258. foreach($entry as $item) {
  259. $supplier = new Supplier();
  260. $supplier = $supplier->where('ID','=',$item['Supplier'])->first();
  261. // $category = $supplier->Category=='L'?"LO":"FO";
  262.  
  263.  
  264. $today = Carbon::today();
  265. $po = new PurchaseOrder();
  266. $po->OrderNumber = sprintf("%s%s",
  267. // $category,
  268. // $supplier->SupplierType==1?"PH":"US",
  269. $today->format('my'),
  270. str_pad($current,3,'0',STR_PAD_LEFT)
  271. );
  272. $po->ChargeNo = $item['OrderNumber'];
  273. $po->ChargeType = 'S';
  274. $po->Series = self::$series;
  275. $po->Supplier = $supplier->ID;
  276. $po->OrderDate = $today;
  277. $po->DeliveryDate = $today->addDays($supplier->SupplierType==1?14:90);
  278. $po->PaymentTerm = 0;
  279. $po->Total = 0;
  280. $po->APAccount = 0;
  281. $po->Status = 'D'; // draft
  282.  
  283. $remarks = array();
  284. $remark = array('userid'=>auth()->user()->ID, 'message'=>"Draft Created.", 'time'=>Carbon::now()->toDateTimeString());
  285. array_push($remarks, $remark);
  286. $po->Remarks = json_encode(['data'=>$remarks]);
  287.  
  288. $po->save();
  289. usleep( 1000 );
  290.  
  291. self::$series++;
  292.  
  293. for ($i = 0; $i < $item['Counter']; $i++) {
  294. $orderItem = new OrderItem();
  295. $orderItem = $orderItem->where('ID','=',$item[$i]['OrderItem'])->firstOrFail();
  296. $orderItem->PurchaseOrder = $po->ID;
  297. //$orderItem->Requisition = $item[$i]['Requisition'];
  298. //$orderItem->LineItem = $item[$i]['LineItem'];
  299. //$orderItem->Quote = $item[$i]['Quote'];
  300. $orderItem->save();
  301.  
  302. echo "<pre>".$orderItem."</pre><br/>";
  303.  
  304. $lineItem = new LineItem();
  305. $lineItem = $lineItem->where('ID','=',$item[$i]['LineItem'])->first();
  306. $lineItem->Ordered = true; // ordered
  307. $lineItem->save();
  308.  
  309. $quote = new Quote();
  310. $quote = $quote->where('ID','=',$item[$i]['Quote'])->first();
  311. $po->Total += ($quote->Amount * $lineItem->Quantity);
  312. }
  313.  
  314. $po->save();
  315.  
  316.  
  317. $log = new StatusLog();
  318. $log->OrderNumber = $po->OrderNumber;
  319. $log->TransactionType = 'PO';
  320. $log->LogType = 'D';
  321. $log->save();
  322.  
  323. // ** Step 4: Send Mail **
  324. /*
  325. * 1. Send mail to Creator
  326. * 2. Send mail to PurchasingManager
  327. * 3. Send mail to OM
  328. * 4. Send mail to PM
  329. * 5. Send mail to GM
  330. *
  331. */
  332.  
  333. }
  334.  
  335.  
  336.  
  337. });
  338. }
  339. return redirect()->to('/purchase-order');
  340. }
  341.  
  342. /**
  343. * Display the specified resource.
  344. *
  345. * @param \App\PurchaseOrder $purchaseOrder
  346. * @return \Illuminate\Http\Response
  347. */
  348. public function show($purchaseOrder)
  349. {
  350. $po = PurchaseOrder::where('OrderNumber','=',$purchaseOrder)->first();
  351. return view('purchasing.purchase-orders.view', ['data'=>$po]);
  352. }
  353.  
  354. /**
  355. * Show the form for editing the specified resource.
  356. *
  357. * @param \App\PurchaseOrder $purchaseOrder
  358. * @return \Illuminate\Http\Response
  359. */
  360. public function edit(PurchaseOrder $purchaseOrder)
  361. {
  362. //
  363. }
  364.  
  365. /**
  366. * Update the specified resource in storage.
  367. *
  368. * @param \Illuminate\Http\Request $request
  369. * @param \App\PurchaseOrder $purchaseOrder
  370. * @return \Illuminate\Http\Response
  371. */
  372. public function update(Request $request, $purchaseOrder) {
  373. try{
  374. $po = new PurchaseOrder();
  375. $po = $po->where('OrderNumber','=',$purchaseOrder)->first();
  376.  
  377. if(isset($request->APAccount)) {
  378. $po->APAccount = $request->APAccount;
  379. }
  380. if(isset($request->Priority)) {
  381. $po->Priority = $request->Priority;
  382. }
  383. if(isset($request->PaymentTerm)) {
  384. $po->PaymentTerm = $request->PaymentTerm;
  385. }
  386. if(isset($request->ChargeType)) {
  387. $po->ChargeType = $request->ChargeType;
  388. }
  389. if(isset($request->ProductLine)) {
  390. $po->ProductLine = $request->ProductLine;
  391. }
  392. if(isset($request->Remarks)) {
  393. $remarks = json_decode($po->Remarks, true);
  394. $remark = array('userid'=>auth()->user()->ID, 'message'=>$request->Remarks, 'time'=>Carbon::now()->toDateTimeString());
  395. array_push($remarks['data'], $remark);
  396. $po->Remarks = json_encode($remarks);
  397. }
  398. $po->save();
  399. return response()->json(['success'=>1,'message'=>"Successfully updated Purchase Order Draft # $po->OrderNumber."]);
  400. }catch(\Exception $exception) {
  401. return response()->json(['success'=>0,'message'=>"Failed to update Purchase Order."]);
  402. }
  403. }
  404.  
  405.  
  406. /**
  407. * Submit the PurchaseOrder.
  408. *
  409. * @param Request $request
  410. * @param \App\PurchaseOrder $purchaseOrder
  411. *
  412. * @return \Illuminate\Http\Response
  413. */
  414. public function submit(Request $request, $purchaseOrder) {
  415. try{
  416. $today = Carbon::today();
  417.  
  418. // Step 1. Load Purchase Order
  419. $po = new PurchaseOrder();
  420. $po = $po->where('OrderNumber','=',$purchaseOrder)->first();
  421.  
  422. // Step 2. Do routine update.
  423. $po->APAccount = $request->APAccount;
  424. // $po->Priority = $request->Priority;
  425. $po->PaymentTerm = $request->PaymentTerm;
  426. $po->ChargeType = $request->ChargeType;
  427. $po->ProductLine = 9;
  428.  
  429. if($po->ChargeType=='S') { // stock,restock, basta existing na to.
  430. $productLine = new ProductLine();
  431. $productLine = $productLine->where('Identifier','=','XX')->firstOrFail();
  432. $po->ProductLine = $productLine->ID;
  433. }
  434. else {
  435.  
  436. }
  437.  
  438.  
  439.  
  440. // Step 3. Update PO # to conform with completed data.
  441.  
  442. $supplier = $po->Supplier();
  443.  
  444. if($supplier->Classification=='F') {
  445. $category = "FO";
  446. } else {
  447. $category = "LO";
  448. }
  449.  
  450. $currency = $po->Supplier()->Currency()->Code;
  451. $currency = substr($currency,0,2);
  452.  
  453.  
  454. $tempOrderNumber = $po->OrderNumber;
  455.  
  456. // move logs
  457. $statusLog = new StatusLog();
  458. $logs = $statusLog->where('OrderNumber','=',$tempOrderNumber);
  459.  
  460.  
  461.  
  462. $po->OrderNumber = sprintf("%s%s%s%s",
  463. $category,
  464. $currency,
  465. $po->ProductLine()->Identifier,
  466. $tempOrderNumber
  467. );
  468.  
  469.  
  470.  
  471. // Step 4. Set Status to P. Pending Purchasing Manager's approval.
  472. $po->Status = 'P'; // pending purchasing manager's approval.
  473. $po->save();
  474.  
  475.  
  476. // Step 2. Send email to creator and to Purchasing Manager.
  477. session()->flash(1,['success'=>1,'message'=>"Successfully updated Purchase Order Draft # $po->OrderNumber."]);
  478. return redirect()->to("/purchase-order/view/$po->OrderNumber");
  479. }catch(\Exception $exception) {
  480. dd($exception);
  481. session()->flash(0,['success'=>0,'message'=>"Failed to update Purchase Order."]);
  482. return back()->withInput();
  483. }
  484. }
  485.  
  486.  
  487. /**
  488. * Remove the specified resource from storage.
  489. *
  490. * @param \App\PurchaseOrder $purchaseOrder
  491. * @return \Illuminate\Http\Response
  492. */
  493. public function destroy(PurchaseOrder $purchaseOrder)
  494. {
  495. //
  496. }
  497.  
  498. /**
  499. * Fetches all available data.
  500. *
  501. * @return Json
  502. */
  503. public function getPurchaseOrderData($status)
  504. {
  505. $data = array();
  506. $order = new PurchaseOrder();
  507. if($status==='Z') {
  508. $orders = $order->all();
  509. }
  510. else {
  511. if($status=='P') {
  512. $orders = $order
  513. ->where('Status','=','P')
  514. ->orWhere('Status','=',1)
  515. ->orWhere('Status','=',2)
  516. ->orWhere('Status','=',3)
  517. ->get();
  518. }
  519. else {
  520. $orders = $order->where('Status','=',$status)->get();
  521. }
  522. }
  523. foreach($orders as $order)
  524. {
  525. // dd($order->OrderItems()[0]->);
  526. $entry = array();
  527. $entry['OrderNumber'] = $order->OrderNumber;
  528. $entry['Supplier'] = $order->Supplier()->Name;
  529. $entry['OrderDate'] = $order->OrderDate->format('F d, Y');
  530. // $entry['DeliveryDate'] = $order->DeliveryDate->format('F d, Y');
  531. $entry['Total'] = ($order->Supplier()->SupplierType==1?'PHP':'USD').' '.number_format($order->Total,2,'.',',');
  532. $entry['Status'] = IssuanceHelper::ParsePurchaseOrderStatus($order->Status);
  533. array_push($data, $entry);
  534. }
  535. return response()->json(['aaData'=>$data]);
  536. }
  537.  
  538. public function showItemsForQuotation() {
  539. return view('purchasing.purchase-orders.forquote');
  540. }
  541.  
  542. public function getOrderItemData()
  543. {
  544. $data = array();
  545.  
  546. $rs = new Requisition();
  547.  
  548. $pr = $rs->where([
  549. ['Status', '!=', 'A'],
  550. ['Type', '=', 'PR']
  551. ])->get();
  552.  
  553.  
  554. foreach ($pr as $request) {
  555. $entry = array();
  556.  
  557. $entry['ID'] = $request->ID;
  558. $entry['PRNumber'] = $request->OrderNumber;
  559. $entry['RequiredBy'] = $request->Date->format('F d, Y');
  560. foreach ($request->LineItems() as $lineItem) {
  561. if(!$lineItem->Ordered) {
  562.  
  563. $product = $lineItem->Product();
  564. $validQuotes = count($product->ValidQuotes());
  565.  
  566. if($validQuotes < 5 && !$lineItem->Quoted) {
  567. $entry['QuoteCount'] = $validQuotes;
  568. $entry['Quantity'] = $lineItem->Quantity;
  569. $entry['LineItemID'] = $lineItem->ID;
  570. $entry['UniqueID'] = $product->UniqueID;
  571. $entry['Product'] = sprintf('[%s] %s', $product->Name, $product->Description);
  572.  
  573. array_push($data, $entry);
  574. }
  575. }
  576. }
  577. }
  578.  
  579. return response()->json(['data'=>$data]);
  580. }
  581.  
  582. public function getItemsForQuotationApproval() {
  583. $data = array();
  584.  
  585. $rs = new Requisition();
  586.  
  587. $pr = $rs->where([
  588. // ['Status', '=', '2'],
  589. ['Type', '=', 'PR']
  590. ])->get();
  591.  
  592.  
  593. foreach ($pr as $request) {
  594. $entry = array();
  595.  
  596. $entry['ID'] = $request->ID;
  597. $entry['PRNumber'] = $request->OrderNumber;
  598. $entry['RequiredBy'] = $request->Date->format('F d, Y');
  599. foreach ($request->LineItems() as $lineItem) {
  600. if(!$lineItem->Ordered && !$lineItem->Quoted) {
  601. $entry['Quantity'] = $lineItem->Quantity;
  602. $entry['LineItemID'] = $lineItem->ID;
  603. $entry['UniqueID'] = $lineItem->Product()->UniqueID;
  604. $entry['Product'] = sprintf('[%s] %s', $lineItem->Product()->Name, $lineItem->Product()->Description);
  605.  
  606. if($preferredQuote = $lineItem->Product()->PreferredQuote()) {
  607. $entry['Quote'] = sprintf('%s %s', $preferredQuote->Currency()->Code, number_format($preferredQuote->Amount,2,'.',','));
  608. $entry['QuoteID'] = $preferredQuote->ID;
  609. $entry['Currency'] = $preferredQuote->Currency()->Code;
  610. $entry['TotalPrice'] = sprintf('%s %s', $preferredQuote->Currency()->Code, number_format($preferredQuote->Amount * $lineItem->Quantity,2,'.',','));
  611. array_push($data, $entry);
  612. }
  613. }
  614. }
  615. }
  616.  
  617. return response()->json(['data'=>$data]);
  618. }
  619.  
  620.  
  621. public function getOrderItemsReadyForPO() {
  622. $data = array();
  623. $requisition = new Requisition();
  624. $lineItem = new LineItem();
  625.  
  626. $orderItem = new OrderItem();
  627. $orderItems = $orderItem->where('PurchaseOrder','=',0)->get();
  628. foreach($orderItems as $orderItem) {
  629. if($orderItem->PurchaseOrder == 0) {
  630. $lineItem = $orderItem->LineItem();
  631. $product = $lineItem->Product();
  632.  
  633. $pr = $requisition->where('OrderNumber','=',$lineItem->OrderNumber)->firstOrFail();
  634. if($pr->Status == 'A') {
  635. $quote = $orderItem->SelectedQuote();
  636.  
  637. $entry = array();
  638. $entry['ID'] = $pr->ID;
  639. $entry['OrderItemID'] = $orderItem->ID;
  640. $entry['PRNumber'] = $lineItem->OrderNumber;
  641. $entry['RequiredBy'] = $pr->Date->format('F d, Y');
  642. $entry['Quantity'] = $lineItem->Quantity;
  643. $entry['LineItemID'] = $lineItem->ID;
  644. $entry['UniqueID'] = $product->UniqueID;
  645. $entry['Product'] = sprintf('[%s] %s', $product->Name, $product->Description);
  646. $entry['Supplier'] = $quote->Supplier()->Name;
  647. $entry['Quote'] = sprintf('%s %s', $quote->Currency()->Code, number_format($quote->Amount,2,'.',','));
  648. $entry['QuoteID'] = $quote->ID;
  649. $entry['Currency'] = $quote->Currency()->Code;
  650. $entry['TotalPrice'] = sprintf('%s %s', $quote->Currency()->Code, number_format($quote->Amount * $lineItem->Quantity,2,'.',','));
  651. array_push($data, $entry);
  652. }
  653. }
  654. }
  655. return response()->json(['data'=>$data]);
  656. }
  657.  
  658. public function productquoteview($lineItem) {
  659. $lineItem = LineItem::where('ID','=',$lineItem)->first();
  660. return view('templates.orderitem-quote',['lineItem'=>$lineItem]);
  661. }
  662.  
  663. public function toggle($po)
  664. {
  665. $po = PurchaseOrder::where('OrderNumber','=',$po)->first();
  666. $po->Status = $po->Status=='P'?'A':'P';
  667. $po->save();
  668.  
  669. return redirect()->back();
  670. }
  671.  
  672. public function approve(Request $request, $po) {
  673. $po = PurchaseOrder::where('OrderNumber','=',$po)->first();
  674. $user = auth()->user();
  675. if($user->isPurchasingManager()) {
  676. $po->Status = '1';
  677. $po->PurchasingManager = Carbon::today();
  678.  
  679. $log = new StatusLog();
  680. $log->OrderNumber = $po->OrderNumber;
  681. $log->TransactionType = 'PO';
  682. $log->LogType = '1';
  683. $log->save();
  684. }
  685. if($user->isOperationsDirector()) {
  686. $po->Status = '2';
  687. $po->OperationsManager = Carbon::today();
  688.  
  689. $log = new StatusLog();
  690. $log->OrderNumber = $po->OrderNumber;
  691. $log->TransactionType = 'PO';
  692. $log->LogType = '2';
  693. $log->save();
  694. }
  695. if($user->isPlantManager()) {
  696. $po->Status = '3';
  697. $po->PlantManager = Carbon::today();
  698.  
  699. $log = new StatusLog();
  700. $log->OrderNumber = $po->OrderNumber;
  701. $log->TransactionType = 'PO';
  702. $log->LogType = '3';
  703. $log->save();
  704. }
  705. if($user->isGeneralManager()) {
  706. $po->Status = 'A';
  707. $po->GeneralManager = Carbon::today();
  708.  
  709. $log = new StatusLog();
  710. $log->OrderNumber = $po->OrderNumber;
  711. $log->TransactionType = 'PO';
  712. $log->LogType = 'A';
  713. $log->save();
  714. }
  715.  
  716. $remarks = json_decode($po->Remarks, true);
  717. $remark = array('userid'=>auth()->user()->ID, 'message'=>$request->Remarks, 'time'=>Carbon::now()->toDateTimeString());
  718. array_push($remarks['data'], $remark);
  719. $po->Remarks = json_encode($remarks);
  720.  
  721. $po->save();
  722.  
  723. // send mail.
  724. return redirect()->back();
  725. }
  726.  
  727.  
  728. public function reject(Request $request, $purchaseOrder) {
  729. $po = PurchaseOrder::where('OrderNumber','=',$purchaseOrder)->first();
  730. $po->Status = 'X';
  731.  
  732. $remarks = json_decode($po->Remarks, true);
  733. $remark = array('userid'=>auth()->user()->ID, 'message'=>$request->Remarks, 'time'=>Carbon::now()->toDateTimeString());
  734. array_push($remarks['data'], $remark);
  735. $po->Remarks = json_encode($remarks);
  736.  
  737. $po->save();
  738.  
  739. $log = new StatusLog();
  740. $log->OrderNumber = $po->OrderNumber;
  741. $log->TransactionType = 'PO';
  742. $log->LogType = 'X';
  743. $log->save();
  744.  
  745.  
  746. //TODO: Send email
  747.  
  748. return redirect()->back();
  749. }
  750.  
  751. public function void($po)
  752. {
  753. $po = PurchaseOrder::where('OrderNumber','=',$po)->first();
  754. $po->Status = 'V';
  755. $po->save();
  756.  
  757. return redirect()->back();
  758. }
  759.  
  760.  
  761. public function getSelectDataForRR(Request $request) {
  762. $po = new PurchaseOrder();
  763.  
  764. $data = array();
  765. if($request->q) {
  766. $poForRR = $po
  767. ->where('Status','=','A')
  768. ->orWhere('OrderNumber','LIKE','%'.$request->q.'%')
  769. ->get(); // Approved lang pwede.
  770. for($i=0;$i<count($poForRR);$i++){
  771. $entry['id'] = $poForRR[$i]->ID;
  772. $entry['text'] = $poForRR[$i]->OrderNumber;
  773. array_push($data, $entry);
  774. }
  775. } else {
  776. $poForRR = $po->where('Status','=','A')->get(); // Approved lang pwede.
  777. for($i=0;$i<count($poForRR);$i++){
  778. $entry['id'] = $poForRR[$i]->ID;
  779. $entry['text'] = $poForRR[$i]->OrderNumber;
  780. array_push($data, $entry);
  781. }
  782. }
  783. return response()->json(['results'=>$data]);
  784. }
  785.  
  786. public function getOrderItemsForRR($id) {
  787. $po = new OrderItem();
  788. $items = $po->where('PurchaseOrder','=',$id)->get();
  789.  
  790. $data = array();
  791. if(count($items)>0) {
  792. foreach($items as $item) {
  793. $lineItem = $item->LineItem();
  794. $product = $lineItem->Product();
  795.  
  796. $entry = array();
  797. $entry['ID'] = $item->ID;
  798. $entry['Description'] = $product->Description;
  799. $entry['Quantity'] = $lineItem->Quantity;
  800. $entry['UOM'] = $product->UOM()->Abbreviation;
  801. $entry['Receivable'] = $lineItem->getRemainingDeliverableQuantity();
  802. array_push($data, $entry);
  803. }
  804. } else {
  805. }
  806.  
  807. return response()->json(['data'=>$data]);
  808. }
  809.  
  810. public function generatePurchaseOrderForm($orderNumber) {
  811. $purchaseOrder = new PurchaseOrder();
  812.  
  813. try{
  814. $po = $purchaseOrder->where('OrderNumber', '=',$orderNumber)->first();
  815. if($po) {
  816. $file = Storage::url('/app/template/po.xlsx');
  817.  
  818.  
  819. // $user = User::where('ID','=',$po->Requester)->first();
  820. $supplier = $po->Supplier();
  821.  
  822. $address = array(
  823. $supplier->AddressLine1,
  824. $supplier->AddressLine2,
  825. $supplier->City,
  826. $supplier->State,
  827. $supplier->Zip && $supplier->Country?$supplier->Zip." ".$supplier->Country:""
  828. );
  829.  
  830.  
  831.  
  832. $dto = new DTO();
  833. $dto->Address = array_filter($address);
  834.  
  835. $dto->VendorName = $supplier->Name;
  836. $dto->PurchaseOrderNumber = $po->OrderNumber;
  837. $dto->DateIssued = Carbon::today()->format('n/j/y');
  838. $dto->VendorID = $supplier->Code;
  839. $dto->DeliveryDate = $po->DeliveryDate->format('n/j/y');
  840. $dto->Terms = $po->PaymentTerm()->Description;
  841. $dto->Currency = $po->Supplier()->Currency()->Code;
  842. $dto->OrderItems = $po->OrderItems();
  843.  
  844. $purchaseRequest = null;
  845. $counter = 21;
  846. $total = 0;
  847. foreach($po->OrderItems() as $orderItem) {
  848. $lineItem = $orderItem->LineItem();
  849. $purchaseRequest = $orderItem->Requisition();
  850. // $data["A$counter"] = $lineItem->Quantity;
  851. // $data["B$counter"] = $lineItem->Product()->UOM()->Abbreviation;
  852. // $data["C$counter"] = $lineItem->Product()->Description;
  853. // $data["J$counter"] = $orderItem->SelectedQuote()->Amount;
  854. // $data["M$counter"] = $orderItem->SelectedQuote()->Amount * $lineItem->Quantity;
  855. $total += $orderItem->SelectedQuote()->Amount * $lineItem->Quantity;
  856.  
  857. $counter++;
  858. }
  859. //
  860. // $data["C$counter"] = "X X X NOTHING FOLLOWS X X X";
  861. $dto->Total = $total;
  862.  
  863.  
  864. $dto->ChargeType = $po->ChargeType=='S'?"STOCKS":substr($purchaseRequest->ChargedTo()->Name, 0, 3);
  865. $dto->ChargeNo = sprintf("%s/%s",$purchaseRequest->OrderNumber,$dto->ChargeType);
  866. // $data['D18'] = $chargeNo;
  867.  
  868. $dto->PurchasingManager = Role::findUserWithRole('PurchasingManager')->Person()->AbbreviatedName();
  869. $dto->OperationsManager = Role::findUserWithRole('OperationsDirector')->Person()->AbbreviatedName();
  870. $dto->PlantManager = Role::findUserWithRole('PlantManager')->Person()->AbbreviatedName();
  871. $dto->GeneralManager = Role::findUserWithRole('GeneralManager')->Person()->AbbreviatedName();
  872.  
  873. return view('report.templates.purchaseorder',['data'=>$dto]);
  874.  
  875.  
  876. } else {
  877. $data = new DTO();
  878. $data->Title = "Purchase Order $orderNumber";
  879. $data->Class = "Purchase Order";
  880. $data->Description = "We cannot not find the $data->Class in the database.";
  881. return response()
  882. ->view('errors.404',['data'=>$data]
  883. ,404);
  884. }
  885. } catch(\Exception $exc) {
  886. dd($exc);
  887. }
  888. }
  889.  
  890.  
  891.  
  892. /**
  893. * Display the specified resource.
  894. *
  895. * @param \App\PurchaseOrder $purchaseOrder
  896. * @return \Illuminate\Http\Response
  897. */
  898. public function getPurchaseOrderDataByOrderNumber($purchaseOrder)
  899. {
  900. $po = PurchaseOrder::where('OrderNumber','=',$purchaseOrder)->first();
  901. return view('purchasing.receive-orders.receive.content',['data'=>$po]);
  902. }
  903.  
  904. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement