diff --git a/app/admin/controller/LocalController.php b/app/admin/controller/LocalController.php index cbf42721b..f83debfda 100644 --- a/app/admin/controller/LocalController.php +++ b/app/admin/controller/LocalController.php @@ -2,17 +2,62 @@ namespace app\admin\controller; +use app\admin\logic\beforehand_order_cart_info\BeforehandOrderCartInfoLogic; +use app\common\model\beforehand_order\BeforehandOrder; +use app\common\model\beforehand_order_cart_info\BeforehandOrderCartInfo; +use app\common\model\CeshiCopy; +use app\common\model\purchase_product_offer\PurchaseProductOffer; use app\common\model\store_category\StoreCategory; use app\common\model\store_product\StoreProduct; use app\common\model\store_product_group_price\StoreProductGroupPrice; use app\common\model\StoreProductPriceList; use PhpOffice\PhpSpreadsheet\IOFactory; +use support\exception\BusinessException; use support\Redis; +use think\facade\Db; class LocalController extends BaseAdminController { - public $notNeedLogin = ['setPrice']; + public $notNeedLogin = ['activityPrice', 'searchProduct', 'setPrice', 'index', 'updateProductPriceList', 'importOrder']; + + public function activityPrice() + { + $product = Db::table('la_ceshi_copy_copy')->select()->toArray(); + $userShip = 43; + $insert = []; + foreach ($product as $k => $v) { + $rate = bcdiv($v['price'], $v['price_two'], 2); + $rate = $rate * 100; + $insert[] = [ + 'product_id' => $v['product_id'], + 'group_id' => $userShip, + 'price_type' => 3, + 'base_rate' => $rate, + 'price' => $v['price'], + ]; + } + StoreProductGroupPrice::insertAll($insert); + return $this->data($insert); + } + + public function searchProduct() + { + $product = Db::table('la_ceshi')->select()->toArray(); + $product = reset_index($product, 'name'); + $product2 = Db::table('la_ceshi_copy_copy')->select()->toArray(); + $update = []; + foreach ($product2 as $v) { + if (isset($product[$v['name']])) { + $update[] = [ + 'id' => $v['id'], + 'product_id' => $product[$v['name']]['product_id'], + ]; + } + } + $result = (new CeshiCopy())->saveAll($update); + return $this->data($update); + } public function setPrice() { @@ -267,4 +312,144 @@ class LocalController extends BaseAdminController return false; } + public function importOrder() + { + $file = $this->request->file('file'); + $reader = IOFactory::createReader('Xlsx'); + $spreadsheet = $reader->load($file->getRealPath()); + $sheets = $spreadsheet->getAllSheets(); + foreach ($sheets as $sheetIndex => $sheet) { + $params['is_buyer'] = -1; + $params['buyer_id'] = 20; + $params['admin_id'] = 1; + $params['chef'] = '厨师'; + $params['chef_phone'] = '13513513513'; + $params['distribution_personnel'] = '余友"'; + $params['days'] = 0; + $params['mark'] = '内部备注'; + $params['merchandiser'] = 0; + $params['purpose'] = '备注'; + $params['store_id'] = 4; + $params['splitting_officer'] = '分单员'; + $params['regional_manager'] = '张波'; + $params['system_store_name'] = '门店员'; + $params['transporter'] = '运输员'; + $params['pay_price'] = 0; + $params['total_price'] = 0; + $params['arrival_time'] = date('Y-m-d H:i:s'); + $rows = $sheet->toArray(); + foreach ($rows as $k => $row) { + if ($k < 1 || empty($row[2])) { + continue; + } + $product = StoreProduct::where('id', $row[0])->withTrashed()->field('id,package,store_info,marques,unit,price,purchase,after_sales')->findOrEmpty()->toArray(); + if (empty($product)) { + continue; + } + $totalPrice = bcmul($product['price'], $row[2], 2); + $params['total_price'] = bcadd($params['total_price'], $totalPrice, 2); + $params['pay_price'] = $params['total_price']; + $params['product_arr'][] = [ + 'product_id' => $product['id'], + 'nums' => $row[2], + 'package' => $product['package'], + 'store_info' => $product['store_info'], + 'marques' => $product['marques'], + 'unit' => $product['unit'], + 'price' => $product['price'], + 'purchase' => $product['purchase'], + 'after_sales' => $product['after_sales'], + 'total_price' => $totalPrice, + 'purchase_total' => $row[3], + ]; + } + $beforeOrderId = $this->batchCreate($params); + $purchaseOffer = PurchaseProductOffer::where('order_id', $beforeOrderId)->field('id,product_id,buyer_nums')->select()->toArray(); + foreach ($purchaseOffer as $v) { + $inStorageParams = [ + 'admin_id' => 0, + 'bhoid' => $beforeOrderId, + 'buyer_nums' => $v['buyer_nums'], + 'id' => $v['id'], + 'product_id' => $v['product_id'], + 'warehouse_id' => 1, + 'warehouse_num' => $v['buyer_nums'], + ]; + BeforehandOrderCartInfoLogic::putInStorage($inStorageParams); + } + } + + return $this->success('导入成功'); + } + + public function batchCreate($params) + { + if (empty($params['buyer_id'])) { + throw new BusinessException('请选择采购员'); + } + Db::startTrans(); + try { + $procurementOrder = new BeforehandOrder(); + $procurementOrder->order_id = getNewOrderId('CG'); + $procurementOrder->buyer_id = $params['buyer_id']; + $procurementOrder->admin_id = $params['admin_id']; + $procurementOrder->order_type = 9; + $procurementOrder->total_price = $params['total_price']; + $procurementOrder->pay_price = $params['pay_price']; + $procurementOrder->save(); + $purchaseOffer = []; + $cartInfo = []; + foreach ($params['product_arr'] as $product) { + if ($product['product_id'] <= 0) { + continue; + } + $cartInfo[] = [ + 'bhoid' => $procurementOrder['id'], + 'product_id' => $product['product_id'], + 'unit' => $product['unit'], + 'cart_num' => $product['nums'], + 'accept_num' => $product['nums'], + 'mark' => $product['mark'] ?? '', + 'is_buyer' => 1, + 'procurement_order_id' => $procurementOrder['id'], + 'total_price' => bcmul($product['price'], $product['nums'], 2), + 'pay_price' => bcmul($product['price'], $product['nums'], 2), + 'purchase' => $product['purchase'], + 'uid' => $params['uid'] ?? 0, + 'price' => $product['price'], + 'package' => $product['package'], + 'create_time' => time(), + 'update_time' => time(), + ]; + $purchaseOffer[] = [ + 'order_id' => $procurementOrder['id'], + 'product_id' => $product['product_id'], + 'unit' => $product['unit'], + 'is_buyer' => 1, + 'need_num' => $product['nums'], + 'mark' => $product['mark'] ?? '', + 'buyer_id' => $params['buyer_id'], + 'buyer_nums' => $product['nums'], + 'total_price' => $product['purchase_total'], + 'status' => 0, + 'source_order_info' => [ + [ + 'source_order_id' => $procurementOrder['id'], + 'product_id' => $product['product_id'], + 'need_num' => $product['nums'], + 'mark' => $product['mark'] ?? '', + ] + ] + ]; + } + PurchaseProductOffer::insertAll($purchaseOffer); + BeforehandOrderCartInfo::insertAll($cartInfo); + Db::commit(); + return $procurementOrder->id; + } catch (\Exception $e) { + Db::rollback(); + throw new BusinessException($e->getMessage()); + } + } + } \ No newline at end of file