예제 #1
0
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     if ($request->get('customer_name') == null) {
         return response()->json(['response_order_message' => 'fail', 'response_order_message_detail' => 'Customer name is required']);
     }
     $customer = Customer::where('name', '=', $request->get('customer_name'))->first();
     if ($customer == null) {
         $customer = new Customer();
         $customer->name = $request->get('customer_name');
         $customer->slug = str_slug($customer->name);
         $customer->phone_number = $request->get('customer_phone_number');
         $customer->email = $request->get('customer_email');
         $customer->save();
         $customerAddress = new CustomerAddress();
         $customerAddress->line1 = $request->get('line1');
         $customerAddress->line2 = $request->get('line2');
         $customerAddress->district = $request->get('district');
         $customerAddress->province = $request->get('province');
         $customerAddress->post_code = $request->get('post_code');
         $customerAddress->customer_id = $customer->id;
         $customerAddress->save();
     }
     $order = new Order();
     $order->customer_id = $customer->id;
     $order->status = 0;
     $order->save();
     $orderDetail = new OrderDetail();
     $orderDetail->order_id = $order->id;
     $orderDetail->product_id = $request->get('product_id');
     $orderDetail->amount = $request->get('amount');
     $orderDetail->total_price = Product::findOrNew($orderDetail->product_id)->price * $orderDetail->amount;
     $orderDetail->save();
     return $order->id;
 }
예제 #2
0
 public function postCheckout(Request $request)
 {
     \DB::beginTransaction();
     $customer = Customer::create($request->all());
     $order = $customer->orders()->create(['quantity' => \Cart::getTotalQuantity(), 'total' => \Cart::getTotal(), 'shipped' => false]);
     foreach (\Cart::getContent() as $cart) {
         $order->details()->create(['deal_id' => $cart->id, 'quantity' => $cart->quantity, 'price' => $cart->price]);
     }
     $order->push();
     $customer->push();
     \DB::commit();
     \Cart::clear();
     return view('frontend.checkout.done')->with('categories', Category::all());
 }
 public function generatestats()
 {
     if ($this->shouldLockIfNoPermission('data.edit')) {
         return;
     }
     if ($this->request->is('post')) {
         $reportName = time();
         $modelCustomer = new Customer($this->db);
         $modelDevice = new Device($this->db);
         $modelPart = new Part($this->db);
         $user = $this->session->get('user');
         $output = 'Created By: ' . $user['username'] . "\n";
         $customerData = $modelCustomer->findAllCustomers();
         if (null === $customerData) {
             $this->flash->error('Unable to get customers!');
             return $this->redirect('/reports');
         }
         $output .= "\nCUSTOMERS\n------------\n";
         $count = count($customerData);
         $output .= ($count === 1 ? 'There is ' : 'There are ') . $count . ($count === 1 ? ' customer' : ' customers') . " total.\n";
         $ids = [];
         foreach ($customerData as $row) {
             $ids[] = $row['id'];
         }
         $deviceData = $modelDevice->findAllLinkedToCustomerIds($ids);
         if (null === $deviceData) {
             $this->flash->error('Unable to get devices!');
             return $this->redirect('/reports');
         }
         $totalDeviceData = ['Tower' => ['data' => [], 'singular_description' => 'tower', 'plural_description' => 'towers'], 'Laptop' => ['data' => [], 'singular_description' => 'laptop', 'plural_description' => 'laptops'], 'Phone' => ['data' => [], 'singular_description' => 'phone', 'plural_description' => 'phones'], 'Printer' => ['data' => [], 'singular_description' => 'printer', 'plural_description' => 'printers'], 'Tablet' => ['data' => [], 'singular_description' => 'tablet', 'plural_description' => 'tablets'], 'Other' => ['data' => [], 'singular_description' => 'other device', 'plural_description' => 'other devices']];
         $keys = array_keys($totalDeviceData);
         foreach ($deviceData as $row) {
             if (in_array($row['type'], $keys)) {
                 $totalDeviceData[$row['type']]['data'][] = $row;
             } else {
                 $totalDeviceData['Other']['data'][] = $row;
             }
         }
         $output .= "\nDEVICES\n------------\n";
         foreach ($totalDeviceData as $row) {
             $total = $count === 1 ? $row['singular_description'] : $row['plural_description'];
             $count = count($row['data']);
             $output .= ($count === 1 ? 'There is ' : 'There are ') . $count . ' ' . $total . " total.\n";
         }
         foreach ($deviceData as $row) {
             $ids[] = $row['id'];
         }
         $data = $modelPart->findAllLinkedToDeviceIds($ids);
         if (null === $data) {
             $this->flash->error('Unable to get parts!');
             return $this->redirect('/reports');
         }
         $output .= "\nPARTS\n------------\n";
         $total = $count === 1 ? ' part' : ' parts';
         $count = count($data);
         $output .= ($count === 1 ? 'There is ' : 'There are ') . $count . $total . " total.\n";
         $output .= "\nNEW DEVICES\n------------\n";
         foreach ($deviceData as $row) {
             $found = false;
             if ($row['status'] === 'New') {
                 foreach ($customerData as $customer) {
                     if ($customer['id'] === $row['customer_id'] || strlen(trim($row['status'])) === 0) {
                         $found = true;
                         $output .= $customer['first_name'] . ' ' . $customer['last_name'];
                         $output .= '  --> ' . $row['type'] . "\n";
                     }
                 }
                 if (!$found) {
                     $output .= 'Error: There is a device with a missing customer!';
                 }
             }
         }
         $output .= "\nPENDING DEVICES\n------------\n";
         foreach ($deviceData as $row) {
             $found = false;
             if ($row['status'] === 'Pending') {
                 foreach ($customerData as $customer) {
                     if ($customer['id'] === $row['customer_id']) {
                         $found = true;
                         $output .= $customer['first_name'] . ' ' . $customer['last_name'];
                         $output .= '  --> ' . $row['type'] . "\n";
                     }
                 }
                 if (!$found) {
                     $output .= 'Error: There is a device with a missing customer!';
                 }
             }
         }
         if (!is_dir($this->reportsFolder)) {
             mkdir($this->reportsFolder, self::REPORT_FOLDER_PERMISSIONS, true);
         }
         $reportName .= '-stats';
         $filename = $this->reportsFolder . DS . 'generated-' . $reportName . '.txt';
         file_put_contents($filename, $output);
         return $this->redirect('/reports/view/' . $reportName);
     }
 }
 public function generateInvoice($id)
 {
     $modelDevice = new Device($this->db);
     $device = $modelDevice->findById($id);
     if ($device === null) {
         $this->flash->error('Unable to generate invoice!');
         return $this->redirect('/devices/view/' . $id);
     }
     $modelCustomer = new Customer($this->db);
     $customer = $modelCustomer->findById($device['customer_id']);
     if ($customer === null) {
         $this->flash->error('Unable to generate invoice!');
         return $this->redirect('/devices/view/' . $id);
     }
     $modelPart = new Part($this->db);
     $parts = $modelPart->findAllByDeviceId($device['id']);
     if ($parts === null) {
         $this->flash->error('Unable to generate invoice!');
         return $this->redirect('/devices/view/' . $id);
     }
     $infoShop = [$customer['shop'], $customer['shop_periods']];
     $infoAddress = [\ICanBoogie\titleize($customer['address']), strtoupper($customer['state']) . ' ' . \ICanBoogie\titleize($customer['zip'])];
     $infoStaff = [];
     if (strlen(trim($customer['room_number'])) > 0) {
         $infoStaff[] = 'Room #' . $customer['room_number'];
     }
     if (strlen(trim($customer['phone_ext'])) > 0) {
         $infoStaff[] = 'Ext.' . $customer['phone_ext'];
     }
     $this->set('now', date('n/j/Y', time()));
     $this->set('parts', $parts);
     $this->set('device', $device);
     $this->set('customer', $customer);
     $this->set('infoShop', implode(', ', array_filter(array_map('trim', $infoShop), 'strlen')));
     $this->set('infoStaff', implode(', ', array_filter(array_map('trim', $infoStaff), 'strlen')));
     $this->set('infoAddress', implode(', ', array_filter(array_map('trim', $infoAddress), 'strlen')));
 }
 public function delete($id)
 {
     if ($this->shouldLockIfNoPermission('data.edit')) {
         return;
     }
     $customerService = new Customer($this->db);
     if ($this->request->is('post')) {
         $deviceService = new Device($this->db);
         $devices = $deviceService->findAllByCustomerId($id);
         if (null === $devices) {
             $this->flash->error('Unable to delete customer!');
             return $this->redirect('/customers');
         }
         $customerService->deleteById($id);
         $partService = new Part($this->db);
         foreach ($devices as $device) {
             $partService->deleteAllByDeviceId($device['id']);
         }
         $deviceService->deleteAllByCustomerId($id);
         $this->flash->success('Deleted customer successfully!');
         return $this->redirect('/customers');
     }
     $item = $customerService->findById($id);
     $this->set('item', $item);
     $this->set('id', $id);
 }