CreateProcurementTotalBill.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\ProcurementCheckSheet;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\DB;
  6. class CreateProcurementTotalBill extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'createProcurementTotalBill';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '采购生成总账单';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. $date=$this->getDate();
  37. $procurementCheckSheets=ProcurementCheckSheet::query()
  38. ->selectRaw('procurements.supplier_id supplier_id,procurement_check_sheets.created_at created_at,procurement_check_sheets.status status,SUM(procurement_check_sheets.account_payable) account_payable')
  39. ->leftJoin('procurement_deliveries','procurement_check_sheets.procurement_delivery_id','procurement_deliveries.id')
  40. ->leftJoin('procurements','procurement_deliveries.procurement_id','procurements.id')
  41. ->where('procurement_check_sheets.created_at','like',$date."%")
  42. ->groupBy('supplier_id')
  43. ->get();
  44. $totalBill=[];
  45. foreach ($procurementCheckSheets as $procurementCheckSheet){
  46. $totalBill[]=[
  47. 'counting_month'=>$date.'-01',
  48. 'supplier_id'=>$procurementCheckSheet->supplier_id,
  49. 'total_payable'=>$procurementCheckSheet->account_payable,
  50. 'status'=>$procurementCheckSheet->status,
  51. 'created_at'=>date('Y-m-01 00:00:00'),
  52. 'updated_at'=>date('Y-m-01 00:00:00'),
  53. ];
  54. }
  55. if (count($totalBill)>0){
  56. DB::table("procurement_total_bills")->insert($totalBill);
  57. app('LogService')->log(__METHOD__,"采购管理-生成月账单报表",json_encode($totalBill));
  58. }
  59. }
  60. private function getDate(){//获取当前日期的上月 月份
  61. $month = (int)date('m');
  62. $year = (int)date('Y');
  63. if ($month == 1){
  64. $year--;
  65. $lastMonth = '12';
  66. }else{
  67. $lastMonth = ($month-1) < 10 ? "0".($month-1) : ($month-1);
  68. }
  69. $date=$year."-".$lastMonth;
  70. return $date;
  71. }
  72. }