Import.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Import;
  3. use App\Model\Employee;
  4. use App\Model\Finance;
  5. use App\Model\FinanceDetail;
  6. use App\Model\Inventory;
  7. use App\Model\InventoryInSub;
  8. use App\Model\InventoryOutSub;
  9. use App\Model\InventorySub;
  10. use App\Model\RollFilm;
  11. use App\Model\RollFilmCompare;
  12. use App\Model\RollFilmInventory;
  13. use App\Model\Settings;
  14. use App\Model\Storehouse;
  15. use Illuminate\Support\Facades\DB;
  16. use Maatwebsite\Excel\Concerns\ToArray;
  17. class Import implements ToArray {
  18. private $msg = '';
  19. public $crt_id = 0;
  20. public function array (array $array){
  21. $this->handleData($array);
  22. }
  23. public function setCrt($crt_id){
  24. $this->crt_id = $crt_id;
  25. }
  26. public function getMsg(){
  27. return $this->msg;
  28. }
  29. public function setMsg($msg){
  30. $this->msg = $msg;
  31. }
  32. public function handleData (array $array) {
  33. // 去除表头
  34. unset($array[0]);
  35. if(empty($array)) {
  36. $this->setMsg('数据不能为空!');
  37. return ;
  38. }
  39. //第一次表格数据校验 非空 已经过滤数据
  40. foreach ($array as $key => $value){
  41. if(empty($value[0]) && empty($value[1]) && empty($value[2]) && empty($value[3])) {
  42. unset($array[$key]);
  43. }elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3])) {
  44. $this->setMsg('数据必须完整填写!');
  45. return ;
  46. }else{
  47. if(! is_numeric($value[3])){
  48. $this->setMsg('请输入正确的金额!');
  49. return ;
  50. }
  51. }
  52. }
  53. if(empty($array)) {
  54. $this->setMsg('数据不能为空!');
  55. return ;
  56. }
  57. $array = array_values($array);
  58. //默认拆分金额
  59. $setting = Settings::where('name','defaultAmount')->first();
  60. if(empty($setting) || ! is_numeric($setting->value) || $setting->value <= 0) {
  61. $this->setMsg('请确认默认拆分金额是否正确!');
  62. return ;
  63. }
  64. $defaultAmount = (float)$setting->value;
  65. //生成时间
  66. $crt_time = time();
  67. //出账主表和子表数据
  68. $main = $sub = [];
  69. foreach ($array as $key => $value){
  70. //主表数据
  71. $main[] = [
  72. 'finance_account_name' => $value[0],
  73. 'account' => $value[1],
  74. 'ifsc' => $value[2],
  75. 'amount' => $value[3],
  76. 'crt_id' => $this->crt_id,
  77. 'crt_time' => $crt_time
  78. ];
  79. $totalAmount = (float)$value[3];
  80. $num = floor($totalAmount / $defaultAmount); // 拆分出来的次数
  81. if((int)$num <= 1){
  82. $sub[$key][] = [
  83. 'amount' => $totalAmount
  84. ];
  85. }else{
  86. $remainingAmount = $totalAmount % $defaultAmount; // 剩余的金额
  87. $equalAmount = floor($totalAmount / $num); // 平均金额
  88. $lastAmount = $equalAmount + $remainingAmount; // 最后金额(加上剩余的金额)
  89. for ($i = 0;$i < (int)$num; $i++){
  90. if ($i == (int)$num - 1) {
  91. // 最后一次
  92. $tmp = $lastAmount;
  93. } else {
  94. $tmp = $equalAmount;
  95. }
  96. $sub[$key][] = [
  97. 'amount' => $tmp
  98. ];
  99. }
  100. }
  101. }
  102. try{
  103. DB::beginTransaction();
  104. Finance::insert($main);
  105. //获取上一次插入的所有id
  106. $last_insert_id = Finance::where('crt_time',$crt_time)->select('id')->get()->toArray();
  107. $last_insert_id = array_column($last_insert_id,'id');
  108. $insert_sub = [];
  109. foreach ($sub as $key => $value){
  110. foreach ($value as $v){
  111. $insert_sub[] = [
  112. 'amount' => $v['amount'],
  113. 'finance_id' => $last_insert_id[$key],
  114. 'crt_time' => $crt_time
  115. ];
  116. }
  117. }unset($main);unset($sub);
  118. FinanceDetail::insert($insert_sub);
  119. DB::commit();
  120. }catch (\Exception $e){
  121. DB::rollBack();
  122. $this->setMsg($e->getMessage());
  123. }
  124. }
  125. }