invoice fx

This commit is contained in:
merdan 2022-11-13 15:09:00 +05:00
parent e4f6272a7e
commit 6bfa344381
2 changed files with 7 additions and 479 deletions

View File

@ -153,7 +153,7 @@ class Products extends ProductController
}
public function discountedProducts(){
return ProductResource::collection($this->productRepository->getDiscountedProducts(request()->input('category')));
return ProductResource::collection($this->productRepository->getDiscountedProducts(request()->input('vendor'),request()->input('category')));
}
public function popularProducts(){

View File

@ -119,25 +119,6 @@ class ProductRepository extends WProductRepository
$qb->where('product_flat.visible_individually', 1);
}
// if(isset($params['color'])){
// $qb->whereIn('product_flat.color', explode(',', $params['color']));
// }
// if(isset($params['size'])){
// $qb->whereIn('product_flat.size', explode(',', $params['size']));
// }
// if (isset($params['search'])) {
// $qb->where('product_flat.name', 'like', '%' . urldecode($params['search']) . '%');
// }
//
// if (isset($params['name'])) {
// $qb->where('product_flat.name', 'like', '%' . urldecode($params['name']) . '%');
// }
//
// if (isset($params['url_key'])) {
// $qb->where('product_flat.url_key', 'like', '%' . urldecode($params['url_key']) . '%');
// }
# sort direction
$orderDirection = 'asc';
if (isset($params['order']) && in_array($params['order'], ['desc', 'asc'])) {
@ -156,10 +137,7 @@ class ProductRepository extends WProductRepository
$qb = $this->checkSortAttributeAndGenerateQuery($qb, $sortOptions[0], $orderDirection);
}
}
//select distinct `product_flat`.* from `product_flat` inner join `product_categories` on `product_categories`.`product_id` = `product_flat`.`product_id` where `product_flat`.`locale` = 'tm' and `product_flat`.`url_key` is not null and `product_categories`.`category_id` in (6) and `product_flat`.`status` = 1 and `product_flat`.`visible_individually` = 1 and `product_flat`.`color` in (24435) group by `product_flat`.`id` order by `product_flat`.`created_at` desc
//select distinct `product_flat`.id,`product_flat`.name, `product_flat`.color, `product_flat`.size, `product_flat`.locale, `product_flat`.product_id,`product_flat`.parent_id, `product_flat`.visible_individually,`product_flat`.url_key from `product_flat` left join `product_categories` on `product_categories`.`product_id` = `product_flat`.`product_id` where `product_flat`.`locale` = 'tm' and `product_flat`.`url_key` is not null and `product_categories`.`category_id` in (6) and `product_flat`.`status` = 1 and `product_flat`.`visible_individually` = 1 group by `product_flat`.`id` order by `product_flat`.`created_at` desc limit 10;
//select distinct `product_flat`.id,`product_flat`.name, `product_flat`.color, `product_flat`.size, `product_flat`.product_id,`product_flat`.parent_id, `product_flat`.visible_individually,`product_flat`.url_key from `product_flat` left join `product_categories` on `product_categories`.`product_id` = `product_flat`.`product_id` inner join `product_flat` as `variants` on `product_flat`.`id` = COALESCE(variants.parent_id, variants.id) left join `product_attribute_values` on `product_attribute_values`.`product_id` = `variants`.`product_id` where `product_categories`.`category_id` in (6,7) and `product_flat`.`visible_individually` = 1 and ((`product_attribute_values`.`attribute_id` = 23 and (find_in_set(24437, product_attribute_values.integer_value)))) group by `variants`.`id`, `product_flat`.`id` having COUNT(*) = 1 limit 100;
if ($priceFilter = request('price')) {
if ($priceFilter = request('price')) {
$priceRange = explode(',', $priceFilter);
if (count($priceRange) > 0) {
@ -286,9 +264,9 @@ class ProductRepository extends WProductRepository
return $results;
}
public function getDiscountedProducts($categoryId = null){
public function getDiscountedProducts($seller_id,$categoryId = null){
$results = app(ProductFlatRepository::class)->scopeQuery(function ($query) use ($categoryId) {
$results = app(ProductFlatRepository::class)->scopeQuery(function ($query) use ($seller_id,$categoryId) {
$channel = core()->getRequestedChannelCode();
$locale = core()->getRequestedLocaleCode();
@ -300,8 +278,9 @@ class ProductRepository extends WProductRepository
->where('product_flat.status', 1)
->where('product_flat.visible_individually', 1)
->where('product_flat.channel', $channel)
->where('product_flat.locale', $locale);
->where('product_flat.locale', $locale)
->leftJoin('marketplace_products', 'product_flat.product_id', '=', 'marketplace_products.product_id')
->where('marketplace_products.marketplace_seller_id', $seller_id);
if ($categoryId) {
$query->leftJoin('product_categories', 'product_categories.product_id', '=', 'product_flat.product_id')
->whereIn('product_categories.category_id', explode(',', $categoryId));
@ -338,223 +317,6 @@ class ProductRepository extends WProductRepository
return $results;
}
public function calculatePrice($price){
$originalPrice = Arr::get($price, 'originalPrice.value');
$discountedPrice = Arr::get($price, 'discountedPrice.value');
$price_attributes = [
// 'min_price'=>$discountedPrice,'max_price'=>$originalPrice
];
if($originalPrice > $discountedPrice){
$price_attributes['price'] = $originalPrice;
$price_attributes['special_price'] = $discountedPrice;
}
else{
$price_attributes['price'] = $discountedPrice;
}
return $price_attributes;
}
public function createProduct($data){
$time_start = microtime(true);
$product['sku'] = $data['product_group_id'];
// return array_map(fn($value): int => $value * 2, range(1, 5));
$product['type'] = (!empty($data['color_variants']) || !empty($data['size_variants'])) ? 'configurable':'simple';
$attributes = Arr::only($data,['source','cinsiyet']);
try {
DB::beginTransaction();
if (array_key_exists('attributes', $data)) {
$attributes [] = Arr::collapse($data['attributes']);
$grp = $this->getAttributeFamily(array_keys($attributes));
$product['attribute_family_id'] = $grp ? $grp->attribute_family_id :
(($product['type'] == 'configurable') ? 2 : 1);//default_configurable_product: default_simple_prodcut
} else
$product['attribute_family_id'] = $product['type'] == 'configurable' ? 2 : 1;
if(!empty($data['brand']))
{
$code = Str::slug($data['brand']);
if(! $brand = $this->brandRepository->findOneByField('code',$code))
$brand = $this->brandRepository->create(['name' => $data['brand'],
'code' => $code]);
$product['brand_id'] = $brand->id;
if(!empty($data['categories'])) {
$existing_ids = $brand->categories()->whereIn('id', $data['categories'])->pluck('id');
$brand->categories()->attach(array_diff($data['categories'],$existing_ids->toArray()));
}
}
//create product
$parentProduct = $this->getModel()->create($product);
$desc = implode(array_map(fn($value): string => '<p>' . $value['description'] . '</p>', $data['descriptions']));
$main_attributes = [
'sku' => $parentProduct->sku,
'product_number' => $data['product_number'],
'name' => $data['name'],
'weight' => $data['weight'] ?? 0.45,
'source' => $data['url_key'],
'status' => 1,
'visible_individually' => 1,
'url_key' => $parentProduct->sku,
'short_description' => $desc,
'description' => $desc,
'favoritesCount' => $data['favorite_count']
];
if (!empty($data['images'])) {
$this->assignImages($parentProduct, $data['images']);
}
if(!empty($data['categories'])){
$parentProduct->categories()->attach($data['categories']);
}
if ($product['type'] == 'configurable') {
$variant = null;
//create variants color
if (!empty($data['color_variants'])) {
$attribute = $this->attributeRepository->findOneByField('code', 'color');
$parentProduct->super_attributes()->attach($attribute->id);
foreach ($data['color_variants'] as $colorVariant) {
$description = implode(array_map(fn($value): string => '<p>' . $value['description'] . '</p>', $colorVariant['descriptions']));
if (!empty($colorVariant['size_variants'])) {
// $first = reset( $colorVariant['size_variants'] );
foreach ($colorVariant['size_variants'] as $sizeVariant)
{
$sizeNumber = $sizeVariant['itemNumber'] ?:$sizeVariant['attributeValue'];
if($sizeVariant['sellable'] && $variant = $this->createVariant($parentProduct, "{$data['product_group_id']}-{$colorVariant['product_number']}-{$sizeNumber}"))
{
if(!empty($data['categories'])){
$variant->categories()->attach($data['categories']);
}
$this->assignImages($variant, $colorVariant['images']);
$attributes = [
'sku' => $variant->sku,
'product_number' => "{$colorVariant['product_number']}-{$sizeVariant['itemNumber']}",
'color' => $this->getAttributeOptionId('color', $colorVariant['color']),
'name' => $colorVariant['name'],
'size' => $this->getAttributeOptionId('size', $sizeVariant['attributeValue']),
// 'price' => $sizeVariant['price'],
'weight' => $colorVariant['weight'] ?? 0.45,
'status' => 1,
'visible_individually' => 1,
'url_key' => $variant->sku,
'source' => $colorVariant['url_key'],
'description' => $description,
'short_description' => $description,
'favoritesCount' => $colorVariant['favorite_count']
];
// $attributes[] = $this->calculatePrice($sizeVariant['price']);
$this->assignAttributes($variant, array_merge($attributes,$this->calculatePrice($sizeVariant['price'])));
}
}
}
elseif($colorVariant['sellable'] && $variant = $this->createVariant($parentProduct, "{$data['product_group_id']}-{$colorVariant['product_number']}"))
{
if(!empty($data['categories'])){
$variant->categories()->attach($data['categories']);
}
$this->assignImages($variant, $colorVariant['images']);
$attributes = [
'sku' => $variant->sku,
'product_number' => $colorVariant['product_number'],
'color' => $this->getAttributeOptionId('color', $colorVariant['color']),
'name' => $colorVariant['name'],
// 'price' => Arr::get($colorVariant, 'price.discountedPrice.value'),
'weight' => $colorVariant['weight'] ?? 0.45,
'status' => 1,
'visible_individually' => 1,
'url_key' => $variant->sku,
'source' => $colorVariant['url_key'],
'description' => $description,
'short_description' => $description,
'favoritesCount' => $colorVariant['favorite_count']
];
$this->assignAttributes($variant, array_merge($attributes,$this->calculatePrice($colorVariant['price'])));
}
}
}
if (!empty($data['size_variants'])) {
$attribute = $this->attributeRepository->findOneByField('code', 'size');
$parentProduct->super_attributes()->attach($attribute->id);
$desc = implode(array_map(fn($value): string => '<p>' . $value['description'] . '</p>', $data['descriptions']));
foreach ($data['size_variants'] as $sizeVariant) {
if($sizeVariant['sellable'] && $variant = $this->createVariant($parentProduct, "{$data['product_group_id']}-{$data['product_number']}-{$sizeVariant['itemNumber']}")){
if(!empty($data['categories'])){
$variant->categories()->attach($data['categories']);
}
$this->assignImages($variant, $data['images']);
$attributes = [
'sku' => $variant->sku,
'size' => $this->getAttributeOptionId('size', $sizeVariant['attributeValue']),
'product_number' => "{$data['product_number']}-{$sizeVariant['itemNumber']}",
'name' => $data['name'],
// 'price' => $sizeVariant['price'],
'weight' => $data['weight'] ?? 0.45,
'status' => 1,
'featured'=> 0,
'new' => 0,
'visible_individually' => 1,
'url_key' => $variant->sku,
'source' => $data['url_key'],
'description' => $desc,
'short_description' => $desc,
'favoritesCount' => $data['favorite_count']
];
if (!empty($data['color'])) {
$attributes['color'] = $this->getAttributeOptionId('color', $data['color']);
}
$this->assignAttributes($variant, array_merge($attributes,$this->calculatePrice($sizeVariant['price'])));
}
}
}
if($variant){
$parentProduct->getTypeInstance()->setDefaultVariantId($variant->id);
}
}
else{
$main_attributes = array_merge($main_attributes, $this->calculatePrice($data['price']));
}
$this->assignAttributes($parentProduct, $main_attributes);
if($data['vendor'] && $seller = $this->vendorRepository->findOneByField('url',$data['vendor'])){
$this->createSellerProduct($parentProduct, $seller->id);
}
Event::dispatch('catalog.product.create.after', $parentProduct);
DB::commit();
return $parentProduct;
}
catch(\Exception $ex){
DB::rollBack();
Log::error($ex);
Log::info($data);
return false;
}
}
/**
* Variant join.
*
@ -573,71 +335,7 @@ class ProductRepository extends WProductRepository
->leftJoin('product_attribute_values', 'product_attribute_values.product_id', '=', 'variants.product_id');
}
}
public function updateProduct($product,$data){
$time_start = microtime(true);
try{
DB::beginTransaction();
if($product->type === 'simple'){
$this->updateAttribute($product,$data);
}
elseif($product->type === 'configurable'){
if (!empty($data['color_variants'])) {
foreach ($data['color_variants'] as $colorVariant) {
if (!empty($colorVariant['size_variants']))
foreach ($colorVariant['size_variants'] as $sizeVariant) {
if($variant = $this->findOneByField('sku', "{$data['product_group_id']}-{$colorVariant['product_number']}-{$sizeVariant['itemNumber']}"))
$this->updateAttribute($variant,$sizeVariant);
}
elseif($variant = $this->findOneByField('sku', "{$data['product_group_id']}-{$colorVariant['product_number']}"))
{
$this->updateAttribute($variant,$colorVariant);
}
}
}
if (!empty($data['size_variants'])){
foreach ($data['size_variants'] as $sizeVariant) {
if($variant = $this->findOneByField('sku', "{$data['product_group_id']}-{$data['product_number']}-{$sizeVariant['itemNumber']}"))
{
$this->updateAttribute($variant,$data);
}
}
}
}
Event::dispatch('catalog.product.update.after', $product);
DB::commit();
return true;
}
catch(\Exception $ex){
DB::rollBack();
Log::error($ex);
// Log::info($data);
return false;
}
}
public function updateAttribute($product,$data){
// $flat = $product->
if(isset($data['sellable']) && !$data['sellable']){
//$attribute = $this->attributeRepository->findOneByField('code', 'status'); status id = 8
$this->attributeValueRepository->updateOrCreate(['product_id'=>$product->id,'attribute_id'=>8],['boolean_value'=>0]);
}else{
$originalPrice = Arr::get($data, 'price.originalPrice.value');
$discountedPrice = Arr::get($data, 'price.discountedPrice.value');
if($discountedPrice >= $originalPrice){
$this->attributeValueRepository->updateOrCreate(['product_id'=>$product->id,'attribute_id'=>11],['float_value'=>$discountedPrice]);// price id 11
$this->attributeValueRepository->updateOrCreate(['product_id'=>$product->id,'attribute_id'=>13],['float_value'=>null]);//special price id 13
}else{
$this->attributeValueRepository->updateOrCreate(['product_id'=>$product->id,'attribute_id'=>11],['float_value'=>$originalPrice]);// price id 11
$this->attributeValueRepository->updateOrCreate(['product_id'=>$product->id,'attribute_id'=>13],['float_value'=>$discountedPrice]);//special price id 13
}
}
}
/**
* Returns the all products of the seller
*
@ -749,54 +447,7 @@ class ProductRepository extends WProductRepository
return $results;
}
public function createSellerProduct($product, $seller_id){
Event::dispatch('marketplace.catalog.product.create.before');
$sellerProduct = $this->vendorProductRepository->create([
'marketplace_seller_id' => $seller_id,
'is_approved' => 1,
'condition' => 'new',
'description' => 'scraped product',
'is_owner' => 1,
'product_id' => $product->id,
]);
foreach ($sellerProduct->product->variants as $baseVariant) {
$this->vendorProductRepository->create([
'parent_id' => $sellerProduct->id,
'product_id' => $baseVariant->id,
'is_owner' => 1,
'marketplace_seller_id' => $seller_id,
'is_approved' => 1,
'condition' => 'new',
]);
}
Event::dispatch('marketplace.catalog.product.create.after', $sellerProduct);
return $sellerProduct;
}
// private function assignBrand($product, $brand_name){
// $brand = $this->brandRepository->firstOrCreate([
// 'name' => $brand_name,
// 'code' => Str::slug($brand_name),
// ]);
// $product->brand()->associate($brand);
//// $product->save();
//// $brand->products()->attach($product);
//// $brand->save();
// }
public function assignImages($product,$images){
foreach($images as $image){
$this->imageRepository->create([
'type' => 'cdn',
'path' => $image,
'product_id' => $product->id,
]);
}
}
/**
* Search product by attribute.
*
@ -870,111 +521,6 @@ class ProductRepository extends WProductRepository
return $results;
}
public function assignAttributes($product, $attributes){
foreach($attributes as $code => $value){
if(! $attribute = $this->attributeRepository->findOneByField('code', $code))
{
continue;
}
$attr = [
'product_id' => $product->id,
'attribute_id' => $attribute->id,
'value' => $value
];
if($attribute->value_per_channel){
$attr['channel'] = config('app.channel');
}
try {
if ($attribute->value_per_locale){
foreach (core()->getAllLocales() as $locale){
$attr['locale'] = $locale->code;
$this->attributeValueRepository->create($attr);
}
}else{
$this->attributeValueRepository->create($attr);
}
}
catch(\Exception $ex){
// Log::info($attr);
Log::error($ex->getMessage());
}
}
}
public function createVariant($product, $sku){
try{
return $this->getModel()->create([
'parent_id' => $product->id,
'type' => 'simple',
'attribute_family_id' => $product->attribute_family_id,
'sku' => $sku,
'brand_id' => $product->brand_id
]);
}
catch(\Exception $ex){
Log::error($ex->getMessage());
return false;
}
}
private function createFlat($product){
$channel = core()->getDefaultChannel();
foreach ($channel->locales as $locale){
$productFlat = $this->productFlatRepository->findOneWhere([
'product_id' => $product->id,
'channel' => $channel->code,
'locale' => $locale->code,
]);
if (! $productFlat) {
$productFlat = $this->productFlatRepository->create([
'product_id' => $product->id,
'channel' => $channel->code,
'locale' => $locale->code,
]);
}
}
}
private function attributeValues($values,$attributeCode){
$attribute = $this->attributeRepository->getAttributeByCode($attributeCode);
$all_options = $attribute->options()
->orderBy('sort_order','asc')
->get();
$options = $all_options->whereIn('admin_name',$values)->pluck('admin_name')->toArray();
//create new options if doesn exist
if(count($values) != count($options)
&& $new_options = array_diff($values,$options)){
$order = $all_options->last()->sort_order ?? 0;
foreach($new_options as $new_option){
$order++;
$option = $this->optionRepository->create([
'admin_name' => $new_option,
'sort_order' => $order,
'attribute_id' => $attribute->id
]);
$this->optionTranslationRepository->create(['attribute_option_id'=>$option->id,'label'=> $new_option,'locale'=>'tm']);
}
$options = array_merge($options,$new_options);
}
return $options;
}
public function getAttributeOptionId($attr,$value){
$attribute_id = $this->attributeRepository->getAttributeByCode($attr)->id;
@ -989,24 +535,6 @@ class ProductRepository extends WProductRepository
return $option->id;
}
//find attribute family
private function getAttributeFamily($attrubetCodes){
$count = count($attrubetCodes);
$str = "'" . implode("','", $attrubetCodes) . "'";
$grups = $this->attributeGroupRepo->leftJoin('attribute_group_mappings','attribute_groups.id','=','attribute_group_mappings.attribute_group_id')
->leftJoin('attributes',function($join) use ($attrubetCodes) {
$join->on('attributes.id','=','attribute_group_mappings.attribute_id')
->whereIn('code',$attrubetCodes);
})
->groupBy('attribute_groups.id')
->havingRaw("SUM(IF(attributes.code IN($str),1,0)) = $count")
->select('attribute_groups.attribute_family_id')
->first();
return $grups;
}
public function variants($product_id){
$channel = core()->getRequestedChannelCode();