Activity.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\common\Ocr;
  4. use app\admin\model\ActionLog;
  5. use app\admin\model\DataGroup as DataGroupModel;
  6. use app\admin\model\ProcessOrder;
  7. use app\BaseController;
  8. use think\App;
  9. use think\facade\Db;
  10. class Activity extends Base
  11. {
  12. public $noble=[];
  13. public function __construct(App $app)
  14. {
  15. parent::__construct($app);
  16. $this->noble=\think\facade\Config::get("noble");
  17. }
  18. //活动列表
  19. public function list(){
  20. $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
  21. $size = isset($this->post['size']) && $this->post['size'] !==""? intval($this->post['size']):"10";
  22. $where = [["ga.is_del","=",0]];
  23. $activity_name = isset($this->post['activity_name']) && $this->post['activity_name'] !=="" ? trim($this->post['activity_name']):"";
  24. if($activity_name!=""){
  25. $where[]=['ga.activity_name',"like","%$activity_name%"];
  26. }
  27. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  28. if($status!==""){
  29. $where[]=['ga.status',"=",$status];
  30. }
  31. $activity_code = isset($this->post['activity_code']) && $this->post['activity_code'] !=="" ? trim($this->post['activity_code']):"";
  32. if($activity_code!==""){
  33. $where[]=['ga.activity_code',"like","%$activity_code%"];
  34. }
  35. $starttime = isset($this->post['starttime']) && $this->post['starttime']!=="" ? $this->post['starttime']:"";
  36. if($starttime!==""){
  37. $where[]=['ga.start',">=",date('Y-m-d H:i:s',strtotime($starttime))];
  38. }
  39. $endtime = isset($this->post['endtime']) && $this->post['endtime']!=="" ? $this->post['endtime']:"";
  40. if($endtime!==""){
  41. $where[]=['ga.end',"<",date('Y-m-d H:i:s',strtotime($endtime)+24*3600)];
  42. }
  43. $platform_code = isset($this->post['platform_code']) && $this->post['platform_code'] !=="" ? trim($this->post['platform_code']):"";
  44. if($platform_code!==""){
  45. $where[]=['ga.platform_code',"=",$platform_code];
  46. }
  47. $company_id = isset($this->post['company_id']) && $this->post['company_id'] !=="" ? trim($this->post['company_id']):"";
  48. if($company_id!==""){
  49. $where[]=['ga.company_id',"=",$company_id];
  50. }
  51. $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? trim($this->post['creater']) :"";
  52. if($creater!=""){
  53. $where[]=['ga.creater',"like","%$creater%"];
  54. }
  55. $createrid = isset($this->post['createrid']) && $this->post['createrid'] !=="" ? trim($this->post['createrid']) :"";
  56. if($createrid!==""){
  57. $where[]=['ga.createrid',"=",$createrid];
  58. }
  59. // $role=$this->checkRole();
  60. // if(!empty($role['write'])){
  61. // $where[]=["ga.createrid","in",$role['write']];
  62. // }
  63. // $role = $this->checkDataShare();
  64. // if (!empty($role[DataGroupModel::$type_全部])) $where[] = ['ga.createrid', 'in', $role[DataGroupModel::$type_全部]];
  65. $role = $this->checkDataShare();
  66. $hand = resign_hand_user($this->uid,0);
  67. if (!empty($role[DataGroupModel::$type_全部])) {
  68. $arr= array_unique(array_merge($hand,$role[DataGroupModel::$type_全部]));
  69. $where[] = ['ga.createrid', 'in',$arr];
  70. }
  71. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  72. if ($company_name !== "") $where[] = ["ga.createrid", 'in', get_company_item_user_by_name($company_name)];
  73. $count = Db::name('good_activity')
  74. ->alias('ga')
  75. ->where($where)
  76. ->count();
  77. $total = ceil($count / $size);
  78. $page = $page >= $total ? $total : $page;
  79. $list = Db::name('good_activity')
  80. ->alias('ga')
  81. ->field('ga.*,p.platform_name,u.itemid')
  82. ->leftJoin("depart_user u", "u.uid=ga.createrid AND u.is_del=0")
  83. ->leftJoin("platform p", "p.id=ga.platform_code")
  84. ->append(['company_name','is_allow_update'])
  85. ->withAttr('company_name',function($val,$data){
  86. return implode('/', array_column(GetPart($data['itemid']), 'name'));
  87. })->withAttr('is_allow_update',function($val,$data)use($role){
  88. return (in_array($this->roleid, [1, 33]) || in_array($data['createrid'], $role[DataGroupModel::$type_可编辑])) ? 1 : 0;//是否具有编辑权限
  89. })->where($where)
  90. ->page($page,$size)
  91. ->order("ga.addtime desc")
  92. ->select()
  93. ->toArray();
  94. return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
  95. }
  96. //活动商品列表
  97. public function linst()
  98. {
  99. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  100. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  101. $where = [["a.is_del", "=", 0]];
  102. $skuCode = isset($this->post['skuCode']) && $this->post['skuCode'] !== "" ? trim($this->post['skuCode']) : "";
  103. if ($skuCode !== "") {
  104. $where[] = ['a.skuCode', "like", "%$skuCode%"];
  105. }
  106. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name']) : "";
  107. if ($good_name != "") {
  108. $where[] = ['a.good_name', "like", "%$good_name%"];
  109. }
  110. $activity_code = isset($this->post['activity_code']) && $this->post['activity_code'] !=="" ? trim($this->post['activity_code']):"";
  111. if($activity_code!==""){
  112. $where[]=['a.activity_code',"like","%$activity_code%"];
  113. }
  114. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  115. if($status!==""){
  116. $where[]=['a.status',"=",$status];
  117. }
  118. $activity_name = isset($this->post['activity_name']) && $this->post['activity_name'] !=="" ? trim($this->post['activity_name']):"";
  119. if($activity_name!=""){
  120. $where[]=['b.activity_name',"like","%$activity_name%"];
  121. }
  122. $platform_code = isset($this->post['platform_code']) && $this->post['platform_code'] !=="" ? trim($this->post['platform_code']):"";
  123. if($platform_code!=""){
  124. $where[]=['b.platform_code',"=",$platform_code];
  125. }
  126. $count = Db::name('activity_info')->alias("a")
  127. ->join("good_activity b","b.activity_code=a.activity_code","left")
  128. ->join("platform c","c.id=b.platform_code","left")
  129. ->where($where)->count();
  130. $total = ceil($count / $size);
  131. $page = $page >= $total ? $total : $page;
  132. $list = Db::name('activity_info')
  133. ->alias("a")
  134. ->join("good_activity b","b.activity_code=a.activity_code","left")
  135. ->join("platform c","c.id=b.platform_code","left")
  136. ->where($where)
  137. ->page($page,$size)
  138. ->order("a.addtime desc")
  139. ->field("a.*,b.activity_name,c.platform_name,c.platform_code")
  140. ->select()
  141. ->toArray();
  142. return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
  143. }
  144. public function zilist(){
  145. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  146. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  147. $where = [["a.is_del", "=", 0]];
  148. $spuCode = isset($this->post['spuCode']) && $this->post['spuCode'] !== "" ? trim($this->post['spuCode']) : "";
  149. if ($spuCode != "") $where[] = ['a.spuCode', "like", "%$spuCode%"];
  150. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name']) : "";
  151. if ($good_name != "") $where[] = ['a.good_name', "like", "%$good_name%"];
  152. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  153. if($status!="") $where[]=['a.status',"=",$status];
  154. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  155. if ($supplierNo != "") $where[] = ['a.supplierNo', "like", "%$supplierNo%"];
  156. $name = isset($this->post['name']) && $this->post['name'] !== "" ? trim($this->post['name']) : "";
  157. if ($name != "") $where[] = ['b.name', "like", "%$name%"];
  158. $start = isset($this->post['start']) && $this->post['start'] !== "" ? trim($this->post['start']) : "";
  159. if ($start != "") $where[] = ['a.addtime', ">=", $start];
  160. $end = isset($this->post['end']) && $this->post['end'] !== "" ? trim($this->post['end']) : "";
  161. if ($end != "") $where[] = ['a.addtime', "<=", $end.' 23:59:59'];
  162. $count = Db::name('good_zixun')
  163. ->alias("a")
  164. ->join("supplier b","b.code=a.supplierNo","left")
  165. ->where($where)
  166. ->count();
  167. $total = ceil($count / $size);
  168. $page = $page >= $total ? $total : $page;
  169. $list = Db::name('good_zixun')
  170. ->alias("a")
  171. ->join("supplier b","b.code=a.supplierNo","left")
  172. ->where($where)
  173. ->page($page,$size)
  174. ->order("a.addtime desc")
  175. ->field("a.*,b.name")
  176. ->select()
  177. ->toArray();
  178. //halt($list);
  179. $data=[];
  180. //分类/规格/品牌 等字段取出对应文字
  181. foreach ($list as $value){
  182. $value['cat_info']= made($value['cat_id'],[]);
  183. $platform = Db::name("platform")->where(["id"=>$value['platform_id']])->find();
  184. $value['platform_name'] = isset($platform['platform_name']) ? $platform['platform_name']:"";
  185. $value['platform_code_en'] = isset($platform['platform_code_en']) ? $platform['platform_code_en']:"";
  186. $supplier = Db::name("supplier")->where(["code"=>$value['supplierNo']])->find();
  187. $value['supplier_name'] = isset($supplier['name']) ? $supplier['name']:"";
  188. $brand=Db::name("brand")->where(["id"=>$value['brand_id']])->find();
  189. $value["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
  190. $unit = Db::name("unit")->where(["id"=>$value['good_unit']])->find();
  191. $value['unit'] =isset($unit['unit'])?$unit['unit']:"";
  192. $company = Db::name("business")->where(["companyNo"=>$value['companyNo']])->find();
  193. $value['company'] = isset($company['company'])?$company['company']:"";
  194. // $company = Db::name("business")->where(["companyNo"=>$value['companyNo']])->find();
  195. // $value['company'] = isset($company['company'])?$company['company']:"";
  196. $value['stock_total'] = Db::name("good_stock")->where(['spuCode'=>$value['spuCode'],"is_del"=>0])->sum("usable_stock");
  197. // $value['status']=$value['exam_status'];
  198. // $value['exclusive']=makeExcluse($value['is_exclusive']);
  199. $value['good_info_img']=$value['good_info_img'];
  200. $value['good_img']=$value['good_img'];
  201. $spec = Db::name("good_spec")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->select()->toArray();
  202. $speclist=[];
  203. if(!empty($spec)){
  204. foreach ($spec as $val){
  205. $temp=[];
  206. $temp['spec_id']=$val['spec_id'];
  207. $temp['spec_value_id']=$val['spec_value_id'];
  208. $sp = Db::name("specs")->where(["id"=>$val['spec_id']])->find();
  209. $temp['spec_name']=isset($sp["spec_name"]) ? $sp["spec_name"]:"";
  210. $spv = Db::name("spec_value")->where(["id"=>$val['spec_value_id']])->find();
  211. $temp['spec_value']=isset($spv["spec_value"]) ? $spv["spec_value"]:"";
  212. $speclist[]=$temp;
  213. }
  214. }
  215. $value['specinfo']=$speclist;
  216. $value['noble_name']=isset($value['noble_metal'])&&$value['noble_metal']!=0?$this->noble[$value['noble_metal']] :"";
  217. $data[]=$value;
  218. }
  219. return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
  220. }
  221. //创建活动
  222. public function create(){
  223. $activity_name = isset($this->post['activity_name'])&&$this->post['activity_name']!="" ? trim($this->post['activity_name']):"";
  224. if($activity_name==""){
  225. return error_show(1004,"参数activity_name不能为空");
  226. }
  227. $platform_code = isset($this->post['platform_code'])&&$this->post['platform_code']!="" ? trim($this->post['platform_code']):"";
  228. if($platform_code==""){
  229. return error_show(1004,"参数platform_code不能为空");
  230. }
  231. $company_id = isset($this->post['company_id'])&&$this->post['company_id']!="" ? trim($this->post['company_id']):"";
  232. if($company_id==""){
  233. return error_show(1004,"参数company_id不能为空");
  234. }
  235. $activity_start = isset($this->post['activity_start'])&&$this->post['activity_start']!="" ? $this->post['activity_start']:"";
  236. if($activity_start==""){
  237. return error_show(1004,"参数activity_start不能为空");
  238. }
  239. $activity_end = isset($this->post['activity_end'])&&$this->post['activity_end']!="" ? $this->post['activity_end']:"";
  240. if($activity_end==""){
  241. return error_show(1004,"参数activity_end不能为空");
  242. }
  243. $activity_desc = isset($this->post['activity_desc'])&&$this->post['activity_desc']!="" ?
  244. trim($this->post['activity_desc']):"";
  245. if($activity_desc==""){
  246. return error_show(1004,"参数activity_desc不能为空");
  247. }
  248. $is_stock = isset($this->post['is_stock'])&&$this->post['is_stock']!=="" ?
  249. intval($this->post['is_stock']):"";
  250. if($is_stock===""){
  251. return error_show(1004,"参数is_stock不能为空");
  252. }
  253. $good_list= isset($this->post['good_list'])&&$this->post['good_list']!=="" ?$this->post['good_list']:"";
  254. if($good_list==""){
  255. return error_show(1004,"参数good_list不能为空");
  256. }
  257. $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  258. if($token==''){
  259. return error_show(105,"参数token不能为空");
  260. }
  261. $user =GetUserInfo($token);
  262. if(empty($user)||$user['code']!=0){
  263. return error_show(102,"用户数据不存在");
  264. }
  265. $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  266. $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  267. $activity_code = makeNo("AT");
  268. Db::startTrans();
  269. try{
  270. $data=[
  271. "activity_name"=>$activity_name,
  272. "activity_code"=>$activity_code,
  273. "is_stock"=>$is_stock,
  274. "start"=>$activity_start,
  275. "end"=>$activity_end,
  276. "platform_code"=>$platform_code,
  277. "activity_desc"=>$activity_desc,
  278. "company_id"=>$company_id,
  279. "creater"=>$creater,
  280. "createrid"=>$createrid,
  281. "status"=>1,
  282. "is_del"=>0,
  283. "addtime"=>date("Y-m-d H:i:s"),
  284. "updatetime"=>date("Y-m-d H:i:s")
  285. ];
  286. $inset = Db::name("good_activity")->insertGetId($data);
  287. if($inset>0){
  288. if($good_list!=""&&!empty($good_list)){
  289. $temp=[];
  290. $skuCode =array_column($good_list,"skuCode");
  291. $ist = Db::name("good_activity")
  292. ->alias("a")
  293. ->leftJoin("activity_info b", "a.activity_code = b.activity_code and b.is_del=0")
  294. ->where(["b.skuCode" => $skuCode, "a.is_del" => 0])
  295. ->where([["a.status", "<>", 8]])
  296. ->where(function ($query) use ($activity_end, $activity_start) {
  297. return $query->whereOr([[["start", "<=", $activity_start], ["end", ">=", $activity_end]], ["start|end", "between", [$activity_start, $activity_end]]]);
  298. })
  299. ->select()
  300. ->toArray();
  301. if(!empty($ist)){
  302. Db::rollback();
  303. $limit=[];
  304. foreach ($ist as $value){
  305. $limit[]="活动 ".$value['activity_name']. ' 已存在商品 '. $value['good_name'];
  306. }
  307. return app_show(1007,'活动商品已存在', $limit);
  308. }
  309. foreach ($good_list as $value){
  310. $lemp = [];
  311. $good = Db::name('good')->alias("a")->leftJoin("good_platform b", "a.spuCode=b.spuCode")->where(["skuCode" => $value['skuCode'], "a.is_del" => 0])->findOrEmpty();
  312. $lemp['skuCode']=$value['skuCode'];
  313. $lemp['good_name']=isset($good['good_name']) ?$good['good_name']:"" ;
  314. $lemp['activity_code']=$activity_code;
  315. $lemp['activity_stock']=$value['activity_stock'];
  316. $lemp['moq_num']=0;
  317. $lemp['cost_price']=0;
  318. $lemp['sale_price']=0;
  319. $lemp['activity_price']=0;
  320. $lemp['status']=0;
  321. $lemp['remark']="";
  322. $lemp['is_del']=0;
  323. $lemp['addtime']=date("Y-m-d H:i:s");
  324. $lemp['updatetime']=date("Y-m-d H:i:s");
  325. $temp[]=$lemp;
  326. //当是库存品的时候校验可用库存数,不满足的时候不能添加
  327. if ($is_stock == 1) {
  328. $usable_stock = Db::name('good_stock')
  329. ->where(['spuCode' => $good['spuCode'], 'is_del' => 0])
  330. ->sum('usable_stock');
  331. if ($value['activity_stock'] > $usable_stock) {
  332. Db::rollback();
  333. // throw new \Exception($lemp['good_name'] . '的库存只有' . $usable_stock);
  334. return app_show(1005,'库存不足',[$lemp['good_name']. '的库存只有' . $usable_stock]);
  335. }
  336. }
  337. }
  338. $iall=Db::name("activity_info")->insertAll($temp);
  339. if($iall==0){
  340. Db::rollback();
  341. return error_show(1005,"新建失败");
  342. }
  343. }
  344. $stn = ["order_code" => $activity_code, "status" => 0, "action_remark" => '', "action_type" => "create"];
  345. ActionLog::logAdd($this->post['token'], $stn, "HD", $data['status'], $data);
  346. $process = ["order_code" => $activity_code, "order_id" => $inset, "order_status" => $data['status'], "order_type" => 'HD', "before_status" => 0, 'holder_id' => $data['createrid']];
  347. ProcessOrder::AddProcess($this->post['token'], $process);
  348. Db::commit();
  349. return app_show(0,"新建成功");
  350. }
  351. }catch (\Exception $e){
  352. Db::rollback();
  353. return error_show(1005,$e->getMessage());
  354. }
  355. }
  356. //采购定价
  357. public function cost(){
  358. $good_list= isset($this->post['good_list'])&&!empty($this->post['good_list']) ?$this->post['good_list']:"";
  359. if($good_list==""){
  360. return error_show(1004,"参数good_list不能为空");
  361. }
  362. $activity_code= isset($this->post['activity_code'])&&$this->post['activity_code']!=="" ?trim($this->post['activity_code']):"";
  363. if($activity_code==""){
  364. return error_show(1004,"参数activity_code不能为空");
  365. }
  366. $activity = Db::name("good_activity")->where(["activity_code"=>$activity_code,"is_del"=>0])->find();
  367. if($activity==false){
  368. return error_show(1004,"未找到活动数据");
  369. }
  370. Db::startTrans();
  371. try{
  372. foreach ($good_list as $value){
  373. $temp=[];
  374. $ladd = Db::name("good_ladder")->where(["skuCode"=>$value['skuCode'],"is_del"=>0])->where
  375. ([["min_num","<=",$value['moq_num']]])->order("min_num desc")->find();
  376. $temp["id"]=$value['id'];
  377. $temp["cost_price"]=$value['cost_price'];
  378. $temp["moq_num"]=$value['moq_num'];
  379. $temp["sale_price"]=isset($ladd['sale_price'])?$ladd['sale_price']:0;
  380. $temp["updatetime"]=date("Y-m-d H:i:s");
  381. $up=Db::name("activity_info")->save($temp);
  382. if($up==false){
  383. Db::rollback();
  384. return error_show(1004,"商品数据更新失败");
  385. }
  386. }
  387. $old_activity_status = $activity['status'];
  388. $activity['status']=2;
  389. $activity['updatetime']=date("Y-m-d H:i:s");
  390. $act = Db::name("good_activity")->save($activity);
  391. if($act){
  392. $stn = ["order_code" => $activity_code, "status" => $old_activity_status, "action_remark" => '', "action_type" => "status"];
  393. ActionLog::logAdd($this->post['token'], $stn, "HD", $activity['status'], $activity);
  394. $process = ["order_code" => $activity_code, "order_id" => $activity['id'], "order_status" => $activity['status'], "order_type" => 'HD', "before_status" => $old_activity_status, 'holder_id' => $activity['createrid']];
  395. ProcessOrder::AddProcess($this->post['token'], $process);
  396. Db::commit();
  397. return error_show(0,"商品数据更新成功");
  398. }else{
  399. Db::rollback();
  400. return error_show(1004,"商品数据更新失败");
  401. }
  402. }catch (\Exception $e){
  403. Db::rollback();
  404. return error_show(1004,$e->getMessage());
  405. }
  406. }
  407. //财务定价
  408. public function activity(){
  409. $good_list= isset($this->post['good_list'])&&!empty($this->post['good_list']) ?$this->post['good_list']:"";
  410. if($good_list==""){
  411. return error_show(1004,"参数good_list不能为空");
  412. }
  413. $activity_code= isset($this->post['activity_code'])&&$this->post['activity_code']!=="" ?trim($this->post['activity_code']):"";
  414. if($activity_code==""){
  415. return error_show(1004,"参数activity_code不能为空");
  416. }
  417. $activity = Db::name("good_activity")->where(["activity_code"=>$activity_code,"is_del"=>0])->find();
  418. if($activity==false){
  419. return error_show(1004,"未找到活动数据");
  420. }
  421. Db::startTrans();
  422. try{
  423. foreach ($good_list as $value){
  424. $temp=[];
  425. $temp["id"]=$value['id'];
  426. $temp["activity_price"]=$value['activity_price'];
  427. $temp['is_activity'] = $value['is_activity'];
  428. $temp['settle_price'] = $value['settle_price'];
  429. $temp["updatetime"]=date("Y-m-d H:i:s");
  430. $up=Db::name("activity_info")->save($temp);
  431. if($up==false){
  432. Db::rollback();
  433. return error_show(1004,"商品数据更新失败");
  434. }
  435. }
  436. $old_activity_status = $activity['status'];
  437. $activity['status']=3;
  438. $activity['updatetime']=date("Y-m-d H:i:s");
  439. $act = Db::name("good_activity")->save($activity);
  440. if($act){
  441. $stn = ["order_code" => $activity_code, "status" => $old_activity_status, "action_remark" => '', "action_type" => "status"];
  442. ActionLog::logAdd($this->post['token'], $stn, "HD", $activity['status'], $activity);
  443. $process = ["order_code" => $activity_code, "order_id" => $activity['id'], "order_status" => $activity['status'], "order_type" => 'HD', "before_status" => $old_activity_status, 'holder_id' => $activity['createrid']];
  444. ProcessOrder::AddProcess($this->post['token'], $process);
  445. Db::commit();
  446. return error_show(0,"商品数据更新成功");
  447. }else{
  448. Db::rollback();
  449. return error_show(1004,"商品数据更新失败");
  450. }
  451. }catch (\Exception $e){
  452. Db::rollback();
  453. return error_show(1004,$e->getMessage());
  454. }
  455. }
  456. //财务审核定价
  457. public function exam(){
  458. $good_list= isset($this->post['good_list'])&&!empty($this->post['good_list']) ?$this->post['good_list']:"";
  459. if($good_list==""){
  460. return error_show(1004,"参数good_list不能为空");
  461. }
  462. $activity_code= isset($this->post['activity_code'])&&$this->post['activity_code']!=="" ?trim($this->post['activity_code']):"";
  463. if($activity_code==""){
  464. return error_show(1004,"参数activity_code不能为空");
  465. }
  466. $activity = Db::name("good_activity")->where(["activity_code"=>$activity_code,"is_del"=>0])->find();
  467. if($activity==false){
  468. return error_show(1004,"未找到活动数据");
  469. }
  470. Db::startTrans();
  471. try{
  472. $isRejectAll = true;//是否全部驳回
  473. foreach ($good_list as $value){
  474. $temp=[];
  475. $temp["id"]=$value['id'];
  476. $temp["status"]=$value['status'];
  477. $temp["remark"]=$value['remark'];
  478. $temp["updatetime"]=date("Y-m-d H:i:s");
  479. $up=Db::name("activity_info")->save($temp);
  480. if($up==false){
  481. Db::rollback();
  482. return error_show(1004,"商品数据更新失败");
  483. }
  484. if ($isRejectAll && ($value['status'] == 1)) $isRejectAll = false;//只要有一个商品通过,即不能全部驳回
  485. }
  486. $old_activity_status = $activity['status'];
  487. $activity['status'] = $isRejectAll ? 8 : 4;//4审核通过,8审核未通过
  488. $activity['updatetime']=date("Y-m-d H:i:s");
  489. $act = Db::name("good_activity")->save($activity);
  490. if($act){
  491. $stn = ["order_code" => $activity_code, "status" => $old_activity_status, "action_remark" => '', "action_type" => "status"];
  492. ActionLog::logAdd($this->post['token'], $stn, "HD", $activity['status'], $activity);
  493. $process = ["order_code" => $activity_code, "order_id" => $activity['id'], "order_status" => $activity['status'], "order_type" => 'HD', "before_status" => $old_activity_status, 'holder_id' => $activity['createrid']];
  494. ProcessOrder::AddProcess($this->post['token'], $process);
  495. Db::commit();
  496. return error_show(0,"商品数据审核成功");
  497. }else{
  498. Db::rollback();
  499. return error_show(1004,"商品数据审核失败");
  500. }
  501. }catch (\Exception $e){
  502. Db::rollback();
  503. return error_show(1004,$e->getMessage());
  504. }
  505. }
  506. //活动详情
  507. public function info()
  508. {
  509. $activity_code = isset($this->post['activity_code']) && $this->post['activity_code'] !== "" ? trim($this->post['activity_code']) : "";
  510. if ($activity_code == "") {
  511. return error_show(1004, "参数activity_code不能为空");
  512. }
  513. $activity = Db::name("good_activity")
  514. ->alias('ga')
  515. ->field('ga.*,p.platform_name,p.platform_type')
  516. ->leftJoin('platform p', 'p.id=ga.platform_code AND p.is_del=0')
  517. ->where(["ga.activity_code" => $activity_code, "ga.is_del" => 0])
  518. ->find();
  519. if ($activity == false) {
  520. return error_show(1004, "未找到活动数据");
  521. }
  522. $activity_info = Db::name("activity_info")
  523. ->where(["activity_code" => $activity_code, "is_del" => 0])
  524. ->select()
  525. ->toArray();
  526. if (!empty($activity_info)) {
  527. //状态不是(0待产品部审核,1待采购定价),才会有起订量,才可以考虑计算售价
  528. if ($activity['status'] > 1) {
  529. //判断是否是启用实时金价的贵金属
  530. $rs = Db::name('good_platform')
  531. ->alias('a')
  532. ->join('good g', 'g.spuCode=a.spuCode')
  533. ->join('good_nake c', 'c.spuCode=a.spuCode')
  534. ->whereIn('a.skuCode', array_column($activity_info, 'skuCode'))
  535. ->column('g.cat_id,g.is_gold_price,g.demo_fee,g.open_fee,g.weight,c.cost_fee,c.package_fee,c.mark_fee,c.cert_fee,g.noble_metal,c.nake_fee,c.delivery_fee', 'a.skuCode');
  536. foreach ($activity_info as &$value) {
  537. //如果启用了实时金价,且是贵金属
  538. if (isset($rs[$value['skuCode']])) {
  539. $top_cat = made($rs[$value['skuCode']]['cat_id']);
  540. if (isset($top_cat[0]['id']) && $top_cat[0]['id'] == 6 && $rs[$value['skuCode']]['is_gold_price'] == 1) {
  541. $gold_price = Db::name("gold_price1")->where(["type" => $rs[$value['skuCode']]['noble_metal'], "is_del" => 0, "status" => 1])->order("addtime desc")->value('price');
  542. $value['sale_price'] = round($rs[$value['skuCode']]['demo_fee'] / $value['moq_num'] + $rs[$value['skuCode']]['open_fee'] / $value['moq_num'] + $rs[$value['skuCode']]['weight'] * $gold_price + $rs[$value['skuCode']]['cost_fee'] * $rs[$value['skuCode']]['weight'] + $rs[$value['skuCode']]['package_fee'] + $rs[$value['skuCode']]['mark_fee'] + $rs[$value['skuCode']]['cert_fee'] + $rs[$value['skuCode']]['nake_fee'] + $rs[$value['skuCode']]['delivery_fee'], 2);
  543. }
  544. }
  545. }
  546. }
  547. $activity['info'] = $activity_info;
  548. } else $activity['info'] = [];
  549. // $activity['info'] = empty($activity_info) ? [] : $activity_info;
  550. return app_show(0, "获取成功", $activity);
  551. }
  552. //修改活动状态
  553. public function actstatus(){
  554. $activity_code= isset($this->post['activity_code'])&&$this->post['activity_code']!=="" ?trim($this->post['activity_code']):"";
  555. if($activity_code==""){
  556. return error_show(1004,"参数activity_code不能为空");
  557. }
  558. $activity = Db::name("good_activity")->where(["activity_code"=>$activity_code,"is_del"=>0])->find();
  559. if($activity==false){
  560. return error_show(1004,"未找到活动数据");
  561. }
  562. $status = isset($this->post['status'])&&$this->post['status']!=="" ?intval($this->post['status']):"";
  563. if($status==""){
  564. return error_show(1004,"参数status不能为空");
  565. }
  566. $remark =isset($tihs->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  567. if($remark=="" &&$status==8){
  568. return error_show(1004,"参数remark不能为空");
  569. }
  570. $old_status = $activity['status'];
  571. $activity['status']=$status;
  572. $activity['remark']=$remark;
  573. $activity['updatetime']=date("Y-m-d H:i:s");
  574. $up = Db::name("good_activity")->save($activity);
  575. if($up){
  576. $stn = ["order_code" => $activity_code, "status" => $old_status, "action_remark" => '', "action_type" => "status"];
  577. ActionLog::logAdd($this->post['token'], $stn, "HD", $activity['status'], $activity);
  578. $process = ["order_code" => $activity_code, "order_id" => $activity['id'], "order_status" => $activity['status'], "order_type" => 'HD', "before_status" => $old_status, 'holder_id' => $activity['createrid']];
  579. ProcessOrder::AddProcess($this->post['token'], $process);
  580. return app_show(0,"修改成功");
  581. }else{
  582. return error_show(1004,"修改失败");
  583. }
  584. }
  585. public function query(){
  586. $skuCode= isset($this->post['skuCode'])&&$this->post['skuCode']!=""?trim($this->post['skuCode']):"";
  587. if($skuCode==""){
  588. return error_show(1004,"参数skuCode不能为空");
  589. }
  590. $where=['a.is_del'=>0,"b.is_del"=>0,"a.status"=>6,"b.status"=>1,"b.skuCode"=>$skuCode];
  591. $condition=['a.skuCode' =>$skuCode,"a.is_del"=>0,'a.exam_status'=>6];
  592. $company = isset($this->post['companyNo'])&&$this->post['companyNo']!=""?trim($this->post['companyNo']):"";
  593. if($company!=""){
  594. $where['a.company_id'] = $company;
  595. $condition['companyNo'] = $company;
  596. }
  597. $plat =isset($this->post['platform_id'])&&$this->post['platform_id']!=""?intval($this->post['platform_id'])
  598. :"";
  599. if($plat!==""){
  600. $where['a.platform_code'] = $plat;
  601. $condition['platform_code'] = $plat;
  602. }
  603. $good = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  604. ->where($condition)->select()->toArray();
  605. $goodlist=[];
  606. if(!empty($good)){
  607. foreach ($good as $value){
  608. $temp=[];
  609. $stock =Db::name("good_stock")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->sum('usable_stock');
  610. //如果是非库存商品,库存数改为最大(即将库存设为无限)
  611. $temp['stock_num'] = $value['is_stock'] == 1 ? intval($stock) : 9999;
  612. $temp['skuCode']=$value['skuCode'];
  613. $temp['spuCode']=$value['spuCode'];
  614. $temp['is_stock']=$value['is_stock'];
  615. $temp['good_name']=$value['good_name'];
  616. $temp['good_img']=$value['good_img'];
  617. $temp['good_info_img']=$value['good_info_img'];
  618. $temp['noble_metal']=$value['noble_metal'];
  619. $temp['noble_name']=$value['noble_metal']!=0?$this->noble[$value['noble_metal']]:"";
  620. $goodlist[]=$temp;
  621. }
  622. }
  623. $list = Db::name('good_activity')->alias("a")
  624. ->join("activity_info b","b.activity_code=a.activity_code","left")
  625. ->where($where)->field('b.id,a.activity_name,a.activity_code,a.is_stock,a.start,a.end,b.skuCode,b.good_name,b.activity_stock,
  626. b.cost_price,b.moq_num,b.sale_price,b.activity_price')->select()->toArray();
  627. return app_show(0,"获取成功",["act"=>$list,"good"=>$goodlist]);
  628. }
  629. //新增库存
  630. public function editStock(){
  631. $actCode=isset($this->post['actCode'])&&$this->post['actCode']!='' ? trim($this->post['actCode']):"";
  632. if($actCode==''){
  633. return error_show(1004,"参数 actCode 不能为空");
  634. }
  635. $skuCode=isset($this->post['skuCode'])&&$this->post['skuCode']!='' ? trim($this->post['skuCode']):"";
  636. if($skuCode==''){
  637. return error_show(1004,"参数 skuCode 不能为空");
  638. }
  639. $act_stock=isset($this->post['act_stock'])&&$this->post['act_stock']!='' ? intval($this->post['act_stock']):"";
  640. if($act_stock==''){
  641. return error_show(1004,"参数 act_stock 不能为空");
  642. }
  643. $act =Db::name("good_activity")->where(["activity_code"=>$actCode,"is_del"=>0])->findOrEmpty();
  644. if(empty($act)){
  645. return error_show(1005,"活动信息不存在");
  646. }
  647. if(!in_array($act['status'],[4,5,6]))return error_show(1005,"活动暂时不能修改");
  648. $goodinfo=Db::name("activity_info")->where(["activity_code"=>$actCode,"skuCode"=>$skuCode,"is_del"=>0])
  649. ->findOrEmpty();
  650. if(empty($goodinfo)){
  651. return error_show(1005,"活动商品信息不存在");
  652. }
  653. $good = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  654. ->where(['a.skuCode' =>$skuCode,"a.is_del"=>0,'a.exam_status'=>6])->findOrEmpty();
  655. if(empty($good)){
  656. return error_show(1005,"商品信息不存在或已下架");
  657. }
  658. if($good['is_stock']==1){
  659. $stock =Db::name("good_stock")->where(["spuCode"=>$good['spuCode'],"is_del"=>0])->sum('usable_stock');
  660. if($act_stock+$goodinfo['activity_stock']> $stock){
  661. return error_show(1005,"商品库存不足");
  662. }
  663. }
  664. $update =["activity_stock"=>$act_stock+$goodinfo['activity_stock'],"updatetime"=>date("Y-m-d H:i:s")];
  665. $up=Db::name("activity_info")->where($goodinfo)->update($update);
  666. return $up ? app_show(0,"活动库存增加成功"):error_show(1006,"活动库存增加失败");
  667. }
  668. }