You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

214 lines
7.5 KiB

<?php
namespace app\admin\controller\ykjp\sell;
use addons\shopro\model\GoodsSkuPrice;
use addons\shopro\model\Order as OrderModel;
use addons\shopro\model\OrderItem;
use app\common\controller\Backend;
use app\common\model\User;
use think\Db;
class Order extends Backend
{
public function _initialize()
{
parent::_initialize();
$this->model = new \addons\shopro\model\Order();
}
//订单列表
public function index()
{
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if (false === $this->request->isAjax()) {
return $this->view->fetch();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
//只展示已支付出货单
$list = $this->model
->with('itemSlim')
->where($where)
->where('status', \addons\shopro\model\Order::STATUS_PAYED)
->order($sort, $order)
->paginate($limit);
// var_dump($list);
foreach ($list as &$row) {
$row['area'] = $row['province_name'] . $row['city_name'] . $row['area_name'];
$row['delivery_status'] = $this->model->getDeliveryStatus($row['delivery_status']);
}
$result = array("total" => $list->total(), "rows" => $list->items());
return json($result);
}
//订单详情
public function detail($ids) {
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$detail = $this->model
->with('itemSlim')
->where('id', $ids)
->where($where)
->order($sort, $order)
->find()
->toArray();
$detail['area'] = $detail['province_name'].$detail['city_name'].$detail['area_name'];
$detail['delivery_info'] = $detail['delivery_info'] ? json_decode($detail['delivery_info'], true) : [];
if ($detail['delivery_user_id']) {
$detail['delivery_user_name'] = User::get($detail['delivery_user_id'])->nickname ?? '';
$detail['delivery_user_phone'] = User::get($detail['delivery_user_id'])->mobile ?? '';
}
// echo "<pre>";
// print_r($detail['item_slim']);exit();
foreach ($detail['item_slim'] as &$row) {
$row['goods_code'] = $row['goods_id'] ?? 0;
$row['warehouse_name'] = '';
if ($row['warehouse_id']) {
$row['warehouse_name'] = \app\admin\model\ykjp\information\basisinfo\Warehouse::get($row['warehouse_id'])->name ?? '';
}
$row['unit'] = '';
$row['goods_total'] = $row['goods_price'] * $row['goods_num'];
}
$this->assign("detail", $detail);
$this->assign("product", $detail['item_slim']);
return $this->view->fetch("detail");
}
//指派代发
public function appoint()
{
$order_id = $this->request->get('id');
$goods_id = $this->request->get('goods_id');
if (false === $this->request->isAjax()) {
$order = $this->model->get($order_id);
$this->assign('user_id', $order['delivery_user_id']);
$this->assign('order_id', $order_id);
$this->assign('remark', $order['delivery_user_remark']);
return $this->view->fetch();
}
$user_id = $this->request->post("user_id");
$remark = $this->request->post("remark");
$order = $this->model->get($order_id);
$order->delivery_user_id = $user_id;
$order->delivery_user_remark = $remark;
$order->save();
$this->success('指派成功');
}
//发货
public function deliver()
{
$order_id = $this->request->get('id');
$goods_id = $this->request->get('goods_id');
if (false === $this->request->isAjax()) {
$orderInfo = $this->model->get($order_id);
$delivery_info = $orderInfo['delivery_info'] ? json_decode($orderInfo['delivery_info'], true) : [];
$this->assign('delivery_info', $delivery_info);
return $this->view->fetch();
}
$delivery_info = (array)$this->request->post('delivery_info/a');
//判断订单是否发货 发货提示 已发货
$orderInfo = $this->model->get($order_id);
// var_dump($orderInfo['warehouse_id']);exit();
//订单不存在 提示
if (!$orderInfo) {
$this->error('订单不存在');
}
if ($orderInfo['delivery_status'] == OrderModel::DISPATCH_STATUS_SENDED) {
$this->error('该订单已发货');
}
//商品库存不足提示
$orderItem = OrderItem::where('order_id', $order_id)->where('goods_id', $goods_id)->select();
$goods_sum = 0;
$stock_sum = 0;
foreach ($orderItem as $item) {
$goods_sum += $item['goods_num'];
$goodsSkuPrice = GoodsSkuPrice::get($item['goods_sku_price_id']);
$stock_sum += $goodsSkuPrice['stock'];
}
if ($stock_sum < $goods_sum) {
$this->error('商品库存不足');
}
//更新订单发货状态
$orderInfo->delivery_status = OrderModel::DISPATCH_STATUS_SENDED;
//更新配送信息
$orderInfo->delivery_info = json_encode($delivery_info);
$orderInfo->save();
//更新商品发货状态
$orderItem = OrderItem::where('order_id', $order_id)->where('goods_id', $goods_id)->select();
foreach ($orderItem as $item) {
$item->dispatch_status = OrderModel::DISPATCH_STATUS_SENDED;
$item->dispatch_type = 'store';//仓库发货
$item->ext = json_encode(['dispatch_date' => $delivery_info['time']]);//配送时间
$item->save();
//更新商品库存
$goodsSkuPrice = GoodsSkuPrice::get($item['goods_sku_price_id']);
$goodsSkuPrice->stock = $goodsSkuPrice->stock - $item['goods_num'];
$goodsSkuPrice->save();
//记录库存流水
Db::name('ykjp_inventory_statement')->insert([
'product_id' => $item['goods_sku_price_id'],
'warehouse_id' => $orderInfo['warehouse_id'],
'partition_id' =>0,
'number' => $item['goods_num'],
'type' => '销售出库',
'updatetime' => time(),
]);
}
$this->success('发货成功');
}
//获取进销存用户
public function getJxcUser()
{
//当前页
$page = $this->request->request("pageNumber");
//分页大小
$pagesize = $this->request->request("pageSize");
//主键
$primarykey = $this->request->request("keyField");
//主键值
$primaryvalue = $this->request->request("keyValue");
//搜索字段
$search_field = $this->request->request("showField");
$search_val = $this->request->request($search_field);
if ($primarykey) {
//获取当前企业账号
$list = User::where('user_type',1);
if ($primaryvalue) {
$list->where([$primarykey => ['in', $primaryvalue]]);
$pagesize = 999999;
}
if ($search_val) {
$list->where($search_field, "like", "%{$search_val}%");
}
$data = $list->field(['id', $search_field])
->order('id', 'DESC')
->page($page,$pagesize)
->select();
$total = $data->count();
return json(['list' => $data, 'total' => $total]);
}
}
}