Customers Add+Edit, API WP

This commit is contained in:
Kevin Adametz 2020-06-12 14:46:51 +02:00
parent dc63fa9fb2
commit 75a0f9a38a
120 changed files with 11894 additions and 6134 deletions

View file

@ -38,12 +38,6 @@ class FileRepository extends BaseRepository {
$this->{$name} = $value;
}
/* public function load($id){
$this->model = $id;
}
*/
public function uploadFile( $form_data )
{
@ -106,7 +100,7 @@ class FileRepository extends BaseRepository {
'network_name' => 'required|max:255',
'input_file_now' => 'required|mimes:png,pdf,jpg,jpeg|max:30000'
);
$validator = Validator::make(Input::all(), $rules);
$validator = Validator::make(Request::all(), $rules);
if ($validator->fails()) {
// get the error messages from the validator

View file

@ -0,0 +1,156 @@
<?php
namespace App\Repositories;
use App\Models\Country;
use App\Models\ShoppingUser;
use Validator;
use Response;
use Excel;
use Storage;
use App\Models\Import;
class ImportRepository extends BaseRepository {
public function __construct(){
}
public function upload( $form_data )
{
$validator = Validator::make($form_data, Import::$rules, Import::$messages);
if ($validator->fails()) {
return Response::json([
'error' => true,
'message' => $validator->messages()->first(),
'code' => 400
], 400);
}
$file = $form_data['file'];
$originalName = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$originalNameWithoutExt = substr($originalName, 0, strlen($originalName) - strlen($extension) - 1);
$filename = $this->sanitize($originalNameWithoutExt);
$allowed_filename = $filename.".".$extension;
Storage::disk('import')->put($allowed_filename, file_get_contents($file->getRealPath()));
return Response::json([
'error' => false,
'filename' => $allowed_filename,
'filedata' => 'xls',
'redirect' => url(route('sysadmin_import_show', ['xls', $allowed_filename])),
'code' => 200
], 200);
}
public function read($type, $file, $skip, $limit)
{
if(!Storage::disk('import')->has($file)){
return false;
}
return $this->import(storage_path("app/import/").$file, $file, $skip, $limit);
}
protected function import($file_path, $file, $skip, $limit){
$salutation = array(1 => 'mr', 2 => 'ms', 3=>null);
$ret = [
'count' => 0,
'not' => 0,
'imported' => [],
'has_imported' => [],
];
/*
* wp_order_number
wp_order_date
billing_company
billing_salutation
billing_firstname
billing_lastname
billing_address
billing_address_2
billing_zipcode
billing_city
billing_country_code
billing_country
billing_email
billing_phone
shipping_salutation
shipping_company
shipping_firstname
shipping_lastname
shipping_address
shipping_address_2
shipping_zipcode
shipping_city
shipping_country_code
shipping_country
*/
$rows = Excel::toArray(new \App\Imports\ImportCollection(), $file_path);
foreach ($rows[0] as $row){
$ret['count']++;
if(isset($row['billing_email'])){
if(ShoppingUser::where('billing_email', '=', $row['billing_email'])->count() > 0){
$ssuser = ShoppingUser::where('billing_email', '=', $row['billing_email'])->first();
if($ssuser->member_id){
$ret['has_imported'][] = $row['billing_email']." - ".$ssuser->member->email;
continue;
}
}
$row['billing_salutation'] = $salutation[$row['billing_salutation']];
$row['billing_country_id'] = Country::getCountryIdByCodeOrOne($row['billing_country_code']);
$row['billing_phone'] = strlen($row['billing_phone']) <= 3 ? '' : $row['billing_phone'];
$row['same_as_billing'] = true;
if(isset($row['shipping_salutation']) && $row['shipping_salutation'] > 0){
$row['shipping_salutation'] = $salutation[$row['shipping_salutation']];
$row['shipping_country_id'] = Country::getCountryIdByCodeOrOne($row['shipping_country_code']);
$row['same_as_billing'] = false;
}
$row['member_id'] = 3;
$row['number'] = ShoppingUser::max('number') + 1;
$row['has_buyed'] = true;
$row['subscribed'] = false;
$ret['imported'][] = $row['billing_email'];
ShoppingUser::create($row);
}else{
$ret['not']++;
}
}
return $ret;
}
public function sanitize($string, $force_lowercase = true, $anal = false)
{
$strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
"}", "\\", "|", ";", ":", "\"", "'", "&#8216;", "&#8217;", "&#8220;", "&#8221;", "&#8211;", "&#8212;",
"—", "–", ",", "<", ".", ">", "/", "?");
$clean = trim(str_replace($strip, "", strip_tags($string)));
$clean = preg_replace('/\s+/', "-", $clean);
$clean = ($anal) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean ;
return ($force_lowercase) ?
(function_exists('mb_strtolower')) ?
mb_strtolower($clean, 'UTF-8') :
strtolower($clean) :
$clean;
}
}

View file

@ -4,6 +4,7 @@ namespace App\Repositories;
use App\Models\CountryPrice;
use App\Models\Product;
use App\Models\ProductAttribute;
use App\Models\ProductCategory;
@ -38,6 +39,8 @@ class ProductRepository extends BaseRepository {
$this->updateCategories(isset($data['categories']) ? $data['categories'] : array());
$this->updateAttributes(isset($data['attributes']) ? $data['attributes'] : array());
$this->updateCountryPrices($data);
return $this->model;
}
@ -84,6 +87,30 @@ class ProductRepository extends BaseRepository {
return true;
}
public function updateCountryPrices($data)
{
if(!isset($data['country_prices']) || !is_array($data['country_prices'])){
return false;
}
foreach ($data['country_prices'] as $k => $country_id) {
$cp = CountryPrice::updateOrCreate([
'country_id' => $country_id,
'product_id' => $this->model->id,
],
[
'c_price' => isset($data['c_price'][$country_id]) ? reFormatNumber($data['c_price'][$country_id]) : null,
'c_tax' => isset($data['c_tax'][$country_id]) ? reFormatNumber($data['c_tax'][$country_id]) : null,
'c_price_old' => isset($data['c_price_old'][$country_id]) ? reFormatNumber($data['c_price_old'][$country_id]) : null,
'c_currency' => isset($data['c_currency'][$country_id]) ? reFormatNumber($data['c_currency'][$country_id]) : null,
]);
}
return true;
}
public function copy($model)
{