Created
February 22, 2016 21:40
-
-
Save danielmalone/0482f09dbd85a7aaa7db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use App\Product; | |
use App\Http\Requests; | |
use App\Http\Controllers\Controller; | |
class PagesController extends Controller | |
{ | |
public function index() { | |
return view('home'); | |
} | |
public function pasted(Request $request) { | |
if ($request->mode === 'replace') { | |
Product::truncate(); | |
} | |
$lines = explode("\n", $request->sku_quantity); | |
foreach ($lines as $line) { | |
$line = trim($line); | |
$parts = explode(',', $line); | |
$sku = trim($parts[0]); | |
$quantity = trim($parts[1]); | |
if ($request->mode === 'add' && $new = Product::where('sku', $sku)->first()) { | |
$currentQuantity = $new->quantity; | |
Product::where('sku', $sku)->update(['quantity' => $quantity + $currentQuantity]); | |
} | |
if ($quantity > 0 && ! isset($new)) { | |
Product::firstOrCreate(['sku' => $sku, 'quantity' => $quantity]); | |
} | |
} | |
return view('success'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment