| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace app\api\service;
- use app\BaseService;
- use app\common\model\ActivityProductModel;
- use app\common\model\ConfigModel;
- use app\common\model\ProductModel;
- use app\common\model\StoreProductModel;
- class ProductService extends BaseService
- {
- private $productModel;
- private $storeProductModel;
- private $activityProductModel;
- private $configModel;
- public function __construct()
- {
- $this->productModel = new ProductModel();
- $this->configModel = new ConfigModel();
- $this->storeProductModel = new StoreProductModel();
- $this->activityProductModel = new ActivityProductModel();
- }
- public function search($store_id,$bar_code = null, $product_name = null, $page = 1, $size = 10) {
- $storeProducts = $this->storeProductModel->search([
- 'store_id' => $store_id,
- 'bar_code' => $bar_code,
- 'product_name' => $product_name,
- 'page' => $page,
- "size" => $size
- ]);
- $items = $storeProducts->items();
- $config = $this->configModel->findConfig();
- $productIds = array_map(function ($data) {
- return $data['product_id'];
- }, $items);
- $activityProducts = $this->activityProductModel->fetchByProductIds($productIds)->toArray();
- foreach ($items as &$item) {
- $aProducts = array_filter($activityProducts, function ($aProduct) use ($item) {
- return $aProduct['product_id'] == $item['product_id'];
- });
- $res = compare($item['product']['real_price'] ?? 0, $aProducts);
- $item['product']['activity_price'] = fixed2Float($res['min_num']);
- $item['activity'] = $res['item'];
- $sales_tax_rate = $item['product']['sales_tax_rate'] > 0 ? $item['product']['sales_tax_rate'] : $config->sales_tax_rate;
- $item['product']['sales_tax_rate'] = $sales_tax_rate;
- $item['product']['sales_tax'] = fixed2Float($sales_tax_rate > 0 && $item['product']['activity_price'] > 0 ? ($item['product']['activity_price'] * ($sales_tax_rate / 100)) : 0);
- }
- return [$items, $storeProducts->total()];
- }
- // 订单总额 = 商品价格 + 商品年费 + 商品消费税
- // 应收金额 = 商品的活动价 + 商品年费 + 商品消费税
- // 优惠金额 = 订单总额 - 应收金额 + 商品消费税
- // 消费税 = 商品的消费税
- }
|