ProductService.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace app\api\service;
  3. use app\BaseService;
  4. use app\common\model\ActivityProductModel;
  5. use app\common\model\ConfigModel;
  6. use app\common\model\ProductModel;
  7. use app\common\model\StoreProductModel;
  8. class ProductService extends BaseService
  9. {
  10. private $productModel;
  11. private $storeProductModel;
  12. private $activityProductModel;
  13. private $configModel;
  14. public function __construct()
  15. {
  16. $this->productModel = new ProductModel();
  17. $this->configModel = new ConfigModel();
  18. $this->storeProductModel = new StoreProductModel();
  19. $this->activityProductModel = new ActivityProductModel();
  20. }
  21. public function search($store_id,$bar_code = null, $product_name = null, $page = 1, $size = 10) {
  22. $storeProducts = $this->storeProductModel->search([
  23. 'store_id' => $store_id,
  24. 'bar_code' => $bar_code,
  25. 'product_name' => $product_name,
  26. 'page' => $page,
  27. "size" => $size
  28. ]);
  29. $items = $storeProducts->items();
  30. $config = $this->configModel->findConfig();
  31. $productIds = array_map(function ($data) {
  32. return $data['product_id'];
  33. }, $items);
  34. $activityProducts = $this->activityProductModel->fetchByProductIds($productIds)->toArray();
  35. foreach ($items as &$item) {
  36. $aProducts = array_filter($activityProducts, function ($aProduct) use ($item) {
  37. return $aProduct['product_id'] == $item['product_id'];
  38. });
  39. $res = compare($item['product']['real_price'] ?? 0, $aProducts);
  40. $item['product']['activity_price'] = fixed2Float($res['min_num']);
  41. $item['activity'] = $res['item'];
  42. $sales_tax_rate = $item['product']['sales_tax_rate'] > 0 ? $item['product']['sales_tax_rate'] : $config->sales_tax_rate;
  43. $item['product']['sales_tax_rate'] = $sales_tax_rate;
  44. $item['product']['sales_tax'] = fixed2Float($sales_tax_rate > 0 && $item['product']['activity_price'] > 0 ? ($item['product']['activity_price'] * ($sales_tax_rate / 100)) : 0);
  45. }
  46. return [$items, $storeProducts->total()];
  47. }
  48. // 订单总额 = 商品价格 + 商品年费 + 商品消费税
  49. // 应收金额 = 商品的活动价 + 商品年费 + 商品消费税
  50. // 优惠金额 = 订单总额 - 应收金额 + 商品消费税
  51. // 消费税 = 商品的消费税
  52. }