CompanyCatProfit.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\Db;
  4. use think\facade\Validate;
  5. //公司分类毛利
  6. class CompanyCatProfit extends Base
  7. {
  8. //列表
  9. public function getList()
  10. {
  11. $param = $this->request->only(['page' => 1, 'size' => 10, 'companyNo' => '', 'cat_name' => '', 'pid' => 0], 'post', 'trim');
  12. if ($param['companyNo'] == '') return json_show(1004, '业务公司编号不能为空');
  13. $where = [['a.is_del', '=', 0], ['a.pid', '=', $param['pid']]];
  14. if ($param['cat_name'] !== '') $where[] = ['a.cat_name', 'like', '%' . $param['cat_name'] . '%'];
  15. $count = Db::name('cat')
  16. ->alias('a')
  17. ->leftJoin('company_cat_profit b', 'b.cat_id=a.id AND companyNo=\'' . $param['companyNo'] . '\'')
  18. ->where($where)
  19. ->count('a.id');
  20. $list = Db::name('cat')
  21. ->alias('a')
  22. ->field('a.id cat_id,a.cat_name,a.level,b.rate,b.order_rate,b.money_rate,b.low_rate,b.sale_rate,b.status')
  23. ->leftJoin('company_cat_profit b', 'b.is_del=0 AND b.cat_id=a.id AND companyNo=\'' . $param['companyNo'] . '\'')
  24. ->where($where)
  25. ->withAttr('status', function ($val) {
  26. return (int)$val;
  27. })
  28. ->order('a.id', 'desc')
  29. ->page($param['page'], $param['size'])
  30. ->select()
  31. ->toArray();
  32. return json_show(0, '获取列表成功', ['count' => $count, 'list' => $list]);
  33. }
  34. //设置毛利率相关
  35. public function set()
  36. {
  37. $param = $this->request->only(['cat_id', 'companyNo', 'rate', 'order_rate', 'money_rate', 'low_rate', 'sale_rate'], 'post', 'trim');
  38. $val = Validate::rule([
  39. 'cat_id|分类id' => 'require|number|gt:0',
  40. 'companyNo|公司编码' => 'require|length:18',
  41. 'rate|主管利率' => 'require|float|between:0,100',
  42. 'order_rate|预算成本' => 'require|float|between:0,100',
  43. 'money_rate|财务利率' => 'require|float|between:0,100',
  44. 'low_rate|最低利率' => 'require|float|between:0,100',
  45. 'sale_rate|成本售价' => 'require|float|between:0,100',
  46. ]);
  47. if (!$val->check($param)) return json_show(1004, $val->getError());
  48. $tmp = Db::name('company_cat_profit')
  49. ->field('id')
  50. ->where(['is_del' => 0, 'companyNo' => $param['companyNo'], 'cat_id' => $param['cat_id']])
  51. ->findOrEmpty();
  52. $date = date('Y-m-d H:i:s');
  53. if (empty($tmp)) {
  54. $rs = Db::name('company_cat_profit')
  55. ->insert([
  56. 'companyNo' => $param['companyNo'],
  57. 'cat_id' => $param['cat_id'],
  58. 'rate' => $param['rate'],
  59. 'order_rate' => $param['order_rate'],
  60. 'money_rate' => $param['money_rate'],
  61. 'low_rate' => $param['low_rate'],
  62. 'sale_rate' => $param['sale_rate'],
  63. 'status' => 0,
  64. 'is_del' => 0,
  65. 'addtime' => $date,
  66. 'updatetime' => $date
  67. ]);
  68. } else {
  69. $rs = Db::name('company_cat_profit')
  70. ->where(['is_del' => 0, 'id' => $tmp['id']])
  71. ->update([
  72. 'rate' => $param['rate'],
  73. 'order_rate' => $param['order_rate'],
  74. 'money_rate' => $param['money_rate'],
  75. 'low_rate' => $param['low_rate'],
  76. 'sale_rate' => $param['sale_rate'],
  77. 'updatetime' => $date
  78. ]);
  79. }
  80. return $rs ? json_show(0, '设置成功') : json_show(1004, '设置失败');
  81. }
  82. //启禁用
  83. public function status()
  84. {
  85. $param = $this->request->only(['cat_id', 'companyNo', 'status'], 'post', 'trim');
  86. $val = Validate::rule([
  87. 'cat_id|分类id' => 'require|number|gt:0',
  88. 'companyNo|公司编码' => 'require|length:18',
  89. 'status|状态' => 'require|number|in:0,1',
  90. ]);
  91. if ($val->check($param) == false) return json_show(1004, $val->getError());
  92. $cat = Db::name('cat')
  93. ->field('id,level')
  94. ->where(['id' => $param['cat_id'], 'is_del' => 0])
  95. ->findOrEmpty();
  96. if (empty($cat)) return json_show(1005, '该分类不存在');
  97. $tmp = Db::name('company_cat_profit')
  98. ->field('id,status')
  99. ->where(['is_del' => 0, 'companyNo' => $param['companyNo'], 'cat_id' => $param['cat_id']])
  100. ->findOrEmpty();
  101. if (empty($tmp)) return json_show(1004, '该分类尚未设置毛利,无法操作');
  102. if ($tmp['status'] == $param['status']) return json_show(1004, '重复操作');
  103. //要操作的分类id集合
  104. switch ($cat['level']) {
  105. case 1:
  106. $first_ids = Db::name('cat')
  107. ->where(['is_del' => 0, 'pid' => $param['cat_id']])
  108. ->column('id');
  109. $second_ids = Db::name('cat')
  110. ->where('is_del', 0)
  111. ->whereIn('pid', $first_ids)
  112. ->column('id');
  113. $cat_ids = array_merge($first_ids, $second_ids);
  114. break;
  115. case 2:
  116. $cat_ids = Db::name('cat')
  117. ->where(['is_del' => 0, 'pid' => $param['cat_id']])
  118. ->column('id');
  119. break;
  120. }
  121. $cat_ids[] = $param['cat_id'];
  122. $rs = Db::name('company_cat_profit')
  123. ->where(['is_del' => 0, 'companyNo' => $param['companyNo']])
  124. ->whereIn('cat_id', $cat_ids)
  125. ->where('status', '<>', $param['status'])
  126. ->update([
  127. 'status' => $param['status'],
  128. 'updatetime' => date('Y-m-d H:i:s')
  129. ]);
  130. return $rs ? json_show(0, '操作成功') : json_show(1004, '操作失败,该记录不存在或重复操作');
  131. }
  132. //删除
  133. public function delete()
  134. {
  135. $param = $this->request->only(['cat_id', 'companyNo'], 'post', 'trim');
  136. $val = Validate::rule([
  137. 'cat_id|分类id' => 'require|number|gt:0',
  138. 'companyNo|公司编码' => 'require|length:18',
  139. ]);
  140. if ($val->check($param) == false) return json_show(1004, $val->getError());
  141. $rs = Db::name('company_cat_profit')
  142. ->where(['is_del' => 0, 'companyNo' => $param['companyNo'], 'cat_id' => $param['cat_id']])
  143. ->update([
  144. 'is_del' => 1,
  145. 'updatetime' => date('Y-m-d H:i:s')
  146. ]);
  147. return $rs ? json_show(0, '删除成功') : json_show(1004, '删除失败');
  148. }
  149. //筛选指定公司下的所有分类
  150. public function getListByCompany()
  151. {
  152. $param = $this->request->only(['companyNo', 'cat_name' => '', 'page' => 1, 'size' => 10], 'post', 'trim');
  153. $val = Validate::rule([
  154. 'companyNo|公司编码' => 'require|length:18',
  155. 'cat_name|分类名称' => 'max:255'
  156. ]);
  157. if ($val->check($param) == false) return json_show(1004, $val->getError());
  158. $where = [
  159. ['a.companyNo', '=', $param['companyNo']],
  160. ['a.is_del', '=', 0],
  161. ['b.level', '=', 3],
  162. ];
  163. if ($param['cat_name'] !== '') $where[] = ['b.search', 'like', '%' . $param['cat_name'] . '%'];
  164. //如果搜索名字,要把所有符合要求的三级分类id展现出来
  165. // if ($param['cat_name'] != '') {
  166. //
  167. // $cat_id = Db::name('cat')
  168. // ->where(['is_del' => 0])
  169. // ->whereLike('cat_name', '%' . $param['cat_name'] . '%')
  170. // ->column('id');
  171. // if (!empty($cat_id)) {
  172. // $catid = [];
  173. //// $tmp_1 = Db::name('cat')
  174. //// ->where(['is_del'=>0,'pid'=>$cat_id,'level'=>3])
  175. //// ->column('id');
  176. //// if(!empty($tmp_1)) $catid = array_merge($catid,$tmp_1);
  177. //
  178. // $tmp_1 = Db::name('cat')
  179. // ->where(['is_del' => 0, 'pid' => $cat_id])
  180. // ->column('id');
  181. // if (!empty($tmp_1)) {
  182. // $catid = array_merge($catid, $tmp_1);
  183. // $tmp_2 = Db::name('cat')
  184. // ->where(['is_del' => 0, 'pid' => $tmp_1])
  185. // ->column('id');
  186. // if (!empty($tmp_2)) {
  187. // $catid = array_merge($catid, $tmp_2);
  188. // $tmp_3 = Db::name('cat')
  189. // ->where(['is_del' => 0, 'pid' => $tmp_2])
  190. // ->column('id');
  191. // if (!empty($tmp_3)) $catid = array_merge($catid, $tmp_3);
  192. // }
  193. // }
  194. //
  195. // if ($catid) $where[] = ['a.cat_id', 'in', $catid];
  196. // }
  197. //
  198. // }
  199. $count = Db::name('company_cat_profit')
  200. ->alias('a')
  201. ->leftJoin('cat b', 'b.id=a.cat_id')
  202. // ->leftJoin('cat c', 'b.id=b.pid')
  203. // ->leftJoin('cat d', 'd.id=c.pid')
  204. ->where($where)
  205. ->count('a.id');
  206. $list = Db::name('company_cat_profit')
  207. ->alias('a')
  208. ->leftJoin('cat b', 'b.id=a.cat_id')
  209. // ->leftJoin('cat c', 'c.id=b.pid')
  210. // ->leftJoin('cat d', 'd.id=c.pid')
  211. ->field('a.id,b.search cat_name,a.cat_id,a.rate,a.order_rate,a.money_rate,a.low_rate,a.sale_rate,a.status')
  212. ->where($where)
  213. ->order(['a.id' => 'desc'])
  214. ->page($param['page'], $param['size'])
  215. ->select()
  216. ->toArray();
  217. return json_show(0, '获取成功', ['count' => $count, 'list' => $list]);
  218. }
  219. }