885 行
39 KiB
PHP
可执行文件
885 行
39 KiB
PHP
可执行文件
<?php
|
|
|
|
namespace CashierApi\Controller;
|
|
|
|
//[1.8.50]
|
|
use Common\Business\Goods;
|
|
|
|
/**
|
|
* 收银端添加商品
|
|
*/
|
|
class CreateGoodsController extends BaseController
|
|
{
|
|
/* 收银机获取商品分类
|
|
* */
|
|
public function getCate()
|
|
{
|
|
if (IS_POST) {
|
|
$merchant_id = self::$merchant_id;//商户id;
|
|
$industry_id = self::$store['industry_id'];
|
|
$type = I('post.type');
|
|
if (!$merchant_id) {
|
|
$rt['status'] = 0;
|
|
$rt['msg'] = '参数缺失';
|
|
$this->ajaxReturn($rt);
|
|
}
|
|
$retailGoodsClass = D('RetailGoodsClass');
|
|
$w = array($retailGoodsClass::MERCHANT_ID => $merchant_id, $retailGoodsClass::STATUS => 0);
|
|
if (!empty($industry_id)) {
|
|
$w['industry_id'] = ['in', [0, $industry_id]];
|
|
$w1['industry_id'] = ['in', [0, $industry_id]];
|
|
$w2['industry_id'] = ['in', [0, $industry_id]];
|
|
}
|
|
$w[$retailGoodsClass::TYPE] = 1; // 一级分类
|
|
$one = $retailGoodsClass->getSelect($w, $retailGoodsClass::ID . ',' . $retailGoodsClass::CLASS_NAME, $retailGoodsClass::CODE . ' asc');
|
|
if ($one) {
|
|
foreach ($one as $k1 => $v1) {
|
|
$w1[$retailGoodsClass::TYPE] = 2;
|
|
$w1[$retailGoodsClass::PID] = $v1[$retailGoodsClass::ID];
|
|
$w1[$retailGoodsClass::STATUS] = 0;
|
|
$two = $retailGoodsClass->getSelect($w1, $retailGoodsClass::ID . ',' . $retailGoodsClass::CLASS_NAME, $retailGoodsClass::CODE . ' asc');
|
|
if ($two) {
|
|
foreach ($two as $k2 => $v2) {
|
|
$w2[$retailGoodsClass::TYPE] = 3;
|
|
$w2[$retailGoodsClass::PID] = $v2[$retailGoodsClass::ID];
|
|
$w2[$retailGoodsClass::STATUS] = 0;
|
|
$three = $retailGoodsClass->getSelect($w2, $retailGoodsClass::ID . ',' . $retailGoodsClass::CLASS_NAME, $retailGoodsClass::CODE . ' asc');
|
|
if ($three) {
|
|
if ($type) {
|
|
$two[$k2]['childtwo'] = $three;
|
|
} else {
|
|
$two[$k2]['child'] = $three;
|
|
}
|
|
$two[$k2]['haschild'] = 1;
|
|
$two[$k2]['show'] = false;
|
|
} else {
|
|
if ($type) {
|
|
$two[$k2]['childtwo'] = array();
|
|
} else {
|
|
$two[$k2]['child'] = array();
|
|
}
|
|
$two[$k2]['haschild'] = 0;
|
|
}
|
|
}
|
|
if ($type) {
|
|
$one[$k1]['childone'] = $two;
|
|
} else {
|
|
$one[$k1]['child'] = $two;
|
|
}
|
|
$one[$k1]['haschild'] = 1;
|
|
$one[$k1]['show'] = false;
|
|
} else {
|
|
if ($type) {
|
|
$one[$k1]['childone'] = array();
|
|
} else {
|
|
$one[$k1]['child'] = array();
|
|
}
|
|
$one[$k1]['haschild'] = 0;
|
|
}
|
|
}
|
|
}
|
|
$goodsclass = $one;
|
|
if ($goodsclass) {
|
|
$rt['status'] = 1;
|
|
$rt['msg'] = '搜索成功';
|
|
$rt['goods_cate'] = $goodsclass;
|
|
} else {
|
|
$rt['status'] = 0;
|
|
$rt['msg'] = '搜索失败';
|
|
}
|
|
$this->ajaxReturn($rt);
|
|
}
|
|
}
|
|
|
|
/* 收银机添加分类
|
|
* @param $class_id 上级分类id
|
|
* @param $type 分类类别
|
|
* @param $merchantid 商户id
|
|
* @param $class_name 分类名称
|
|
* @param $code 分类编码
|
|
* @return array
|
|
* */
|
|
public function addCate()
|
|
{
|
|
$class_id = I('post.class_id') ? I('post.class_id') : 0; // 分类id
|
|
$type = I('post.type'); // 1.顶级分类 2.二级分类 3.三级分类
|
|
$merchant_id = self::$merchant_id;//商户id; // 商户id
|
|
$store_id = self::$store_id; //门店id
|
|
$industry_id = self::$store['industry_id'];
|
|
$class_name = I('post.class_name'); // 分类名称
|
|
$code = I('post.code'); // 编码
|
|
if (!$type || !$merchant_id || !$class_name) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '参数缺失';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
/* 判断分类是否已被使用 */
|
|
if ($class_id && $class_id != 0) {
|
|
$retailGoods = D('RetailGoods');
|
|
$retailMgoods = D('RetailMgoods');
|
|
$goods = $retailGoods->getFind(array($retailGoods::GOODS_CLASS_ID => $class_id, $retailGoods::STATUS => array('neq', 2)));
|
|
$mGoods = $retailMgoods->getFind(array($retailMgoods::GOODS_CLASS_ID => $class_id, $retailMgoods::STATUS => array('neq', 2)));
|
|
if ($goods || $mGoods) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '该上级分类下已存在商品,不能添加下级分类';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
}
|
|
$retailGoodsClass = D('RetailGoodsClass');
|
|
/* 判断分类是否存在 */
|
|
$find_class = $retailGoodsClass->getFind(array($retailGoodsClass::PID => $class_id, $retailGoodsClass::STATUS => 0, $retailGoodsClass::CLASS_NAME => $class_name));
|
|
if ($find_class) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '同级分类下该分类已存在';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
/* 判断编号 */
|
|
if ($code) {
|
|
$add[$retailGoodsClass::CODE] = $code;
|
|
} else {
|
|
$result = $this->auto_class_code($merchant_id, $class_id); // 自动获取分类编码
|
|
if ($result['status'] == 1) {
|
|
$add[$retailGoodsClass::CODE] = $result['code'];
|
|
} else {
|
|
$res['status'] = 0;
|
|
$res['msg'] = $result['msg'];
|
|
$this->ajaxReturn($res);
|
|
}
|
|
}
|
|
$add[$retailGoodsClass::CLASS_NAME] = $class_name;
|
|
$add[$retailGoodsClass::TYPE] = $type;
|
|
$add[$retailGoodsClass::MERCHANT_ID] = $merchant_id;
|
|
$add[$retailGoodsClass::ADD_TIME] = time();
|
|
$add[$retailGoodsClass::STATUS] = 0;
|
|
$add[$retailGoodsClass::PID] = $class_id;
|
|
$add['store_id'] = $store_id;
|
|
$add['industry_id'] = $industry_id;
|
|
$add['add_type'] = 2;
|
|
$create = $retailGoodsClass->addOrder($add);
|
|
if ($create) {
|
|
$res['status'] = 1;
|
|
$res['msg'] = '添加成功';
|
|
} else {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '添加失败';
|
|
}
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
/* 自动获取分类编码
|
|
@param $merchantid 商户id
|
|
@param $pid 上级分类id
|
|
* @return array
|
|
* */
|
|
public function auto_class_code($merchantid, $pid)
|
|
{
|
|
$retailGoodsClass = D('RetailGoodsClass');
|
|
if ($pid == 0) { // 顶级分类
|
|
$code = $retailGoodsClass->getMax(array($retailGoodsClass::TYPE => 1, $retailGoodsClass::STATUS => 0, $retailGoodsClass::MERCHANT_ID => $merchantid), $retailGoodsClass::CODE);
|
|
if ($code) {
|
|
if (!is_numeric($code)) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '一级分类最大编码中存在非数字,无法自动编号';
|
|
return $res;
|
|
} else {
|
|
$temp_code = $code + 1;
|
|
}
|
|
} else {
|
|
$temp_code = 1;
|
|
}
|
|
} else { // 其他
|
|
/* 获取上级分类 */
|
|
$class = $retailGoodsClass->getFind(array($retailGoodsClass::ID => $pid, $retailGoodsClass::STATUS => 0));
|
|
if (!$class) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '未查询到上级分类';
|
|
return $res;
|
|
}
|
|
if (!is_numeric($class['code'])) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '上级分类序号非数字,无法自动编号';
|
|
return $res;
|
|
} else {
|
|
$code = $retailGoodsClass->getMax(array($retailGoodsClass::TYPE => 1, $retailGoodsClass::STATUS => 0, $retailGoodsClass::MERCHANT_ID => $merchantid), $retailGoodsClass::CODE);
|
|
if ($code) {
|
|
if (!is_numeric($code)) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '同级分类最大编码中存在非数字,无法自动编号';
|
|
return $res;
|
|
} else {
|
|
$temp_code = $code + 1;
|
|
}
|
|
} else {
|
|
$temp_code = $class['code'] . '1';
|
|
}
|
|
}
|
|
|
|
}
|
|
if ($temp_code) {
|
|
$res['status'] = 1;
|
|
$res['code'] = $temp_code;
|
|
} else {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '获取失败';
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
/*收银端当日添加商品的列表
|
|
* @param $merchant_id 商户id
|
|
* @param $store_id 门店id
|
|
* @param $source_type //2收银端添加
|
|
*/
|
|
public function getTodayGoods()
|
|
{
|
|
$merchant_id = self::$merchant_id;//商户id;
|
|
$store_id = self::$store_id;//门店id;
|
|
$page = I('post.page');
|
|
$limit = I('post.limit');
|
|
$stime = time();
|
|
$stat_time = strtotime(date('Y-m-d 00:00:00', $stime));
|
|
$end_time = $stat_time + 86399;
|
|
$retailGoods = D('RetailGoods');
|
|
$retailGoodsClass = D('RetailGoodsClass');
|
|
$where['g.' . $retailGoods::MERCHANT_ID] = $merchant_id;
|
|
$where['g.' . $retailGoods::STORE_ID] = $store_id;
|
|
$where['g.' . $retailGoods::STATUS] = 0;
|
|
$where['g.' . $retailGoods::SOURCE_TYPE] = 3;
|
|
$where['g.' . $retailGoods::ADD_TIME] = array('between', array($stat_time, $end_time));
|
|
$field = array(
|
|
'g.' . $retailGoods::GOODS_NAME,
|
|
'g.' . $retailGoods::GOODS_NUMBER,
|
|
'g.' . $retailGoods::RETAIL_PRICE,
|
|
'g.' . $retailGoods::ONLINE_PRICE,
|
|
'g.' . $retailGoods::PURCHASE_PRICE,
|
|
'g.' . $retailGoods::GOODS_NUM,
|
|
'g.' . $retailGoods::LESS_NUM,
|
|
'g.' . $retailGoods::MORE_NUM,
|
|
'g.' . $retailGoods::GOODS_CODE,
|
|
'g.' . $retailGoods::ADD_TIME,
|
|
'c.' . $retailGoodsClass::CLASS_NAME,
|
|
);
|
|
$goodsAll = $retailGoods->getCashierGoodsList($where, $field, $retailGoods::ADD_TIME . " desc", $page . ',' . $limit);
|
|
$goodsList = $goodsAll['list'];
|
|
$goodsCount = $goodsAll['count'];
|
|
// $goods_info = M('retail_goods as g')
|
|
// ->join("ls_retail_goods_class as c on g.goods_class_id=c.id")
|
|
// ->where($where)
|
|
// ->page($page, $limit)
|
|
// ->order("addtime desc")
|
|
// ->field("g.goods_name,g.good_number,g.retail_price,g.online_price,g.purch_price,g.goods_num,g.less_num,g.more_num,g.goods_code,g.addtime,c.class_name")
|
|
// ->select();
|
|
if ($goodsList) {
|
|
foreach ($goodsList as $key => &$val) {
|
|
$val[$retailGoods::ADD_TIME] = date('Y-m-d H:i:s', $val[$retailGoods::ADD_TIME]);
|
|
}
|
|
}
|
|
$res['status'] = 1;
|
|
$res['goods_info'] = $goodsList;
|
|
$res['count'] = $goodsCount ? $goodsCount : 0;
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
/* 收银机添加商品
|
|
* @param $store_id 门店id
|
|
* @param $merchant_id 商户id
|
|
* @param $cashier_id 收银员id
|
|
* @param $goods_code 条码
|
|
* @param $goods_number 货号
|
|
* @param $goods_name 品名
|
|
* @param $retail_price 店内售价
|
|
* @param $online_price 商城售价
|
|
* @param $purch_price 进价
|
|
* @param $goods_class_id 分类
|
|
* @param $goods_num 库存
|
|
* @param $more_num 超量
|
|
* @param $less_num 预警
|
|
* @return array
|
|
* */
|
|
public function addGoods()
|
|
{
|
|
$cashier_id = self::$cashier_id;//收银员id;
|
|
$merchant_id = self::$merchant_id;//商户id;
|
|
$store_id = self::$store_id;//门店id 查询门店优惠券
|
|
if (!$cashier_id || !$store_id || !$merchant_id) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '收营员登入过期';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
/* 添加商品权限 1.开启 2.关闭 查看进价权限 1.开启 2.关闭 */
|
|
$storeJurisdiction = D('StoreJurisdiction');
|
|
$jurisdiction = $storeJurisdiction->getOrderFind(array($storeJurisdiction::STORE_ID => $store_id), $storeJurisdiction::SHOW_PURCH_PRICE . ',' . $storeJurisdiction::ADD_GOODS);
|
|
if (!$jurisdiction[$storeJurisdiction::SHOW_PURCH_PRICE] || $jurisdiction[$storeJurisdiction::SHOW_PURCH_PRICE] == 2 || !$jurisdiction[$storeJurisdiction::ADD_GOODS] || $jurisdiction[$storeJurisdiction::ADD_GOODS] == 2) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '该门店暂无添加商品权限';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$retailCashier = D('RetailCashier');
|
|
$cashier_add_goods = $retailCashier->getOrderFind(array($retailCashier::ID => $cashier_id, $retailCashier::STATUS => 1), $retailCashier::ADD_GOODS_STATE);
|
|
if (!$cashier_add_goods || $cashier_add_goods == 2) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '暂无添加商品权限';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$warehouse_id = D('SSaleWarehouse')->getFindField(array('merchant_id' => $merchant_id, 'status' => 1, 'store_id' => $store_id), "id"); //销售仓库id
|
|
if (!$warehouse_id) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = "请先创建销售仓";
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$retailGoods = D('RetailGoods');
|
|
$add[$retailGoods::STORE_ID] = $store_id; // 门店id
|
|
$add[$retailGoods::MERCHANT_ID] = $merchant_id; // 商户id
|
|
$goods_code = I('post.goods_code'); // 条码
|
|
$goods_number = I('post.goods_number'); // 货号
|
|
$goods_name = I('post.goods_name'); // 品名
|
|
$retail_price = I('post.retail_price'); // 店内售价
|
|
$online_price = I('post.online_price'); // 商城售价
|
|
$purch_price = I('post.purch_price'); // 进价
|
|
$goods_class_id = I('post.goods_class_id'); // 分类
|
|
$goods_num = is_numeric(I('post.goods_num')) ? I('post.goods_num') : 0; // 库存
|
|
$more_num = I('post.more_num'); // 超量
|
|
$less_num = I('post.less_num'); // 预警
|
|
$pricing_method = I('post.pricing_method'); // 称重
|
|
$specifications = I('post.specification'); //规格
|
|
$company = I('post.company'); //单位
|
|
$origin = I('post.origin'); //产地
|
|
if ($pricing_method == 2 && $company == '') {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '请输入单位';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
if (!$goods_name || !$goods_class_id) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '请完善表单';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
/* 条码 */
|
|
if ($goods_code) {
|
|
$ids_c = $retailGoods->getOrderFind(array($retailGoods::STORE_ID => $store_id, $retailGoods::GOODS_CODE => $goods_code, $retailGoods::STATUS => array('in', '0,1')), $retailGoods::ID);
|
|
if ($ids_c) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '条码已存在';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$add[$retailGoods::GOODS_CODE] = $goods_code;
|
|
} else {
|
|
$j = 0;
|
|
while (1) {
|
|
$code = rand(1000000000, 9999999999);
|
|
if ($j > 30) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '条码生成失败,请重试';
|
|
$this->ajaxReturn($res);
|
|
break;
|
|
}
|
|
$id_g = $retailGoods->getOrderFind(array($retailGoods::STORE_ID => $store_id, $retailGoods::GOODS_CODE => $code, $retailGoods::STATUS => array('in', '0,1')), $retailGoods::ID);
|
|
if ($id_g) {
|
|
$j++;
|
|
continue;
|
|
}
|
|
$retailSkuGoods = D('RetailSkuGoods');
|
|
$id_sg = $retailSkuGoods->getOrderFind(array($retailSkuGoods::STORE_ID => $store_id, $retailSkuGoods::SKU_CODE => $code, $retailSkuGoods::STATUS => 0), $retailSkuGoods::ID);
|
|
if ($id_sg) {
|
|
$j++;
|
|
continue;
|
|
}
|
|
$add[$retailGoods::GOODS_CODE] = $code; // 生成成功 跳出循环
|
|
break;
|
|
}
|
|
}
|
|
/* 货号 */
|
|
if ($goods_number) {
|
|
$ids_n = $retailGoods->getOrderFind(array($retailGoods::STORE_ID => $store_id, $retailGoods::GOODS_NUMBER => $goods_number, $retailGoods::STATUS => array('in', '0,1')), $retailGoods::ID);
|
|
if ($ids_n) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '货号已存在';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$add[$retailGoods::GOODS_NUMBER] = $goods_number;
|
|
} else {
|
|
$i = 0;
|
|
while (1) {
|
|
if ($i > 30) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '货号生成失败,请重试';
|
|
$this->ajaxReturn($res);
|
|
break;
|
|
}
|
|
$num = rand(1000, 99999);
|
|
$id = $retailGoods->getOrderFind(array($retailGoods::STORE_ID => $store_id, $retailGoods::GOODS_NUMBER => $num, $retailGoods::STATUS => array('in', '0,1')), $retailGoods::ID);
|
|
if (!$id) {
|
|
$add[$retailGoods::GOODS_NUMBER] = $num; // 生成成功 跳出循环
|
|
break;
|
|
}
|
|
$i++;
|
|
}
|
|
}
|
|
/* 店内售价 */
|
|
if ($retail_price <= 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '店内售价不能小于等于0';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
if (!is_numeric($retail_price)) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '店内售价数值不正确';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
/* 商城售价 */
|
|
if ($online_price <= 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '商城售价不能小于等于0';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
if (!is_numeric($online_price) && $more_num != 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '商城售价数值不正确';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
/* 进价 */
|
|
if ($purch_price <= 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '进价不能小于等于0';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
if (!is_numeric($purch_price) && $more_num != 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '进价数值不正确';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
/* 库存 */
|
|
if ($goods_num < 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '库存数值不能小于0';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
if (!is_numeric($goods_num) && $more_num != 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '库存数值不正确';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
/* 超量 */
|
|
if ($more_num < 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '超量数值不能小于0';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
if (!is_numeric($more_num) && $more_num != 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '超量数值不正确';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
/* 预警 */
|
|
if ($less_num < 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '预警数值不能小于0';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
if (!is_numeric($less_num) && $less_num != 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '预警数值不正确';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$add[$retailGoods::GOODS_NAME] = $goods_name;
|
|
$add[$retailGoods::RETAIL_PRICE] = $retail_price;
|
|
$add[$retailGoods::ONLINE_PRICE] = $online_price;
|
|
$add[$retailGoods::PURCHASE_PRICE] = $purch_price;
|
|
$add[$retailGoods::GOODS_CLASS_ID] = $goods_class_id;
|
|
$add[$retailGoods::GOODS_NUM] = $goods_num;
|
|
$add[$retailGoods::MORE_NUM] = $more_num && $more_num > 0 ? $more_num : 0;
|
|
$add[$retailGoods::LESS_NUM] = $less_num && $less_num > 0 ? $less_num : 0;
|
|
$add[$retailGoods::STATUS] = 0;
|
|
$add[$retailGoods::ADD_TIME] = time();
|
|
$add[$retailGoods::SOURCE_TYPE] = 3; //1店长添加 2商户同步 3收银员添加)
|
|
$add[$retailGoods::CASHIER_ID] = $cashier_id; //收银员id
|
|
$add[$retailGoods::PRICING_METHOD] = $pricing_method == 2 ? 2 : 1; //是否称重
|
|
$add['specification'] = $specifications ? $specifications : ''; //规格
|
|
$add['company'] = $company ? $company : ''; //单位
|
|
if (!empty($origin)) {
|
|
$origin_info = M('retail_goods_yieldly')->where(array('merchantid' => $merchant_id, 'storeid' => $store_id, 'name' => $origin))->find();
|
|
if ($origin_info) {
|
|
$goods_yieldly_id = $origin['id'];
|
|
} else {
|
|
$origin_data = array(
|
|
'name' => $origin,
|
|
'merchantid' => $merchant_id,
|
|
'storeid' => $store_id,
|
|
'addtime' => time(),
|
|
'state' => 0
|
|
);
|
|
$goods_yieldly_id = M('retail_goods_yieldly')->add($origin_data);
|
|
if (!$goods_yieldly_id) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '产地添加失败';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
}
|
|
$add['goods_yieldly_id'] = $goods_yieldly_id;
|
|
}
|
|
$create = $retailGoods->addOrder($add);
|
|
if ($create) {
|
|
$add['warehouse_id'] = $warehouse_id;
|
|
$add['goods_id'] = $create;
|
|
$addSaleGoods = A('Api/Inventory')->addSaleGoods($add, 1);
|
|
if ($addSaleGoods['status'] == 1) {
|
|
$res['status'] = 1;
|
|
$res['msg'] = '添加成功';
|
|
$this->ajaxReturn($res);
|
|
} else {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '添加失败';
|
|
}
|
|
} else {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '添加失败';
|
|
}
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
/* 收银机添加商品
|
|
* @param $store_id 门店id
|
|
* @param $merchant_id 商户id
|
|
* @param $cashier_id 收银员id
|
|
* @param $goods_code 条码
|
|
* @param $goods_number 货号
|
|
* @param $goods_name 品名
|
|
* @param $retail_price 店内售价
|
|
* @param $online_price 商城售价
|
|
* @param $purch_price 进价
|
|
* @param $goods_class_id 分类
|
|
* @param $goods_num 库存
|
|
* @param $more_num 超量
|
|
* @param $less_num 预警
|
|
* @param weight_value 重量
|
|
* @param third_goods_code 第三方商品编码
|
|
* @return array
|
|
* */
|
|
public function addGoods_v1()
|
|
{
|
|
$cashier_id = self::$cashier_id;//收银员id;
|
|
$merchant_id = self::$merchant_id;//商户id;
|
|
$store_id = self::$store_id;//门店id 查询门店优惠券
|
|
if (!$cashier_id || !$store_id || !$merchant_id) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '收营员登入过期';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
/* 添加商品权限 1.开启 2.关闭 查看进价权限 1.开启 2.关闭 */
|
|
$storeJurisdiction = D('StoreJurisdiction');
|
|
$jurisdiction = $storeJurisdiction->getOrderFind(array($storeJurisdiction::STORE_ID => $store_id), $storeJurisdiction::SHOW_PURCH_PRICE . ',' . $storeJurisdiction::ADD_GOODS);
|
|
if (!$jurisdiction[$storeJurisdiction::SHOW_PURCH_PRICE] || $jurisdiction[$storeJurisdiction::SHOW_PURCH_PRICE] == 2 || !$jurisdiction[$storeJurisdiction::ADD_GOODS] || $jurisdiction[$storeJurisdiction::ADD_GOODS] == 2) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '该门店暂无添加商品权限';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$retailCashier = D('RetailCashier');
|
|
$cashier_add_goods = $retailCashier->getOrderFind(array($retailCashier::ID => $cashier_id, $retailCashier::STATUS => 1), $retailCashier::ADD_GOODS_STATE);
|
|
if (!$cashier_add_goods || $cashier_add_goods == 2) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '暂无添加商品权限';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$warehouse_id = D('SSaleWarehouse')->getFindField(array('merchant_id' => $merchant_id, 'status' => 1, 'store_id' => $store_id), "id"); //销售仓库id
|
|
if (!$warehouse_id) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = "请先创建销售仓";
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$retailGoods = D('RetailGoods');
|
|
$add[$retailGoods::STORE_ID] = $store_id; // 门店id
|
|
$add[$retailGoods::MERCHANT_ID] = $merchant_id; // 商户id
|
|
$goods_code = I('post.goods_code'); // 条码
|
|
$goods_number = I('post.goods_number'); // 货号
|
|
$goods_name = I('post.goods_name'); // 品名
|
|
$retail_price = I('post.retail_price'); // 店内售价
|
|
$online_price = I('post.online_price'); // 商城售价
|
|
$purch_price = I('post.purch_price'); // 进价
|
|
$goods_class_id = I('post.goods_class_id'); // 分类
|
|
$goods_num = is_numeric(I('post.goods_num')) ? I('post.goods_num') : 0; // 库存
|
|
$more_num = I('post.more_num'); // 超量
|
|
$less_num = I('post.less_num'); // 预警
|
|
$pricing_method = I('post.pricing_method'); // 称重
|
|
$specifications = I('post.specification'); //规格
|
|
$company = I('post.company'); //单位
|
|
$origin = I('post.origin'); //产地
|
|
$weight_value = I('post.weight_value'); //重量
|
|
$goods_image = I('post.goods_image'); //商品云库图片
|
|
if (self::$merchant['platform_type'] == 2) {
|
|
//第三方平台
|
|
$third_goods_code = I('post.third_goods_code'); //第三方商品编码
|
|
if (empty($third_goods_code)) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '请输入第三方的商品编码';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
}
|
|
if ($pricing_method == 2) {
|
|
if (empty($company)) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '请输入单位';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
switch ($company) {
|
|
case 'g':
|
|
case 'G':
|
|
case '克':
|
|
$weight_value = 0.001;
|
|
break;
|
|
case '斤':
|
|
case '市斤':
|
|
$weight_value = 0.5;
|
|
break;
|
|
default:
|
|
$weight_value = 1;
|
|
break;
|
|
}
|
|
} else {
|
|
if (!empty($weight_value) && !preg_match("/^[+-]?\d*\.?\d{0,3}$/", $weight_value)) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '请输入正确的重量';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
}
|
|
if (!$goods_name || !$goods_class_id) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '请完善表单';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$item = array(
|
|
'add_time' => time(),
|
|
'from_type_id' => 28,
|
|
'localUrl' => 'Store/Goods',
|
|
'user_type' => 4,
|
|
'merchant_id' => $merchant_id,
|
|
'store_id' => $store_id
|
|
);
|
|
$uploads = uploadFiles($item);//本次请求的所有图片上传
|
|
if (!empty($uploads['image_file'])) {
|
|
$add['goods_img_id'] = $uploads['image_file']['id']; //商品封面图
|
|
}
|
|
/* 条码 */
|
|
if ($goods_code) {
|
|
$ids_c = $retailGoods->getOrderFind(array($retailGoods::STORE_ID => $store_id, $retailGoods::GOODS_CODE => $goods_code, $retailGoods::STATUS => array('in', '0,1')), $retailGoods::ID);
|
|
if ($ids_c) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '条码已存在';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
if (!$add['goods_img_id'] && $goods_image) {
|
|
$item['path'] = $goods_image;
|
|
$goodsApi = new \Common\Business\Goods(); //商品类
|
|
$codeImage = $goodsApi->basisCodeAddImageGallery($item); //阿里云条码查询的商品图片存入库图片库
|
|
$add['goods_img_id'] = $codeImage['goods_image']['id'];
|
|
}
|
|
$add[$retailGoods::GOODS_CODE] = $goods_code;
|
|
} else {
|
|
$j = 0;
|
|
while (1) {
|
|
$code = rand(1000000000, 9999999999);
|
|
if ($j > 30) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '条码生成失败,请重试';
|
|
$this->ajaxReturn($res);
|
|
break;
|
|
}
|
|
$id_g = $retailGoods->getOrderFind(array($retailGoods::STORE_ID => $store_id, $retailGoods::GOODS_CODE => $code, $retailGoods::STATUS => array('in', '0,1')), $retailGoods::ID);
|
|
if ($id_g) {
|
|
$j++;
|
|
continue;
|
|
}
|
|
$retailSkuGoods = D('RetailSkuGoods');
|
|
$id_sg = $retailSkuGoods->getOrderFind(array($retailSkuGoods::STORE_ID => $store_id, $retailSkuGoods::SKU_CODE => $code, $retailSkuGoods::STATUS => 0), $retailSkuGoods::ID);
|
|
if ($id_sg) {
|
|
$j++;
|
|
continue;
|
|
}
|
|
$add[$retailGoods::GOODS_CODE] = $code; // 生成成功 跳出循环
|
|
break;
|
|
}
|
|
}
|
|
/* 货号 */
|
|
if ($goods_number) {
|
|
$ids_n = $retailGoods->getOrderFind(array($retailGoods::STORE_ID => $store_id, $retailGoods::GOODS_NUMBER => $goods_number, $retailGoods::STATUS => array('in', '0,1')), $retailGoods::ID);
|
|
if ($ids_n) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '货号已存在';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$add[$retailGoods::GOODS_NUMBER] = $goods_number;
|
|
} else {
|
|
$i = 0;
|
|
while (1) {
|
|
if ($i > 30) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '货号生成失败,请重试';
|
|
$this->ajaxReturn($res);
|
|
break;
|
|
}
|
|
$num = rand(1000, 99999);
|
|
$id = $retailGoods->getOrderFind(array($retailGoods::STORE_ID => $store_id, $retailGoods::GOODS_NUMBER => $num, $retailGoods::STATUS => array('in', '0,1')), $retailGoods::ID);
|
|
if (!$id) {
|
|
$add[$retailGoods::GOODS_NUMBER] = $num; // 生成成功 跳出循环
|
|
break;
|
|
}
|
|
$i++;
|
|
}
|
|
}
|
|
/* 店内售价 */
|
|
if ($retail_price <= 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '店内售价不能小于等于0';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
if (!is_numeric($retail_price)) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '店内售价数值不正确';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
/* 商城售价 */
|
|
if ($online_price <= 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '商城售价不能小于等于0';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
if (!is_numeric($online_price) && $more_num != 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '商城售价数值不正确';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
/* 进价 */
|
|
if ($purch_price <= 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '进价不能小于等于0';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
if (!is_numeric($purch_price) && $more_num != 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '进价数值不正确';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
/* 库存 */
|
|
if ($goods_num < 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '库存数值不能小于0';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
if (!is_numeric($goods_num) && $more_num != 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '库存数值不正确';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
/* 超量 */
|
|
if ($more_num < 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '超量数值不能小于0';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
if (!is_numeric($more_num) && $more_num != 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '超量数值不正确';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
/* 预警 */
|
|
if ($less_num < 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '预警数值不能小于0';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
if (!is_numeric($less_num) && $less_num != 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '预警数值不正确';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$add[$retailGoods::GOODS_NAME] = $goods_name;
|
|
$add[$retailGoods::RETAIL_PRICE] = $retail_price;
|
|
$add[$retailGoods::ONLINE_PRICE] = $online_price;
|
|
$add[$retailGoods::PURCHASE_PRICE] = $purch_price;
|
|
$add[$retailGoods::GOODS_CLASS_ID] = $goods_class_id;
|
|
$add[$retailGoods::GOODS_NUM] = $goods_num;
|
|
$add[$retailGoods::MORE_NUM] = $more_num && $more_num > 0 ? $more_num : 0;
|
|
$add[$retailGoods::LESS_NUM] = $less_num && $less_num > 0 ? $less_num : 0;
|
|
$add[$retailGoods::STATUS] = 0;
|
|
$add[$retailGoods::ADD_TIME] = time();
|
|
$add[$retailGoods::SOURCE_TYPE] = 3; //1店长添加 2商户同步 3收银员添加)
|
|
$add[$retailGoods::CASHIER_ID] = $cashier_id; //收银员id
|
|
$add[$retailGoods::PRICING_METHOD] = $pricing_method == 2 ? 2 : 1; //是否称重
|
|
$add['specification'] = $specifications ? $specifications : ''; //规格
|
|
$add['company'] = $company ? $company : ''; //单位
|
|
$add['weight_value'] = $weight_value ? $weight_value : null; //重量
|
|
if (!empty($origin)) {
|
|
$origin_info = M('retail_goods_yieldly')->where(array('merchantid' => $merchant_id, 'storeid' => $store_id, 'name' => $origin))->find();
|
|
if ($origin_info) {
|
|
$goods_yieldly_id = $origin['id'];
|
|
} else {
|
|
$origin_data = array(
|
|
'name' => $origin,
|
|
'merchantid' => $merchant_id,
|
|
'storeid' => $store_id,
|
|
'addtime' => time(),
|
|
'state' => 0
|
|
);
|
|
$goods_yieldly_id = M('retail_goods_yieldly')->add($origin_data);
|
|
if (!$goods_yieldly_id) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '产地添加失败';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
}
|
|
$add['goods_yieldly_id'] = $goods_yieldly_id;
|
|
}
|
|
if (self::$merchant['platform_type'] == 2) {
|
|
//第三方平台
|
|
$ThirdGoodsClass = new \LittleGift\Business\Goods();
|
|
$verifyResult = $ThirdGoodsClass->verifyQ3Code([
|
|
'merchant_id' => self::$merchant_id,
|
|
'third_goods_code' => $third_goods_code
|
|
]);
|
|
if ($verifyResult['status'] != 1) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = $verifyResult['message'];
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$verifyCode = $ThirdGoodsClass->GoodsVerify([
|
|
'merchant_id' => self::$merchant_id,
|
|
'code_arr' => [$third_goods_code]
|
|
]);
|
|
if ($verifyCode['status'] == 0) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '添加失败' . 'Q3 编码验证失败';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
if (!empty($verifyCode['data']['arr_diff'])) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '添加失败' . 'Q3 编码验证失败';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
}
|
|
$create = $retailGoods->addOrder($add);
|
|
if ($create) {
|
|
$add['warehouse_id'] = $warehouse_id;
|
|
$add['goods_id'] = $create;
|
|
unset($add['weight_value']);
|
|
$addSaleGoods = A('Api/Inventory')->addSaleGoods($add, 1);
|
|
if ($addSaleGoods['status'] == 1) {
|
|
$GoodsClass = new Goods();
|
|
$GoodsClass->goodsEditRecord([
|
|
'merchant_id' => $merchant_id,
|
|
'store_id' => $store_id,
|
|
'add_time' => $_SERVER['REQUEST_TIME'],
|
|
'goods_id' => $create
|
|
]);
|
|
$res['status'] = 1;
|
|
$res['msg'] = '添加成功';
|
|
$this->ajaxReturn($res);
|
|
} else {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '添加失败';
|
|
}
|
|
} else {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '添加失败';
|
|
}
|
|
$this->ajaxReturn($res);
|
|
}
|
|
} |