public function determineWorkOrders(Request $request)
 {
     if ($request->input('productsToFulfill') && is_array($request->input('productsToFulfill'))) {
         try {
             \DB::beginTransaction();
             // Determine work orders (if any for these PO products
             $workOrdersToCreate = $this->workOrderSchedulerService->determineWorkOrdersForPo($request->input('productsToFulfill'));
             if ($request->input('purchaseOrderId')) {
                 // This is on the detail page, instead of just previewing
                 // the PO products and the workorders actually create them now.
                 $po = PurchaseOrder::where('id', $request->input('purchaseOrderId'))->first();
                 foreach ($request->input('productsToFulfill') as $p) {
                     PurchaseOrderProduct::create(['purchase_order_id' => $po->id, 'product_id' => $p['product_id'], 'quantity' => $p['quantity']]);
                     // Deduct the quantity ordered from the current stock for the product
                     $this->workOrderSchedulerService->deductStockFromProducts([$p]);
                 }
                 if ($workOrdersToCreate['workOrdersToCreate'] > 0) {
                     if (isset($po)) {
                         // Create any work orders for the PO product
                         $this->workOrderSchedulerService->generateWorkOrdersForPo($po, $workOrdersToCreate['workOrders']);
                     }
                 }
             }
             \DB::commit();
             return response()->json($workOrdersToCreate);
         } catch (\Exception $ex) {
             \DB::rollBack();
             throw $ex;
         }
     }
 }
示例#2
0
 /**
  * Save 
  */
 public function save($po = null, $data = null)
 {
     DB::transaction(function () use(&$data, &$po) {
         if (is_null($po)) {
             $po = PurchaseOrder::create($data);
         } else {
             $po->fill($data)->save();
         }
         if (!$data['items']) {
             throw new UnprocessableEntityHttpException('Empty line items');
         }
         $po->items()->delete();
         foreach ($data['items'] as $item) {
             $po->items()->create($item);
         }
     });
     return $this->getById($po->id);
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     $purchaseOrder = PurchaseOrder::where('id', $id)->first();
     if (isset($purchaseOrder)) {
         try {
             $workOrderScheduleService = new WorkOrderSchedulerService();
             \DB::beginTransaction();
             // Restore stock for any non workorder quantities
             $workOrderScheduleService->restoreStockForProducts($purchaseOrder->id);
             // Delete any work orders for this PO
             $workOrderScheduleService->deleteWorkOrdersForPo($purchaseOrder->id);
             // Now delete the PO itself
             $purchaseOrder->delete();
             \DB::commit();
         } catch (\Exception $ex) {
             \DB::rollBack();
             throw $ex;
         }
     }
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update(Request $request, $id)
 {
     $purchaseOrder = PurchaseOrder::findOrFail($id);
     $purchaseOrder->fill($request->all());
     $purchaseOrder->save();
     Session::flash('message', $purchaseOrder->name . ' was updated !');
     return \Redirect::back();
 }
示例#5
0
 public function getSalesChannelReport(Request $request)
 {
     $query = PurchaseOrder::select(DB::raw('sales_channels.name'), DB::raw('count(purchase_orders.id) as pocount'), DB::raw('sum(purchase_orders.total) as pototal'));
     $reportParams = $request->input('reportParams');
     if (isset($reportParams['sales_channel_from_date']) && $reportParams['sales_channel_from_date'] !== '') {
         $sDate = new Carbon($reportParams['sales_channel_from_date']);
         $query->whereDate('purchase_orders.created_at', '>=', $sDate->toDateString());
     }
     if (isset($reportParams['sales_channel_to_date']) && $reportParams['sales_channel_to_date'] !== '') {
         $eDate = new Carbon($reportParams['sales_channel_to_date']);
         $query->whereDate('purchase_orders.created_at', '<=', $eDate->toDateString());
     }
     $query->join('sales_channels', 'purchase_orders.sales_channel_id', '=', 'sales_channels.id');
     $query->groupBy(DB::raw('purchase_orders.sales_channel_id'));
     $query->orderBy('sales_channels.name', 'asc');
     $dataPoints = $query->get();
     return response()->json($dataPoints);
 }
示例#6
0
 public static function filterAndPaginate($name)
 {
     return PurchaseOrder::name($name)->orderBy('name', 'ASC')->paginate();
 }