FinancialManager.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 异常 5 取消数据
  56. 'balance_num'=>'decimal',//库存数量
  57. 'total_num'=>'decimal',//总库存数量,
  58. 'fz_date'=>'varchar',
  59. 'apply_id'=>'int',
  60. 'apply_name'=>'string',
  61. 'create_time'=>'datetime',
  62. 'update_time'=>'datetime',
  63. 'delete_time'=>'datetime',
  64. ];
  65. protected $createTime='create_time';
  66. protected $updateTime='update_time';
  67. protected $deleteTime='delete_time';
  68. public static $ManagerType=[1=>'入库',2=>'出库',3=>'入库红冲',4=>'出库红冲'];
  69. public static $ManagerSource=[1=>'结算',2=>'线下订单'];
  70. public static $ManagerGoodType=[1=>'库存',2=>'非库存',3=>'采返'];
  71. public static $PlatformType=[1=>'ToB',2=>'ToC'];
  72. public static $StatusCn=[1=>'待处理',2=>'正常',3=>'计提',4=>'异常' ,5=>'取消数据'];
  73. public function ProductRela(){
  74. return $this->hasMany(ManagerProduct::class,'manager_id','id');
  75. }
  76. public function FinancialTz(){
  77. return $this->hasOne(FinancialTz::class,'manager_id','id');
  78. }
  79. public function setErrorRemarkAttr($value,$data){
  80. return ($data['status']==4|| $data['status']==1)?$value:'';
  81. }
  82. public function CreateData($data){
  83. if(!empty($data)){
  84. switch ($data['channel']){
  85. case 1:
  86. $this->OrderImport($data);
  87. break;
  88. case 2:
  89. $this->OfflineImport($data);
  90. break;
  91. case 3:
  92. $this->CImport($data);
  93. break;
  94. default:
  95. throw new \Exception('导入渠道不存在');
  96. break;
  97. }
  98. }
  99. return true;
  100. }
  101. // 订单导入
  102. public function OrderImport($data){
  103. $data['total_num'] = $data['balance_num'] =$data['inv_num'];
  104. if(!empty($data['idArr'])) $product=array_map(function ($item) use($data){
  105. $temp=[];
  106. $temp['product_id']=$item['id'];
  107. $temp['type']=$data['type'];
  108. $temp['num']=$item['num'];
  109. $temp['apply_id'] = $data['apply_id'];
  110. $temp['apply_name'] = $data['apply_name'];
  111. $temp['status'] = 0;
  112. return $temp;
  113. },$data['idArr']);
  114. $info=$this->create($data);
  115. if($info->isEmpty()) throw new \Exception('添加失败');
  116. if (isset($product)) ManagerProduct::AddProduct($info->id,$product);
  117. }
  118. // 线下订单导入入库
  119. public function inProduct(&$data){
  120. $productID=[];
  121. try{
  122. $product = FinancialProducts::with(['productStock','ProductsCombind'])->where($this->getCondition($data))->findOrEmpty();
  123. if($product->isEmpty()){
  124. if($data['type']==4) throw new \Exception('红冲未找到财务商品信息');
  125. if($data['channel']==2 || $data['channel']==3) throw new \Exception('入库未找到财务商品信息');
  126. else{
  127. $product_data=[
  128. 'skuCode'=>$data['goodNo'],
  129. 'goodName'=>$data['goodName'],
  130. 'buyer_name'=>$data['inv_buyer_name'],
  131. 'buyer_code'=>$data['inv_buyer_code'],
  132. 'seller_name'=>$data['inv_seller_name'],
  133. 'seller_code'=>$data['inv_seller_code'],
  134. 'good_source'=>1,
  135. 'good_type'=>$data['goodType'],
  136. 'inv_good_name'=>$data['inv_good_name'],
  137. 'unit'=>$data['inv_unit'],
  138. 'unit_price'=>$data['inv_price'],
  139. 'subunit_price'=>$data['inv_subprice'],
  140. 'unit_weight'=>$data['unit_weight']??0,
  141. 'cat_code'=>$data['inv_cat_code'],
  142. 'cat_tax'=>$data['inv_tax'],
  143. "spec"=>$data['inv_spec'],
  144. 'inv_type'=>$data['inv_type'],
  145. "is_combind"=>0,
  146. 'basic_status'=>1,
  147. 'spectral'=>'',
  148. 'apply_id'=>$data['apply_id'],
  149. 'apply_name'=>$data['apply_name'],
  150. 'status'=>1
  151. ];
  152. $product = FinancialProducts::create($product_data);
  153. }
  154. }
  155. if($product->status!=1) throw new \Exception('商品未启用');
  156. if($product->is_combind==1){
  157. $rednum=$data['balance_num'];
  158. if($product->ProductsCombind->isEmpty()) throw new \Exception('组合商品未找到明细');
  159. $product->ProductsCombind->each(function ($item) use ($rednum, &$productID,$data) {
  160. ProductStock::AddStock($item->child_id, $rednum * $item->child_num);
  161. $productID[] = ['product_id' => $item->child_id, 'type'=>1,'status'=>1,'num' => $rednum * $item->child_num,'apply_id'=>$data['apply_id'], 'apply_name'=>$data['apply_name']];
  162. });
  163. }else{
  164. ProductStock::AddStock($product->id,$data['balance_num']);
  165. $productID[]=["product_id"=>$product->id,"type"=>1,'status'=>1,"num"=>$data['balance_num'],'apply_id'=>$data['apply_id'], 'apply_name'=>$data['apply_name']];
  166. }
  167. $data['balance_num']="0";
  168. $data['status']=2;
  169. return $productID;
  170. }catch (\Exception $e){
  171. throw new \Exception($e->getMessage());
  172. }
  173. }
  174. // 出库
  175. public function outProduct(&$data){
  176. $productID=[];
  177. try{
  178. if(($data['channel'] == 1 || $data['channel']==3) && empty($data['relaArr'])) {
  179. $product = FinancialProducts::with(['productStock','ProductsCombind'])->where($this->getCondition($data))->findOrEmpty();
  180. if($product->isEmpty()) throw new \Exception('出库未找到财务商品信息');
  181. if($product->status!=1) throw new \Exception('商品未启用');
  182. if($data['balance_num']>$product->residue_stock) throw new \Exception('出库数量大于库存');
  183. $item=['id'=>$product->id,'num'=> $data['balance_num']];
  184. $this->processSingleProduct($item, $productID,$data);
  185. } else {
  186. if (isset($data['relaArr']) && is_array($data['relaArr'])&& !empty($data['relaArr'])) {
  187. if(floatval($data['balance_num'])!= floatval(array_sum(array_column($data['relaArr'], 'num')))){
  188. throw new \Exception('出库商品数量不正确');
  189. }else{
  190. foreach ($data['relaArr'] as $item) {
  191. if (!isset($item['id']) || !isset($item['num'])) {
  192. throw new \Exception('relaArr 中的元素缺少 id 或 num');
  193. }
  194. $this->processSingleProduct($item, $productID, $data);
  195. }
  196. }
  197. } else {
  198. throw new \Exception('relaArr 为空或格式不正确');
  199. }
  200. }
  201. if($data['balance_num']=='0')$data['status']=2;
  202. }catch (\Exception $e){
  203. throw new \Exception($e->getMessage());
  204. }
  205. return $productID;
  206. }
  207. // 单商品处理
  208. private function processSingleProduct($data, &$productID, &$mainData = null) {
  209. $product = FinancialProducts::with(['productStock',"ProductsCombind"])->findOrEmpty($data['id']);
  210. if (!$product->isEmpty()) {
  211. if($product->status!=1) throw new \Exception('商品未启用');
  212. if($mainData['type']!=2 && $product->basic_status==2) throw new \Exception($product->skuCode.'商品不可为预估成本商品');
  213. $rednum = $data['num'];
  214. if($mainData['balance_num']< $data['num']) throw new \Exception('出库数量大于订单数量');
  215. if ($product->residue_stock < $data['num']) {
  216. if($mainData['type']==3) throw new \Exception($product->skuCode.'商品库存不足');
  217. $rednum = $product->residue_stock;
  218. if ($mainData !== null) {
  219. $mainData['balance_num'] = bcsub($mainData['balance_num'], $product->residue_stock, 8);
  220. }
  221. } else {
  222. if ($mainData !== null) {
  223. $mainData['balance_num'] = bcsub($mainData['balance_num'], $data['num'], 8);
  224. }
  225. }
  226. if($product->is_combind==1 ){
  227. if($product->ProductsCombind->isEmpty()) throw new \Exception('组合商品未找到明细');
  228. $productID=ProductsCombind::CombindSubStock($product->id,$rednum);
  229. array_map(function (&$item) use ($mainData){
  230. ProductStock::OutStock($item['product_id'], $item['num']);
  231. $item['status']=1;
  232. $item['apply_id']=$mainData['apply_id'];
  233. $item['apply_name']=$mainData['apply_name'];
  234. },$productID);
  235. }else{
  236. ProductStock::OutStock($product->id, $rednum);
  237. $productID[] = ['product_id' => $product->id, 'type'=>2,'status'=>1,'num' => $rednum,'apply_id'=>$mainData['apply_id'], 'apply_name'=>$mainData['apply_name']];
  238. }
  239. } else {
  240. throw new \Exception('出库未找到财务商品信息');
  241. }
  242. }
  243. public function getCondition($data){
  244. $where=[];
  245. if(($data['channel']==1|| $data['channel']==3) && (empty($data['relaArr'])|| !isset($data['relaArr']))) {
  246. $where=[
  247. 'skuCode'=>$data['goodNo'],
  248. 'goodName'=>$data['goodName'],
  249. 'good_type'=>$data['goodType'],
  250. 'seller_code'=>$data['inv_seller_code'],
  251. 'buyer_code'=>$data['inv_buyer_code'],
  252. 'inv_good_name'=>$data['inv_good_name'],
  253. 'unit'=>$data['inv_unit'],
  254. 'unit_price'=>$data['inv_price'],
  255. 'subunit_price'=>$data['inv_subprice'],
  256. 'inv_type'=>$data['inv_type'],
  257. 'cat_tax'=>$data['inv_tax'],
  258. 'cat_code'=>$data['inv_cat_code'],
  259. "basic_status"=>1
  260. ];
  261. if($data['type']==2){
  262. $good_source = $data['channel']==2?2:1;
  263. $where=[
  264. ['skuCode',"=",$data['goodNo']],
  265. ['good_type',"=",$data['goodType']],
  266. ['good_source',"=",$good_source],
  267. ['buyer_code',"=",$data['inv_seller_code']],
  268. ['basic_status',"=",1],
  269. ];
  270. }
  271. }
  272. else $where[]=[
  273. 'id','in',array_column($data['relaArr'],"id")
  274. ];
  275. return $where;
  276. }
  277. // 线下订单导入
  278. public function OfflineImport($data){
  279. $data['total_num'] = $data['balance_num'] =$data['relaArr'][0]['num'];
  280. if($data['orderCode']=="")$data['orderCode']=makeNo("PQR");
  281. if(!empty($data['idArr'])) $product=array_map(function ($item) use($data){
  282. $temp=[];
  283. $temp['product_id']=$item['id'];
  284. $temp['type']=$data['type'];
  285. $temp['num']=$item['num'];
  286. $temp['apply_id'] = $data['apply_id'];
  287. $temp['apply_name'] = $data['apply_name'];
  288. $temp['status'] = 0;
  289. return $temp;
  290. },$data['idArr']);
  291. $info=$this->create($data);
  292. if($info->isEmpty()) throw new \Exception('添加失败');
  293. if(isset($product) && !empty($product))ManagerProduct::AddProduct($info->id,$product);
  294. }
  295. // c端无发票导入
  296. public function CImport($data){
  297. $data['total_num'] = $data['balance_num'] =$data['num'];
  298. if(!empty($data['idArr'])) $product=array_map(function ($item) use($data){
  299. $temp=[];
  300. $temp['product_id']=$item['id'];
  301. $temp['type']=$data['type'];
  302. $temp['num']=$item['num'];
  303. $temp['apply_id'] = $data['apply_id'];
  304. $temp['apply_name'] = $data['apply_name'];
  305. $temp['status'] = 0;
  306. return $temp;
  307. },$data['idArr']);
  308. $info=$this->create($data);
  309. if($info->isEmpty()) throw new \Exception('添加失败');
  310. if(isset($product) && !empty($product))ManagerProduct::AddProduct($info->id,$product);
  311. }
  312. // 校验数据
  313. public function CheckDatas($data){
  314. $error=[];
  315. $invoice = PayInvoice::where('hpNo',$data['invoiceCode'])->findOrEmpty();
  316. if($invoice->isEmpty()){
  317. $invoice = InvoicePool::where("invNo",$data['invoiceCode'])->findOrEmpty();
  318. if($invoice->isEmpty()){
  319. $error[] = ['code'=>$data['invoiceCode'],'msg'=>'发票号未找到数据'];
  320. }
  321. }
  322. if(!$invoice->isEmpty() && $invoice->status!=4) $error[] = ['code'=>$data['invoiceCode'],'msg'=>'发票状态不正确'];
  323. $order = CgdInfo::where("sequenceNo",$data['orderCode'])->findOrEmpty();
  324. if($order->isEmpty()){
  325. $order = QrdInfo::where("sequenceNo",$data['orderCode'])->findOrEmpty();
  326. if($order->isEmpty())$error[] = ['code'=>$data['orderCode'],'msg'=>'订单未找到数据'];
  327. }
  328. $good = Good::where("spuCode",$data['goodNo'])->findOrEmpty();
  329. if($good->isEmpty()) $error[] = ['code'=>$data['goodNo'],'msg'=>'采销商品未找到数据'];
  330. if(!$order->isEmpty() && $order->cxCode!=$data['cxCode']) $error[] = ['code'=>$data['orderCode'],'msg'=>'订单主单号不一致'];
  331. return $error;
  332. }
  333. public function OutKet(&$data)
  334. {
  335. $productID=[];
  336. try{
  337. if (isset($data['relaArr']) && is_array($data['relaArr'])) {
  338. $ketArr=[];
  339. foreach ($data['relaArr'] as $item) {
  340. if (!isset($item['id']) || !isset($item['num'])) {
  341. throw new \Exception('relaArr 中的元素缺少 id 或 num');
  342. }
  343. $ket=$this->processKtProduct($item, $productID, $data);
  344. if(!empty($ket)) $ketArr[]=$ket;
  345. }
  346. if(!empty($ketArr)){
  347. $create=[
  348. 'manager_id'=>$data['id'],
  349. 'ktCode'=>makeNo('CL'),
  350. 'apply_id'=>$data['cl_uid'],
  351. 'apply_name'=>$data['cl_uname'],
  352. 'num'=>"0",
  353. 'out_fee'=>"0",
  354. 'check_fee'=>0,
  355. 'status'=>1,
  356. ];
  357. FinancialTz::MakeInfo($ketArr,$create);
  358. }
  359. if($data['balance_num']==0) $data['status']=empty($ketArr)?2:3; //计提
  360. } else {
  361. throw new \Exception('relaArr 为空或格式不正确');
  362. }
  363. }catch (\Exception $e){
  364. throw new \Exception($e->getMessage());
  365. }
  366. return $productID;
  367. }
  368. public function processKtProduct($item, &$productID, &$data)
  369. {
  370. $kt=[];
  371. $product = FinancialProducts::with(['productStock',"ProductsCombind"])->findOrEmpty($item['id']);
  372. if (!$product->isEmpty()) {
  373. if($product->status!=1) throw new \Exception('商品未启用');
  374. if($product->is_combind==0){
  375. if($product->residue_stock<$item['num']){
  376. $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];
  377. }
  378. ProductStock::OutStock($product->id,$item['num'],2);
  379. $productID[]=["product_id"=>$product->id,'num'=>$item['num'],'status'=>1,'type'=>2,'apply_id'=>$data['cl_uid'],'apply_name'=>$data['cl_uname']];
  380. $data['balance_num']=bcsub($data['balance_num'],$item['num'],8);
  381. }else{
  382. $productID=ProductsCombind::CombindSubStock($product->id,$item['num'],2);
  383. 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];
  384. array_map(function (&$item)use(&$kt,$data){
  385. ProductStock::OutStock($item['product_id'], $item['num'],2);
  386. $item['status']=1;
  387. $item['apply_id']=$data['cl_uid'];
  388. $item['apply_name']=$data['cl_uname'];
  389. },$productID);
  390. $data['balance_num']=bcsub($data['balance_num'],$item['num'],8);
  391. }
  392. }else{
  393. throw new \Exception('商品未找到');
  394. }
  395. return $kt;
  396. }
  397. }