2024-12-23 17:03:51 +08:00
< ? php
namespace app\admin\controller ;
2025-02-20 17:35:12 +08:00
use app\admin\logic\beforehand_order_cart_info\BeforehandOrderCartInfoLogic ;
2025-03-21 17:15:38 +08:00
use app\admin\logic\product_source_link_info\ProductSourceLinkInfoLogic ;
2025-04-07 10:46:37 +08:00
use app\admin\logic\inventory_transfer_order\InventoryTransferOrderLogic ;
2025-04-16 10:24:40 +08:00
use app\admin\logic\store_product\StoreProductLogic ;
use app\admin\service\ProductPriceService ;
2025-02-20 17:35:12 +08:00
use app\common\model\beforehand_order\BeforehandOrder ;
use app\common\model\beforehand_order_cart_info\BeforehandOrderCartInfo ;
use app\common\model\CeshiCopy ;
2025-03-17 16:40:34 +08:00
use app\common\model\product_source_link\ProductSourceLink ;
use app\common\model\product_source_link_info\ProductSourceLinkInfo ;
2025-02-20 17:35:12 +08:00
use app\common\model\purchase_product_offer\PurchaseProductOffer ;
2024-12-23 17:03:51 +08:00
use app\common\model\store_category\StoreCategory ;
use app\common\model\store_product\StoreProduct ;
2024-12-30 17:57:27 +08:00
use app\common\model\store_product_group_price\StoreProductGroupPrice ;
2025-04-16 10:24:40 +08:00
use app\common\model\store_product_price\StoreProductPrice ;
2025-02-12 17:17:11 +08:00
use app\common\model\StoreProductPriceList ;
2025-03-17 16:40:34 +08:00
use app\common\model\warehouse_order\WarehouseOrder ;
use app\common\model\warehouse_product\WarehouseProduct ;
2025-04-07 10:46:37 +08:00
use app\common\model\store_branch_product\StoreBranchProduct ;
use app\common\model\system_store\SystemStore ;
2025-04-16 10:24:40 +08:00
use app\common\model\warehouse_product_storege\WarehouseProductStorege ;
2024-12-23 17:03:51 +08:00
use PhpOffice\PhpSpreadsheet\IOFactory ;
2025-02-20 17:35:12 +08:00
use support\exception\BusinessException ;
2024-12-23 17:03:51 +08:00
use support\Redis ;
2025-02-20 17:35:12 +08:00
use think\facade\Db ;
2024-12-23 17:03:51 +08:00
class LocalController extends BaseAdminController
{
2025-04-16 10:24:40 +08:00
public $notNeedLogin = [ 'activityPrice' , 'searchProduct' , 'setPrice' , 'index' , 'updateProductPriceList' , 'importOrder' , 'warehousing' , 'outbound' , 'syncPrice' , 'importProduct' ];
2025-04-07 10:46:37 +08:00
public $ids = [ 1829 , 1828 , 1827 , 1826 , 1825 , 1824 , 1823 , 1821 , 1820 , 1814 , 1813 , 1811 , 1810 , 1809 , 1808 , 1807 , 1806 , 1800 , 1799 , 1798 , 1796 , 1795 , 1794 , 1793 , 1792 , 1791 , 1790 , 1789 , 1788 , 1787 , 1786 , 1785 , 1784 , 1783 , 1782 , 1781 , 1780 , 1779 , 1778 , 1777 , 1776 , 1775 , 1774 , 1773 , 1772 , 1771 , 1770 , 1768 , 1765 , 1764 , 1763 , 1762 , 1761 , 1760 , 1759 , 1758 , 1757 , 1756 , 1755 , 1754 , 1753 , 1752 , 1751 , 1750 , 1749 , 1748 , 1747 , 1746 , 1745 , 1744 , 1743 , 1742 , 1741 , 1740 , 1739 , 1738 , 1737 , 1736 , 1735 , 1733 , 1732 , 1731 , 1730 , 1729 , 1728 , 1727 , 1726 , 1725 , 1724 , 1723 , 1722 , 1720 , 1719 , 1718 , 1717 , 1716 , 1715 , 1714 , 1713 , 1712 , 1711 , 1710 , 1709 , 1708 , 1707 , 1706 , 1705 , 1704 , 1703 , 1701 , 1700 , 1699 , 1698 , 1697 , 1696 , 1695 , 1694 , 1693 , 1692 , 1691 , 1690 , 1689 , 1688 , 1687 , 1686 , 1685 , 1684 , 1683 , 1682 , 1681 , 1680 , 1679 , 1678 , 1677 , 1676 , 1675 , 1674 , 1673 , 1672 , 1671 , 1670 , 1668 , 1667 , 1666 , 1665 , 1664 , 1663 , 1660 , 1659 , 1658 , 1657 , 1656 , 1655 , 1654 , 1652 , 1651 , 1650 , 1649 , 1648 , 1647 , 1646 , 1645 , 1644 , 1643 , 1642 , 1641 , 1640 , 1639 , 1638 , 1637 , 1636 , 1635 , 1634 , 1633 , 1632 , 1631 , 1630 , 1629 , 1628 , 1627 , 1626 , 1623 , 1622 ];
2025-03-17 16:40:34 +08:00
2025-04-16 10:24:40 +08:00
public function importProduct ()
{
$file = $this -> request -> file ( 'file' );
$reader = IOFactory :: createReader ( 'Xlsx' );
$spreadsheet = $reader -> load ( $file -> getRealPath ());
$data = $spreadsheet -> getActiveSheet () -> toArray ();
$createTime = time ();
$updateTime = time ();
foreach ( $data as $k => $v ) {
if ( $k < 1 ) {
continue ;
}
$productName = $v [ 0 ];
$num = $v [ 1 ];
$package = $v [ 2 ];
$product = StoreProduct :: where ( 'store_name' , $productName ) -> find ();
if ( empty ( $product )) {
$product = StoreProductLogic :: add ([
'store_name' => $productName ,
'image' => '' ,
'cate_id' => 15627 ,
'cate_arr' => [ 15324 , 15627 ],
'package' => $package ,
'unit' => 0 ,
'price' => 0 ,
'vip_price' => 0 ,
'cost' => 0 ,
'purchase' => 0 ,
'is_return' => 0 ,
'is_store_all' => 1 ,
'product_type' => 3 ,
]);
}
$warehouseProduct = WarehouseProductStorege :: where ( 'product_id' , $product -> id ) -> find ();
if ( empty ( $warehouseProduct )) {
WarehouseProductStorege :: create ([
'product_id' => $product -> id ,
'nums' => $num ,
'warehouse_id' => 1 ,
'create_time' => $createTime ,
'update_time' => $updateTime ,
]);
} else {
$warehouseProduct -> save ([ 'nums' => $num ]);
}
}
}
public function syncPrice ( ProductPriceService $productPriceService )
{
$data = Db :: connect ( 'local' ) -> query ( 'SELECT id, product_id,purchase_price FROM (SELECT id,product_id,purchase_price,ROW_NUMBER() OVER (PARTITION BY product_id ORDER BY id DESC) as rn FROM la_store_product_price) subquery WHERE rn = 1' );
$products = StoreProduct :: whereIn ( 'id' , array_column ( $data , 'product_id' )) -> field ( 'id,store_name,top_cate_id,cate_id' ) -> select () -> toArray ();
$products = reset_index ( $products , 'id' );
$update = [];
$update2 = [];
foreach ( $data as $item ) {
$product = $products [ $item [ 'product_id' ]] ? ? [];
if ( empty ( $product )) {
continue ;
}
$priceRateList = $productPriceService -> getProductPriceRate ( $product [ 'id' ], true );
$rate = $priceRateList [ 'supply_rate' ];
$costRate = $priceRateList [ 'merchant_rate' ];
$vipPriceRate = $priceRateList [ 'vip_rate' ];
$priceRate = $priceRateList [ 'price_rate' ];
$purchase = bcmul ( $item [ 'purchase_price' ], $rate , 2 );
$cost = bcmul ( $purchase , $costRate , 2 );
$price = bcmul ( $purchase , $priceRate , 2 );
$vipPrice = bcmul ( $purchase , $vipPriceRate , 2 );
$update [] = [
'id' => $item [ 'id' ],
'purchase_lv' => $rate ,
'purchase' => $purchase ,
'cost_lv' => $costRate ,
'cost' => $cost ,
'price_lv' => $priceRate ,
'price' => $price ,
'vip_lv' => $vipPriceRate ,
'vip_price' => $vipPrice ,
];
$update2 [] = [
'id' => $item [ 'product_id' ],
'purchase' => $purchase ,
'cost' => $cost ,
'price' => $price ,
'vip_price' => $vipPrice ,
];
}
( new StoreProductPrice ()) -> saveAll ( $update );
( new StoreProduct ()) -> saveAll ( $update2 );
}
2025-03-17 16:40:34 +08:00
public function warehousing ()
{
$warehousingIds = BeforehandOrder :: whereIn ( 'id' , $this -> ids ) -> where ( 'warehousing_id' , '>' , 0 ) -> column ( 'warehousing_id' );
$warehouseOrders = WarehouseOrder :: field ( 'id,warehouse_id' ) -> whereIn ( 'id' , $warehousingIds ) -> where ( 'financial_pm' , 1 ) -> select () -> toArray ();
$productSourceLinkInfo = [];
foreach ( $warehouseOrders as $order ) {
2025-03-18 15:20:56 +08:00
$products = WarehouseProduct :: field ( 'id,product_id,nums,purchase,create_time' ) -> where ( 'oid' , $order [ 'id' ]) -> select () -> toArray ();
2025-03-17 16:40:34 +08:00
foreach ( $products as $product ) {
$productSourceLink = ProductSourceLink :: where ( 'product_id' , $product [ 'product_id' ]) -> where ( 'purchase_uid' , 20 ) -> where ( 'warehouse_id' , $order [ 'warehouse_id' ]) -> find ();
if ( empty ( $productSourceLink )) {
$productSourceLink = new ProductSourceLink ();
$productSourceLink -> product_id = $product [ 'product_id' ];
$productSourceLink -> purchase_uid = 20 ;
$productSourceLink -> warehouse_id = $order [ 'warehouse_id' ];
$productSourceLink -> save ();
}
$productSourceLinkInfo [] = [
'oid' => $productSourceLink [ 'id' ],
'product_id' => $product [ 'product_id' ],
'warehouse_id' => $order [ 'warehouse_id' ],
'nums' => $product [ 'nums' ],
'current_nums' => $product [ 'nums' ],
'types' => ProductSourceLinkInfo :: TypeIn ,
'link_id' => $product [ 'id' ],
'price' => $product [ 'purchase' ],
'total_price' => bcmul ( $product [ 'purchase' ], $product [ 'nums' ], 2 ),
2025-03-18 15:20:56 +08:00
'create_time' => strtotime ( $product [ 'create_time' ]),
2025-03-17 16:40:34 +08:00
];
}
}
( new ProductSourceLinkInfo ()) -> insertAll ( $productSourceLinkInfo );
}
public function outbound ()
{
2025-03-18 15:20:56 +08:00
$outboundIds = BeforehandOrder :: whereIn ( 'id' , $this -> ids ) -> where ( 'outbound_id' , '>' , 0 ) -> column ( 'outbound_id' );
$outboundOrders = WarehouseOrder :: field ( 'id,warehouse_id,store_id' ) -> whereIn ( 'id' , $outboundIds ) -> where ( 'financial_pm' , 0 ) -> select () -> toArray ();
foreach ( $outboundOrders as $order ) {
$products = WarehouseProduct :: field ( 'id,product_id,nums,purchase,create_time' ) -> where ( 'oid' , $order [ 'id' ]) -> select () -> toArray ();
2025-03-17 16:40:34 +08:00
foreach ( $products as $product ) {
2025-03-21 17:15:38 +08:00
$productSourceLinkInfoLogic = new ProductSourceLinkInfoLogic ();
$productSourceLinkInfoLogic -> warehouseProduct = $product ;
$productSourceLinkInfoLogic -> setInfo ([
2025-03-17 16:40:34 +08:00
'warehouse_id' => $order [ 'warehouse_id' ],
'store_id' => $order [ 'store_id' ],
'link_id' => $product [ 'id' ],
2025-03-18 15:20:56 +08:00
'create_time' => strtotime ( $product [ 'create_time' ]),
2025-03-17 16:40:34 +08:00
]);
2025-03-21 17:15:38 +08:00
$productSourceLinkInfoLogic -> outbound ();
2025-03-17 16:40:34 +08:00
}
}
}
2025-02-20 17:35:12 +08:00
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 );
}
2024-12-30 16:31:28 +08:00
public function setPrice ()
{
$file = $this -> request -> file ( 'file' );
$reader = IOFactory :: createReader ( 'Xlsx' );
$spreadsheet = $reader -> load ( $file -> getRealPath ());
$data = $spreadsheet -> getActiveSheet () -> toArray ();
$insert = [];
2024-12-30 17:57:27 +08:00
$update = [];
2024-12-30 16:31:28 +08:00
$productIds = [];
foreach ( $data as $k => $row ) {
if ( $k < 2 ) {
continue ;
}
if ( empty ( $row [ 0 ])) {
continue ;
}
$productId = intval ( $row [ 0 ]);
if ( empty ( $productId )) {
continue ;
}
$productIds [] = $productId ;
}
2024-12-30 17:57:27 +08:00
$products = StoreProduct :: whereIn ( 'id' , $productIds ) -> field ( 'id,store_name,purchase' ) -> select () -> toArray ();
2024-12-30 16:31:28 +08:00
$products = reset_index ( $products , 'id' );
foreach ( $data as $k => $row ) {
if ( $k < 2 ) {
continue ;
}
if ( empty ( $row [ 0 ])) {
continue ;
}
$productId = intval ( $row [ 0 ]);
if ( empty ( $productId )) {
continue ;
}
$product = $products [ $productId ] ? ? [];
if ( empty ( $product )) {
continue ;
}
2024-12-30 17:57:27 +08:00
$groupIds = [
5 => 23 ,
7 => 24 ,
9 => 25 ,
11 => 26 ,
13 => 27 ,
15 => 28 ,
17 => 29 ,
19 => 30 ,
21 => 31 ,
23 => 32 ,
25 => 33 ,
27 => 34 ,
29 => 35 ,
31 => 36 ,
33 => 37 ,
35 => 1 ,
37 => 18 ,
39 => 22 ,
2024-12-30 16:31:28 +08:00
];
2024-12-30 17:57:27 +08:00
for ( $i = 5 ; $i < 35 ; $i = $i + 2 ) {
$rate = intval ( rtrim ( $row [ $i ] ? ? 0 , '%' ));
if ( empty ( $rate )) {
continue ;
}
$price = $row [ $i + 1 ] ? ? 0 ;
if ( empty ( $price )) {
$price = $product [ 'purchase' ] * ( 1 + $rate / 100 );
}
$item = [
'product_id' => $productId ,
'group_id' => $groupIds [ $i ],
'price_type' => 3 ,
'base_rate' => 100 + $rate ,
'price' => $price
];
$insert [] = $item ;
}
$productGroupPrices = StoreProductGroupPrice :: where ( 'product_id' , $productId ) -> whereIn ( 'group_id' , [ 1 , 18 , 22 ]) -> select () -> toArray ();
$groupIds = array_flip ( $groupIds );
foreach ( $productGroupPrices as $productGroupPrice ) {
$cellId = $groupIds [ $productGroupPrice [ 'group_id' ]] ? ? 0 ;
$rate = intval ( rtrim ( $row [ $cellId ] ? ? 0 , '%' ));
if ( empty ( $rate )) {
continue ;
}
$price = $row [ $cellId + 1 ] ? ? 0 ;
if ( empty ( $price )) {
$price = $product [ 'purchase' ] * ( 1 + $rate / 100 );
}
if ( $price != $productGroupPrice [ 'price' ]) {
$update [] = [
'id' => $productGroupPrice [ 'id' ],
'base_rate' => $rate ,
'price' => $price ,
];
}
}
}
if ( count ( $insert ) > 0 ) {
StoreProductGroupPrice :: insertAll ( $insert );
}
if ( count ( $update ) > 0 ) {
( new StoreProductGroupPrice ()) -> saveAll ( $update );
2024-12-30 16:31:28 +08:00
}
2024-12-31 11:29:25 +08:00
return $this -> success ( '插入成功:' . count ( $insert ) . '条,更新成功:' . count ( $update ) . '条' );
2024-12-30 16:31:28 +08:00
}
2024-12-25 14:10:28 +08:00
public function fixCategory ()
{
$topCate = StoreCategory :: where ( 'pid' , 0 ) -> field ( 'id,name,pid' ) -> order ( 'name' ) -> select () -> toArray ();
$topCate = reset_index ( $topCate , 'name' );
$sql = [];
$time = time ();
foreach ( $topCate as $item ) {
if ( isset ( $topCate [ $item [ 'name' ] . '类' ])) {
$target = $topCate [ $item [ 'name' ] . '类' ];
$sql [] = " ##原分类id: { $item [ 'id' ] } ,原分类名: { $item [ 'name' ] } , 目标分类id: { $target [ 'id' ] } ,目标分类名: { $target [ 'name' ] } " ;
$sql [] = " update la_store_product set top_cate_id= { $target [ 'id' ] } where top_cate_id= { $item [ 'id' ] } ; " ;
$sql [] = " update la_store_product set two_cate_id= { $target [ 'id' ] } where two_cate_id= { $item [ 'id' ] } ; " ;
$sql [] = " update la_store_category set delete_time= $time where id= { $item [ 'id' ] } ; " ;
}
$secondCate = StoreCategory :: where ( 'pid' , $item [ 'id' ]) -> field ( 'id,name,pid' ) -> order ( 'name' ) -> select () -> toArray ();
$secondCate = reset_index ( $secondCate , 'name' );
foreach ( $secondCate as $item2 ) {
if ( isset ( $secondCate [ $item2 [ 'name' ] . '类' ])) {
$target = $secondCate [ $item2 [ 'name' ] . '类' ];
$sql [] = " ##原分类id: { $item2 [ 'id' ] } ,原分类名: { $item2 [ 'name' ] } , 目标分类id: { $target [ 'id' ] } ,目标分类名: { $target [ 'name' ] } " ;
$sql [] = " update la_store_product set two_cate_id= { $target [ 'id' ] } where two_cate_id= { $item2 [ 'id' ] } ; " ;
$sql [] = " update la_store_product set cate_id= { $target [ 'id' ] } where cate_id= { $item2 [ 'id' ] } ; " ;
$sql [] = " update la_store_category set delete_time= $time where id= { $item2 [ 'id' ] } ; " ;
}
$thirdCate = StoreCategory :: where ( 'pid' , $item2 [ 'id' ]) -> field ( 'id,name,pid' ) -> order ( 'name' ) -> select () -> toArray ();
$thirdCate = reset_index ( $thirdCate , 'name' );
foreach ( $thirdCate as $item3 ) {
if ( isset ( $thirdCate [ $item3 [ 'name' ] . '类' ])) {
$target = $thirdCate [ $item3 [ 'name' ] . '类' ];
$sql [] = " ##原分类id: { $item3 [ 'id' ] } ,原分类名: { $item3 [ 'name' ] } , 目标分类id: { $target [ 'id' ] } ,目标分类名: { $target [ 'name' ] } " ;
$sql [] = " update la_store_product set cate_id= { $target [ 'id' ] } where cate_id= { $item3 [ 'id' ] } ; " ;
$sql [] = " update la_store_category set delete_time= $time where id= { $item3 [ 'id' ] } ; " ;
}
}
}
}
file_put_contents ( public_path () . '/update.sql' , implode ( PHP_EOL , $sql ));
return $this -> success ( '数据已更新完成' , $sql );
}
2024-12-23 17:03:51 +08:00
2025-02-12 17:12:21 +08:00
public function updateProductPriceList ()
{
$file = $this -> request -> file ( 'file' );
$reader = IOFactory :: createReader ( 'Xlsx' );
$spreadsheet = $reader -> load ( $file -> getRealPath ());
$data = $spreadsheet -> getActiveSheet () -> toArray ();
$updateCount = 0 ;
$insert = [];
$time = time ();
foreach ( $data as $k => $row ) {
if ( $k < 1 || empty ( $row [ 0 ])) {
continue ;
}
$product = StoreProduct :: where ( 'id' , $row [ 0 ]) -> value ( 'id' );
if ( empty ( $product )) {
continue ;
}
for ( $i = 1 ; $i < 5 ; $i ++ ) {
if ( empty ( $row [ 1 + $i ])) {
continue ;
}
$insert [] = [
'product_id' => $row [ 0 ],
'price_type' => $i ,
'rate' => intval ( rtrim ( $row [ 1 + $i ], '%' )) + 100 ,
'create_time' => $time ,
];
}
}
StoreProductPriceList :: insertAll ( $insert );
return $this -> success ( '更新成功:' . $updateCount . '条' );
}
2024-12-23 17:03:51 +08:00
public function index ()
{
$file = $this -> request -> file ( 'file' );
$reader = IOFactory :: createReader ( 'Xlsx' );
$spreadsheet = $reader -> load ( $file -> getRealPath ());
$data = $spreadsheet -> getActiveSheet () -> toArray ();
$updateCount = 0 ;
2024-12-23 17:42:48 +08:00
$finishCount = Redis :: get ( 'updateFinishCount' );
2024-12-23 17:03:51 +08:00
$finishCount = empty ( $finishCount ) ? 0 : $finishCount ;
if ( $finishCount >= count ( $data )) {
2024-12-23 17:42:48 +08:00
return $this -> success ( '数据已更新完成' );
2024-12-23 17:03:51 +08:00
}
$max = $finishCount + 100 ;
foreach ( $data as $k => $row ) {
if ( $k < ( 1 + $finishCount ) || empty ( $row [ 0 ])) {
continue ;
}
if ( $k > $max ) {
break ;
}
$product = StoreProduct :: where ( 'id' , $row [ 0 ]) -> field ( 'id,store_name,top_cate_id,two_cate_id,cate_id' ) -> findOrEmpty () -> toArray ();
if ( empty ( $product )) {
continue ;
}
$result = $this -> updateProduct ( $product , $row );
if ( $result ) {
$updateCount ++ ;
}
}
2024-12-23 17:42:48 +08:00
Redis :: set ( 'updateFinishCount' , 100 + $finishCount );
return $this -> success ( '更新成功:' . $updateCount . '条' );
2024-12-23 17:03:51 +08:00
}
public function updateProduct ( $product , $row )
{
2024-12-23 17:42:48 +08:00
$topCateName = $row [ 1 ];
$secondCateName = $row [ 2 ];
$cateName = $row [ 3 ];
2024-12-23 17:03:51 +08:00
$topCate = StoreCategory :: where ( 'name' , $topCateName ) -> value ( 'id' );
$updateData = [];
if ( ! empty ( $topCate ) && $topCate != $product [ 'top_cate_id' ]) {
$updateData [ 'top_cate_id' ] = $topCate ;
}
$secondCateId = StoreCategory :: where ( 'pid' , $topCate ) -> where ( 'name' , $secondCateName ) -> value ( 'id' );
if ( empty ( $secondCateId )) {
$secondCate = new StoreCategory ();
$secondCate -> name = $secondCateName ;
$secondCate -> pid = $topCate ;
$secondCate -> save ();
$secondCateId = $secondCate -> id ;
}
if ( $secondCateId != $product [ 'two_cate_id' ]) {
$updateData [ 'two_cate_id' ] = $secondCateId ;
}
$cateId = StoreCategory :: where ( 'pid' , $secondCateId ) -> where ( 'name' , $cateName ) -> value ( 'id' );
if ( empty ( $cateId )) {
$cate = new StoreCategory ();
$cate -> name = $cateName ;
$cate -> pid = $secondCateId ;
$cate -> save ();
$cateId = $cate -> id ;
}
if ( $cateId != $product [ 'cate_id' ]) {
$updateData [ 'cate_id' ] = $cateId ;
}
if ( ! empty ( $updateData )) {
StoreProduct :: where ( 'id' , $row [ 0 ]) -> update ( $updateData );
echo '更新成功ID: ' . $row [ 0 ] . PHP_EOL ;
return true ;
}
return false ;
}
2025-02-20 17:35:12 +08:00
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' ],
2025-02-20 17:58:36 +08:00
'buyer_confirm' => 1 ,
2025-02-20 17:35:12 +08:00
'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 ());
}
}
2025-04-16 10:24:40 +08:00
2025-04-07 10:46:37 +08:00
public function importStorege ()
{
$file = $this -> request -> file ( 'file' );
$reader = IOFactory :: createReader ( 'Xlsx' );
$spreadsheet = $reader -> load ( $file -> getRealPath ());
2025-04-07 12:10:28 +08:00
$sheets = $spreadsheet -> getActiveSheet () -> toArray ();
2025-04-07 10:46:37 +08:00
$new_params = [];
2025-04-07 12:10:28 +08:00
$params = [];
$system_store_id = 0 ;
$params [ 'product_arr' ] = [];
foreach ( $sheets as $key => $row ) {
if ( $key == 0 ) {
continue ;
}
if ( ! $system_store_id ) {
2025-04-07 10:46:37 +08:00
$system_store_id = SystemStore :: where ( 'name' , $row [ 1 ]) -> value ( 'id' );
}
2025-04-16 10:24:40 +08:00
$product = StoreBranchProduct :: where ( 'product_id' , $row [ 0 ]) -> where ( 'store_id' , $system_store_id ) -> field ( 'id,product_id,stock' ) -> findOrEmpty ();
2025-04-07 14:01:01 +08:00
if ( $row [ 10 ] == $product -> stock && intval ( $product -> stock ) > 0 ) {
2025-04-07 12:10:28 +08:00
$arr = [
'nums' => $row [ 10 ],
'product_id' => $product -> product_id ,
'purchase' => 0 ,
'total_price' => $product -> total_price ,
];
$params [ 'product_arr' ][] = $arr ;
} else {
$arr = [];
$arr [ 'product_id' ] = $row [ 0 ];
$arr [ 'product_name' ] = $row [ 2 ] ? ? '' ;
$arr [ 'store_id' ] = $system_store_id ;
$arr [ 'nums' ] = $row [ 10 ] ? ? 0 ;
$arr [ 'stock' ] = $product -> stock ? ? 0 ;
$new_params [] = $arr ;
}
2025-04-07 10:46:37 +08:00
}
2025-04-07 12:10:28 +08:00
// 生成铺货单
$params [ 'mark' ] = " 门店铺货-excel导入 " ;
$params [ 'one_id' ] = $system_store_id ; //门店id
$params [ 'one_type' ] = 1 ; //1门店2仓库
$params [ 'two_id' ] = 1 ; //1海吉星仓库
$params [ 'two_type' ] = 2 ; //1门店2仓库
2025-04-16 10:24:40 +08:00
$params [ 'types' ] = 0 ; //0减库存 1不减库存
2025-04-07 12:10:28 +08:00
InventoryTransferOrderLogic :: add ( $params , $this -> adminId );
$new_params = json_encode ( $new_params , true );
file_put_contents ( public_path () . '/output.text' , $new_params );
2025-04-07 10:46:37 +08:00
return $this -> success ( '导入成功' );
}
}