FinancialManager.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. namespace app\cxinv\model;
  3. use think\facade\Log;use think\Model;use think\model\concern\SoftDelete;
  4. class FinancialManager extends Base{
  5. use SoftDelete;
  6. protected $schema=[
  7. 'id'=>'int',//主键,自动递增
  8. 'code'=>'string',//订单或记录的唯一标识符
  9. 'invoiceCode'=>'string',//订单或记录的唯一标识符
  10. 'type'=>'int',//记录类型 1入库2出库 3入库红冲 4 出库红冲
  11. 'source'=>'int',//数据来源,1结算 2 线下订单
  12. 'channel'=>'int',//渠道 1订单导入 2 非订单商品导入 3 c端无发票导入
  13. 'seller_code'=>'string',//卖方代码
  14. 'seller_name'=>'string',//卖方名称
  15. 'buyer_code'=>'string',//买方代码
  16. 'buyer_name'=>'string',//买方名称
  17. 'orderCode'=>'string',//订单编号
  18. 'cxCode'=>'string',//客户编号
  19. 'poCode'=>'string',//采购订单编号
  20. 'platform_type'=>'int',//平台类型,1ToB 2ToC
  21. 'goodType'=>'int',//商品类型 1库存 2非库存 3采返
  22. 'goodNo'=>'string',//商品编号
  23. 'goodName'=>'string',//商品名称
  24. 'unit'=>'string',//商品单位
  25. 'num'=>'int',//商品数量
  26. 'goodPrice'=>'decimal',//商品单价
  27. 'totalPrice'=>'decimal',//商品总价
  28. 'cat_code'=>'string',//商品分类代码
  29. 'cat_name'=>'string',//商品分类名称
  30. 'tax'=>'decimal',//税率或税费
  31. 'inv_fee'=>'decimal',//发票费用
  32. 'inv_seller_code'=>'string',//发票卖方代码
  33. 'inv_seller_name'=>'string',//发票卖方名称
  34. 'inv_buyer_code'=>'string',//发票买方代码
  35. 'inv_buyer_name'=>'string',//发票买方名称
  36. 'inv_number'=>'string',//发票编号
  37. 'inv_type'=>'string',//发票类型
  38. 'inv_item_id'=>'int',//发票项目ID
  39. 'inv_good_name'=>'string',//发票商品名称
  40. 'inv_cat_code'=>'string',//发票商品分类代码
  41. 'inv_spec'=>'string',//发票商品规格
  42. 'inv_unit'=>'string',//发票商品单位
  43. 'inv_num'=>'decimal',//发票商品数量
  44. 'inv_subprice'=>'decimal',//发票商品子项单价
  45. 'inv_subtotal'=>'decimal',//发票商品子项总价
  46. 'inv_tax'=>'decimal',//发票税额
  47. 'inv_tax_total'=>'decimal',//发票总税额
  48. 'inv_price'=>'decimal',//发票单价
  49. 'inv_total'=>'decimal',//发票总价
  50. 'inv_open_date'=>'datetime',
  51. 'cat_diff'=>'int',//分类差异
  52. 'tax_diff'=>'int',//税费差异
  53. 'remark'=>'string',//备注或说明
  54. 'error_remark'=>'string',//备注或说明
  55. 'status'=>'int',//1 待处理 2 正常 3计提 4 异常
  56. 'balance_num'=>'decimal',//库存数量
  57. 'total_num'=>'decimal',//总库存数量,
  58. 'check_fee'=>'decimal',
  59. 'is_checkOrder'=>'int',
  60. 'fz_date'=>'varchar',
  61. 'apply_id'=>'int',
  62. 'apply_name'=>'string',
  63. 'create_time'=>'datetime',
  64. 'update_time'=>'datetime',
  65. 'delete_time'=>'datetime',
  66. ];
  67. protected $createTime='create_time';
  68. protected $updateTime='update_time';
  69. protected $deleteTime='delete_time';
  70. public static $ManagerType=[1=>'入库',2=>'出库'];
  71. public static $ManagerSource=[1=>'结算',2=>'线下订单'];
  72. public static $PlatformType=[1=>'ToB',2=>'ToC'];
  73. public static $StatusCn=[1=>'待处理',2=>'正常',3=>'计提',4=>'异常'];
  74. public function ProductRela(){
  75. return $this->hasMany(ManagerProduct::class,'manager_id','id');
  76. }
  77. public function setErrorRemarkAttr($value,$data){
  78. return ($data['status']==4|| $data['status']==1)?$value:'';
  79. }
  80. public function CreateData($data){
  81. if(!empty($data)){
  82. switch ($data['channel']){
  83. case 1:
  84. $this->OrderImport($data);
  85. break;
  86. case 2:
  87. $this->OfflineImport($data);
  88. break;
  89. case 3:
  90. $this->CImport($data);
  91. break;
  92. default:
  93. throw new \Exception('导入渠道不存在');
  94. break;
  95. }
  96. }
  97. return true;
  98. }
  99. // 订单导入
  100. public function OrderImport($data){
  101. $data['total_num'] = $data['balance_num'] =$data['inv_num'];
  102. self::startTrans();
  103. try{
  104. if($data['inv_num']==0) throw new \Exception('发票商品数量不能为0');
  105. if($data['type']==1){
  106. $product= $this->inProduct($data);
  107. $data['error_remark'] = $data['balance_num']!='0'?'入库数量未处理完':'';
  108. }
  109. if($data['type']==2){
  110. $product= $this->outProduct($data);
  111. $data['error_remark'] = $data['balance_num']!='0'?'出库数量未处理完':'';
  112. }
  113. $data['status']=$data['balance_num']=='0'?2:1;
  114. self::commit();
  115. }catch (\Exception $e){
  116. self::rollback();
  117. $data['status'] =4;
  118. $data['error_remark'] = $e->getMessage();
  119. }
  120. $info=$this->create($data);
  121. if($info->isEmpty()) throw new \Exception('添加失败');
  122. if (isset($product)) ManagerProduct::AddProduct($info->id,$product);
  123. }
  124. // 线下订单导入入库
  125. public function inProduct(&$data){
  126. $productID=[];
  127. try{
  128. $product = FinancialProducts::with(['productStock','ProductsCombind'])->where($this->getCondition($data))->findOrEmpty();
  129. if($product->isEmpty()){
  130. if($data['type']==4) throw new \Exception('红冲未找到财务商品信息');
  131. if($data['channel']==2 || $data['channel']==3) throw new \Exception('入库未找到财务商品信息');
  132. else{
  133. $product_data=[
  134. 'skuCode'=>$data['goodNo'],
  135. 'goodName'=>$data['goodName'],
  136. 'buyer_name'=>$data['inv_buyer_name'],
  137. 'buyer_code'=>$data['inv_buyer_code'],
  138. 'seller_name'=>$data['inv_seller_name'],
  139. 'seller_code'=>$data['inv_seller_code'],
  140. 'good_source'=>1,
  141. 'good_type'=>$data['goodType'],
  142. 'inv_good_name'=>$data['inv_good_name'],
  143. 'unit'=>$data['inv_unit'],
  144. 'unit_price'=>$data['inv_price'],
  145. 'subunit_price'=>$data['inv_subprice'],
  146. 'unit_weight'=>$data['unit_weight']??0,
  147. 'cat_code'=>$data['inv_cat_code'],
  148. 'cat_tax'=>$data['inv_tax'],
  149. "spec"=>$data['inv_spec'],
  150. 'inv_type'=>$data['inv_type'],
  151. "is_combind"=>0,
  152. 'basic_status'=>1,
  153. 'spectral'=>'',
  154. 'apply_id'=>$data['apply_id'],
  155. 'apply_name'=>$data['apply_name'],
  156. 'status'=>1
  157. ];
  158. $product = FinancialProducts::create($product_data);
  159. }
  160. }
  161. if($product->status!=1) throw new \Exception('商品未启用');
  162. if($product->is_combind==1){
  163. $rednum=$data['balance_num'];
  164. if($product->ProductsCombind->isEmpty()) throw new \Exception('组合商品未找到明细');
  165. $product->ProductsCombind->each(function ($item) use ($rednum, &$productID,$data) {
  166. ProductStock::AddStock($item->child_id, $rednum * $item->child_num);
  167. $productID[] = ['product_id' => $item->child_id, 'type'=>1,'num' => $rednum * $item->child_num,'apply_id'=>$data['apply_id'], 'apply_name'=>$data['apply_name']];
  168. });
  169. }else{
  170. ProductStock::AddStock($product->id,$data['balance_num']);
  171. $productID[]=["product_id"=>$product->id,"type"=>1,"num"=>$data['balance_num'],'apply_id'=>$data['apply_id'], 'apply_name'=>$data['apply_name']];
  172. }
  173. $data['balance_num']="0";
  174. $data['status']=2;
  175. return $productID;
  176. }catch (\Exception $e){
  177. throw new \Exception($e->getMessage());
  178. }
  179. }
  180. // 出库
  181. public function outProduct(&$data){
  182. $productID=[];
  183. try{
  184. if(($data['channel'] == 1 || $data['channel']==3) && empty($data['relaArr'])) {
  185. $product = FinancialProducts::with(['productStock','ProductsCombind'])->where($this->getCondition($data))->findOrEmpty();
  186. if($product->isEmpty()) throw new \Exception('出库未找到财务商品信息');
  187. if($product->status!=1) throw new \Exception('商品未启用');
  188. if($data['balance_num']>$product->residue_stock) throw new \Exception('出库数量大于库存');
  189. $item=['id'=>$product->id,'num'=> $data['balance_num']];
  190. $this->processSingleProduct($item, $productID,$data);
  191. } else {
  192. if (isset($data['relaArr']) && is_array($data['relaArr'])&& !empty($data['relaArr'])) {
  193. foreach ($data['relaArr'] as $item) {
  194. if (!isset($item['id']) || !isset($item['num'])) {
  195. throw new \Exception('relaArr 中的元素缺少 id 或 num');
  196. }
  197. $this->processSingleProduct($item, $productID, $data);
  198. }
  199. } else {
  200. throw new \Exception('relaArr 为空或格式不正确');
  201. }
  202. }
  203. if($data['balance_num']=='0')$data['status']=2;
  204. }catch (\Exception $e){
  205. throw new \Exception($e->getMessage());
  206. }
  207. return $productID;
  208. }
  209. // 单商品处理
  210. private function processSingleProduct($data, &$productID, &$mainData = null) {
  211. $product = FinancialProducts::with(['productStock',"ProductsCombind"])->findOrEmpty($data['id']);
  212. if (!$product->isEmpty()) {
  213. if($product->status!=1) throw new \Exception('商品未启用');
  214. if($mainData['type']!=2 && $product->basic_status==2) throw new \Exception($product->skuCode.'商品不可为预估成本商品');
  215. $rednum = $data['num'];
  216. if($mainData['balance_num']< $data['num']) throw new \Exception('出库数量大于订单数量');
  217. if ($product->residue_stock < $data['num']) {
  218. if($mainData['type']==3) throw new \Exception($product->skuCode.'商品库存不足');
  219. $rednum = $product->residue_stock;
  220. if ($mainData !== null) {
  221. $mainData['balance_num'] = bcsub($mainData['balance_num'], $product->residue_stock, 8);
  222. }
  223. } else {
  224. if ($mainData !== null) {
  225. $mainData['balance_num'] = bcsub($mainData['balance_num'], $data['num'], 8);
  226. }
  227. }
  228. if($product->is_combind==1 ){
  229. if($product->ProductsCombind->isEmpty()) throw new \Exception('组合商品未找到明细');
  230. $productID=ProductsCombind::CombindSubStock($product->id,$rednum);
  231. array_map(function (&$item) use ($mainData){
  232. ProductStock::OutStock($item['product_id'], $item['num']);
  233. $item['apply_id']=$mainData['apply_id'];
  234. $item['apply_name']=$mainData['apply_name'];
  235. },$productID);
  236. }else{
  237. ProductStock::OutStock($product->id, $rednum);
  238. $productID[] = ['product_id' => $product->id, 'type'=>2,'num' => $rednum,'apply_id'=>$mainData['apply_id'], 'apply_name'=>$mainData['apply_name']];
  239. }
  240. // $mainData['manager_status']=$mainData['balance_num']=='0'?1:2;
  241. } else {
  242. throw new \Exception('出库未找到财务商品信息');
  243. }
  244. }
  245. public function getCondition($data){
  246. $where=[];
  247. if(($data['channel']==1|| $data['channel']==3) && (empty($data['relaArr'])|| !isset($data['relaArr']))) {
  248. $where=[
  249. 'skuCode'=>$data['goodNo'],
  250. 'goodName'=>$data['goodName'],
  251. 'good_type'=>$data['goodType'],
  252. 'seller_code'=>$data['inv_seller_code'],
  253. 'buyer_code'=>$data['inv_buyer_code'],
  254. 'inv_good_name'=>$data['inv_good_name'],
  255. 'unit'=>$data['inv_unit'],
  256. 'unit_price'=>$data['inv_price'],
  257. 'subunit_price'=>$data['inv_subprice'],
  258. 'inv_type'=>$data['inv_type'],
  259. 'cat_tax'=>$data['inv_tax'],
  260. 'cat_code'=>$data['inv_cat_code'],
  261. "basic_status"=>1
  262. ];
  263. if($data['type']==2){
  264. $good_source = $data['channel']==2?2:1;
  265. $where=[
  266. ['skuCode',"=",$data['goodNo']],
  267. ['good_type',"=",$data['goodType']],
  268. ['good_source',"=",$good_source],
  269. ['buyer_code',"=",$data['inv_seller_code']],
  270. ['basic_status',"=",1],
  271. ];
  272. }
  273. }
  274. else $where[]=[
  275. 'id','in',array_column($data['relaArr'],"id")
  276. ];
  277. return $where;
  278. }
  279. // 线下订单导入
  280. public function OfflineImport($data){
  281. $data['total_num'] = $data['balance_num'] =$data['relaArr'][0]['num'];
  282. if($data['orderCode']=="")$data['orderCode']=makeNo("PQR");
  283. self::startTrans();
  284. try{
  285. if($data['type']==1){
  286. $product=$this->inProduct($data);
  287. $data['error_remark'] = $data['balance_num']!='0'?'入库数量未处理完':'';
  288. }
  289. if($data['type']==2){
  290. $product=$this->outProduct($data);
  291. $data['error_remark'] = $data['balance_num']!='0'?'出库数量未处理完':'';
  292. }
  293. $data['status']=$data['balance_num']=='0'?2:1;
  294. self::commit();
  295. }catch (\Exception $e){
  296. self::rollback();
  297. $data['status'] =4;
  298. $data['error_remark'] = $e->getMessage();
  299. }
  300. $info=$this->create($data);
  301. if($info->isEmpty()) throw new \Exception('添加失败');
  302. if(isset($product) && !empty($product))ManagerProduct::AddProduct($info->id,$product);
  303. }
  304. // c端无发票导入
  305. public function CImport($data){
  306. $data['total_num'] = $data['balance_num'] =$data['num'];
  307. self::startTrans();
  308. try{
  309. if($data['type']==1){
  310. $product=$this->inProduct($data);
  311. $data['error_remark'] = $data['balance_num']!='0'?'入库数量未处理完':'';
  312. }
  313. if($data['type']==2){
  314. $product=$this->outProduct($data);
  315. $data['error_remark'] = $data['balance_num']!='0'?'出库数量未处理完':'';
  316. }
  317. $data['status']=$data['balance_num']=='0'?2:1;
  318. self::commit();
  319. }catch (\Exception $e){
  320. self::rollback();
  321. $data['status'] =4;
  322. $data['error_remark'] = $e->getMessage();
  323. }
  324. $info=$this->create($data);
  325. if($info->isEmpty()) throw new \Exception('添加失败');
  326. if(isset($product) && !empty($product))ManagerProduct::AddProduct($info->id,$product);
  327. }
  328. // 校验数据
  329. public function CheckDatas($data){
  330. $error=[];
  331. $invoice = PayInvoice::where('hpNo',$data['invoiceCode'])->findOrEmpty();
  332. if($invoice->isEmpty()){
  333. $invoice = InvoicePool::where("invNo",$data['invoiceCode'])->findOrEmpty();
  334. if($invoice->isEmpty()){
  335. $error[] = ['code'=>$data['invoiceCode'],'msg'=>'发票号未找到数据'];
  336. }
  337. }
  338. if(!$invoice->isEmpty() && $invoice->status!=4) $error[] = ['code'=>$data['invoiceCode'],'msg'=>'发票状态不正确'];
  339. $order = CgdInfo::where("sequenceNo",$data['orderCode'])->findOrEmpty();
  340. if($order->isEmpty()){
  341. $order = QrdInfo::where("sequenceNo",$data['orderCode'])->findOrEmpty();
  342. if($order->isEmpty())$error[] = ['code'=>$data['orderCode'],'msg'=>'订单未找到数据'];
  343. }
  344. $good = Good::where("spuCode",$data['goodNo'])->findOrEmpty();
  345. if($good->isEmpty()) $error[] = ['code'=>$data['goodNo'],'msg'=>'采销商品未找到数据'];
  346. if(!$order->isEmpty() && $order->cxCode!=$data['cxCode']) $error[] = ['code'=>$data['orderCode'],'msg'=>'订单主单号不一致'];
  347. return $error;
  348. }
  349. public function OutKet(&$data)
  350. {
  351. $productID=[];
  352. try{
  353. if (isset($data['relaArr']) && is_array($data['relaArr'])) {
  354. $ketArr=[];
  355. foreach ($data['relaArr'] as $item) {
  356. if (!isset($item['id']) || !isset($item['num'])) {
  357. throw new \Exception('relaArr 中的元素缺少 id 或 num');
  358. }
  359. $ket=$this->processKtProduct($item, $productID, $data);
  360. if(!empty($ket)) $ketArr[]=$ket;
  361. }
  362. if(!empty($ketArr)){
  363. $create=[
  364. 'manager_id'=>$data['id'],
  365. 'ktCode'=>makeNo('CL'),
  366. 'apply_id'=>$data['cl_uid'],
  367. 'apply_name'=>$data['cl_uname'],
  368. 'num'=>"0",
  369. 'out_fee'=>"0",
  370. 'check_fee'=>0,
  371. 'status'=>1,
  372. ];
  373. FinancialTz::MakeInfo($ketArr,$create);
  374. }
  375. if($data['balance_num']==0) $data['status']=empty($ketArr)?2:3; //计提
  376. } else {
  377. throw new \Exception('relaArr 为空或格式不正确');
  378. }
  379. }catch (\Exception $e){
  380. throw new \Exception($e->getMessage());
  381. }
  382. return $productID;
  383. }
  384. public function processKtProduct($item, &$productID, &$data)
  385. {
  386. $kt=[];
  387. $product = FinancialProducts::with(['productStock',"ProductsCombind"])->findOrEmpty($item['id']);
  388. if (!$product->isEmpty()) {
  389. if($product->status!=1) throw new \Exception('商品未启用');
  390. if($product->is_combind==0){
  391. if($product->residue_stock<$item['num']){
  392. $kt=["product_id"=>$product->id,"type"=>1,'num'=>bcsub($item['num'],$product->residue_stock,8),"subunit_price"=>$product->subunit_price,"unit_price"=>$product->unit_price];
  393. }
  394. ProductStock::OutStock($product->id,$item['num'],2);
  395. $productID[]=["product_id"=>$product->id,'num'=>$item['num'],'type'=>2,'apply_id'=>$data['cl_uid'],'apply_name'=>$data['cl_uname']];
  396. $data['balance_num']=bcsub($data['balance_num'],$item['num'],8);
  397. }else{
  398. $productID=ProductsCombind::CombindSubStock($product->id,$item['num'],2);
  399. if($product->combind_stock<$item['num']) $kt =['product_id'=>$product->id,'type'=>1,'num'=>bcsub($item['num'],$product->combind_stock,8),'subunit_price'=>$product->subunit_price,'unit_price'=>$product->unit_price];
  400. array_map(function (&$item)use(&$kt,$data){
  401. ProductStock::OutStock($item['product_id'], $item['num'],2);
  402. $item['apply_id']=$data['cl_uid'];
  403. $item['apply_name']=$data['cl_uname'];
  404. },$productID);
  405. $data['balance_num']=bcsub($data['balance_num'],$item['num'],8);
  406. }
  407. }else{
  408. throw new \Exception('商品未找到');
  409. }
  410. return $kt;
  411. }
  412. }