FinancialProducts.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. namespace app\cxinv\controller;
  3. use app\cxinv\model\ProductCheck;
  4. use app\cxinv\model\ProductFz;
  5. use app\cxinv\model\ProductsCombind;
  6. use think\App;
  7. class FinancialProducts extends Base
  8. {
  9. public function __construct(App $app)
  10. {
  11. parent::__construct($app);
  12. $this->model = new \app\cxinv\model\FinancialProducts();
  13. }
  14. public function List()
  15. {
  16. $param = $this->request->param(["skuCode" => "", "good_type" => "", "buyer_code" => "", "buyer_name" => "", "start" => "",
  17. "end" => "", "is_combind" => "","good_source"=>"" ,"basic_status" => "", "page" => 1, "size" => 15], "post", "trim");
  18. $where = [];
  19. if ($param['skuCode'] != "") $where[] = ["skuCode", "like", "%{$param['skuCode']}%"];
  20. if ($param['good_type'] != "") $where[] = ["good_type", "=", $param['good_type']];
  21. if($param['good_source']!="") $where[]= ["good_source","=",$param['good_source']];
  22. if ($param['buyer_code'] != "") $where[] = ["buyer_code", "=", $param['buyer_code']];
  23. if ($param['buyer_name'] != "") $where[] = ["buyer_name", "like", "%{$param['buyer_name']}%"];
  24. if ($param['start'] != "") $where[] = ["create_time", ">=", startTime($param['start'])];
  25. if ($param['end'] != "") $where[] = ["create_time", "<=", endTime($param['end'])];
  26. if ($param['is_combind'] != "") $where[] = ["is_combind", "=", $param['is_combind']];
  27. if ($param['basic_status'] !== "") $where[] = ["basic_status", "=", $param['basic_status']];
  28. $list = $this->model
  29. ->with(["catInfo", "ProductsCombind" => ['products'], "ProductStock"])
  30. ->where($where)
  31. ->order("id desc")
  32. ->paginate(['page' => $param['page'], 'list_rows' => $param['size']]);
  33. return success('获取成功', ['list' => $list->items(), 'count' => $list->total()]);
  34. }
  35. public function create()
  36. {
  37. $param = $this->request->param(["skuCode" => "", "goodName" => "", "good_type" => "", "buyer_code" => "", "buyer_name" => "",
  38. "seller_code" => "", "seller_name" => "", "good_source" => "", "inv_good_name" => "", "is_combind" => "", "spec" => "", "good_code" => "",
  39. "unit" => "", "unit_price" => "", "subunit_price" => "", "unit_weight" => "", "cat_code" => "", "cat_tax" => "", "inv_type" => "",
  40. "basic_status" => 1, 'spectral' => '', "childArr" => []], "post", "trim");
  41. $valid = $this->validate($param, [
  42. 'skuCode|商品编号' => 'require|max:255',
  43. 'goodName|商品名称' => 'require|max:255',
  44. 'good_type|商品类型' => 'require|in:1,2,3',
  45. 'buyer_code|买方公司纳税识别号' => 'require|max:255',
  46. 'buyer_name|买方公司名称' => 'require|max:255',
  47. 'seller_code|卖方公司纳税识别号' => 'require|max:255',
  48. 'seller_name|卖方公司名称' => 'require|max:255',
  49. 'good_source|商品来源' => 'require|in:1,2',
  50. 'inv_good_name|发票商品名称' => 'require|max:255',
  51. 'is_combind|是否组合商品' => 'require|in:0,1',
  52. 'spec|规格' => 'max:255',
  53. 'good_code|商品代码' => 'max:255',
  54. 'unit|单位' => 'require|max:255',
  55. 'unit_price|成本税前单价' => 'require|float',
  56. 'subunit_price|成本税后单价' => 'require|float',
  57. 'unit_weight|重量' => 'float',
  58. "spectral|分光" => 'max:255',
  59. 'cat_code|进项类目' => 'require|max:255',
  60. 'cat_tax|进项税率' => 'require|max:255',
  61. 'inv_type|发票类型' => 'require|max:255',
  62. 'basic_status|进项成本状态' => 'require|in:1,2',
  63. 'childArr|商品子商品' => 'requireIf:is_combind,1|array'
  64. ]);
  65. if ($valid !== true) return error($valid);
  66. $param['apply_id'] = $this->uid;
  67. $param['apply_name'] = $this->uname;
  68. if ($param['is_combind'] == 1) {
  69. foreach ($param['childArr'] as $key => $item) {
  70. $iteminfo = $this->model->where('id', $item['child_id'])->findOrEmpty();
  71. if ($iteminfo->isEmpty()) return error("子商品{$iteminfo->skuCode}不存在");
  72. if ($item['child_num'] <= 0) return error("子商品{$iteminfo->skuCode}数量必须大于0");
  73. if ($iteminfo->basic_status == 2) return error("子商品{$iteminfo->skuCode}成本状态为预估成本");
  74. $param['childArr'][$key]['skuCode'] = $iteminfo->skuCode;
  75. }
  76. }
  77. $where = $param;
  78. unset($where['childArr']);
  79. $isT = $this->model->where($where)->findOrEmpty();
  80. if (!$isT->isEmpty()) return error("商品编号{$param['skuCode']}已存在");
  81. $this->model->startTrans();
  82. try {
  83. $res = $this->model->create($param);
  84. if ($res->isEmpty()) throw new \Exception('添加失败');
  85. if ($param['is_combind'] == 1) {
  86. $parentid = $res->id;
  87. $childRes = (new ProductsCombind)->saveAll(array_map(function ($item) use ($parentid) {
  88. $item['parent_id'] = $parentid;
  89. return $item;
  90. }, $param['childArr']));
  91. if ($childRes->isEmpty()) throw new \Exception('添加失败');
  92. }
  93. $this->model->commit();
  94. } catch (\Exception $e) {
  95. $this->model->rollback();
  96. return error($e->getMessage());
  97. }
  98. return success('添加成功');
  99. }
  100. public function update()
  101. {
  102. $param = $this->request->param(["id" => "", "skuCode" => "", "goodName" => "", "good_type" => "", "buyer_code" => "", "buyer_name" => "",
  103. "seller_code" => "", "seller_name" => "", "good_source" => "", "inv_good_name" => "", "is_combind" => "", "spec" => "", "good_code" => "",
  104. "unit" => "", "unit_price" => "", "subunit_price" => "", "unit_weight" => "", "cat_code" => "", "cat_tax" => "", "inv_type" => "",
  105. "basic_status" => "", "childArr" => []], "post", "trim");
  106. $valid = $this->validate($param, [
  107. 'id|商品ID' => 'require|integer',
  108. 'skuCode|商品编号' => 'require|max:255',
  109. 'goodName|商品名称' => 'require|max:255',
  110. 'good_type|商品类型' => 'require|in:1,2,3',
  111. 'buyer_code|买方公司纳税识别号' => 'require|max:255',
  112. 'buyer_name|买方公司名称' => 'require|max:255',
  113. 'seller_code|卖方公司纳税识别号' => 'require|max:255',
  114. 'seller_name|卖方公司名称' => 'require|max:255',
  115. 'good_source|商品来源' => 'require|in:1,2',
  116. 'inv_good_name|发票商品名称' => 'require|max:255',
  117. 'is_combind|是否组合商品' => 'require|in:0,1',
  118. 'spec|规格' => 'max:255',
  119. 'good_code|商品代码' => 'max:255',
  120. 'unit|单位' => 'require|max:255',
  121. 'unit_price|成本税前单价' => 'require|float',
  122. 'subunit_price|成本税后单价' => 'require|float',
  123. 'unit_weight|重量' => 'float',
  124. "spectral|分光" => 'max:255',
  125. 'cat_code|进项类目' => 'require|max:255',
  126. 'cat_tax|进项税率' => 'require|max:255',
  127. 'inv_type|发票类型' => 'require|max:255',
  128. 'basic_status|进项成本状态' => 'require|in:1,2',
  129. 'childArr|商品子商品' => 'requireIf:is_combind,1|array'
  130. ]);
  131. if ($valid !== true) return error($valid);
  132. $info = $this->model->with(['ProductsCombind'])->findOrEmpty($param['id']);
  133. if ($info->isEmpty()) return error('数据不存在');
  134. if ($info->is_combind == 1 || $param['is_combind'] == 1) {
  135. $childIds = array_column($info->ProductsCombind->toArray(), "id");
  136. $paramChilds = array_column($param['childArr'], 'id');
  137. $delIds = array_diff($childIds, $paramChilds);
  138. $add = [];
  139. foreach ($param['childArr'] as $key => $item) {
  140. $iteminfo = $this->model->where('id', $item['child_id'])->findOrEmpty();
  141. if ($iteminfo->isEmpty()) return error("子商品{$iteminfo->skuCode}不存在");
  142. if ($item['child_num'] <= 0) return error("子商品{$iteminfo->skuCode}数量必须大于0");
  143. if ($iteminfo->basic_status == 2) return error("子商品{$iteminfo->skuCode}成本状态为预估成本");
  144. $item['id'] = $item['id'] ?? null;
  145. $item['parent_id'] = $param['id'];
  146. $param['childArr'][$key]['skuCode'] = $iteminfo->skuCode;
  147. $add[] = $item;
  148. }
  149. }
  150. $where = $param;
  151. unset($where['childArr']);
  152. unset($where['id']);
  153. $isT = $this->model->where($where)->where("id", "<>", $param['id'])->findOrEmpty();
  154. if (!$isT->isEmpty()) return error("商品编号{$param['skuCode']}已存在");
  155. $this->model->startTrans();
  156. try {
  157. $res = $info->save($param);
  158. if (!$res) throw new \Exception('更新失败');
  159. if ($param['is_combind'] == 1) {
  160. if (!empty($delIds)) ProductsCombind::where(["parent_id" => $param['id'], "id" => $delIds])->delete();
  161. if (!empty($add)) {
  162. $childRes = (new ProductsCombind)->saveAll($add);
  163. if ($childRes->isEmpty()) throw new \Exception('更新失败');
  164. }
  165. }
  166. $this->model->commit();
  167. } catch (\Exception $e) {
  168. $this->model->rollback();
  169. return error($e->getMessage());
  170. }
  171. return success('更新成功');
  172. }
  173. public function info()
  174. {
  175. $id = $this->request->param("id", "0", "int");
  176. $info = $this->model->with(["catInfo", "ProductsCombind" => ['products'], 'ProductStock'])->findOrEmpty($id);
  177. if ($info->isEmpty()) return error('数据不存在');
  178. return success('获取成功', $info);
  179. }
  180. public function delete()
  181. {
  182. $id = $this->request->param("id", "0", "int");
  183. $info = $this->model->findOrEmpty($id);
  184. if ($info->isEmpty()) return error('数据不存在');
  185. $res = $info->delete();
  186. if ($info->is_combind == 1) {
  187. ProductsCombind::where("parent_id", $id)->delete();
  188. }
  189. return $res ? success('删除成功') : error('删除失败');
  190. }
  191. public function status()
  192. {
  193. $param = $this->request->param(["id" => "", "status" => ""], "post", "trim");
  194. $valid = $this->validate($param, [
  195. 'id|商品ID' => 'require|integer',
  196. 'status|状态' => 'require|in:0,1'
  197. ]);
  198. if ($valid !== true) return error($valid);
  199. $info = $this->model->findOrEmpty($param['id']);
  200. if ($info->isEmpty()) return error('数据不存在');
  201. $res = $info->save(['status' => $param['status']]);
  202. return $res ? success('修改成功') : error('修改失败');
  203. }
  204. public function getGoods()
  205. {
  206. $param = $this->request->param(["skuCode" => "", "good_type" => "", "buyer_code" => "", "seller_code" => "", "start" => "",
  207. "end" => "", "is_combind" => "","good_source"=>"", "basic_status" => "", "limit" => 100], "post", "trim");
  208. $valid = $this->validate($param, [
  209. 'skuCode|商品编号' => 'max:255',
  210. 'good_type|商品类型' => 'in:1,2,3',
  211. 'buyer_code|买方公司纳税识别号' => 'max:255',
  212. 'seller_code|卖方公司纳税识别号' => 'max:255',
  213. 'start|开始时间' => 'date',
  214. 'end|结束时间' => 'date',
  215. 'is_combind|是否组合商品' => 'in:0,1',
  216. 'basic_status|进项成本状态' => 'in:1,2',
  217. ]);
  218. if ($valid !== true) return error($valid);
  219. $where = [];
  220. if ($param['basic_status'] !== '') $where[] = ['basic_status', '=', $param['basic_status']];
  221. if($param['good_source']!="") $where[]= ["good_source","=",$param['good_source']];
  222. if (!empty($param['skuCode'])) $where[] = ['skuCode', 'like', '%' . $param['skuCode'] . '%'];
  223. if (!empty($param['good_type'])) $where[] = ['good_type', '=', $param['good_type']];
  224. if (!empty($param['buyer_code'])) $where[] = ['buyer_code', 'like', '%' . $param['buyer_code'] . '%'];
  225. if (!empty($param['seller_code'])) $where[] = ['seller_code', 'like', '%' . $param['seller_code'] . '%'];
  226. if (!empty($param['start']) && !empty($param['end'])) $where[] = ['create_time', 'between', [$param['start'], $param['end']]];
  227. $list = $this->model->with(['catInfo', 'ProductStock', 'ProductsCombind' => ['products']])->where($where)->order('id desc')->limit($param['limit'])->select();
  228. return success('获取成功', $list);
  229. }
  230. public function combindGood()
  231. {
  232. $param = $this->request->param(["parent_id" => "", "childArr" => []], "post", "trim");
  233. $valid = $this->validate($param, [
  234. 'parent_id|商品ID' => 'require|integer',
  235. 'childArr|子商品' => 'require|array'
  236. ]);
  237. if ($valid !== true) return error($valid);
  238. $parent = $this->model->findOrEmpty($param['pid']);
  239. if ($parent->isEmpty()) return error('数据不存在');
  240. $combind = new ProductsCombind();
  241. $ist = $combind->where(['parent_id' => $param['parent_id']])->column("child_id");
  242. $del = array_diff($ist, array_column($param['childArr'], 'child_id'));
  243. $add = [];
  244. foreach ($param['childArr'] as $item) {
  245. $vali = $this->validate($item, [
  246. 'child_id|子商品ID' => 'require|integer',
  247. 'child_num|子商品数量' => 'require|integer'
  248. ]);
  249. if ($vali !== true) return error($vali);
  250. $add[] = [
  251. "id" => $ist[$item['child_id']] ?? null,
  252. "parent_id" => $param['parent_id'],
  253. "child_num" => $item['child_num'],
  254. "child_id" => $item['child_id']
  255. ];
  256. }
  257. $this->model->startTrans();
  258. try {
  259. if (!empty($del)) $combind->where(["parent_id" => $param['parent_id'], "child_id" => $del])->delete();
  260. if (!empty($add)) $combind->saveAll($add);
  261. $this->model->commit();
  262. } catch (\Exception $e) {
  263. $this->model->rollback();
  264. return error($e->getMessage());
  265. }
  266. return success('修改成功');
  267. }
  268. public function CheckCreate()
  269. {
  270. $param = $this->request->param(["product_id" => "", 'check_num' => "", "check_type" => 0, "fz_date" => ""], "post", "trim");
  271. $valid = $this->validate($param, [
  272. 'product_id|商品ID' => 'require|integer',
  273. 'check_num|盘点数量' => 'require|float',
  274. 'check_type|盘点类型' => 'require|in:0,1,2',
  275. 'fz_date|调整单封账日期' => 'requireIf:check_type,2|max:255'
  276. ]);
  277. if ($valid !== true) return error($valid);
  278. $product = $this->model->with(['catInfo', 'ProductStock'])->findOrEmpty($param['product_id']);
  279. if ($product->isEmpty()) return error('商品数据不存在');
  280. if ($product->is_combind == 1) return error('组合商品不允许盘点');
  281. if ($product->basic_status ==2) return error('预估成本商品不允许盘点');
  282. if ($param['check_type'] == 2) {
  283. $isT = ProductFz::where(["fz_date" => $param['fz_date'], "company_code" => $product->buyer_code])->findOrEmpty();
  284. if (!$isT->isEmpty() && $isT->status != 0) return error('该日期已封账,不允许再新增');
  285. }
  286. $diff_num = bcsub($param['check_num'], $product->residue_stock ?? "0", 8);
  287. if ($diff_num == 0) return error('盘点数量与库存数量一致,无需盘点');
  288. $is_diff = $diff_num > 0 ? 1 : 0;
  289. $data = [
  290. 'checkCode' => makeNo("CWPD"),
  291. 'product_id' => $param['product_id'],
  292. 'check_num' => $param['check_num'],
  293. 'diff_num' => $diff_num,
  294. 'stock_num' => $product->residue_stock ?? "0",
  295. 'unit_price' => $product->unit_price,
  296. 'diff_fee' => bcmul($diff_num, $product->unit_price, 8),
  297. 'check_type' => $param['check_type'],
  298. 'fz_date' => $param['fz_date'],
  299. 'is_diff' => $is_diff,
  300. "apply_id" => $this->uid,
  301. "apply_name" => $this->uname
  302. ];
  303. $this->model->startTrans();
  304. try {
  305. $use = ProductCheck::create($data);
  306. if ($use->isEmpty()) throw new \think\Exception("新增盘点单失败");
  307. } catch (\Exception $e) {
  308. $this->model->rollback();
  309. return error($e->getMessage());
  310. }
  311. $this->model->commit();
  312. return success('盘点单创建成功');
  313. }
  314. public function CheckBatch()
  315. {
  316. $list = $this->request->param("list", [], "trim");
  317. if (empty($list)) return error('请选择要批量盘点的商品');
  318. $create = [];
  319. foreach ($list as $item) {
  320. // product_id check_num balance_num check_type fz_date
  321. $valid = $this->validate($item, [
  322. 'product_id|商品ID' => 'require|integer',
  323. 'check_num|盘点数量' => 'require|float',
  324. 'balance_num|库存数量' => 'require|float',
  325. 'check_type|盘点类型' => 'require|in:0,1,2',
  326. 'fz_date|调整单封账日期' => 'requireIf:check_type,2|max:255'
  327. ]);
  328. if ($valid !== true) return error($valid);
  329. $product = $this->model->with(['catInfo', 'ProductStock'])->findOrEmpty($item['product_id']);
  330. if ($product->isEmpty()) return error($item['product_id'] . '商品数据不存在');
  331. if ($product->is_combind == 1) return error($item['goodName'] . '组合商品不允许盘点');
  332. if ($product->basic_status ==2) return error($item['goodName'] . '预估成本商品不允许盘点');
  333. if ($item['check_type'] == 2) {
  334. $isT = ProductFz::where(["fz_date" => $item['fz_date'], "company_code" => $product->buyer_code])->findOrEmpty();
  335. if (!$isT->isEmpty() && $isT->status != 0) return error('该账期已封账,不允许再新增');
  336. }
  337. if ($item['balance_num'] != $product->residue_stock ?? "0") return error($product['goodName'] . '库存数量与盘点库存数量不一致');
  338. $diff_num = bcsub($item['check_num'], $item['balance_num'], 8);
  339. $isdiff = $diff_num > 0 ? 1 : 0;
  340. $create[] = [
  341. 'checkCode' => makeNo("CWPD"),
  342. 'product_id' => $item['product_id'],
  343. 'check_num' => $item['check_num'],
  344. 'diff_num' => $diff_num,
  345. 'stock_num' => $product->residue_stock ?? "0",
  346. 'unit_price' => $product->unit_price,
  347. 'diff_fee' => bcmul($diff_num, $product->unit_price, 8),
  348. 'check_type' => $item['check_type'],
  349. 'fz_date' => $item['fz_date'],
  350. 'is_diff' => $isdiff,
  351. "apply_id" => $this->uid,
  352. "apply_name" => $this->uname
  353. ];
  354. }
  355. $this->model->startTrans();
  356. try {
  357. $use = (new ProductCheck)->saveAll($create);
  358. if ($use->isEmpty()) throw new \think\Exception("批量新增盘点单失败");
  359. $this->model->commit();
  360. } catch (\Exception $e) {
  361. $this->model->rollback();
  362. return error($e->getMessage());
  363. }
  364. return success('批量盘点单创建成功');
  365. }
  366. public function CheckList()
  367. {
  368. $param = $this->request->param(["checkCode" => "", "check_type" => "", "start" => "", "end" => "", "skuCode" => "", "buyer_code" => "", "page" => 1, "size" => 20]
  369. , "post", "trim");
  370. $where = [];
  371. if ($param['checkCode'] !== "") $where[] = ['checkCode', 'like', '%' . $param['checkCode'] . '%'];
  372. if ($param['check_type'] !== "") $where[] = ['check_type', '=', $param['check_type']];
  373. if (!empty($param['start']) && !empty($param['end'])) $where[] = ['product_check.create_time', 'between', [startTime($param['start']), endTime($param['end'])]];
  374. if ($param['skuCode'] !== "") $where[] = ['product.skuCode', 'like', '%' . $param['skuCode'] . '%'];
  375. if ($param['buyer_code'] !== "") $where[] = ['product.buyer_code', '=', $param['buyer_code']];
  376. $list = ProductCheck::with(['product' => ['ProductStock']])
  377. ->withJoin(['product'], 'LEFT')
  378. ->where($where)
  379. ->order('id desc')
  380. ->paginate(['page' => $param['page'], 'list_rows' => $param['size']]);
  381. return success("获取成功", ["list" => $list->items(), "count" => $list->total()]);
  382. }
  383. public function CheckInfo()
  384. {
  385. $id = $this->request->param('id', 0, 'intval');
  386. if (empty($id)) return error('盘点单ID不能为空');
  387. $info = ProductCheck::with(['product' => ['ProductStock']])
  388. ->findOrEmpty($id);
  389. if ($info->isEmpty()) return error('盘点单不存在');
  390. return success("获取成功", $info);
  391. }
  392. }