Activity.php 38 KB

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