FinancialProducts.php 20 KB

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