multi-store/app/admin/logic/purchase_order/PurchaseOrderLogic.php

285 lines
11 KiB
PHP

<?php
namespace app\admin\logic\purchase_order;
use app\common\model\purchase_order\PurchaseOrder;
use app\common\logic\BaseLogic;
use app\common\model\purchase_order_info\PurchaseOrderInfo;
use app\common\model\purchase_product_offer\PurchaseProductOffer;
use app\common\model\store_order\StoreOrder;
use app\common\model\store_order_cart_info\StoreOrderCartInfo;
use app\common\model\store_product\StoreProduct;
use app\common\model\system_store\SystemStore;
use think\facade\Db;
/**
* 采购订单逻辑
* Class PurchaseOrderLogic
* @package app\admin\logic\purchase_order
*/
class PurchaseOrderLogic extends BaseLogic
{
public static function mergeOrder($params)
{
$oid_arr = [];
$store_arr = [];
$purchaseOrderInfo = new PurchaseOrderInfo();
foreach ($params as $k => $v) {
$oid_arr[] = $v['oid'];
$store_arr[] = $v['store_id'];
}
$store_arr = array_unique($store_arr);
// Db::startTrans();
// try {
$price = StoreOrder::whereIn('id', $oid_arr)->whereIn('store_id', $store_arr)->group('store_id')->field('store_id,SUM(pay_price) as pay_price,SUM(total_price) as total_price')->select()->toArray();
$purchase_arr = [];
$order_arr = [];
$total_price = 0;
$pay_price = 0;
foreach ($price as $k => $v) {
foreach($params as $key => $value){
if($value['store_id']==$v['store_id']){
$order_arr[] = $value['oid'];
}
}
$total_price += $v['total_price'];
$pay_price += $v['pay_price'];
$data = [
'store_id' => $v['store_id'],
'order_arr' => json_encode($order_arr),
'order_id' => getNewOrderId('CG'),
'total' => $v['total_price'],
'actual' => $v['pay_price'],
'money' => $v['pay_price'],
'paid' => 1,
'is_mer' => 1,
'create_time' => time(),
'update_time' => time(),
];
//创建门店合计订单
$res = PurchaseOrder::create($data);
$purchase_arr[] = ['id'=>$res['id'],'store_id'=>$v['store_id']];
$purchase_id[] =$res['id'];
}
$info = StoreOrderCartInfo::where('oid', 'in', $order_arr)->field('store_id, product_id,price,SUM(total_price) as total_price, SUM(cart_num) as cart_num')->group('store_id, product_id')->select()->toArray();
//创建门店合计商品
foreach($purchase_arr as $k=>$v){
foreach ($info as $item) {
if($v['store_id']==$item['store_id']){
$arr['oid'] = $v['id'];
$arr['store_id'] = $item['store_id'];
$arr['product_id'] = $item['product_id'];
$arr['price'] = $item['price'];
$arr['total_price'] = $item['total_price'];
$arr['cart_num'] = $item['cart_num'];
$arr['status'] = 1;
$purchase_order_info[] = $arr;
}
}
}
$purchaseOrderInfo->saveAll($purchase_order_info);
//---------------生成平台订单-----------------
$data = [
'store_id' => 0,
'store_arr' => json_encode($store_arr),
'order_arr' => json_encode($purchase_id),
'order_id' => getNewOrderId('PT'),
'total' => $total_price,
'actual' => $pay_price,
'money' => $pay_price,
'paid' => 1,
'is_mer' => 2,
'create_time' => time(),
'update_time' => time(),
];
$res = PurchaseOrder::create($data);
$info = PurchaseOrderInfo::where('oid', 'in', $purchase_id)->field('store_id, product_id,price,SUM(total_price) as total_price, SUM(cart_num) as cart_num')->group('store_id, product_id')->select();
$purchase_order_info_two=[];
foreach ($info as $item) {
$arr['oid'] = $res['id'];
$arr['store_id'] = $item['store_id'];
$arr['product_id'] = $item['product_id'];
$arr['price'] = $item['price'];
$arr['total_price'] = $item['total_price'];
$arr['cart_num'] = $item['cart_num'];
$arr['status'] = 1;
$purchase_order_info_two[] = $arr;
}
$purchaseOrderInfo->saveAll($purchase_order_info_two);
$productOffer = [];
foreach ($info as $item) {
$arr['order_id'] = $res['id'];
$arr['product_id'] = $item['product_id'];
$arr['price'] = $item['price'];
$arr['total_price'] = $item['total_price'];
$arr['need_num'] = $item['cart_num'];
$arr['unit'] = StoreProduct::where('id', $item['product_id'])->value('unit');
$productOffer[] = $arr;
}
$purchaseProductOffer = new PurchaseProductOffer();
$purchaseProductOffer->saveAll($productOffer);
// StoreOrder::whereIn('id', $oid_arr)->update(['is_merge' => 1]);
// Db::commit();
// return true;
// } catch (\Throwable $e) {
// Db::rollback();
// self::setError($e->getMessage());
// return false;
// }
}
/**
* @notes 合并今日商户订单
* @param array $params
* @return bool
* @author admin
* @date 2024/08/01 16:32
*/
public static function StoreTodayOrder(): bool
{
Db::startTrans();
try {
$store_arr = StoreOrder::where(['paid' => 1, 'refund_status' => 0])->whereDay('create_time')->group('store_id')->column('store_id');
$purchaseOrderInfo = new PurchaseOrderInfo();
foreach ($store_arr as $store_id) {
$purchase_order_info = [];
$order_arr = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'store_id' => $store_id])->whereDay('create_time')->column('id');
$price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'store_id' => $store_id])->whereDay('create_time')->field('SUM(pay_price) as pay_price,SUM(total_price) as total_price')->find();
$data = [
'store_id' => $store_id,
'order_arr' => json_encode($order_arr),
'order_id' => getNewOrderId('CG'),
'total' => $price['total_price'],
'actual' => $price['pay_price'],
'money' => $price['pay_price'],
'paid' => 1,
'is_mer' => 1,
'create_time' => time(),
'update_time' => time(),
];
$res = PurchaseOrder::create($data);
$info = StoreOrderCartInfo::where('oid', 'in', $order_arr)->field('store_id, product_id,price,SUM(total_price) as total_price, SUM(cart_num) as cart_num')->group('store_id, product_id')->select();
foreach ($info as $item) {
$arr['oid'] = $res['id'];
$arr['store_id'] = $item['store_id'];
$arr['product_id'] = $item['product_id'];
$arr['price'] = $item['price'];
$arr['total_price'] = $item['total_price'];
$arr['cart_num'] = $item['cart_num'];
$arr['status'] = 1;
$purchase_order_info[] = $arr;
}
$purchaseOrderInfo->saveAll($purchase_order_info);
}
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 合并今日平台订单
* @param array $params
* @return bool
* @author admin
* @date 2024/08/01 16:32
*/
public static function platformTodayOrder()
{
Db::startTrans();
try {
$purchaseOrderInfo = new PurchaseOrderInfo();
$purchase_order_info = [];
$order_arr = PurchaseOrder::whereDay('create_time')->where('is_mer', 1)->column('id');
$price = PurchaseOrder::whereDay('create_time')->field('SUM(actual) as pay_price,SUM(total) as total_price')->find();
$data = [
'store_id' => 0,
'order_arr' => json_encode($order_arr),
'order_id' => getNewOrderId('PT'),
'total' => $price['total_price'],
'actual' => $price['pay_price'],
'money' => $price['pay_price'],
'paid' => 1,
'is_mer' => 2,
'create_time' => time(),
'update_time' => time(),
];
$res = PurchaseOrder::create($data);
$info = PurchaseOrderInfo::where('oid', 'in', $order_arr)->field('store_id, product_id,price,SUM(total_price) as total_price, SUM(cart_num) as cart_num')->group('store_id, product_id')->select();
foreach ($info as $item) {
$arr['oid'] = $res['id'];
$arr['store_id'] = $item['store_id'];
$arr['product_id'] = $item['product_id'];
$arr['price'] = $item['price'];
$arr['total_price'] = $item['total_price'];
$arr['cart_num'] = $item['cart_num'];
$arr['status'] = 1;
$purchase_order_info[] = $arr;
}
$purchaseOrderInfo->saveAll($purchase_order_info);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 删除采购订单
* @param array $params
* @return bool
* @author admin
* @date 2024/08/01 16:32
*/
public static function delete(array $params): bool
{
return PurchaseOrder::destroy($params['id']);
}
/**
* @notes 获取采购订单详情
* @param $params
* @return array
* @author admin
* @date 2024/08/01 16:32
*/
public static function detail($id): array
{
$data = PurchaseOrder::findOrEmpty($id)->toArray();
if ($data) {
if ($data['store_id']) {
$data['system_store'] = SystemStore::where('id', $data['store_id'])->value('name');
} else {
$data['system_store'] = '平台';
}
switch ($data['storage']) {
case 0:
$data['storage_name'] = '未入库';
break;
case 1:
$data['storage_name'] = '部分入库';
break;
case 2:
$data['storage_name'] = '已入库';
break;
default:
$data['storage_name'] = '未入库';
}
}
return $data;
}
}